From 07669aadbbe2016e03ed64ebdd954260d76cd237 Mon Sep 17 00:00:00 2001 From: Tim Su Date: Tue, 8 Nov 2011 02:12:44 -0800 Subject: [PATCH] Added new gtasks sync metadata for saving last sync time --- .../astrid/sync/SyncMetadataService.java | 15 ++++-- .../astrid/gtasks/GtasksMetadataService.java | 17 +++++++ .../astrid/gtasks/GtasksSyncMetadata.java | 49 +++++++++++++++++++ .../gtasks/sync/GtasksSyncOnSaveService.java | 3 +- 4 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 astrid/plugin-src/com/todoroo/astrid/gtasks/GtasksSyncMetadata.java diff --git a/api/src/com/todoroo/astrid/sync/SyncMetadataService.java b/api/src/com/todoroo/astrid/sync/SyncMetadataService.java index 265481e0f..6bfda336a 100644 --- a/api/src/com/todoroo/astrid/sync/SyncMetadataService.java +++ b/api/src/com/todoroo/astrid/sync/SyncMetadataService.java @@ -97,13 +97,22 @@ abstract public class SyncMetadataService { if(lastSyncDate == 0) tasks = taskDao.query(Query.select(Task.ID).where(Criterion.none)); else - tasks = taskDao.query(Query.select(Task.ID).where(Criterion.and( - Task.MODIFICATION_DATE.gt(lastSyncDate), - Task.LAST_SYNC.lte(lastSyncDate))).orderBy(Order.asc(Task.ID))); + tasks = taskDao.query(Query.select(Task.ID).where( + Task.MODIFICATION_DATE.gt(lastSyncDate)).orderBy(Order.asc(Task.ID))); + tasks = filterLocallyUpdated(tasks, lastSyncDate); return joinWithMetadata(tasks, true, properties); } + /** + * @param tasks + * @param lastSyncDate + */ + protected TodorooCursor filterLocallyUpdated(TodorooCursor tasks, long lastSyncDate) { + // override hook + return tasks; + } + private TodorooCursor joinWithMetadata(TodorooCursor tasks, boolean both, Property... properties) { try { diff --git a/astrid/plugin-src/com/todoroo/astrid/gtasks/GtasksMetadataService.java b/astrid/plugin-src/com/todoroo/astrid/gtasks/GtasksMetadataService.java index 657d2af73..c271ef76f 100644 --- a/astrid/plugin-src/com/todoroo/astrid/gtasks/GtasksMetadataService.java +++ b/astrid/plugin-src/com/todoroo/astrid/gtasks/GtasksMetadataService.java @@ -4,6 +4,7 @@ package com.todoroo.astrid.gtasks; import java.util.ArrayList; +import java.util.HashSet; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; @@ -74,6 +75,22 @@ public final class GtasksMetadataService extends SyncMetadataService filterLocallyUpdated(TodorooCursor tasks, long lastSyncDate) { + HashSet taskIds = new HashSet(); + for(tasks.moveToFirst(); !tasks.isAfterLast(); tasks.moveToNext()) + taskIds.add(tasks.get(Task.ID)); + + TodorooCursor metadata = metadataDao.query(Query.select(Metadata.TASK).where( + Criterion.and(MetadataCriteria.withKey(GtasksSyncMetadata.METADATA_KEY), + GtasksSyncMetadata.LAST_SYNC.lt(lastSyncDate)))); + for(metadata.moveToFirst(); !metadata.isAfterLast(); metadata.moveToNext()) + taskIds.remove(metadata.get(Metadata.TASK)); + + return taskDao.query(Query.select(Task.ID).where( + Task.ID.in(taskIds.toArray(new Long[taskIds.size()])))); + } + // --- list iterating helpers diff --git a/astrid/plugin-src/com/todoroo/astrid/gtasks/GtasksSyncMetadata.java b/astrid/plugin-src/com/todoroo/astrid/gtasks/GtasksSyncMetadata.java new file mode 100644 index 000000000..2ff903c1a --- /dev/null +++ b/astrid/plugin-src/com/todoroo/astrid/gtasks/GtasksSyncMetadata.java @@ -0,0 +1,49 @@ +package com.todoroo.astrid.gtasks; + +import com.todoroo.andlib.data.Property; +import com.todoroo.andlib.data.Property.LongProperty; +import com.todoroo.andlib.data.TodorooCursor; +import com.todoroo.andlib.sql.Query; +import com.todoroo.astrid.dao.MetadataDao; +import com.todoroo.astrid.dao.MetadataDao.MetadataCriteria; +import com.todoroo.astrid.data.Metadata; + +/** + * Metadata entries for synchronizing a GTasks Task + * @author Tim Su + * + */ +public class GtasksSyncMetadata { + + /** metadata key */ + public static final String METADATA_KEY = "gtasks-sync"; //$NON-NLS-1$ + + /** last sync date*/ + public static final LongProperty LAST_SYNC = new LongProperty(Metadata.TABLE, + Metadata.VALUE1.name); + + /** + * Helper to set value + * @param metadataDao + * @param id + * @param property + * @param now + */ + public static void set(MetadataDao metadataDao, long taskId, + Property property, T value) { + TodorooCursor cursor = metadataDao.query(Query.select(Metadata.PROPERTIES). + where(MetadataCriteria.byTaskAndwithKey(taskId, METADATA_KEY))); + Metadata metadata = new Metadata(); + if(cursor.getCount() == 0) { + metadata.setValue(Metadata.TASK, taskId); + metadata.setValue(Metadata.KEY, METADATA_KEY); + } else { + cursor.moveToFirst(); + metadata.readFromCursor(cursor); + } + + metadata.setValue(property, value); + metadataDao.persist(metadata); + } + +} diff --git a/astrid/plugin-src/com/todoroo/astrid/gtasks/sync/GtasksSyncOnSaveService.java b/astrid/plugin-src/com/todoroo/astrid/gtasks/sync/GtasksSyncOnSaveService.java index 33094dc4f..cb47e3b47 100644 --- a/astrid/plugin-src/com/todoroo/astrid/gtasks/sync/GtasksSyncOnSaveService.java +++ b/astrid/plugin-src/com/todoroo/astrid/gtasks/sync/GtasksSyncOnSaveService.java @@ -21,6 +21,7 @@ import com.todoroo.astrid.data.Task; import com.todoroo.astrid.gtasks.GtasksMetadata; import com.todoroo.astrid.gtasks.GtasksMetadataService; import com.todoroo.astrid.gtasks.GtasksPreferenceService; +import com.todoroo.astrid.gtasks.GtasksSyncMetadata; import com.todoroo.astrid.gtasks.GtasksTaskListUpdater; import com.todoroo.astrid.gtasks.api.GtasksApiUtilities; import com.todoroo.astrid.gtasks.api.GtasksService; @@ -253,7 +254,7 @@ public final class GtasksSyncOnSaveService { } task.setValue(Task.MODIFICATION_DATE, DateUtilities.now()); - task.setValue(Task.LAST_SYNC, DateUtilities.now()); + GtasksSyncMetadata.set(metadataDao, task.getId(), GtasksSyncMetadata.LAST_SYNC, DateUtilities.now()); Flags.set(Flags.GTASKS_SUPPRESS_SYNC); taskDao.saveExisting(task); }