diff --git a/astrid/plugin-src/com/todoroo/astrid/tags/Astrid44SyncMigrator.java b/astrid/plugin-src/com/todoroo/astrid/tags/Astrid44SyncMigrator.java index 003b74d8c..16b5d639b 100644 --- a/astrid/plugin-src/com/todoroo/astrid/tags/Astrid44SyncMigrator.java +++ b/astrid/plugin-src/com/todoroo/astrid/tags/Astrid44SyncMigrator.java @@ -8,6 +8,7 @@ import com.todoroo.andlib.service.Autowired; import com.todoroo.andlib.service.DependencyInjectionService; import com.todoroo.andlib.sql.Criterion; import com.todoroo.andlib.sql.Query; +import com.todoroo.andlib.utility.DateUtilities; import com.todoroo.andlib.utility.Pair; import com.todoroo.andlib.utility.Preferences; import com.todoroo.astrid.dao.MetadataDao.MetadataCriteria; @@ -44,7 +45,7 @@ public class Astrid44SyncMigrator { return; // -------------- - // First assert that a TagData object exists for each tag metadata + // First ensure that a TagData object exists for each tag metadata // -------------- Query noTagDataQuery = Query.select(Metadata.PROPERTIES).where(Criterion.and( MetadataCriteria.withKey(TagMetadata.KEY), @@ -68,7 +69,7 @@ public class Astrid44SyncMigrator { } // -------------- - // Then assert that every remote model has a remote id, by generating one using the uuid generator for all those without one + // Then ensure that every remote model has a remote id, by generating one using the uuid generator for all those without one // -------------- Query tagsWithoutUUIDQuery = Query.select(TagData.ID, TagData.REMOTE_ID).where(Criterion.or(TagData.REMOTE_ID.isNull(), TagData.REMOTE_ID.eq(0))); assertUUIDsExist(tagsWithoutUUIDQuery, new TagData(), tagDataDao); @@ -79,6 +80,41 @@ public class Astrid44SyncMigrator { Query updatesWithoutUUIDQuery = Query.select(Update.ID, Update.REMOTE_ID).where(Criterion.or(Update.REMOTE_ID.isNull(), Update.REMOTE_ID.eq(0))); assertUUIDsExist(updatesWithoutUUIDQuery, new Update(), updateDao); + + // -------------- + // Finally, ensure that all tag metadata entities have all important fields filled in + // -------------- + Query incompleteQuery = Query.select(Metadata.PROPERTIES).where(Criterion.and( + MetadataCriteria.withKey(TagMetadata.KEY), + Criterion.or(TagMetadata.TASK_UUID.eq(0), TagMetadata.TASK_UUID.isNull(), + TagMetadata.TAG_UUID.eq(0), TagMetadata.TAG_UUID.isNull()))); + TodorooCursor incompleteMetadata = metadataService.query(incompleteQuery); + try { + Metadata m = new Metadata(); + for (incompleteMetadata.moveToFirst(); !incompleteMetadata.isAfterLast(); incompleteMetadata.moveToNext()) { + m.readFromCursor(incompleteMetadata); + boolean changes = false; + + if (!m.containsNonNullValue(TagMetadata.TASK_UUID) && m.getValue(TagMetadata.TASK_UUID) == 0) { + updateTaskUuid(m); + changes = true; + } + + if (!m.containsNonNullValue(TagMetadata.TAG_UUID) && m.getValue(TagMetadata.TAG_UUID) == 0) { + updateTagUuid(m); + changes = true; + } + + if (changes) + metadataService.save(m); + + } + } finally { + incompleteMetadata.close(); + } + + + Preferences.setBoolean(PREF_MIGRATED_TASKS_TO_TAGS, true); } @@ -97,4 +133,24 @@ public class Astrid44SyncMigrator { } } + private void updateTaskUuid(Metadata m) { + long taskId = m.getValue(Metadata.TASK); + Task task = taskDao.fetch(taskId, Task.REMOTE_ID); + if (task != null) { + m.setValue(TagMetadata.TASK_UUID, task.getValue(Task.REMOTE_ID)); + } else { + m.setValue(Metadata.DELETION_DATE, DateUtilities.now()); + } + } + + private void updateTagUuid(Metadata m) { + String tag = m.getValue(TagMetadata.TAG_NAME); + TagData tagData = tagDataService.getTag(tag, TagData.REMOTE_ID); + if (tagData != null) { + m.setValue(TagMetadata.TAG_UUID, tagData.getValue(TagData.REMOTE_ID)); + } else { + m.setValue(Metadata.DELETION_DATE, DateUtilities.now()); + } + } + }