mirror of https://github.com/tasks/tasks
Remove MetadataService
parent
de251be167
commit
b0aaceb952
@ -1,61 +0,0 @@
|
||||
package com.todoroo.astrid.service;
|
||||
|
||||
import com.todoroo.astrid.dao.MetadataDao;
|
||||
import com.todoroo.astrid.dao.TaskDao;
|
||||
import com.todoroo.astrid.data.Metadata;
|
||||
import com.todoroo.astrid.data.Task;
|
||||
|
||||
import org.tasks.injection.InjectingTestCase;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class MetadataServiceTest extends InjectingTestCase {
|
||||
|
||||
@Inject MetadataService metadataService;
|
||||
@Inject MetadataDao metadataDao;
|
||||
@Inject TaskDao taskDao;
|
||||
|
||||
Metadata metadata;
|
||||
|
||||
@Override
|
||||
public void setUp() {
|
||||
super.setUp();
|
||||
metadata = new Metadata();
|
||||
}
|
||||
|
||||
public void testDontSaveMetadataWithoutTaskId() {
|
||||
try {
|
||||
metadataService.save(metadata);
|
||||
fail("expected exception");
|
||||
} catch(IllegalArgumentException e) {
|
||||
assertTrue(e.getMessage().startsWith("metadata needs to be attached to a task"));
|
||||
}
|
||||
}
|
||||
|
||||
public void testSaveMetadata() {
|
||||
metadata.setTask(1L);
|
||||
metadataService.save(metadata);
|
||||
|
||||
assertNotNull(metadataDao.fetch(metadata.getId()));
|
||||
}
|
||||
|
||||
public void testDontDeleteValidMetadata() {
|
||||
final Task task = new Task();
|
||||
taskDao.save(task);
|
||||
metadata.setTask(task.getId());
|
||||
metadataService.save(metadata);
|
||||
|
||||
metadataService.removeDanglingMetadata();
|
||||
|
||||
assertNotNull(metadataDao.fetch(metadata.getId()));
|
||||
}
|
||||
|
||||
public void testDeleteDangling() {
|
||||
metadata.setTask(1L);
|
||||
metadataService.save(metadata);
|
||||
|
||||
metadataService.removeDanglingMetadata();
|
||||
|
||||
assertNull(metadataDao.fetch(1));
|
||||
}
|
||||
}
|
@ -1,148 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2012 Todoroo Inc
|
||||
*
|
||||
* See the file "LICENSE" for the full license governing this code.
|
||||
*/
|
||||
package com.todoroo.astrid.service;
|
||||
|
||||
import android.content.ContentValues;
|
||||
|
||||
import com.todoroo.andlib.data.TodorooCursor;
|
||||
import com.todoroo.andlib.sql.Criterion;
|
||||
import com.todoroo.andlib.sql.Join;
|
||||
import com.todoroo.andlib.sql.Query;
|
||||
import com.todoroo.andlib.utility.DateUtilities;
|
||||
import com.todoroo.astrid.dao.MetadataDao;
|
||||
import com.todoroo.astrid.dao.MetadataDao.MetadataCriteria;
|
||||
import com.todoroo.astrid.data.Metadata;
|
||||
import com.todoroo.astrid.data.Task;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
/**
|
||||
* Service layer for {@link Metadata}-centered activities.
|
||||
*
|
||||
* @author Tim Su <tim@todoroo.com>
|
||||
*
|
||||
*/
|
||||
@Singleton
|
||||
public class MetadataService {
|
||||
|
||||
public static interface SynchronizeMetadataCallback {
|
||||
public void beforeDeleteMetadata(Metadata m);
|
||||
}
|
||||
|
||||
private final MetadataDao metadataDao;
|
||||
|
||||
@Inject
|
||||
public MetadataService(MetadataDao metadataDao) {
|
||||
this.metadataDao = metadataDao;
|
||||
}
|
||||
|
||||
// --- service layer
|
||||
|
||||
/**
|
||||
* Clean up metadata. Typically called on startup
|
||||
*/
|
||||
public void removeDanglingMetadata() {
|
||||
metadataDao.deleteWhere(Metadata.ID.in(Query.select(Metadata.ID).from(Metadata.TABLE).join(Join.left(Task.TABLE,
|
||||
Metadata.TASK.eq(Task.ID))).where(Task.TITLE.isNull())));
|
||||
}
|
||||
|
||||
/**
|
||||
* Query underlying database
|
||||
*/
|
||||
public TodorooCursor<Metadata> query(Query query) {
|
||||
return metadataDao.query(query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete from metadata table where rows match a certain condition
|
||||
* @param where predicate for which rows to update
|
||||
* @param metadata values to set
|
||||
*/
|
||||
public void update(Criterion where, Metadata metadata) {
|
||||
metadataDao.update(where, metadata);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save a single piece of metadata
|
||||
*/
|
||||
public void save(Metadata metadata) {
|
||||
if(!metadata.containsNonNullValue(Metadata.TASK)) {
|
||||
throw new IllegalArgumentException("metadata needs to be attached to a task: " + metadata.getMergedValues()); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
metadataDao.persist(metadata);
|
||||
}
|
||||
|
||||
/**
|
||||
* Synchronize metadata for given task id
|
||||
* @return true if there were changes
|
||||
*/
|
||||
public boolean synchronizeMetadata(long taskId, ArrayList<Metadata> metadata,
|
||||
Criterion metadataCriterion, SynchronizeMetadataCallback callback) {
|
||||
boolean dirty = false;
|
||||
HashSet<ContentValues> newMetadataValues = new HashSet<>();
|
||||
for(Metadata metadatum : metadata) {
|
||||
metadatum.setTask(taskId);
|
||||
metadatum.clearValue(Metadata.CREATION_DATE);
|
||||
metadatum.clearValue(Metadata.ID);
|
||||
|
||||
ContentValues values = metadatum.getMergedValues();
|
||||
for(Entry<String, Object> entry : values.valueSet()) {
|
||||
if(entry.getKey().startsWith("value")) //$NON-NLS-1$
|
||||
{
|
||||
values.put(entry.getKey(), entry.getValue().toString());
|
||||
}
|
||||
}
|
||||
newMetadataValues.add(values);
|
||||
}
|
||||
|
||||
TodorooCursor<Metadata> cursor = metadataDao.query(Query.select(Metadata.PROPERTIES).where(Criterion.and(MetadataCriteria.byTask(taskId),
|
||||
metadataCriterion)));
|
||||
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);
|
||||
item.clearValue(Metadata.CREATION_DATE);
|
||||
ContentValues itemMergedValues = item.getMergedValues();
|
||||
|
||||
if(newMetadataValues.contains(itemMergedValues)) {
|
||||
newMetadataValues.remove(itemMergedValues);
|
||||
continue;
|
||||
}
|
||||
|
||||
// not matched. cut it
|
||||
item.setId(id);
|
||||
if (callback != null) {
|
||||
callback.beforeDeleteMetadata(item);
|
||||
}
|
||||
metadataDao.delete(id);
|
||||
dirty = true;
|
||||
}
|
||||
} finally {
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
// everything that remains shall be written
|
||||
for(ContentValues values : newMetadataValues) {
|
||||
Metadata item = new Metadata();
|
||||
item.setCreationDate(DateUtilities.now());
|
||||
item.mergeWith(values);
|
||||
metadataDao.persist(item);
|
||||
dirty = true;
|
||||
}
|
||||
|
||||
return dirty;
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.todoroo.astrid.service;
|
||||
|
||||
import com.todoroo.astrid.data.Metadata;
|
||||
|
||||
public interface SynchronizeMetadataCallback {
|
||||
public void beforeDeleteMetadata(Metadata m);
|
||||
}
|
Loading…
Reference in New Issue