From 0a8f523551a8e4721756289a693f59a2f36df1e8 Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Fri, 21 Sep 2012 15:29:44 -0700 Subject: [PATCH] Make tag creation work better in TaskService.createWithValues --- .../com/todoroo/astrid/tags/TagService.java | 18 ++++++++++++++++ .../todoroo/astrid/service/TaskService.java | 21 +++++++++++-------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/astrid/plugin-src/com/todoroo/astrid/tags/TagService.java b/astrid/plugin-src/com/todoroo/astrid/tags/TagService.java index c67a3f11b..42be060d4 100644 --- a/astrid/plugin-src/com/todoroo/astrid/tags/TagService.java +++ b/astrid/plugin-src/com/todoroo/astrid/tags/TagService.java @@ -235,6 +235,24 @@ public final class TagService { } } + public void createLink(Task task, String tagName) { + TodorooCursor existingTag = tagDataService.query(Query.select(TagData.NAME, TagData.REMOTE_ID) + .where(TagData.NAME.eqCaseInsensitive(tagName))); + try { + TagData tagData; + if (existingTag.getCount() == 0) { + tagData = new TagData(); + tagData.setValue(TagData.NAME, tagName); + tagDataService.save(tagData); + } else { + tagData = new TagData(existingTag); + } + createLink(task, tagData.getValue(TagData.NAME), tagData.getValue(TagData.REMOTE_ID)); + } finally { + existingTag.close(); + } + } + public void createLink(Task task, String tagName, long tagUuid) { TodorooCursor existing = metadataDao.query(Query.select(Metadata.PROPERTIES) .where(Criterion.and(MetadataCriteria.withKey(TagMetadata.KEY), diff --git a/astrid/src/com/todoroo/astrid/service/TaskService.java b/astrid/src/com/todoroo/astrid/service/TaskService.java index 09373a7a0..bfeafafbe 100644 --- a/astrid/src/com/todoroo/astrid/service/TaskService.java +++ b/astrid/src/com/todoroo/astrid/service/TaskService.java @@ -425,13 +425,8 @@ public class TaskService { */ private void quickAdd(Task task, List tags) { save(task); - - Metadata metadata = new Metadata(); for(String tag : tags) { - metadata.setValue(Metadata.KEY, TagMetadata.KEY); - metadata.setValue(Metadata.TASK, task.getId()); - metadata.setValue(TagMetadata.TAG_NAME, tag); - metadataDao.createNew(metadata); + TagService.getInstance().createLink(task, tag); } } @@ -536,9 +531,17 @@ public class TaskService { Metadata metadata = new Metadata(); metadata.setValue(Metadata.TASK, task.getId()); metadata.mergeWith(forMetadata); - if (TagMetadata.KEY.equals(metadata.getValue(Metadata.KEY))) - metadata.setValue(TagMetadata.TASK_UUID, task.getValue(Task.REMOTE_ID)); - PluginServices.getMetadataService().save(metadata); + if (TagMetadata.KEY.equals(metadata.getValue(Metadata.KEY))) { + if (metadata.containsNonNullValue(TagMetadata.TAG_UUID) && metadata.getValue(TagMetadata.TAG_UUID) != 0) { + // This is more efficient + TagService.getInstance().createLink(task, metadata.getValue(TagMetadata.TAG_NAME), metadata.getValue(TagMetadata.TAG_UUID)); + } else { + // This is necessary for backwards compatibility + TagService.getInstance().createLink(task, metadata.getValue(TagMetadata.TAG_NAME)); + } + } else { + PluginServices.getMetadataService().save(metadata); + } } return task;