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
*
* @param idTask
* @param idLabel
* @param idLabels
*
* @return array: tasks/view
*/
public JSONArray tasksSetLabel(long idTask, long idLabel) throws ApiServiceException, IOException {
return getResponse(invokeGet("tasks/set_label.json",
"token", token,
"id_task", idTask,
"id_label", idLabel), "tasks");
public JSONArray tasksSetLabels(long idTask, long... idLabels) throws ApiServiceException, IOException {
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/set_label.json", params), "tasks");
}
/**
@ -223,11 +228,31 @@ public class ProducteevInvoker {
*
* @return array: tasks/view
*/
public JSONArray tasksUnsetLabel(long idTask, long idLabel) throws ApiServiceException, IOException {
return getResponse(invokeGet("tasks/unset_label.json",
public JSONArray tasksUnsetLabels(long idTask, long... idLabels) throws ApiServiceException, IOException {
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,
"id_task", idTask,
"id_label", idLabel), "tasks");
"message", message);
}
// --- labels
@ -248,7 +273,7 @@ public class ProducteevInvoker {
}
/**
* create a task
* create a label
*
* @param idDashboard
* @param title

@ -22,7 +22,7 @@ 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.producteev.ProducteevPreferences;
import com.todoroo.astrid.tags.TagService;
public final class ProducteevDataService {
@ -52,6 +52,8 @@ public final class ProducteevDataService {
@Autowired
private MetadataDao metadataDao;
private final ProducteevPreferences preferences = new ProducteevPreferences();
static final Random random = new Random();
private ProducteevDataService(Context context) {
@ -77,7 +79,7 @@ public final class ProducteevDataService {
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))))),
where(Criterion.and(MetadataCriteria.withKey(ProducteevTask.METADATA_KEY), ProducteevTask.ID.gt(0))))),
TaskCriteria.isActive())));
}
@ -87,7 +89,7 @@ public final class ProducteevDataService {
* @return null if never sync'd
*/
public TodorooCursor<Task> getLocallyUpdated(Property<?>[] properties) {
long lastSyncDate = ProducteevUtilities.getLastSyncDate();
long lastSyncDate = preferences.getLastSyncDate();
if(lastSyncDate == 0)
return taskDao.query(Query.select(Task.ID).where(Criterion.none));
return
@ -100,13 +102,12 @@ public final class ProducteevDataService {
* Searches for a local task with same remote id, updates this task's id
* @param remoteTask
*/
public void findLocalMatch(RTMTaskContainer remoteTask) {
public void findLocalMatch(ProducteevTaskContainer 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))));
ProducteevTask.ID.eq(remoteTask.pdvTask.getValue(ProducteevTask.ID)))));
try {
if(cursor.getCount() == 0)
return;
@ -122,13 +123,14 @@ public final class ProducteevDataService {
* @param task
*/
public void saveTaskAndMetadata(ProducteevTaskContainer task) {
findLocalMatch(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));
task.metadata.add(task.pdvTask);
for(Metadata metadata : task.metadata) {
metadata.setValue(Metadata.TASK, task.task.getId());
metadataDao.createNew(metadata);
@ -158,7 +160,7 @@ public final class ProducteevDataService {
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) {
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)));
try {
if(cursor.getCount() == 0)

@ -7,7 +7,6 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import org.json.JSONArray;
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.rmilk.MilkUtilities;
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.tags.TagService;
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
*/
@SuppressWarnings("nls")
private void authenticate() {
FlurryAgent.onEvent("producteev-started");
@ -175,7 +171,7 @@ public class ProducteevSyncProvider extends SynchronizationProvider<ProducteevTa
} catch (IllegalStateException e) {
// occurs when application was closed
} catch (Exception e) {
handleException("rtm-authenticate", e, true);
handleException("pdv-authenticate", e, true);
} finally {
preferences.stopOngoing();
}
@ -185,13 +181,16 @@ public class ProducteevSyncProvider extends SynchronizationProvider<ProducteevTa
// ----------------------------------------------------- synchronization!
// ----------------------------------------------------------------------
@SuppressWarnings("nls")
protected void performSync() {
try {
// load user information
JSONObject user = invoker.usersView(null);
defaultDashboard = user.getJSONObject("user").getLong("default_dashboard");
// get labels
JSONArray labels = invoker.labelsShowList(defaultDashboard, null);
readLabels(labels);
// read all tasks
JSONArray tasks = invoker.tasksShowList(defaultDashboard,
preferences.getLastServerSync());
@ -206,11 +205,11 @@ public class ProducteevSyncProvider extends SynchronizationProvider<ProducteevTa
MilkUtilities.recordSuccessfulSync();
FlurryAgent.onEvent("rtm-sync-finished"); //$NON-NLS-1$
FlurryAgent.onEvent("pdv-sync-finished"); //$NON-NLS-1$
} catch (IllegalStateException e) {
// occurs when application was closed
} 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
* @throws JSONException
*/
@SuppressWarnings("nls")
private SyncData<ProducteevTaskContainer> populateSyncData(JSONArray tasks) throws JSONException {
// fetch locally created tasks
TodorooCursor<Task> localCreated = dataService.getLocallyCreated(PROPERTIES);
@ -273,6 +271,7 @@ public class ProducteevSyncProvider extends SynchronizationProvider<ProducteevTa
throw new ApiResponseParseException(e);
}
transferIdentifiers(newRemoteTask, task);
push(task, newRemoteTask);
}
/** Create a task container for the given RtmTaskSeries
@ -305,10 +304,7 @@ public class ProducteevSyncProvider extends SynchronizationProvider<ProducteevTa
JSONArray notes = remoteTask.getJSONArray("notes");
for(int i = notes.length() - 1; i >= 0; i--) {
JSONObject note = notes.getJSONObject(i).getJSONObject("note");
if(i == notes.length() - 1)
task.setValue(Task.NOTES, note.getString("message"));
else
metadata.add(ProducteevNote.create(note));
metadata.add(ProducteevNote.create(note));
}
ProducteevTaskContainer container = new ProducteevTaskContainer(task, metadata, remoteTask);
@ -346,8 +342,6 @@ public class ProducteevSyncProvider extends SynchronizationProvider<ProducteevTa
long idTask = local.pdvTask.getValue(ProducteevTask.ID);
// TODO handle task workspace switching
// either delete or re-create if necessary
if(shouldTransmit(local, Task.DELETION_DATE, remote)) {
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)))
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
if(shouldTransmit(local, Task.NOTES, remote)) {
String[] titleAndText = MilkNote.fromNoteField(local.task.getValue(Task.NOTES));
List<RtmTaskNote> notes = null;
if(remote != null && remote.pdvTask.getNotes() != null)
notes = remote.pdvTask.getNotes().getNotes();
if(notes != null && notes.size() > 0) {
String remoteNoteId = notes.get(0).getId();
rtmService.tasks_notes_edit(timeline, remoteNoteId, titleAndText[0],
titleAndText[1]);
} else {
rtmService.tasks_notes_add(timeline, listId, taskSeriesId,
taskId, titleAndText[0], titleAndText[1]);
try {
if(!localTags.equals(remoteTags)) {
HashSet<String> toAdd = new HashSet<String>(localTags);
toAdd.removeAll(remoteTags);
HashSet<String> toRemove = remoteTags;
toRemove.removeAll(localTags);
if(toAdd.size() > 0) {
long[] toAddIds = new long[toAdd.size()];
int index = 0;
for(String label : toAdd) {
if(!labelMap.containsKey(label)) {
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) {
@ -512,6 +530,20 @@ public class ProducteevSyncProvider extends SynchronizationProvider<ProducteevTa
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
// ----------------------------------------------------------------------

Loading…
Cancel
Save