|
|
|
@ -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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|