Moved SyncMetadataService to the main astrid project to remove more references to the api daos

pull/14/head
Sam Bosley 12 years ago
parent 976d1d3a78
commit 6524d1b0a0

@ -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<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 metadataCriteria criteria to load data for comparison from metadata
*/
public void synchronizeMetadata(long taskId, ArrayList<Metadata> metadata,
Criterion metadataCriteria) {
HashSet<ContentValues> newMetadataValues = new HashSet<ContentValues>();
for(Metadata metadatum : metadata) {
metadatum.setValue(Metadata.TASK, taskId);
metadatum.clearValue(Metadata.ID);
newMetadataValues.add(metadatum.getMergedValues());
}
Metadata item = new Metadata();
TodorooCursor<Metadata> 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);
}
}
}

@ -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

@ -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<MilkTaskContainer>{

@ -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<Metadata> {
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> metadata,
Criterion metadataCriteria) {
HashSet<ContentValues> newMetadataValues = new HashSet<ContentValues>();
for(Metadata metadatum : metadata) {
metadatum.setValue(Metadata.TASK, taskId);
metadatum.clearValue(Metadata.ID);
newMetadataValues.add(metadatum.getMergedValues());
}
Metadata item = new Metadata();
TodorooCursor<Metadata> 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))

@ -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<TYPE extends SyncContainer> {
@ -28,8 +32,10 @@ abstract public class SyncMetadataService<TYPE extends SyncContainer> {
// --- 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<TYPE extends SyncContainer> {
// --- implementation
public SyncMetadataService(Context context) {
taskDao = new TaskApiDao(context);
metadataDao = new MetadataApiDao(context);
DependencyInjectionService.getInstance().inject(this);
}
/**
Loading…
Cancel
Save