Move caldav subtasks

gtask_related_email
Alex Baker 5 years ago
parent 415ce8030a
commit 84e954232b

@ -1,12 +1,9 @@
package com.todoroo.astrid.service;
import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.Iterables.transform;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Lists.transform;
import static com.todoroo.andlib.utility.DateUtilities.now;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import com.todoroo.astrid.api.CaldavFilter;
import com.todoroo.astrid.api.Filter;
@ -51,12 +48,16 @@ public class TaskMover {
public void move(List<Long> tasks, Filter selectedList) {
tasks = newArrayList(tasks);
tasks.removeAll(googleTaskDao.findChildrenInList(tasks));
tasks.removeAll(caldavDao.findChildrenInList(tasks));
for (Task task : taskDao.fetch(tasks)) {
performMove(task, selectedList);
task.putTransitory(SyncFlags.GTASKS_SUPPRESS_SYNC, true);
task.setModificationDate(now());
taskDao.save(task);
}
if (selectedList instanceof CaldavFilter) {
caldavDao.updateParents((((CaldavFilter) selectedList).getUuid()));
}
syncAdapters.sync();
}
@ -83,6 +84,7 @@ public class TaskMover {
long id = task.getId();
GoogleTask googleTask = googleTaskDao.getByTaskId(id);
List<GoogleTask> googleTaskChildren = emptyList();
List<CaldavTask> caldavChildren = emptyList();
if (googleTask != null
&& selectedList instanceof GtasksFilter
&& googleTask.getListId().equals(((GtasksFilter) selectedList).getRemoteId())) {
@ -101,8 +103,13 @@ public class TaskMover {
}
if (caldavTask != null) {
caldavTask.setDeleted(now());
caldavDao.update(caldavTask);
List<Long> toDelete = newArrayList(caldavTask.getTask());
List<Long> childIds = caldavDao.getChildren(caldavTask.getTask());
if (!childIds.isEmpty()) {
caldavChildren = caldavDao.getTasks(childIds);
toDelete.addAll(childIds);
}
caldavDao.markDeleted(now(), toDelete);
}
if (selectedList instanceof GtasksFilter) {
@ -119,12 +126,34 @@ public class TaskMover {
return newChild;
}));
}
if (!caldavChildren.isEmpty()) {
List<GoogleTask> children = newArrayList();
for (int i = 0 ; i < caldavChildren.size() ; i++) {
CaldavTask child = caldavChildren.get(i);
GoogleTask newChild = new GoogleTask(child.getTask(), listId);
newChild.setOrder(i);
newChild.setParent(id);
children.add(newChild);
}
googleTaskDao.insert(children);
}
} else if (selectedList instanceof CaldavFilter) {
String listId = ((CaldavFilter) selectedList).getUuid();
caldavDao.insert(
transform(
concat(singletonList(id), transform(googleTaskChildren, GoogleTask::getTask)),
_id -> new CaldavTask(_id, listId)));
CaldavTask newParent = caldavTask == null
? new CaldavTask(id, listId)
: new CaldavTask(id, listId, caldavTask.getRemoteId(), caldavTask.getObject());
caldavDao.insert(newParent);
caldavDao.insert(transform(googleTaskChildren, child -> {
CaldavTask newChild = new CaldavTask(child.getTask(), listId);
newChild.setRemoteParent(newParent.getRemoteId());
return newChild;
}));
caldavDao.insert(transform(caldavChildren, child -> {
CaldavTask newChild = new CaldavTask(child.getTask(), listId, child.getRemoteId(), child.getObject());
newChild.setVtodo(child.getVtodo());
newChild.setRemoteParent(child.getRemoteParent());
return newChild;
}));
}
}
}

@ -58,6 +58,9 @@ public abstract class CaldavDao {
@Query("SELECT * FROM caldav_tasks WHERE cd_deleted > 0 AND cd_calendar = :calendar")
public abstract List<CaldavTask> getDeleted(String calendar);
@Query("UPDATE caldav_tasks SET cd_deleted = :now WHERE cd_task IN (:tasks)")
public abstract void markDeleted(long now, List<Long> tasks);
@Query("SELECT * FROM caldav_tasks WHERE cd_task = :taskId AND cd_deleted = 0 LIMIT 1")
public abstract CaldavTask getTask(long taskId);
@ -67,6 +70,9 @@ public abstract class CaldavDao {
@Query("SELECT * FROM caldav_tasks WHERE cd_task = :taskId")
public abstract List<CaldavTask> getTasks(long taskId);
@Query("SELECT * FROM caldav_tasks WHERE cd_task in (:taskIds) AND cd_deleted = 0")
public abstract List<CaldavTask> getTasks(List<Long> taskIds);
@Query(
"SELECT task.*, caldav_task.* FROM tasks AS task "
+ "INNER JOIN caldav_tasks AS caldav_task ON _id = cd_task "
@ -134,7 +140,7 @@ public abstract class CaldavDao {
+ " INNER JOIN caldav_tasks "
+ " ON _id = cd_task "
+ " WHERE cd_parent IN (:ids) "
+ " AND tasks.deleted = 0 "
+ " AND tasks.deleted = 0 AND caldav_tasks.cd_deleted = 0 "
+ "UNION ALL "
+ " SELECT caldav_tasks.cd_task "
+ " FROM tasks "
@ -142,8 +148,34 @@ public abstract class CaldavDao {
+ " ON _id = caldav_tasks.cd_task "
+ " INNER JOIN recursive_caldav "
+ " ON recursive_caldav.cd_task = caldav_tasks.cd_parent "
+ " WHERE tasks.deleted = 0 "
+ " WHERE tasks.deleted = 0 AND caldav_tasks.cd_deleted = 0 "
+ " ) "
+ "SELECT cd_task FROM recursive_caldav")
abstract List<Long> getChildrenRecursive(List<Long> ids);
public List<Long> findChildrenInList(List<Long> ids) {
return atLeastLollipop()
? findChildrenInListRecursive(ids)
: Collections.emptyList();
}
@Query("WITH RECURSIVE "
+ " recursive_caldav (cd_task) AS ( "
+ " SELECT cd_task "
+ " FROM tasks "
+ " INNER JOIN caldav_tasks "
+ " ON _id = cd_task "
+ " WHERE cd_parent IN (:ids) AND cd_task IN (:ids)"
+ " AND tasks.deleted = 0 AND caldav_tasks.cd_deleted = 0 "
+ "UNION ALL "
+ " SELECT caldav_tasks.cd_task "
+ " FROM tasks "
+ " INNER JOIN caldav_tasks "
+ " ON _id = caldav_tasks.cd_task "
+ " INNER JOIN recursive_caldav "
+ " ON recursive_caldav.cd_task = caldav_tasks.cd_parent "
+ " WHERE tasks.deleted = 0 AND caldav_tasks.cd_deleted = 0 "
+ " ) "
+ "SELECT cd_task FROM recursive_caldav")
abstract List<Long> findChildrenInListRecursive(List<Long> ids);
}

Loading…
Cancel
Save