diff --git a/api/src/com/todoroo/astrid/data/MetadataApiDao.java b/api/src/com/todoroo/astrid/data/MetadataApiDao.java index 6296c4ade..1a92f0b6d 100644 --- a/api/src/com/todoroo/astrid/data/MetadataApiDao.java +++ b/api/src/com/todoroo/astrid/data/MetadataApiDao.java @@ -5,16 +5,10 @@ */ package com.todoroo.astrid.data; -import java.util.ArrayList; -import java.util.HashSet; - -import android.content.ContentValues; import android.content.Context; import com.todoroo.andlib.data.ContentResolverDao; -import com.todoroo.andlib.data.TodorooCursor; import com.todoroo.andlib.sql.Criterion; -import com.todoroo.andlib.sql.Query; /** * Data access object for accessing Astrid's {@link Metadata} table. A @@ -52,55 +46,4 @@ public class MetadataApiDao extends ContentResolverDao { } - /** - * Synchronize metadata for given task id. Deletes rows in database that - * are not identical to those in the metadata list, creates rows that - * have no match. - * - * @param taskId id of task to perform synchronization on - * @param metadata list of new metadata items to save - * @param metadataCriteria criteria to load data for comparison from metadata - */ - public void synchronizeMetadata(long taskId, ArrayList metadata, - Criterion metadataCriteria) { - HashSet newMetadataValues = new HashSet(); - for(Metadata metadatum : metadata) { - metadatum.setValue(Metadata.TASK, taskId); - metadatum.clearValue(Metadata.ID); - newMetadataValues.add(metadatum.getMergedValues()); - } - - Metadata item = new Metadata(); - TodorooCursor cursor = query(Query.select(Metadata.PROPERTIES).where(Criterion.and(MetadataCriteria.byTask(taskId), - metadataCriteria))); - try { - // try to find matches within our metadata list - for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { - item.readFromCursor(cursor); - long id = item.getId(); - - // clear item id when matching with incoming values - item.clearValue(Metadata.ID); - ContentValues itemMergedValues = item.getMergedValues(); - if(newMetadataValues.contains(itemMergedValues)) { - newMetadataValues.remove(itemMergedValues); - continue; - } - - // not matched. cut it - delete(id); - } - } finally { - cursor.close(); - } - - // everything that remains shall be written - for(ContentValues values : newMetadataValues) { - item.clear(); - item.mergeWith(values); - save(item); - } - } - - } diff --git a/astrid/plugin-src/com/todoroo/astrid/gtasks/GtasksMetadataService.java b/astrid/plugin-src/com/todoroo/astrid/gtasks/GtasksMetadataService.java index 7511a33d1..e0e283cf7 100644 --- a/astrid/plugin-src/com/todoroo/astrid/gtasks/GtasksMetadataService.java +++ b/astrid/plugin-src/com/todoroo/astrid/gtasks/GtasksMetadataService.java @@ -30,8 +30,8 @@ import com.todoroo.astrid.data.StoreObject; import com.todoroo.astrid.data.Task; import com.todoroo.astrid.gtasks.sync.GtasksTaskContainer; import com.todoroo.astrid.subtasks.OrderedMetadataListUpdater.OrderedListIterator; -import com.todoroo.astrid.sync.SyncMetadataService; import com.todoroo.astrid.sync.SyncProviderUtilities; +import com.todoroo.astrid.utility.SyncMetadataService; /** * Service for working with GTasks metadata diff --git a/astrid/rmilk-src/org/weloveastrid/rmilk/data/MilkMetadataService.java b/astrid/rmilk-src/org/weloveastrid/rmilk/data/MilkMetadataService.java index d86990065..045d4a58e 100644 --- a/astrid/rmilk-src/org/weloveastrid/rmilk/data/MilkMetadataService.java +++ b/astrid/rmilk-src/org/weloveastrid/rmilk/data/MilkMetadataService.java @@ -19,8 +19,8 @@ import com.todoroo.astrid.dao.MetadataDao.MetadataCriteria; import com.todoroo.astrid.data.Metadata; import com.todoroo.astrid.data.Task; import com.todoroo.astrid.notes.NoteMetadata; -import com.todoroo.astrid.sync.SyncMetadataService; import com.todoroo.astrid.sync.SyncProviderUtilities; +import com.todoroo.astrid.utility.SyncMetadataService; public final class MilkMetadataService extends SyncMetadataService{ diff --git a/astrid/src/com/todoroo/astrid/dao/MetadataDao.java b/astrid/src/com/todoroo/astrid/dao/MetadataDao.java index 52339ce40..b2e720024 100644 --- a/astrid/src/com/todoroo/astrid/dao/MetadataDao.java +++ b/astrid/src/com/todoroo/astrid/dao/MetadataDao.java @@ -5,6 +5,9 @@ */ package com.todoroo.astrid.dao; +import java.util.ArrayList; +import java.util.HashSet; + import android.content.ContentValues; import android.database.Cursor; @@ -110,6 +113,56 @@ public class MetadataDao extends DatabaseDao { return 1; } + /** + * Synchronize metadata for given task id. Deletes rows in database that + * are not identical to those in the metadata list, creates rows that + * have no match. + * + * @param taskId id of task to perform synchronization on + * @param metadata list of new metadata items to save + * @param metadataCriteria criteria to load data for comparison from metadata + */ + public void synchronizeMetadata(long taskId, ArrayList metadata, + Criterion metadataCriteria) { + HashSet newMetadataValues = new HashSet(); + for(Metadata metadatum : metadata) { + metadatum.setValue(Metadata.TASK, taskId); + metadatum.clearValue(Metadata.ID); + newMetadataValues.add(metadatum.getMergedValues()); + } + + Metadata item = new Metadata(); + TodorooCursor cursor = query(Query.select(Metadata.PROPERTIES).where(Criterion.and(MetadataCriteria.byTask(taskId), + metadataCriteria))); + try { + // try to find matches within our metadata list + for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { + item.readFromCursor(cursor); + long id = item.getId(); + + // clear item id when matching with incoming values + item.clearValue(Metadata.ID); + ContentValues itemMergedValues = item.getMergedValues(); + if(newMetadataValues.contains(itemMergedValues)) { + newMetadataValues.remove(itemMergedValues); + continue; + } + + // not matched. cut it + delete(id); + } + } finally { + cursor.close(); + } + + // everything that remains shall be written + for(ContentValues values : newMetadataValues) { + item.clear(); + item.mergeWith(values); + persist(item); + } + } + @Override public boolean persist(Metadata item) { if(!item.containsValue(Metadata.CREATION_DATE)) diff --git a/api/src/com/todoroo/astrid/sync/SyncMetadataService.java b/astrid/src/com/todoroo/astrid/utility/SyncMetadataService.java similarity index 92% rename from api/src/com/todoroo/astrid/sync/SyncMetadataService.java rename to astrid/src/com/todoroo/astrid/utility/SyncMetadataService.java index c70647b72..8a0d28cf2 100644 --- a/api/src/com/todoroo/astrid/sync/SyncMetadataService.java +++ b/astrid/src/com/todoroo/astrid/utility/SyncMetadataService.java @@ -3,7 +3,7 @@ * * See the file "LICENSE" for the full license governing this code. */ -package com.todoroo.astrid.sync; +package com.todoroo.astrid.utility; import java.util.ArrayList; @@ -11,15 +11,19 @@ import android.content.Context; import com.todoroo.andlib.data.Property; import com.todoroo.andlib.data.TodorooCursor; +import com.todoroo.andlib.service.Autowired; +import com.todoroo.andlib.service.DependencyInjectionService; import com.todoroo.andlib.sql.Criterion; import com.todoroo.andlib.sql.Order; import com.todoroo.andlib.sql.Query; +import com.todoroo.astrid.dao.MetadataDao; +import com.todoroo.astrid.dao.MetadataDao.MetadataCriteria; +import com.todoroo.astrid.dao.TaskDao; +import com.todoroo.astrid.dao.TaskDao.TaskCriteria; import com.todoroo.astrid.data.Metadata; -import com.todoroo.astrid.data.MetadataApiDao; -import com.todoroo.astrid.data.MetadataApiDao.MetadataCriteria; import com.todoroo.astrid.data.Task; -import com.todoroo.astrid.data.TaskApiDao; -import com.todoroo.astrid.data.TaskApiDao.TaskCriteria; +import com.todoroo.astrid.sync.SyncContainer; +import com.todoroo.astrid.sync.SyncProviderUtilities; abstract public class SyncMetadataService { @@ -28,8 +32,10 @@ abstract public class SyncMetadataService { // --- instance variables - protected final TaskApiDao taskDao; - protected final MetadataApiDao metadataDao; + @Autowired + protected TaskDao taskDao; + @Autowired + protected MetadataDao metadataDao; // --- abstract methods @@ -54,8 +60,7 @@ abstract public class SyncMetadataService { // --- implementation public SyncMetadataService(Context context) { - taskDao = new TaskApiDao(context); - metadataDao = new MetadataApiDao(context); + DependencyInjectionService.getInstance().inject(this); } /**