First pass at recording outstanding entries in DatabaseDao.update

pull/14/head
Sam Bosley 12 years ago
parent 01d522522a
commit c18e305448

@ -46,7 +46,7 @@ public abstract class AbstractModel implements Parcelable, Cloneable {
protected static final String ID_PROPERTY_NAME = "_id"; //$NON-NLS-1$
/** id field common to all models */
public static final IntegerProperty ID_PROPERTY = new IntegerProperty(null, ID_PROPERTY_NAME);
public static final LongProperty ID_PROPERTY = new LongProperty(null, ID_PROPERTY_NAME);
/** sentinel for objects without an id */
public static final long NO_ID = 0;

@ -11,6 +11,7 @@ import java.util.ArrayList;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import android.content.ContentValues;
import android.database.Cursor;
@ -212,8 +213,50 @@ public class DatabaseDao<TYPE extends AbstractModel> {
* @return # of updated items
*/
public int update(Criterion where, TYPE template) {
return database.update(table.name, template.getSetValues(),
where.toString(), null);
boolean recordOutstanding = shouldRecordOutstanding(template);
final AtomicInteger result = new AtomicInteger(0);
if (recordOutstanding) {
TodorooCursor<TYPE> toUpdate = query(Query.select(AbstractModel.ID_PROPERTY).where(where));
Long[] ids = null;
try {
ids = new Long[toUpdate.getCount()];
for (int i = 0; i < toUpdate.getCount(); i++) {
toUpdate.moveToNext();
ids[i] = toUpdate.get(AbstractModel.ID_PROPERTY);
}
} finally {
toUpdate.close();
}
database.getDatabase().beginTransactionWithListener(new SQLiteTransactionListener() {
@Override
public void onRollback() {
result.set(0);
}
@Override
public void onCommit() {/**/}
@Override
public void onBegin() {/**/}
});
try {
result.set(database.update(table.name, template.getSetValues(),
where.toString(), null));
if (result.get() > 0) {
for (Long id : ids) {
createOutstandingEntries(id, template.getSetValues());
}
}
database.getDatabase().setTransactionSuccessful();
} finally {
database.getDatabase().endTransaction();
}
return result.get();
} else {
return database.update(table.name, template.getSetValues(),
where.toString(), null);
}
}
/**

Loading…
Cancel
Save