Producteev now all compiles. haha. does it work?

pull/14/head
Tim Su 14 years ago
parent bd37bcd567
commit 5c0d050069

@ -204,15 +204,20 @@ public class ProducteevInvoker {
* set a labels to a task * set a labels to a task
* *
* @param idTask * @param idTask
* @param idLabel * @param idLabels
* *
* @return array: tasks/view * @return array: tasks/view
*/ */
public JSONArray tasksSetLabel(long idTask, long idLabel) throws ApiServiceException, IOException { public JSONArray tasksSetLabels(long idTask, long... idLabels) throws ApiServiceException, IOException {
return getResponse(invokeGet("tasks/set_label.json", Object[] params = new Object[idLabels.length * 2 + 2];
"token", token, params[0] = "token";
"id_task", idTask, params[1] = token;
"id_label", idLabel), "tasks"); for(int i = 0; i < idLabels.length; i++) {
params[i*2 + 2] = "id_label[]";
params[i*2 + 3] = idLabels[i];
}
return getResponse(invokeGet("tasks/set_label.json", params), "tasks");
} }
/** /**
@ -223,11 +228,31 @@ public class ProducteevInvoker {
* *
* @return array: tasks/view * @return array: tasks/view
*/ */
public JSONArray tasksUnsetLabel(long idTask, long idLabel) throws ApiServiceException, IOException { public JSONArray tasksUnsetLabels(long idTask, long... idLabels) throws ApiServiceException, IOException {
return getResponse(invokeGet("tasks/unset_label.json", Object[] params = new Object[idLabels.length * 2 + 2];
params[0] = "token";
params[1] = token;
for(int i = 0; i < idLabels.length; i++) {
params[i*2 + 2] = "id_label[]";
params[i*2 + 3] = idLabels[i];
}
return getResponse(invokeGet("tasks/unset_label.json", params), "tasks");
}
/**
* create a note attached to a task
*
* @param idTask
* @param message
*
* @return array tasks::note_view
*/
public JSONObject tasksNoteCreate(long idTask, String message) throws ApiServiceException, IOException {
return invokeGet("tasks/note_create.json",
"token", token, "token", token,
"id_task", idTask, "id_task", idTask,
"id_label", idLabel), "tasks"); "message", message);
} }
// --- labels // --- labels
@ -248,7 +273,7 @@ public class ProducteevInvoker {
} }
/** /**
* create a task * create a label
* *
* @param idDashboard * @param idDashboard
* @param title * @param title

@ -22,7 +22,7 @@ import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.dao.TaskDao.TaskCriteria; import com.todoroo.astrid.dao.TaskDao.TaskCriteria;
import com.todoroo.astrid.model.Metadata; import com.todoroo.astrid.model.Metadata;
import com.todoroo.astrid.model.Task; import com.todoroo.astrid.model.Task;
import com.todoroo.astrid.rmilk.sync.RTMTaskContainer; import com.todoroo.astrid.producteev.ProducteevPreferences;
import com.todoroo.astrid.tags.TagService; import com.todoroo.astrid.tags.TagService;
public final class ProducteevDataService { public final class ProducteevDataService {
@ -52,6 +52,8 @@ public final class ProducteevDataService {
@Autowired @Autowired
private MetadataDao metadataDao; private MetadataDao metadataDao;
private final ProducteevPreferences preferences = new ProducteevPreferences();
static final Random random = new Random(); static final Random random = new Random();
private ProducteevDataService(Context context) { private ProducteevDataService(Context context) {
@ -77,7 +79,7 @@ public final class ProducteevDataService {
return return
taskDao.query(Query.select(properties).join(ProducteevDataService.METADATA_JOIN).where(Criterion.and( taskDao.query(Query.select(properties).join(ProducteevDataService.METADATA_JOIN).where(Criterion.and(
Criterion.not(Task.ID.in(Query.select(Metadata.TASK).from(Metadata.TABLE). 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))))), where(Criterion.and(MetadataCriteria.withKey(ProducteevTask.METADATA_KEY), ProducteevTask.ID.gt(0))))),
TaskCriteria.isActive()))); TaskCriteria.isActive())));
} }
@ -87,7 +89,7 @@ public final class ProducteevDataService {
* @return null if never sync'd * @return null if never sync'd
*/ */
public TodorooCursor<Task> getLocallyUpdated(Property<?>[] properties) { public TodorooCursor<Task> getLocallyUpdated(Property<?>[] properties) {
long lastSyncDate = ProducteevUtilities.getLastSyncDate(); long lastSyncDate = preferences.getLastSyncDate();
if(lastSyncDate == 0) if(lastSyncDate == 0)
return taskDao.query(Query.select(Task.ID).where(Criterion.none)); return taskDao.query(Query.select(Task.ID).where(Criterion.none));
return return
@ -100,13 +102,12 @@ public final class ProducteevDataService {
* Searches for a local task with same remote id, updates this task's id * Searches for a local task with same remote id, updates this task's id
* @param remoteTask * @param remoteTask
*/ */
public void findLocalMatch(RTMTaskContainer remoteTask) { public void findLocalMatch(ProducteevTaskContainer remoteTask) {
if(remoteTask.task.getId() != Task.NO_ID) if(remoteTask.task.getId() != Task.NO_ID)
return; return;
TodorooCursor<Task> cursor = taskDao.query(Query.select(Task.ID). TodorooCursor<Task> cursor = taskDao.query(Query.select(Task.ID).
join(ProducteevDataService.METADATA_JOIN).where(Criterion.and(MetadataCriteria.withKey(ProducteevTask.METADATA_KEY), join(ProducteevDataService.METADATA_JOIN).where(Criterion.and(MetadataCriteria.withKey(ProducteevTask.METADATA_KEY),
ProducteevTask.TASK_SERIES_ID.eq(remoteTask.taskSeriesId), ProducteevTask.ID.eq(remoteTask.pdvTask.getValue(ProducteevTask.ID)))));
ProducteevTask.TASK_ID.eq(remoteTask.taskId))));
try { try {
if(cursor.getCount() == 0) if(cursor.getCount() == 0)
return; return;
@ -122,13 +123,14 @@ public final class ProducteevDataService {
* @param task * @param task
*/ */
public void saveTaskAndMetadata(ProducteevTaskContainer task) { public void saveTaskAndMetadata(ProducteevTaskContainer task) {
findLocalMatch(task);
taskDao.save(task.task, true); taskDao.save(task.task, true);
metadataDao.deleteWhere(Criterion.and(MetadataCriteria.byTask(task.task.getId()), metadataDao.deleteWhere(Criterion.and(MetadataCriteria.byTask(task.task.getId()),
Criterion.or(MetadataCriteria.withKey(ProducteevTask.METADATA_KEY), Criterion.or(MetadataCriteria.withKey(ProducteevTask.METADATA_KEY),
MetadataCriteria.withKey(ProducteevNote.METADATA_KEY), MetadataCriteria.withKey(ProducteevNote.METADATA_KEY),
MetadataCriteria.withKey(TagService.KEY)))); MetadataCriteria.withKey(TagService.KEY))));
task.metadata.add(ProducteevTask.create(task)); task.metadata.add(task.pdvTask);
for(Metadata metadata : task.metadata) { for(Metadata metadata : task.metadata) {
metadata.setValue(Metadata.TASK, task.task.getId()); metadata.setValue(Metadata.TASK, task.task.getId());
metadataDao.createNew(metadata); metadataDao.createNew(metadata);
@ -158,7 +160,7 @@ public final class ProducteevDataService {
metadataCursor.close(); metadataCursor.close();
} }
return new RTMTaskContainer(task, metadata); return new ProducteevTaskContainer(task, metadata);
} }
/** /**
@ -167,7 +169,7 @@ public final class ProducteevDataService {
*/ */
public Metadata getTaskMetadata(long taskId) { public Metadata getTaskMetadata(long taskId) {
TodorooCursor<Metadata> cursor = metadataDao.query(Query.select( TodorooCursor<Metadata> cursor = metadataDao.query(Query.select(
ProducteevTask.LIST_ID, ProducteevTask.TASK_SERIES_ID, ProducteevTask.TASK_ID, ProducteevTask.REPEATING).where( ProducteevTask.ID, ProducteevTask.DASHBOARD_ID).where(
MetadataCriteria.byTaskAndwithKey(taskId, ProducteevTask.METADATA_KEY))); MetadataCriteria.byTaskAndwithKey(taskId, ProducteevTask.METADATA_KEY)));
try { try {
if(cursor.getCount() == 0) if(cursor.getCount() == 0)

@ -7,7 +7,6 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
@ -41,8 +40,6 @@ import com.todoroo.astrid.producteev.api.ApiUtilities;
import com.todoroo.astrid.producteev.api.ProducteevInvoker; import com.todoroo.astrid.producteev.api.ProducteevInvoker;
import com.todoroo.astrid.rmilk.MilkUtilities; import com.todoroo.astrid.rmilk.MilkUtilities;
import com.todoroo.astrid.rmilk.api.ServiceInternalException; import com.todoroo.astrid.rmilk.api.ServiceInternalException;
import com.todoroo.astrid.rmilk.api.data.RtmTaskNote;
import com.todoroo.astrid.rmilk.data.MilkNote;
import com.todoroo.astrid.service.AstridDependencyInjector; import com.todoroo.astrid.service.AstridDependencyInjector;
import com.todoroo.astrid.tags.TagService; import com.todoroo.astrid.tags.TagService;
import com.todoroo.astrid.utility.Preferences; import com.todoroo.astrid.utility.Preferences;
@ -144,7 +141,6 @@ public class ProducteevSyncProvider extends SynchronizationProvider<ProducteevTa
/** /**
* Perform authentication with RTM. Will open the SyncBrowser if necessary * Perform authentication with RTM. Will open the SyncBrowser if necessary
*/ */
@SuppressWarnings("nls")
private void authenticate() { private void authenticate() {
FlurryAgent.onEvent("producteev-started"); FlurryAgent.onEvent("producteev-started");
@ -175,7 +171,7 @@ public class ProducteevSyncProvider extends SynchronizationProvider<ProducteevTa
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
// occurs when application was closed // occurs when application was closed
} catch (Exception e) { } catch (Exception e) {
handleException("rtm-authenticate", e, true); handleException("pdv-authenticate", e, true);
} finally { } finally {
preferences.stopOngoing(); preferences.stopOngoing();
} }
@ -185,13 +181,16 @@ public class ProducteevSyncProvider extends SynchronizationProvider<ProducteevTa
// ----------------------------------------------------- synchronization! // ----------------------------------------------------- synchronization!
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
@SuppressWarnings("nls")
protected void performSync() { protected void performSync() {
try { try {
// load user information // load user information
JSONObject user = invoker.usersView(null); JSONObject user = invoker.usersView(null);
defaultDashboard = user.getJSONObject("user").getLong("default_dashboard"); defaultDashboard = user.getJSONObject("user").getLong("default_dashboard");
// get labels
JSONArray labels = invoker.labelsShowList(defaultDashboard, null);
readLabels(labels);
// read all tasks // read all tasks
JSONArray tasks = invoker.tasksShowList(defaultDashboard, JSONArray tasks = invoker.tasksShowList(defaultDashboard,
preferences.getLastServerSync()); preferences.getLastServerSync());
@ -206,11 +205,11 @@ public class ProducteevSyncProvider extends SynchronizationProvider<ProducteevTa
MilkUtilities.recordSuccessfulSync(); MilkUtilities.recordSuccessfulSync();
FlurryAgent.onEvent("rtm-sync-finished"); //$NON-NLS-1$ FlurryAgent.onEvent("pdv-sync-finished"); //$NON-NLS-1$
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
// occurs when application was closed // occurs when application was closed
} catch (Exception e) { } catch (Exception e) {
handleException("rtm-sync", e, true); //$NON-NLS-1$ handleException("pdv-sync", e, true); //$NON-NLS-1$
} }
} }
@ -235,7 +234,6 @@ public class ProducteevSyncProvider extends SynchronizationProvider<ProducteevTa
* Populate SyncData data structure * Populate SyncData data structure
* @throws JSONException * @throws JSONException
*/ */
@SuppressWarnings("nls")
private SyncData<ProducteevTaskContainer> populateSyncData(JSONArray tasks) throws JSONException { private SyncData<ProducteevTaskContainer> populateSyncData(JSONArray tasks) throws JSONException {
// fetch locally created tasks // fetch locally created tasks
TodorooCursor<Task> localCreated = dataService.getLocallyCreated(PROPERTIES); TodorooCursor<Task> localCreated = dataService.getLocallyCreated(PROPERTIES);
@ -273,6 +271,7 @@ public class ProducteevSyncProvider extends SynchronizationProvider<ProducteevTa
throw new ApiResponseParseException(e); throw new ApiResponseParseException(e);
} }
transferIdentifiers(newRemoteTask, task); transferIdentifiers(newRemoteTask, task);
push(task, newRemoteTask);
} }
/** Create a task container for the given RtmTaskSeries /** Create a task container for the given RtmTaskSeries
@ -305,10 +304,7 @@ public class ProducteevSyncProvider extends SynchronizationProvider<ProducteevTa
JSONArray notes = remoteTask.getJSONArray("notes"); JSONArray notes = remoteTask.getJSONArray("notes");
for(int i = notes.length() - 1; i >= 0; i--) { for(int i = notes.length() - 1; i >= 0; i--) {
JSONObject note = notes.getJSONObject(i).getJSONObject("note"); JSONObject note = notes.getJSONObject(i).getJSONObject("note");
if(i == notes.length() - 1) metadata.add(ProducteevNote.create(note));
task.setValue(Task.NOTES, note.getString("message"));
else
metadata.add(ProducteevNote.create(note));
} }
ProducteevTaskContainer container = new ProducteevTaskContainer(task, metadata, remoteTask); ProducteevTaskContainer container = new ProducteevTaskContainer(task, metadata, remoteTask);
@ -346,8 +342,6 @@ public class ProducteevSyncProvider extends SynchronizationProvider<ProducteevTa
long idTask = local.pdvTask.getValue(ProducteevTask.ID); long idTask = local.pdvTask.getValue(ProducteevTask.ID);
// TODO handle task workspace switching
// either delete or re-create if necessary // either delete or re-create if necessary
if(shouldTransmit(local, Task.DELETION_DATE, remote)) { if(shouldTransmit(local, Task.DELETION_DATE, remote)) {
if(local.task.getValue(Task.DELETION_DATE) > 0) if(local.task.getValue(Task.DELETION_DATE) > 0)
@ -376,26 +370,50 @@ public class ProducteevSyncProvider extends SynchronizationProvider<ProducteevTa
if(TagService.KEY.equals(item.getValue(Metadata.KEY))) if(TagService.KEY.equals(item.getValue(Metadata.KEY)))
remoteTags.add(item.getValue(TagService.TAG)); remoteTags.add(item.getValue(TagService.TAG));
} }
if(!localTags.equals(remoteTags)) {
String[] tags = localTags.toArray(new String[localTags.size()]);
rtmService.tasks_setTags(timeline, listId, taskSeriesId,
taskId, tags);
}
// notes try {
if(shouldTransmit(local, Task.NOTES, remote)) { if(!localTags.equals(remoteTags)) {
String[] titleAndText = MilkNote.fromNoteField(local.task.getValue(Task.NOTES)); HashSet<String> toAdd = new HashSet<String>(localTags);
List<RtmTaskNote> notes = null; toAdd.removeAll(remoteTags);
if(remote != null && remote.pdvTask.getNotes() != null) HashSet<String> toRemove = remoteTags;
notes = remote.pdvTask.getNotes().getNotes(); toRemove.removeAll(localTags);
if(notes != null && notes.size() > 0) {
String remoteNoteId = notes.get(0).getId(); if(toAdd.size() > 0) {
rtmService.tasks_notes_edit(timeline, remoteNoteId, titleAndText[0], long[] toAddIds = new long[toAdd.size()];
titleAndText[1]); int index = 0;
} else { for(String label : toAdd) {
rtmService.tasks_notes_add(timeline, listId, taskSeriesId, if(!labelMap.containsKey(label)) {
taskId, titleAndText[0], titleAndText[1]); JSONArray result = invoker.labelsCreate(defaultDashboard, label);
readLabels(result);
}
toAddIds[index++] = labelMap.get(label);
}
invoker.tasksSetLabels(idTask, toAddIds);
}
if(toRemove.size() > 0) {
long[] toRemoveIds = new long[toRemove.size()];
int index = 0;
for(String label : toRemove) {
if(!labelMap.containsKey(label)) {
JSONArray result = invoker.labelsCreate(defaultDashboard, label);
readLabels(result);
}
toRemoveIds[index++] = labelMap.get(label);
}
invoker.tasksUnsetLabels(idTask, toRemoveIds);
}
} }
// notes
if(shouldTransmit(local, Task.NOTES, remote)) {
String note = local.task.getValue(Task.NOTES);
JSONObject result = invoker.tasksNoteCreate(idTask, note);
local.metadata.add(ProducteevNote.create(result.getJSONObject("note")));
local.task.setValue(Task.NOTES, "");
}
} catch (JSONException e) {
throw new ApiResponseParseException(e);
} }
if(remerge) { if(remerge) {
@ -512,6 +530,20 @@ public class ProducteevSyncProvider extends SynchronizationProvider<ProducteevTa
destination.pdvTask = source.pdvTask; destination.pdvTask = source.pdvTask;
} }
/**
* Read labels into label map
* @throws JSONException
* @throws ApiServiceException
* @throws IOException
*/
private void readLabels(JSONArray labels) throws JSONException, ApiServiceException, IOException {
for(int i = 0; i < labels.length(); i++) {
JSONObject label = labels.getJSONObject(i).getJSONObject("label");
labelMap.put(label.getString("title"), label.getLong("id_label"));
}
}
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// ------------------------------------------------------- helper methods // ------------------------------------------------------- helper methods
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------

Loading…
Cancel
Save