Skip to content

Commit

Permalink
Adding task completion to importers (dtinit#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
olsona committed Jun 7, 2018
1 parent 17a1977 commit 9b3f342
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.dataportabilityproject.datatransfer.google.tasks;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.util.DateTime;
import com.google.api.services.tasks.Tasks;
import com.google.api.services.tasks.model.Task;
import com.google.api.services.tasks.model.TaskList;
Expand Down Expand Up @@ -90,9 +91,13 @@ public ImportResult importItem(
tempTasksData = jobStore.findData(jobId, createCacheKey(), TempTasksData.class);

for (TaskModel oldTask : data.getTasks()) {
// TODO: The TaskModel doesn't contain information about completion, which means these all are
// new uncompleted tasks
Task newTask = new Task().setTitle(oldTask.getText()).setNotes(oldTask.getNotes());
if (oldTask.getCompletedTime() != null) {
newTask.setCompleted(new DateTime(oldTask.getCompletedTime().toEpochMilli()));
}
if (oldTask.getDueTime() != null) {
newTask.setDue(new DateTime(oldTask.getDueTime().toEpochMilli()));
}
String newTaskListId = tempTasksData.lookupNewTaskListId(oldTask.getTaskListId());
try {
tasksService.tasks().insert(newTaskListId, newTask).execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import com.fasterxml.jackson.xml.annotate.JacksonXmlProperty;
import com.google.common.base.MoreObjects;

public class TaskAddResponse extends RememberTheMilkResponse {
public class TaskUpdateResponse extends RememberTheMilkResponse {
// The transaction id associated with the task addition
@JacksonXmlProperty(localName = "transaction")
public Transaction transaction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import org.dataportabilityproject.transfer.rememberthemilk.model.tasks.ListAddResponse;
import org.dataportabilityproject.transfer.rememberthemilk.model.tasks.ListInfo;
import org.dataportabilityproject.transfer.rememberthemilk.model.tasks.RememberTheMilkResponse;
import org.dataportabilityproject.transfer.rememberthemilk.model.tasks.TaskAddResponse;
import org.dataportabilityproject.transfer.rememberthemilk.model.tasks.TaskUpdateResponse;
import org.dataportabilityproject.transfer.rememberthemilk.model.tasks.TaskSeries;
import org.dataportabilityproject.transfer.rememberthemilk.model.tasks.TimelineCreateResponse;

Expand Down Expand Up @@ -83,8 +83,28 @@ public TaskSeries createTask(String name, String timeline, String listId) throws
name,
"list_id",
listId);
TaskAddResponse taskAddResponse = makeRequest(params, TaskAddResponse.class);
return taskAddResponse.list.taskseries.get(0);
TaskUpdateResponse taskUpdateResponse = makeRequest(params, TaskUpdateResponse.class);
return taskUpdateResponse.list.taskseries.get(0);
}

public void completeTask(String timeline, String listId, int seriesId, int taskId)
throws IOException {
// NB: The RTM API does not support setting an arbitrary completion time, so this method can
// only mark a task as having been completed.
Map<String, String> params =
ImmutableMap.of(
"method",
RememberTheMilkMethods.TASKS_COMPLETE.getMethodName(),
"timeline",
timeline,
"list_id",
listId,
"taskseries_id",
String.valueOf(seriesId),
"task_id",
String.valueOf(taskId)
);
makeRequest(params, TaskUpdateResponse.class);
}

public GetListResponse getList(String listId) throws IOException {
Expand Down Expand Up @@ -127,8 +147,9 @@ private <T extends RememberTheMilkResponse> T makeRequest(
private enum RememberTheMilkMethods {
LISTS_GET_LIST("rtm.lists.getList"),
LISTS_ADD("rtm.lists.add"),
TASKS_GET_LIST("rtm.tasks.getList"),
TASKS_ADD("rtm.tasks.add"),
TASKS_COMPLETE("rtm.tasks.complete"),
TASKS_GET_LIST("rtm.tasks.getList"),
TIMELINES_CREATE("rtm.timelines.create");

private final String methodName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ public ImportResult importItem(UUID jobId, AuthData authData, TaskContainerResou
String newList = tempTasksData.lookupNewTaskListId(task.getTaskListId());
TaskSeries addedTask = service.createTask(task.getText(), timeline, newList);
// todo: add notes
if (task.getCompletedTime() != null) {
// NB: this assumes that only one task was added above, and that the task series
// in the response contains only one task.
// TODO: Address recurring events where some are completed and some are not
service.completeTask(timeline, newList, addedTask.id, addedTask.tasks.get(0).id);
}
}
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ public void parseTaskAddResponse() throws IOException {
+ " <tags/>\n"
+ " <participants/>\n"
+ " <notes/>\n"
// The taskSeries in a TaskAddResponse only contains one task, not a list of Tasks
// The taskSeries in a TaskUpdateResponse only contains one task, not a list of Tasks
+ " <task id=\"123456789\" due=\"\" has_due_time=\"0\" added=\"2015-05-07T10:19:54Z\"\n"
+ " completed=\"\" deleted=\"\" priority=\"N\" postponed=\"0\" estimate=\"\"/>\n"
+ " </taskseries>\n"
+ " </list>"
+ "</rsp>\n";

TaskAddResponse taskAddResponse = mapper.readValue(content, TaskAddResponse.class);
TaskUpdateResponse taskUpdateResponse = mapper.readValue(content, TaskUpdateResponse.class);

assertThat(taskAddResponse.stat).isEqualTo("ok");
assertThat(taskUpdateResponse.stat).isEqualTo("ok");
}

@Test
Expand Down

0 comments on commit 9b3f342

Please sign in to comment.