Remove DatabaseDao subclasses

pull/253/head
Alex Baker 10 years ago
parent eb59cde024
commit 85a694b1d1

@ -36,8 +36,10 @@ public class DatabaseDao<TYPE extends AbstractModel> {
private AbstractDatabase database; private AbstractDatabase database;
public DatabaseDao(Class<TYPE> modelClass) { public DatabaseDao(AbstractDatabase database, Class<TYPE> modelClass) {
this.modelClass = modelClass; this.modelClass = modelClass;
this.database = database;
table = database.getTable(this.modelClass);
try { try {
modelClass.getConstructor(); // check for default constructor modelClass.getConstructor(); // check for default constructor
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
@ -50,26 +52,13 @@ public class DatabaseDao<TYPE extends AbstractModel> {
return table; return table;
} }
/**
* Sets database accessed by this DAO. Used for dependency-injected
* initialization by child classes and unit tests
*/
public void setDatabase(AbstractDatabase database) {
if(database == this.database) {
return;
}
this.database = database;
table = database.getTable(modelClass);
}
// --- listeners // --- listeners
public interface ModelUpdateListener<MTYPE> { public interface ModelUpdateListener<MTYPE> {
public void onModelUpdated(MTYPE model); public void onModelUpdated(MTYPE model);
} }
private final ArrayList<ModelUpdateListener<TYPE>> listeners = private final ArrayList<ModelUpdateListener<TYPE>> listeners = new ArrayList<>();
new ArrayList<>();
public void addListener(ModelUpdateListener<TYPE> listener) { public void addListener(ModelUpdateListener<TYPE> listener) {
listeners.add(listener); listeners.add(listener);

@ -7,6 +7,7 @@ package com.todoroo.astrid.dao;
import com.todoroo.andlib.data.Callback; import com.todoroo.andlib.data.Callback;
import com.todoroo.andlib.data.DatabaseDao; import com.todoroo.andlib.data.DatabaseDao;
import com.todoroo.andlib.data.Property;
import com.todoroo.andlib.sql.Criterion; import com.todoroo.andlib.sql.Criterion;
import com.todoroo.andlib.sql.Join; import com.todoroo.andlib.sql.Join;
import com.todoroo.andlib.sql.Query; import com.todoroo.andlib.sql.Query;
@ -14,6 +15,8 @@ import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.astrid.data.Metadata; import com.todoroo.astrid.data.Metadata;
import com.todoroo.astrid.data.Task; import com.todoroo.astrid.data.Task;
import java.util.List;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
@ -24,12 +27,53 @@ import javax.inject.Singleton;
* *
*/ */
@Singleton @Singleton
public class MetadataDao extends DatabaseDao<Metadata> { public class MetadataDao {
private final DatabaseDao<Metadata> dao;
@Inject @Inject
public MetadataDao(Database database) { public MetadataDao(Database database) {
super(Metadata.class); dao = new DatabaseDao<>(database, Metadata.class);
setDatabase(database); }
public void query(Callback<Metadata> callback, Query query) {
query(query, callback);
}
public void query(Query query, Callback<Metadata> callback) {
dao.query(query, callback);
}
public Metadata getFirst(Query query) {
return dao.getFirst(query);
}
public int update(Criterion where, Metadata template) {
return dao.update(where, template);
}
public void createNew(Metadata metadata) {
dao.createNew(metadata);
}
public List<Metadata> toList(Query where) {
return dao.toList(where);
}
public int deleteWhere(Criterion criterion) {
return dao.deleteWhere(criterion);
}
public boolean delete(long id) {
return dao.delete(id);
}
public void saveExisting(Metadata metadata) {
dao.saveExisting(metadata);
}
public Metadata fetch(long id, Property<?>... properties) {
return dao.fetch(id, properties);
} }
// --- SQL clause generators // --- SQL clause generators
@ -55,7 +99,6 @@ public class MetadataDao extends DatabaseDao<Metadata> {
} }
} }
@Override
public boolean persist(Metadata item) { public boolean persist(Metadata item) {
if(!item.containsNonNullValue(Metadata.TASK)) { if(!item.containsNonNullValue(Metadata.TASK)) {
throw new IllegalArgumentException("metadata needs to be attached to a task: " + item.getMergedValues()); //$NON-NLS-1$ throw new IllegalArgumentException("metadata needs to be attached to a task: " + item.getMergedValues()); //$NON-NLS-1$
@ -64,23 +107,23 @@ public class MetadataDao extends DatabaseDao<Metadata> {
item.setCreationDate(DateUtilities.now()); item.setCreationDate(DateUtilities.now());
} }
return super.persist(item); return dao.persist(item);
} }
/** /**
* Clean up metadata. Typically called on startup * Clean up metadata. Typically called on startup
*/ */
public void removeDanglingMetadata() { public void removeDanglingMetadata() {
deleteWhere(Metadata.ID.in(Query.select(Metadata.ID).from(Metadata.TABLE).join(Join.left(Task.TABLE, dao.deleteWhere(Metadata.ID.in(Query.select(Metadata.ID).from(Metadata.TABLE).join(Join.left(Task.TABLE,
Metadata.TASK.eq(Task.ID))).where(Task.TITLE.isNull()))); Metadata.TASK.eq(Task.ID))).where(Task.TITLE.isNull())));
} }
public void byTask(long taskId, Callback<Metadata> callback) { public void byTask(long taskId, Callback<Metadata> callback) {
query(callback, Query.select(Metadata.PROPERTIES).where(Metadata.TASK.eq(taskId))); dao.query(callback, Query.select(Metadata.PROPERTIES).where(Metadata.TASK.eq(taskId)));
} }
public void byTaskAndKey(long taskId, String key, Callback<Metadata> callback) { public void byTaskAndKey(long taskId, String key, Callback<Metadata> callback) {
query(callback, Query.select(Metadata.PROPERTIES).where( dao.query(callback, Query.select(Metadata.PROPERTIES).where(
Criterion.and(Metadata.TASK.eq(taskId), Metadata.KEY.eq(key)))); Criterion.and(Metadata.TASK.eq(taskId), Metadata.KEY.eq(key))));
} }
} }

@ -1,5 +1,6 @@
package com.todoroo.astrid.dao; package com.todoroo.astrid.dao;
import com.todoroo.andlib.data.AbstractDatabase;
import com.todoroo.andlib.data.DatabaseDao; import com.todoroo.andlib.data.DatabaseDao;
import com.todoroo.astrid.data.RemoteModel; import com.todoroo.astrid.data.RemoteModel;
import com.todoroo.astrid.helper.UUIDHelper; import com.todoroo.astrid.helper.UUIDHelper;
@ -15,8 +16,8 @@ import com.todoroo.astrid.helper.UUIDHelper;
*/ */
public class RemoteModelDao<RTYPE extends RemoteModel> extends DatabaseDao<RTYPE> { public class RemoteModelDao<RTYPE extends RemoteModel> extends DatabaseDao<RTYPE> {
public RemoteModelDao(Class<RTYPE> modelClass) { public RemoteModelDao(AbstractDatabase database, Class<RTYPE> modelClass) {
super(modelClass); super(database, modelClass);
} }
@Override @Override

@ -17,32 +17,31 @@ import com.todoroo.astrid.gtasks.GtasksList;
import java.util.List; import java.util.List;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton;
import static com.google.common.collect.Iterables.transform; import static com.google.common.collect.Iterables.transform;
import static com.google.common.collect.Lists.newArrayList; import static com.google.common.collect.Lists.newArrayList;
import static com.todoroo.andlib.sql.Criterion.and; import static com.todoroo.andlib.sql.Criterion.and;
import static com.todoroo.andlib.sql.Query.select; import static com.todoroo.andlib.sql.Query.select;
@Singleton public class StoreObjectDao {
public class StoreObjectDao extends DatabaseDao<StoreObject> {
private final DatabaseDao<StoreObject> dao;
private static Criterion isSavedFilter = StoreObject.TYPE.eq(SavedFilter.TYPE); private static Criterion isSavedFilter = StoreObject.TYPE.eq(SavedFilter.TYPE);
@Inject @Inject
public StoreObjectDao(Database database) { public StoreObjectDao(Database database) {
super(StoreObject.class); dao = new DatabaseDao<>(database, StoreObject.class);
setDatabase(database);
} }
public void getSavedFilters(Callback<StoreObject> callback) { public void getSavedFilters(Callback<StoreObject> callback) {
query(callback, select(StoreObject.PROPERTIES) dao.query(callback, select(StoreObject.PROPERTIES)
.where(isSavedFilter) .where(isSavedFilter)
.orderBy(Order.asc(SavedFilter.NAME))); .orderBy(Order.asc(SavedFilter.NAME)));
} }
public GtasksList getGtasksList(long id) { public GtasksList getGtasksList(long id) {
StoreObject result = fetch(id, StoreObject.PROPERTIES); StoreObject result = dao.fetch(id, StoreObject.PROPERTIES);
if (!result.getType().equals(GtasksList.TYPE)) { if (!result.getType().equals(GtasksList.TYPE)) {
throw new RuntimeException("Not a google task list"); throw new RuntimeException("Not a google task list");
} }
@ -58,18 +57,30 @@ public class StoreObjectDao extends DatabaseDao<StoreObject> {
})); }));
} }
public void persist(StoreObject storeObject) {
dao.persist(storeObject);
}
public void persist(GtasksList list) { public void persist(GtasksList list) {
persist(list.getStoreObject()); persist(list.getStoreObject());
} }
public List<StoreObject> getByType(String type) { public List<StoreObject> getByType(String type) {
return toList(select(StoreObject.PROPERTIES) return dao.toList(select(StoreObject.PROPERTIES)
.where(StoreObject.TYPE.eq(type))); .where(StoreObject.TYPE.eq(type)));
} }
public StoreObject getSavedFilterByName(String title) { public StoreObject getSavedFilterByName(String title) {
return getFirst(select(StoreObject.ID) return dao.getFirst(select(StoreObject.ID)
.where(and(isSavedFilter, SavedFilter.NAME.eq(title)))); .where(and(isSavedFilter, SavedFilter.NAME.eq(title))));
} }
public void delete(long id) {
dao.delete(id);
}
public void createNew(StoreObject storeObject) {
dao.createNew(storeObject);
}
} }

@ -6,6 +6,7 @@
package com.todoroo.astrid.dao; package com.todoroo.astrid.dao;
import com.todoroo.andlib.data.Callback; import com.todoroo.andlib.data.Callback;
import com.todoroo.andlib.data.DatabaseDao;
import com.todoroo.andlib.data.Property; import com.todoroo.andlib.data.Property;
import com.todoroo.andlib.sql.Criterion; import com.todoroo.andlib.sql.Criterion;
import com.todoroo.andlib.sql.Functions; import com.todoroo.andlib.sql.Functions;
@ -23,41 +24,70 @@ import javax.inject.Singleton;
* @author Tim Su <tim@todoroo.com> * @author Tim Su <tim@todoroo.com>
*/ */
@Singleton @Singleton
public class TagDataDao extends RemoteModelDao<TagData> { public class TagDataDao {
private final RemoteModelDao<TagData> dao;
@Inject @Inject
public TagDataDao(Database database) { public TagDataDao(Database database) {
super(TagData.class); dao = new RemoteModelDao<>(database, TagData.class);
setDatabase(database);
} }
/** /**
* Fetch a model object by UUID * Fetch a model object by UUID
*/ */
public TagData fetch(String uuid, Property<?>... properties) { public TagData fetch(String uuid, Property<?>... properties) {
return getFirst(Query.select(properties).where(RemoteModel.UUID_PROPERTY.eq(uuid))); return dao.getFirst(Query.select(properties).where(RemoteModel.UUID_PROPERTY.eq(uuid)));
}
public TagData fetch(long id, Property<?>... properties) {
return dao.fetch(id, properties);
} }
public TagData getTagByName(String name, Property<?>... properties) { public TagData getTagByName(String name, Property<?>... properties) {
return getFirst(Query.select(properties).where(TagData.NAME.eqCaseInsensitive(name))); return dao.getFirst(Query.select(properties).where(TagData.NAME.eqCaseInsensitive(name)));
} }
public void allTags(Callback<TagData> callback) { public void allTags(Callback<TagData> callback) {
// TODO: does this need to be ordered? // TODO: does this need to be ordered?
query(callback, Query.select(TagData.PROPERTIES) dao.query(callback, Query.select(TagData.PROPERTIES)
.where(TagData.DELETION_DATE.eq(0)) .where(TagData.DELETION_DATE.eq(0))
.orderBy(Order.asc(TagData.ID))); .orderBy(Order.asc(TagData.ID)));
} }
public TagData getByUuid(String uuid, Property<?>... properties) { public TagData getByUuid(String uuid, Property<?>... properties) {
return getFirst(Query.select(properties).where(TagData.UUID.eq(uuid))); return dao.getFirst(Query.select(properties).where(TagData.UUID.eq(uuid)));
} }
public void tagDataOrderedByName(Callback<TagData> callback) { public void tagDataOrderedByName(Callback<TagData> callback) {
query(callback, Query.select(TagData.PROPERTIES).where(Criterion.and( dao.query(callback, Query.select(TagData.PROPERTIES).where(Criterion.and(
TagData.DELETION_DATE.eq(0), TagData.DELETION_DATE.eq(0),
TagData.NAME.isNotNull()) TagData.NAME.isNotNull())
).orderBy(Order.asc(Functions.upper(TagData.NAME)))); ).orderBy(Order.asc(Functions.upper(TagData.NAME))));
} }
public void persist(TagData tagData) {
dao.persist(tagData);
}
public void update(Criterion where, TagData template) {
dao.update(where, template);
}
public void saveExisting(TagData tagData) {
dao.saveExisting(tagData);
}
public void addListener(DatabaseDao.ModelUpdateListener<TagData> modelUpdateListener) {
dao.addListener(modelUpdateListener);
}
public void delete(long id) {
dao.delete(id);
}
public void createNew(TagData tag) {
dao.createNew(tag);
}
} }

@ -12,23 +12,22 @@ import com.todoroo.andlib.sql.Query;
import com.todoroo.astrid.data.TaskAttachment; import com.todoroo.astrid.data.TaskAttachment;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton;
@Singleton public class TaskAttachmentDao {
public class TaskAttachmentDao extends RemoteModelDao<TaskAttachment> {
private final RemoteModelDao<TaskAttachment> dao;
@Inject @Inject
public TaskAttachmentDao(Database database) { public TaskAttachmentDao(Database database) {
super(TaskAttachment.class); dao = new RemoteModelDao<>(database, TaskAttachment.class);
setDatabase(database);
} }
public boolean taskHasAttachments(String taskUuid) { public boolean taskHasAttachments(String taskUuid) {
return count(byUuid(taskUuid, TaskAttachment.TASK_UUID).limit(1)) > 0; return dao.count(byUuid(taskUuid, TaskAttachment.TASK_UUID).limit(1)) > 0;
} }
public void getAttachments(String taskUuid, Callback<TaskAttachment> callback) { public void getAttachments(String taskUuid, Callback<TaskAttachment> callback) {
query(callback, byUuid(taskUuid, TaskAttachment.PROPERTIES)); dao.query(callback, byUuid(taskUuid, TaskAttachment.PROPERTIES));
} }
private static Query byUuid(String taskUuid, Property<?>... properties) { private static Query byUuid(String taskUuid, Property<?>... properties) {
@ -37,5 +36,17 @@ public class TaskAttachmentDao extends RemoteModelDao<TaskAttachment> {
TaskAttachment.DELETED_AT.eq(0)) TaskAttachment.DELETED_AT.eq(0))
); );
} }
public void createNew(TaskAttachment attachment) {
dao.createNew(attachment);
}
public void delete(long id) {
dao.delete(id);
}
public void saveExisting(TaskAttachment m) {
dao.saveExisting(m);
}
} }

@ -9,6 +9,7 @@ import android.content.ContentValues;
import android.database.sqlite.SQLiteConstraintException; import android.database.sqlite.SQLiteConstraintException;
import com.todoroo.andlib.data.AbstractModel; import com.todoroo.andlib.data.AbstractModel;
import com.todoroo.andlib.data.DatabaseDao;
import com.todoroo.andlib.data.Property; import com.todoroo.andlib.data.Property;
import com.todoroo.andlib.data.TodorooCursor; import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.sql.Criterion; import com.todoroo.andlib.sql.Criterion;
@ -28,6 +29,9 @@ import org.tasks.R;
import org.tasks.notifications.NotificationManager; import org.tasks.notifications.NotificationManager;
import org.tasks.preferences.Preferences; import org.tasks.preferences.Preferences;
import java.io.ByteArrayOutputStream;
import java.util.List;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
@ -38,10 +42,12 @@ import javax.inject.Singleton;
* *
*/ */
@Singleton @Singleton
public class TaskDao extends RemoteModelDao<Task> { public class TaskDao {
private static final Logger log = LoggerFactory.getLogger(TaskDao.class); private static final Logger log = LoggerFactory.getLogger(TaskDao.class);
private final RemoteModelDao<Task> dao;
private final MetadataDao metadataDao; private final MetadataDao metadataDao;
private final Broadcaster broadcaster; private final Broadcaster broadcaster;
private final ReminderService reminderService; private final ReminderService reminderService;
@ -52,8 +58,7 @@ public class TaskDao extends RemoteModelDao<Task> {
public TaskDao(Database database, MetadataDao metadataDao, Broadcaster broadcaster, public TaskDao(Database database, MetadataDao metadataDao, Broadcaster broadcaster,
ReminderService reminderService, NotificationManager notificationManager, ReminderService reminderService, NotificationManager notificationManager,
Preferences preferences) { Preferences preferences) {
super(Task.class); dao = new RemoteModelDao<>(database, Task.class);
setDatabase(database);
this.preferences = preferences; this.preferences = preferences;
this.metadataDao = metadataDao; this.metadataDao = metadataDao;
this.broadcaster = broadcaster; this.broadcaster = broadcaster;
@ -61,6 +66,42 @@ public class TaskDao extends RemoteModelDao<Task> {
this.notificationManager = notificationManager; this.notificationManager = notificationManager;
} }
public TodorooCursor<Task> query(Query query) {
return dao.query(query);
}
public Task fetch(long id, Property<?>... properties) {
return dao.fetch(id, properties);
}
public int count(Query query) {
return dao.count(query);
}
public TodorooCursor<Task> rawQuery(String selection, String[] selectionArgs, Property.LongProperty id) {
return dao.rawQuery(selection, selectionArgs, id);
}
public int update(Criterion where, Task template) {
return dao.update(where, template);
}
public int deleteWhere(Criterion criterion) {
return dao.deleteWhere(criterion);
}
public void addListener(DatabaseDao.ModelUpdateListener<Task> modelUpdateListener) {
dao.addListener(modelUpdateListener);
}
public List<Task> toList(Query query) {
return dao.toList(query);
}
public void persist(Task task) {
dao.persist(task);
}
// --- SQL clause generators // --- SQL clause generators
/** /**
@ -103,7 +144,7 @@ public class TaskDao extends RemoteModelDao<Task> {
} }
public String uuidFromLocalId(long localId) { public String uuidFromLocalId(long localId) {
TodorooCursor<Task> cursor = query(Query.select(RemoteModel.UUID_PROPERTY).where(AbstractModel.ID_PROPERTY.eq(localId))); TodorooCursor<Task> cursor = dao.query(Query.select(RemoteModel.UUID_PROPERTY).where(AbstractModel.ID_PROPERTY.eq(localId)));
try { try {
if (cursor.getCount() == 0) { if (cursor.getCount() == 0) {
return RemoteModel.NO_UUID; return RemoteModel.NO_UUID;
@ -122,9 +163,8 @@ public class TaskDao extends RemoteModelDao<Task> {
* *
* @return true if delete was successful * @return true if delete was successful
*/ */
@Override
public boolean delete(long id) { public boolean delete(long id) {
boolean result = super.delete(id); boolean result = dao.delete(id);
if(!result) { if(!result) {
return false; return false;
} }
@ -158,7 +198,7 @@ public class TaskDao extends RemoteModelDao<Task> {
} }
public void handleSQLiteConstraintException(Task task) { public void handleSQLiteConstraintException(Task task) {
TodorooCursor<Task> cursor = query(Query.select(Task.ID).where( TodorooCursor<Task> cursor = dao.query(Query.select(Task.ID).where(
Task.UUID.eq(task.getUUID()))); Task.UUID.eq(task.getUUID())));
if (cursor.getCount() > 0) { if (cursor.getCount() > 0) {
cursor.moveToFirst(); cursor.moveToFirst();
@ -167,7 +207,6 @@ public class TaskDao extends RemoteModelDao<Task> {
} }
} }
@Override
public boolean createNew(Task item) { public boolean createNew(Task item) {
if(!item.containsValue(Task.CREATION_DATE)) { if(!item.containsValue(Task.CREATION_DATE)) {
item.setCreationDate(DateUtilities.now()); item.setCreationDate(DateUtilities.now());
@ -189,7 +228,7 @@ public class TaskDao extends RemoteModelDao<Task> {
setDefaultReminders(preferences, item); setDefaultReminders(preferences, item);
ContentValues values = item.getSetValues(); ContentValues values = item.getSetValues();
boolean result = super.createNew(item); boolean result = dao.createNew(item);
if(result) { if(result) {
afterSave(item, values); afterSave(item, values);
} }
@ -222,7 +261,6 @@ public class TaskDao extends RemoteModelDao<Task> {
} }
} }
@Override
public boolean saveExisting(Task item) { public boolean saveExisting(Task item) {
ContentValues values = item.getSetValues(); ContentValues values = item.getSetValues();
if(values == null || values.size() == 0) { if(values == null || values.size() == 0) {
@ -233,7 +271,7 @@ public class TaskDao extends RemoteModelDao<Task> {
item.setModificationDate(DateUtilities.now()); item.setModificationDate(DateUtilities.now());
} }
} }
boolean result = super.saveExisting(item); boolean result = dao.saveExisting(item);
if(result) { if(result) {
afterSave(item, values); afterSave(item, values);
} }
@ -259,7 +297,7 @@ public class TaskDao extends RemoteModelDao<Task> {
} catch (SQLiteConstraintException e) { } catch (SQLiteConstraintException e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
String uuid = item.getUUID(); String uuid = item.getUUID();
TodorooCursor<Task> tasksWithUUID = query(Query.select( TodorooCursor<Task> tasksWithUUID = dao.query(Query.select(
SQL_CONSTRAINT_MERGE_PROPERTIES).where( SQL_CONSTRAINT_MERGE_PROPERTIES).where(
Task.UUID.eq(uuid))); Task.UUID.eq(uuid)));
try { try {
@ -270,7 +308,7 @@ public class TaskDao extends RemoteModelDao<Task> {
continue; continue;
} }
compareAndMergeAfterConflict(curr, fetch(item.getId(), compareAndMergeAfterConflict(curr, dao.fetch(item.getId(),
tasksWithUUID.getProperties())); tasksWithUUID.getProperties()));
return; return;
} }

@ -19,18 +19,26 @@ import javax.inject.Singleton;
* *
* @author Tim Su <tim@todoroo.com> * @author Tim Su <tim@todoroo.com>
*/ */
@Singleton public class TaskListMetadataDao {
public class TaskListMetadataDao extends RemoteModelDao<TaskListMetadata> {
private final RemoteModelDao<TaskListMetadata> dao;
@Inject @Inject
public TaskListMetadataDao(Database database) { public TaskListMetadataDao(Database database) {
super(TaskListMetadata.class); dao = new RemoteModelDao<>(database, TaskListMetadata.class);
setDatabase(database);
} }
public TaskListMetadata fetchByTagId(String tagUuid, Property<?>... properties) { public TaskListMetadata fetchByTagId(String tagUuid, Property<?>... properties) {
return getFirst(Query.select(properties).where(Criterion.or(TaskListMetadata.TAG_UUID.eq(tagUuid), return dao.getFirst(Query.select(properties).where(Criterion.or(TaskListMetadata.TAG_UUID.eq(tagUuid),
TaskListMetadata.FILTER.eq(tagUuid)))); TaskListMetadata.FILTER.eq(tagUuid))));
} }
public void createNew(TaskListMetadata taskListMetadata) {
dao.createNew(taskListMetadata);
}
public void saveExisting(TaskListMetadata list) {
dao.saveExisting(list);
}
} }

@ -10,36 +10,33 @@ import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.astrid.data.UserActivity; import com.todoroo.astrid.data.UserActivity;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton;
@Singleton public class UserActivityDao {
public class UserActivityDao extends RemoteModelDao<UserActivity> {
private final RemoteModelDao<UserActivity> dao;
@Inject @Inject
public UserActivityDao(Database database) { public UserActivityDao(Database database) {
super(UserActivity.class); dao = new RemoteModelDao<>(database, UserActivity.class);
setDatabase(database);
} }
@Override
public boolean createNew(UserActivity item) { public boolean createNew(UserActivity item) {
if (!item.containsValue(UserActivity.CREATED_AT)) { if (!item.containsValue(UserActivity.CREATED_AT)) {
item.setCreatedAt(DateUtilities.now()); item.setCreatedAt(DateUtilities.now());
} }
return super.createNew(item); return dao.createNew(item);
} }
@Override
public boolean saveExisting(UserActivity item) { public boolean saveExisting(UserActivity item) {
ContentValues values = item.getSetValues(); ContentValues values = item.getSetValues();
if (values == null || values.size() == 0) { if (values == null || values.size() == 0) {
return false; return false;
} }
return super.saveExisting(item); return dao.saveExisting(item);
} }
public void getCommentsForTask(String taskUuid, Callback<UserActivity> callback) { public void getCommentsForTask(String taskUuid, Callback<UserActivity> callback) {
query(callback, Query.select(UserActivity.PROPERTIES).where( dao.query(callback, Query.select(UserActivity.PROPERTIES).where(
Criterion.and(UserActivity.ACTION.eq(UserActivity.ACTION_TASK_COMMENT), Criterion.and(UserActivity.ACTION.eq(UserActivity.ACTION_TASK_COMMENT),
UserActivity.TARGET_ID.eq(taskUuid), UserActivity.TARGET_ID.eq(taskUuid),
UserActivity.DELETED_AT.eq(0)) UserActivity.DELETED_AT.eq(0))

@ -50,6 +50,7 @@ public class GtasksSyncService {
private final GtasksPreferenceService gtasksPreferenceService; private final GtasksPreferenceService gtasksPreferenceService;
private final GtasksTokenValidator gtasksTokenValidator; private final GtasksTokenValidator gtasksTokenValidator;
private final GtasksMetadata gtasksMetadataFactory; private final GtasksMetadata gtasksMetadataFactory;
private final LinkedBlockingQueue<SyncOnSaveOperation> operationQueue = new LinkedBlockingQueue<>();
@Inject @Inject
public GtasksSyncService(MetadataDao metadataDao, GtasksMetadataService gtasksMetadataService, public GtasksSyncService(MetadataDao metadataDao, GtasksMetadataService gtasksMetadataService,
@ -63,8 +64,6 @@ public class GtasksSyncService {
this.gtasksMetadataFactory = gtasksMetadataFactory; this.gtasksMetadataFactory = gtasksMetadataFactory;
} }
private final LinkedBlockingQueue<SyncOnSaveOperation> operationQueue = new LinkedBlockingQueue<>();
private abstract class SyncOnSaveOperation { private abstract class SyncOnSaveOperation {
abstract public void op(GtasksInvoker invoker) throws IOException; abstract public void op(GtasksInvoker invoker) throws IOException;
} }
@ -121,16 +120,14 @@ public class GtasksSyncService {
if(model.checkAndClearTransitory(SyncFlags.GTASKS_SUPPRESS_SYNC)) { if(model.checkAndClearTransitory(SyncFlags.GTASKS_SUPPRESS_SYNC)) {
return; return;
} }
if (gtasksPreferenceService.isOngoing() && !model.checkTransitory(TaskService.TRANS_REPEAT_COMPLETE)) //Don't try and sync changes that occur during a normal sync if (gtasksPreferenceService.isOngoing() && !model.checkTransitory(TaskService.TRANS_REPEAT_COMPLETE)) { //Don't try and sync changes that occur during a normal sync
{
return; return;
} }
final ContentValues setValues = model.getSetValues(); final ContentValues setValues = model.getSetValues();
if(setValues == null || !checkForToken()) { if(setValues == null || !checkForToken()) {
return; return;
} }
if (!checkValuesForProperties(setValues, TASK_PROPERTIES)) //None of the properties we sync were updated if (!checkValuesForProperties(setValues, TASK_PROPERTIES)) { //None of the properties we sync were updated
{
return; return;
} }

@ -16,15 +16,12 @@ import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri; import android.net.Uri;
import android.text.TextUtils; import android.text.TextUtils;
import com.todoroo.andlib.data.AbstractDatabase;
import com.todoroo.andlib.data.AbstractModel; import com.todoroo.andlib.data.AbstractModel;
import com.todoroo.andlib.data.DatabaseDao; import com.todoroo.andlib.data.DatabaseDao;
import com.todoroo.astrid.api.AstridApiConstants; import com.todoroo.astrid.api.AstridApiConstants;
import com.todoroo.astrid.dao.Database; import com.todoroo.astrid.dao.Database;
import com.todoroo.astrid.dao.MetadataDao; import com.todoroo.astrid.dao.RemoteModelDao;
import com.todoroo.astrid.dao.StoreObjectDao;
import com.todoroo.astrid.dao.TaskDao; import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.dao.UserActivityDao;
import com.todoroo.astrid.data.Metadata; import com.todoroo.astrid.data.Metadata;
import com.todoroo.astrid.data.StoreObject; import com.todoroo.astrid.data.StoreObject;
import com.todoroo.astrid.data.Task; import com.todoroo.astrid.data.Task;
@ -80,16 +77,13 @@ public class Astrid3ContentProvider extends InjectingContentProvider {
private static final UriMatcher uriMatcher; private static final UriMatcher uriMatcher;
private static AbstractDatabase databaseOverride; private static Database databaseOverride;
// --- instance variables // --- instance variables
private boolean open; private boolean open;
@Inject Lazy<Database> database; @Inject Lazy<Database> database;
@Inject Lazy<TaskDao> taskDao; @Inject Lazy<TaskDao> taskDao;
@Inject Lazy<MetadataDao> metadataDao;
@Inject Lazy<StoreObjectDao> storeObjectDao;
@Inject Lazy<UserActivityDao> userActivityDao;
static { static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
@ -150,41 +144,37 @@ public class Astrid3ContentProvider extends InjectingContentProvider {
} }
private UriHelper<?> generateHelper(Uri uri, boolean populateModel) { private UriHelper<?> generateHelper(Uri uri, boolean populateModel) {
AbstractDatabase db = getDatabase(); final Database db = getDatabase();
if(uri.toString().startsWith(Task.CONTENT_URI.toString())) { if(uri.toString().startsWith(Task.CONTENT_URI.toString())) {
UriHelper<Task> helper = new UriHelper<>(); UriHelper<Task> helper = new UriHelper<>();
helper.model = populateModel ? new Task() : null; helper.model = populateModel ? new Task() : null;
helper.dao = taskDao.get(); helper.dao = new RemoteModelDao<>(db, Task.class);
helper.dao.setDatabase(db);
return helper; return helper;
} else if(uri.toString().startsWith(Metadata.CONTENT_URI.toString())) { } else if(uri.toString().startsWith(Metadata.CONTENT_URI.toString())) {
UriHelper<Metadata> helper = new UriHelper<>(); UriHelper<Metadata> helper = new UriHelper<>();
helper.model = populateModel ? new Metadata() : null; helper.model = populateModel ? new Metadata() : null;
helper.dao = metadataDao.get(); helper.dao = new DatabaseDao<>(db, Metadata.class);
helper.dao.setDatabase(db);
return helper; return helper;
} else if(uri.toString().startsWith(StoreObject.CONTENT_URI.toString())) { } else if(uri.toString().startsWith(StoreObject.CONTENT_URI.toString())) {
UriHelper<StoreObject> helper = new UriHelper<>(); UriHelper<StoreObject> helper = new UriHelper<>();
helper.model = populateModel ? new StoreObject() : null; helper.model = populateModel ? new StoreObject() : null;
helper.dao = storeObjectDao.get(); helper.dao = new DatabaseDao<>(db, StoreObject.class);
helper.dao.setDatabase(db);
return helper; return helper;
} else if(uri.toString().startsWith(UserActivity.CONTENT_URI.toString())) { } else if(uri.toString().startsWith(UserActivity.CONTENT_URI.toString())) {
UriHelper<UserActivity> helper = new UriHelper<>(); UriHelper<UserActivity> helper = new UriHelper<>();
helper.model = populateModel ? new UserActivity() : null; helper.model = populateModel ? new UserActivity() : null;
helper.dao = userActivityDao.get(); helper.dao = new RemoteModelDao<>(db, UserActivity.class);
helper.dao.setDatabase(db);
return helper; return helper;
} }
throw new UnsupportedOperationException("Unknown URI " + uri); throw new UnsupportedOperationException("Unknown URI " + uri);
} }
public static void setDatabaseOverride(AbstractDatabase override) { public static void setDatabaseOverride(Database override) {
databaseOverride = override; databaseOverride = override;
} }
private AbstractDatabase getDatabase() { private Database getDatabase() {
if (!open) { if (!open) {
database.get().openForWriting(); database.get().openForWriting();
open = true; open = true;

Loading…
Cancel
Save