mirror of https://github.com/tasks/tasks
Made significant progress on producteev sync provider. does not compile yet, but really close.
parent
3c8141bd6c
commit
bd37bcd567
@ -0,0 +1,191 @@
|
||||
/**
|
||||
* See the file "LICENSE" for the full license governing this code.
|
||||
*/
|
||||
package com.todoroo.astrid.producteev.sync;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
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.ContextManager;
|
||||
import com.todoroo.andlib.service.DependencyInjectionService;
|
||||
import com.todoroo.andlib.sql.Criterion;
|
||||
import com.todoroo.andlib.sql.Join;
|
||||
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.model.Metadata;
|
||||
import com.todoroo.astrid.model.Task;
|
||||
import com.todoroo.astrid.rmilk.sync.RTMTaskContainer;
|
||||
import com.todoroo.astrid.tags.TagService;
|
||||
|
||||
public final class ProducteevDataService {
|
||||
|
||||
// --- constants
|
||||
|
||||
/** Utility for joining tasks with metadata */
|
||||
public static final Join METADATA_JOIN = Join.left(Metadata.TABLE, Task.ID.eq(Metadata.TASK));
|
||||
|
||||
// --- singleton
|
||||
|
||||
private static ProducteevDataService instance = null;
|
||||
|
||||
public static synchronized ProducteevDataService getInstance() {
|
||||
if(instance == null)
|
||||
instance = new ProducteevDataService(ContextManager.getContext());
|
||||
return instance;
|
||||
}
|
||||
|
||||
// --- instance variables
|
||||
|
||||
protected final Context context;
|
||||
|
||||
@Autowired
|
||||
private TaskDao taskDao;
|
||||
|
||||
@Autowired
|
||||
private MetadataDao metadataDao;
|
||||
|
||||
static final Random random = new Random();
|
||||
|
||||
private ProducteevDataService(Context context) {
|
||||
this.context = context;
|
||||
DependencyInjectionService.getInstance().inject(this);
|
||||
}
|
||||
|
||||
// --- task and metadata methods
|
||||
|
||||
/**
|
||||
* Clears RTM metadata information. Used when user logs out of RTM
|
||||
*/
|
||||
public void clearMetadata() {
|
||||
metadataDao.deleteWhere(Metadata.KEY.eq(ProducteevTask.METADATA_KEY));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets tasks that were created since last sync
|
||||
* @param properties
|
||||
* @return
|
||||
*/
|
||||
public TodorooCursor<Task> getLocallyCreated(Property<?>[] properties) {
|
||||
return
|
||||
taskDao.query(Query.select(properties).join(ProducteevDataService.METADATA_JOIN).where(Criterion.and(
|
||||
Criterion.not(Task.ID.in(Query.select(Metadata.TASK).from(Metadata.TABLE).
|
||||
where(Criterion.and(MetadataCriteria.withKey(ProducteevTask.METADATA_KEY), ProducteevTask.TASK_SERIES_ID.gt(0))))),
|
||||
TaskCriteria.isActive())));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets tasks that were modified since last sync
|
||||
* @param properties
|
||||
* @return null if never sync'd
|
||||
*/
|
||||
public TodorooCursor<Task> getLocallyUpdated(Property<?>[] properties) {
|
||||
long lastSyncDate = ProducteevUtilities.getLastSyncDate();
|
||||
if(lastSyncDate == 0)
|
||||
return taskDao.query(Query.select(Task.ID).where(Criterion.none));
|
||||
return
|
||||
taskDao.query(Query.select(properties).join(ProducteevDataService.METADATA_JOIN).
|
||||
where(Criterion.and(MetadataCriteria.withKey(ProducteevTask.METADATA_KEY),
|
||||
Task.MODIFICATION_DATE.gt(lastSyncDate))));
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches for a local task with same remote id, updates this task's id
|
||||
* @param remoteTask
|
||||
*/
|
||||
public void findLocalMatch(RTMTaskContainer remoteTask) {
|
||||
if(remoteTask.task.getId() != Task.NO_ID)
|
||||
return;
|
||||
TodorooCursor<Task> cursor = taskDao.query(Query.select(Task.ID).
|
||||
join(ProducteevDataService.METADATA_JOIN).where(Criterion.and(MetadataCriteria.withKey(ProducteevTask.METADATA_KEY),
|
||||
ProducteevTask.TASK_SERIES_ID.eq(remoteTask.taskSeriesId),
|
||||
ProducteevTask.TASK_ID.eq(remoteTask.taskId))));
|
||||
try {
|
||||
if(cursor.getCount() == 0)
|
||||
return;
|
||||
cursor.moveToFirst();
|
||||
remoteTask.task.setId(cursor.get(Task.ID));
|
||||
} finally {
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves a task and its metadata
|
||||
* @param task
|
||||
*/
|
||||
public void saveTaskAndMetadata(ProducteevTaskContainer task) {
|
||||
taskDao.save(task.task, true);
|
||||
|
||||
metadataDao.deleteWhere(Criterion.and(MetadataCriteria.byTask(task.task.getId()),
|
||||
Criterion.or(MetadataCriteria.withKey(ProducteevTask.METADATA_KEY),
|
||||
MetadataCriteria.withKey(ProducteevNote.METADATA_KEY),
|
||||
MetadataCriteria.withKey(TagService.KEY))));
|
||||
task.metadata.add(ProducteevTask.create(task));
|
||||
for(Metadata metadata : task.metadata) {
|
||||
metadata.setValue(Metadata.TASK, task.task.getId());
|
||||
metadataDao.createNew(metadata);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a task and its metadata
|
||||
* @param task
|
||||
* @return
|
||||
*/
|
||||
public ProducteevTaskContainer readTaskAndMetadata(TodorooCursor<Task> taskCursor) {
|
||||
Task task = new Task(taskCursor);
|
||||
|
||||
// read tags, notes, etc
|
||||
ArrayList<Metadata> metadata = new ArrayList<Metadata>();
|
||||
TodorooCursor<Metadata> metadataCursor = metadataDao.query(Query.select(Metadata.PROPERTIES).
|
||||
where(Criterion.and(MetadataCriteria.byTask(task.getId()),
|
||||
Criterion.or(MetadataCriteria.withKey(TagService.KEY),
|
||||
MetadataCriteria.withKey(ProducteevTask.METADATA_KEY),
|
||||
MetadataCriteria.withKey(ProducteevNote.METADATA_KEY)))));
|
||||
try {
|
||||
for(metadataCursor.moveToFirst(); !metadataCursor.isAfterLast(); metadataCursor.moveToNext()) {
|
||||
metadata.add(new Metadata(metadataCursor));
|
||||
}
|
||||
} finally {
|
||||
metadataCursor.close();
|
||||
}
|
||||
|
||||
return new RTMTaskContainer(task, metadata);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads metadata out of a task
|
||||
* @return null if no metadata found
|
||||
*/
|
||||
public Metadata getTaskMetadata(long taskId) {
|
||||
TodorooCursor<Metadata> cursor = metadataDao.query(Query.select(
|
||||
ProducteevTask.LIST_ID, ProducteevTask.TASK_SERIES_ID, ProducteevTask.TASK_ID, ProducteevTask.REPEATING).where(
|
||||
MetadataCriteria.byTaskAndwithKey(taskId, ProducteevTask.METADATA_KEY)));
|
||||
try {
|
||||
if(cursor.getCount() == 0)
|
||||
return null;
|
||||
cursor.moveToFirst();
|
||||
return new Metadata(cursor);
|
||||
} finally {
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads task notes out of a task
|
||||
*/
|
||||
public TodorooCursor<Metadata> getTaskNotesCursor(long taskId) {
|
||||
TodorooCursor<Metadata> cursor = metadataDao.query(Query.select(Metadata.PROPERTIES).
|
||||
where(MetadataCriteria.byTaskAndwithKey(taskId, ProducteevNote.METADATA_KEY)));
|
||||
return cursor;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package com.todoroo.astrid.producteev.sync;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import com.todoroo.andlib.data.Property.LongProperty;
|
||||
import com.todoroo.andlib.data.Property.StringProperty;
|
||||
import com.todoroo.astrid.model.Metadata;
|
||||
import com.todoroo.astrid.producteev.api.ApiUtilities;
|
||||
|
||||
/**
|
||||
* Metadata entries for a Producteev note. The first Producteev note becomes
|
||||
* Astrid's note field, subsequent notes are stored in metadata in this
|
||||
* format.
|
||||
*
|
||||
* @author Tim Su <tim@todoroo.com>
|
||||
*
|
||||
*/
|
||||
public class ProducteevNote {
|
||||
|
||||
/** metadata key */
|
||||
public static final String METADATA_KEY = "producteev-note"; //$NON-NLS-1$
|
||||
|
||||
/** note id */
|
||||
public static final LongProperty ID = new LongProperty(Metadata.TABLE,
|
||||
Metadata.VALUE1.name);
|
||||
|
||||
/** note message */
|
||||
public static final StringProperty MESSAGE = Metadata.VALUE2;
|
||||
|
||||
/** note creation date */
|
||||
public static final LongProperty CREATED = new LongProperty(Metadata.TABLE,
|
||||
Metadata.VALUE3.name);
|
||||
|
||||
@SuppressWarnings("nls")
|
||||
public static Metadata create(JSONObject note) {
|
||||
Metadata metadata = new Metadata();
|
||||
metadata.setValue(Metadata.KEY, METADATA_KEY);
|
||||
metadata.setValue(ID, note.optLong("id_note"));
|
||||
metadata.setValue(MESSAGE, note.optString("message"));
|
||||
metadata.setValue(CREATED, ApiUtilities.producteevToUnixTime(
|
||||
note.optString("time_create"), 0));
|
||||
return metadata;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue