Move synchronizeMetadata to SyncMetadataService

pull/189/head
Alex Baker 10 years ago
parent d252e86f50
commit e30f552e6e

@ -5,10 +5,7 @@
*/
package com.todoroo.astrid.dao;
import android.content.ContentValues;
import com.todoroo.andlib.data.DatabaseDao;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.sql.Criterion;
import com.todoroo.andlib.sql.Join;
import com.todoroo.andlib.sql.Query;
@ -16,9 +13,6 @@ import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.astrid.data.Metadata;
import com.todoroo.astrid.data.Task;
import java.util.ArrayList;
import java.util.HashSet;
import javax.inject.Inject;
import javax.inject.Singleton;
@ -61,54 +55,6 @@ public class MetadataDao extends DatabaseDao<Metadata> {
}
/**
* 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
*/
public void synchronizeMetadata(long taskId, ArrayList<Metadata> metadata, String metadataKey) {
HashSet<ContentValues> newMetadataValues = new HashSet<>();
for(Metadata metadatum : metadata) {
metadatum.setTask(taskId);
metadatum.clearValue(Metadata.ID);
newMetadataValues.add(metadatum.getMergedValues());
}
TodorooCursor<Metadata> cursor = query(Query.select(Metadata.PROPERTIES).where(
MetadataCriteria.byTaskAndwithKey(taskId, metadataKey)));
try {
// try to find matches within our metadata list
for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
Metadata item = new Metadata(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) {
Metadata item = new Metadata();
item.mergeWith(values);
persist(item);
}
}
@Override
public boolean persist(Metadata item) {
if(!item.containsNonNullValue(Metadata.TASK)) {

@ -5,6 +5,8 @@
*/
package com.todoroo.astrid.utility;
import android.content.ContentValues;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.sql.Query;
import com.todoroo.astrid.dao.MetadataDao;
@ -13,6 +15,9 @@ import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.data.Metadata;
import com.todoroo.astrid.sync.SyncContainer;
import java.util.ArrayList;
import java.util.HashSet;
abstract public class SyncMetadataService<TYPE extends SyncContainer> {
protected final TaskDao taskDao;
@ -43,7 +48,7 @@ abstract public class SyncMetadataService<TYPE extends SyncContainer> {
public void saveTaskAndMetadata(TYPE task) {
task.prepareForSaving();
taskDao.save(task.task);
metadataDao.synchronizeMetadata(task.task.getId(), task.metadata, getMetadataKey());
synchronizeMetadata(task.task.getId(), task.metadata, getMetadataKey());
}
/**
@ -63,4 +68,52 @@ abstract public class SyncMetadataService<TYPE extends SyncContainer> {
cursor.close();
}
}
/**
* 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> metadata, String metadataKey) {
HashSet<ContentValues> newMetadataValues = new HashSet<>();
for(Metadata metadatum : metadata) {
metadatum.setTask(taskId);
metadatum.clearValue(Metadata.ID);
newMetadataValues.add(metadatum.getMergedValues());
}
TodorooCursor<Metadata> cursor = metadataDao.query(Query.select(Metadata.PROPERTIES).where(
MetadataCriteria.byTaskAndwithKey(taskId, metadataKey)));
try {
// try to find matches within our metadata list
for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
Metadata item = new Metadata(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
metadataDao.delete(id);
}
} finally {
cursor.close();
}
// everything that remains shall be written
for(ContentValues values : newMetadataValues) {
Metadata item = new Metadata();
item.mergeWith(values);
metadataDao.persist(item);
}
}
}

Loading…
Cancel
Save