From d7b6ebbc58634233840ccf652155811e5b041bfb Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Sat, 19 Jul 2014 01:17:19 -0500 Subject: [PATCH] Remove SyncMetadataService --- .../astrid/gtasks/GtasksMetadataService.java | 83 +++++++++++++- .../astrid/utility/SyncMetadataService.java | 105 ------------------ 2 files changed, 78 insertions(+), 110 deletions(-) delete mode 100644 astrid/src/main/java/com/todoroo/astrid/utility/SyncMetadataService.java diff --git a/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksMetadataService.java b/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksMetadataService.java index da3aa4346..e8da63710 100644 --- a/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksMetadataService.java +++ b/astrid/src/main/java/com/todoroo/astrid/gtasks/GtasksMetadataService.java @@ -5,6 +5,7 @@ */ package com.todoroo.astrid.gtasks; +import android.content.ContentValues; import android.text.TextUtils; import com.todoroo.andlib.data.AbstractModel; @@ -22,8 +23,10 @@ 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.utility.SyncMetadataService; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; @@ -38,15 +41,85 @@ import javax.inject.Singleton; * */ @Singleton -public final class GtasksMetadataService extends SyncMetadataService { +public final class GtasksMetadataService { + + private final TaskDao taskDao; + private final MetadataDao metadataDao; @Inject public GtasksMetadataService(TaskDao taskDao, MetadataDao metadataDao) { - super(taskDao, metadataDao); + this.taskDao = taskDao; + this.metadataDao = metadataDao; + } + + /** + * Clears metadata information. Used when user logs out of sync provider + */ + public void clearMetadata() { + metadataDao.deleteWhere(Metadata.KEY.eq(getMetadataKey())); + } + + /** + * Saves a task and its metadata + */ + public void saveTaskAndMetadata(GtasksTaskContainer task) { + task.prepareForSaving(); + taskDao.save(task.task); + synchronizeMetadata(task.task.getId(), task.metadata, getMetadataKey()); + } + + /** + * Reads metadata out of a task + * @return null if no metadata found + */ + public Metadata getTaskMetadata(long taskId) { + return metadataDao.getFirst(Query.select(Metadata.PROPERTIES).where( + MetadataCriteria.byTaskAndwithKey(taskId, getMetadataKey()))); + } + + /** + * 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 metadataKey metadata key + */ + private void synchronizeMetadata(long taskId, ArrayList metadata, String metadataKey) { + final Set newMetadataValues = new HashSet<>(); + for(Metadata metadatum : metadata) { + metadatum.setTask(taskId); + metadatum.clearValue(Metadata.ID); + newMetadataValues.add(metadatum.getMergedValues()); + } + + metadataDao.byTaskAndKey(taskId, metadataKey, new Callback() { + @Override + public void apply(Metadata item) { + 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); + } else { + // not matched. cut it + metadataDao.delete(id); + } + } + }); + + // everything that remains shall be written + for(ContentValues values : newMetadataValues) { + Metadata item = new Metadata(); + item.mergeWith(values); + metadataDao.persist(item); + } } - @Override - public String getMetadataKey() { + private String getMetadataKey() { return GtasksMetadata.METADATA_KEY; } diff --git a/astrid/src/main/java/com/todoroo/astrid/utility/SyncMetadataService.java b/astrid/src/main/java/com/todoroo/astrid/utility/SyncMetadataService.java deleted file mode 100644 index 0a9bb2d7a..000000000 --- a/astrid/src/main/java/com/todoroo/astrid/utility/SyncMetadataService.java +++ /dev/null @@ -1,105 +0,0 @@ -/** - * Copyright (c) 2012 Todoroo Inc - * - * See the file "LICENSE" for the full license governing this code. - */ -package com.todoroo.astrid.utility; - -import android.content.ContentValues; - -import com.todoroo.andlib.data.Callback; -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.data.Metadata; -import com.todoroo.astrid.gtasks.sync.GtasksTaskContainer; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.Set; - -abstract public class SyncMetadataService { - - protected final TaskDao taskDao; - protected final MetadataDao metadataDao; - - // --- abstract methods - - /** @return metadata key identifying this sync provider's metadata */ - abstract public String getMetadataKey(); - - // --- implementation - - public SyncMetadataService(TaskDao taskDao, MetadataDao metadataDao) { - this.taskDao = taskDao; - this.metadataDao = metadataDao; - } - - /** - * Clears metadata information. Used when user logs out of sync provider - */ - public void clearMetadata() { - metadataDao.deleteWhere(Metadata.KEY.eq(getMetadataKey())); - } - - /** - * Saves a task and its metadata - */ - public void saveTaskAndMetadata(GtasksTaskContainer task) { - task.prepareForSaving(); - taskDao.save(task.task); - synchronizeMetadata(task.task.getId(), task.metadata, getMetadataKey()); - } - - /** - * Reads metadata out of a task - * @return null if no metadata found - */ - public Metadata getTaskMetadata(long taskId) { - return metadataDao.getFirst(Query.select(Metadata.PROPERTIES).where( - MetadataCriteria.byTaskAndwithKey(taskId, getMetadataKey()))); - } - - /** - * 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 metadataKey metadata key - */ - private void synchronizeMetadata(long taskId, ArrayList metadata, String metadataKey) { - final Set newMetadataValues = new HashSet<>(); - for(Metadata metadatum : metadata) { - metadatum.setTask(taskId); - metadatum.clearValue(Metadata.ID); - newMetadataValues.add(metadatum.getMergedValues()); - } - - metadataDao.byTaskAndKey(taskId, metadataKey, new Callback() { - @Override - public void apply(Metadata item) { - 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); - } else { - // not matched. cut it - metadataDao.delete(id); - } - } - }); - - // everything that remains shall be written - for(ContentValues values : newMetadataValues) { - Metadata item = new Metadata(); - item.mergeWith(values); - metadataDao.persist(item); - } - } -}