Fix database bugs and make unit tests from previous commit pass

pull/14/head
Sam Bosley 12 years ago
parent 2917683bb8
commit 06f519f231

@ -226,43 +226,11 @@ public class DatabaseDao<TYPE extends AbstractModel> {
}
}
/**
* Creates the given item.
*
* @param database
* @param table
* table name
* @param item
* item model
* @return returns true on success.
*/
public boolean createNew(TYPE item) {
item.clearValue(AbstractModel.ID_PROPERTY);
long newRow = database.insert(table.name,
AbstractModel.ID_PROPERTY.name, item.getMergedValues());
boolean result = newRow >= 0;
if(result) {
item.setId(newRow);
onModelUpdated(item);
item.markSaved();
}
return result;
private interface DatabaseChangeOp {
public boolean makeChange();
}
/**
* Saves the given item. Will not create a new item!
*
* @param database
* @param table
* table name
* @param item
* item model
* @return returns true on success.
*/
public boolean saveExisting(TYPE item) {
ContentValues values = item.getSetValues();
if(values == null || values.size() == 0) // nothing changed
return true;
private boolean insertOrUpdateAndRecordChanges(TYPE item, ContentValues values, DatabaseChangeOp op) {
boolean recordOutstanding = (outstandingTable != null);
final AtomicBoolean result = new AtomicBoolean(false);
@ -279,8 +247,7 @@ public class DatabaseDao<TYPE extends AbstractModel> {
});
}
try {
result.set(database.update(table.name, values,
AbstractModel.ID_PROPERTY.eq(item.getId()).toString(), null) > 0);
result.set(op.makeChange());
if(result.get()) {
if (recordOutstanding)
createOutstandingEntries(item.getId(), values); // Create entries for setValues in outstanding table
@ -294,6 +261,57 @@ public class DatabaseDao<TYPE extends AbstractModel> {
return result.get();
}
/**
* Creates the given item.
*
* @param database
* @param table
* table name
* @param item
* item model
* @return returns true on success.
*/
public boolean createNew(final TYPE item) {
item.clearValue(AbstractModel.ID_PROPERTY);
DatabaseChangeOp insert = new DatabaseChangeOp() {
@Override
public boolean makeChange() {
long newRow = database.insert(table.name,
AbstractModel.ID_PROPERTY.name, item.getMergedValues());
boolean result = newRow >= 0;
if (result)
item.setId(newRow);
return result;
}
};
return insertOrUpdateAndRecordChanges(item, item.getMergedValues(), insert);
}
/**
* Saves the given item. Will not create a new item!
*
* @param database
* @param table
* table name
* @param item
* item model
* @return returns true on success.
*/
public boolean saveExisting(final TYPE item) {
final ContentValues values = item.getSetValues();
if(values == null || values.size() == 0) // nothing changed
return true;
DatabaseChangeOp update = new DatabaseChangeOp() {
@Override
public boolean makeChange() {
return database.update(table.name, values,
AbstractModel.ID_PROPERTY.eq(item.getId()).toString(), null) > 0;
}
};
return insertOrUpdateAndRecordChanges(item, values, update);
}
private boolean createOutstandingEntries(long modelId, ContentValues modelSetValues) {
Set<Entry<String, Object>> entries = modelSetValues.valueSet();
long now = DateUtilities.now();

@ -1,12 +1,19 @@
package com.todoroo.astrid.dao;
import com.todoroo.andlib.data.DatabaseDao;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.service.DependencyInjectionService;
import com.todoroo.astrid.data.OutstandingEntry;
public class OutstandingEntryDao<TYPE extends OutstandingEntry<?>> extends DatabaseDao<TYPE> {
@Autowired
private Database database;
public OutstandingEntryDao(Class<TYPE> modelClass) {
super(modelClass);
DependencyInjectionService.getInstance().inject(this);
setDatabase(database);
}
}

@ -31,7 +31,6 @@ public class TagDataDao extends RemoteModelDao<TagData> {
private static final String[] IGNORE_OUTSTANDING_COLUMNS = new String[] {
TagData.MODIFICATION_DATE.name,
TagData.REMOTE_ID.name,
TagData.PROOF_TEXT.name
};
@Override

@ -321,7 +321,10 @@ public class TaskDao extends RemoteModelDao<Task> {
Task.DETAILS_DATE.name,
Task.CALENDAR_URI.name,
Task.REMOTE_ID.name,
Task.PROOF_TEXT.name
Task.PUSHED_AT.name,
Task.LAST_SYNC.name,
Task.ESTIMATED_SECONDS.name,
Task.TIMER_START.name,
};
@Override

@ -19,6 +19,7 @@ import com.todoroo.astrid.dao.Database;
import com.todoroo.astrid.dao.MetadataDao;
import com.todoroo.astrid.dao.StoreObjectDao;
import com.todoroo.astrid.dao.TagDataDao;
import com.todoroo.astrid.dao.TagOutstandingDao;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.dao.TaskOutstandingDao;
import com.todoroo.astrid.dao.UpdateDao;
@ -74,7 +75,7 @@ public class AstridDependencyInjector extends AbstractDependencyInjector {
injectables.put("updateDao", UpdateDao.class);
injectables.put("userDao", UserDao.class);
injectables.put("taskOutstandingDao", TaskOutstandingDao.class);
injectables.put("tagOutstandingDao", TaskOutstandingDao.class);
injectables.put("tagOutstandingDao", TagOutstandingDao.class);
// com.todoroo.astrid.service
injectables.put("taskService", TaskService.class);

Loading…
Cancel
Save