From 0f308db6aa80df0c22dffca73ed28a7f44eff65a Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Thu, 15 Nov 2012 14:48:16 -0800 Subject: [PATCH] Improvements to NameMaps, AcknowledgeChange, ChangesHappened --- .../com/todoroo/andlib/data/DatabaseDao.java | 4 + .../sync/messages/AcknowledgeChange.java | 36 +++----- .../actfm/sync/messages/ChangesHappened.java | 11 +-- .../astrid/actfm/sync/messages/NameMaps.java | 88 +++++++++++-------- .../sync/messages/RequestDoubleCheck.java | 1 - .../todoroo/astrid/core/PluginServices.java | 32 +++++++ 6 files changed, 103 insertions(+), 69 deletions(-) diff --git a/api/src/com/todoroo/andlib/data/DatabaseDao.java b/api/src/com/todoroo/andlib/data/DatabaseDao.java index 8d4bd2d87..178d97a7a 100644 --- a/api/src/com/todoroo/andlib/data/DatabaseDao.java +++ b/api/src/com/todoroo/andlib/data/DatabaseDao.java @@ -64,6 +64,10 @@ public class DatabaseDao { return table; } + public Class getModelClass() { + return modelClass; + } + /** * Sets database accessed by this DAO. Used for dependency-injected * initialization by child classes and unit tests diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/AcknowledgeChange.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/AcknowledgeChange.java index c423a86c5..ac3362289 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/AcknowledgeChange.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/AcknowledgeChange.java @@ -6,43 +6,29 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; -import android.text.TextUtils; - import com.todoroo.andlib.data.AbstractModel; -import com.todoroo.andlib.service.Autowired; -import com.todoroo.andlib.service.DependencyInjectionService; +import com.todoroo.astrid.core.PluginServices; import com.todoroo.astrid.dao.OutstandingEntryDao; -import com.todoroo.astrid.dao.TagOutstandingDao; -import com.todoroo.astrid.dao.TaskOutstandingDao; public class AcknowledgeChange extends ServerToClientMessage { - @Autowired - private TaskOutstandingDao taskOutstandingDao; - - @Autowired - private TagOutstandingDao tagOutstandingDao; + private final OutstandingEntryDao dao; public AcknowledgeChange(JSONObject json) { super(json); - DependencyInjectionService.getInstance().inject(this); + String table = json.optString("table"); //$NON-NLS-1$ + if (NameMaps.SERVER_TABLE_TASKS.equals(table)) + dao = PluginServices.getTaskOutstandingDao(); + else if (NameMaps.SERVER_TABLE_TAGS.equals(table)) + dao = PluginServices.getTagOutstandingDao(); + else + dao = null; } @Override - @SuppressWarnings("nls") public void processMessage() { - JSONArray idsArray = json.optJSONArray("ids"); - String table = json.optString("table"); - if (idsArray != null && !TextUtils.isEmpty(table)) { - OutstandingEntryDao dao = null; - if (NameMaps.SERVER_TABLE_TASKS.equals(table)) - dao = taskOutstandingDao; - else if (NameMaps.SERVER_TABLE_TAGS.equals(table)) - dao = tagOutstandingDao; - - if (dao == null) - return; - + JSONArray idsArray = json.optJSONArray("ids"); //$NON-NLS-1$ + if (idsArray != null && dao != null) { ArrayList idsList = new ArrayList(); for (int i = 0; i < idsArray.length(); i++) { try { diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ChangesHappened.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ChangesHappened.java index cec17f953..9aee80d17 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ChangesHappened.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ChangesHappened.java @@ -44,11 +44,12 @@ public class ChangesHappened, String> TASK_PROPERTIES_LOCAL_TO_SERVER; private static final Map TASK_COLUMNS_LOCAL_TO_SERVER; - private static final Map TASK_COLUMNS_SERVER_TO_LOCAL; + private static final Map> TASK_PROPERTIES_SERVER_TO_LOCAL; + + private static void putPropertyToServerName(Property property, String serverName, + Map, String> propertyMap, Map nameMap) { + propertyMap.put(property, serverName); + nameMap.put(property.name, serverName); + } static { // Hardcoded local columns mapped to corresponding server names + TASK_PROPERTIES_LOCAL_TO_SERVER = new HashMap, String>(); TASK_COLUMNS_LOCAL_TO_SERVER = new HashMap(); - TASK_COLUMNS_LOCAL_TO_SERVER.put(Task.TITLE.name, "title"); - TASK_COLUMNS_LOCAL_TO_SERVER.put(Task.IMPORTANCE.name, "importance"); - TASK_COLUMNS_LOCAL_TO_SERVER.put(Task.DUE_DATE.name, "due"); - TASK_COLUMNS_LOCAL_TO_SERVER.put(Task.HIDE_UNTIL.name, "hide_until"); - TASK_COLUMNS_LOCAL_TO_SERVER.put(Task.COMPLETION_DATE.name, "completed_at"); - TASK_COLUMNS_LOCAL_TO_SERVER.put(Task.DELETION_DATE.name, "deleted_at"); - TASK_COLUMNS_LOCAL_TO_SERVER.put(Task.NOTES.name, "notes"); - TASK_COLUMNS_LOCAL_TO_SERVER.put(Task.RECURRENCE.name, "repeat"); - TASK_COLUMNS_LOCAL_TO_SERVER.put(Task.USER_ID.name, "user_id"); - TASK_COLUMNS_LOCAL_TO_SERVER.put(Task.USER.name, "user"); - TASK_COLUMNS_LOCAL_TO_SERVER.put(Task.CREATOR_ID.name, "creator_id"); - TASK_COLUMNS_LOCAL_TO_SERVER.put(Task.UUID.name, "uuid"); - TASK_COLUMNS_LOCAL_TO_SERVER.put(Task.PROOF_TEXT.name, "proof_text"); - - // Reverse the mapping to construct the server to local map - TASK_COLUMNS_SERVER_TO_LOCAL = AndroidUtilities.reverseMap(TASK_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(Task.TITLE, "title", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(Task.IMPORTANCE, "importance", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(Task.DUE_DATE, "due", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(Task.HIDE_UNTIL, "hide_until", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(Task.COMPLETION_DATE, "completed_at", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(Task.DELETION_DATE, "deleted_at", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(Task.NOTES, "notes", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(Task.RECURRENCE, "repeat", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(Task.USER_ID, "user_id", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(Task.USER, "user", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(Task.CREATOR_ID, "creator_id", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(Task.UUID, "uuid", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(Task.PROOF_TEXT, "proof_text", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMNS_LOCAL_TO_SERVER); + + TASK_PROPERTIES_SERVER_TO_LOCAL = AndroidUtilities.reverseMap(TASK_PROPERTIES_LOCAL_TO_SERVER); } + + private static final Map, String> TAG_DATA_PROPERTIES_LOCAL_TO_SERVER; private static final Map TAG_DATA_COLUMNS_LOCAL_TO_SERVER; - private static final Map TAG_DATA_COLUMNS_SERVER_TO_LOCAL; + private static final Map> TAG_DATA_PROPERTIES_SERVER_TO_LOCAL; static { // Hardcoded local columns mapped to corresponding server names + TAG_DATA_PROPERTIES_LOCAL_TO_SERVER = new HashMap, String>(); TAG_DATA_COLUMNS_LOCAL_TO_SERVER = new HashMap(); - TAG_DATA_COLUMNS_LOCAL_TO_SERVER.put(TagData.USER_ID.name, "user_id"); - TAG_DATA_COLUMNS_LOCAL_TO_SERVER.put(TagData.USER.name, "user"); //TODO: NOT CORRECT - TAG_DATA_COLUMNS_LOCAL_TO_SERVER.put(TagData.NAME.name, "name"); - TAG_DATA_COLUMNS_LOCAL_TO_SERVER.put(TagData.PICTURE.name, "picture_id"); //TODO: NOT CORRECT - TAG_DATA_COLUMNS_LOCAL_TO_SERVER.put(TagData.MEMBERS.name, "members"); //TODO: NOT CORRECT - TAG_DATA_COLUMNS_LOCAL_TO_SERVER.put(TagData.CREATION_DATE.name, "created_at"); - TAG_DATA_COLUMNS_LOCAL_TO_SERVER.put(TagData.DELETION_DATE.name, "deleted_at"); - TAG_DATA_COLUMNS_LOCAL_TO_SERVER.put(TagData.UUID.name, "uuid"); - TAG_DATA_COLUMNS_LOCAL_TO_SERVER.put(TagData.PROOF_TEXT.name, "proof_text"); - TAG_DATA_COLUMNS_LOCAL_TO_SERVER.put(TagData.TAG_ORDERING.name, "tag_ordering"); //TODO: NOT CORRECT + + putPropertyToServerName(TagData.USER_ID, "user_id", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(TagData.USER, "user", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMNS_LOCAL_TO_SERVER); //TODO: NOT CORRECT + putPropertyToServerName(TagData.NAME, "name", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(TagData.PICTURE, "picture_id", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMNS_LOCAL_TO_SERVER); //TODO: NOT CORRECT + putPropertyToServerName(TagData.MEMBERS, "members", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMNS_LOCAL_TO_SERVER); //TODO: NOT CORRECT + putPropertyToServerName(TagData.CREATION_DATE, "created_at", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(TagData.DELETION_DATE, "deleted_at", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(TagData.UUID, "uuid", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(TagData.PROOF_TEXT, "proof_text", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(TagData.TAG_ORDERING, "tag_ordering", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMNS_LOCAL_TO_SERVER); //TODO: NOT CORRECT // Reverse the mapping to construct the server to local map - TAG_DATA_COLUMNS_SERVER_TO_LOCAL = AndroidUtilities.reverseMap(TAG_DATA_COLUMNS_LOCAL_TO_SERVER); + TAG_DATA_PROPERTIES_SERVER_TO_LOCAL = AndroidUtilities.reverseMap(TAG_DATA_PROPERTIES_LOCAL_TO_SERVER); } - public static String localColumnNameToServerColumnName(Table table, String localColumn) { + public static String localColumnNameToServerColumnName(String table, String localColumn) { Map map = null; - if (table == Task.TABLE) + if (SERVER_TABLE_TASKS.equals(table)) map = TASK_COLUMNS_LOCAL_TO_SERVER; - else if (table == TagData.TABLE) + else if (SERVER_TABLE_TAGS.equals(table)) map = TAG_DATA_COLUMNS_LOCAL_TO_SERVER; if (map == null) @@ -103,12 +115,12 @@ public class NameMaps { return map.get(localColumn); } - public static String serverColumnNameToLocalColumnName(Table table, String serverColumn) { - Map map = null; - if (table == Task.TABLE) - map = TASK_COLUMNS_SERVER_TO_LOCAL; - else if (table == TagData.TABLE) - map = TAG_DATA_COLUMNS_SERVER_TO_LOCAL; + public static Property serverColumnNameToLocalProperty(String table, String serverColumn) { + Map> map = null; + if (SERVER_TABLE_TASKS.equals(table)) + map = TASK_PROPERTIES_SERVER_TO_LOCAL; + else if (SERVER_TABLE_TAGS.equals(table)) + map = TAG_DATA_PROPERTIES_SERVER_TO_LOCAL; if (map == null) return null; diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/RequestDoubleCheck.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/RequestDoubleCheck.java index 8abc8bfc0..54a6a6bad 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/RequestDoubleCheck.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/RequestDoubleCheck.java @@ -19,7 +19,6 @@ public class RequestDoubleCheck extends ClientToServer json.put(TYPE_KEY, "RequestDoubleCheck"); //$NON-NLS-1$ json.put(TABLE_KEY, NameMaps.getServerNameForTable(table)); json.put(UUID_KEY, uuid); - json.put(PUSHED_AT_KEY, pushedAt); } catch (JSONException e) { return null; } diff --git a/astrid/plugin-src/com/todoroo/astrid/core/PluginServices.java b/astrid/plugin-src/com/todoroo/astrid/core/PluginServices.java index 0da07e765..f99ee6d52 100644 --- a/astrid/plugin-src/com/todoroo/astrid/core/PluginServices.java +++ b/astrid/plugin-src/com/todoroo/astrid/core/PluginServices.java @@ -13,6 +13,10 @@ import com.todoroo.andlib.sql.Query; import com.todoroo.astrid.dao.Database; import com.todoroo.astrid.dao.MetadataDao.MetadataCriteria; import com.todoroo.astrid.dao.StoreObjectDao; +import com.todoroo.astrid.dao.TagDataDao; +import com.todoroo.astrid.dao.TagOutstandingDao; +import com.todoroo.astrid.dao.TaskDao; +import com.todoroo.astrid.dao.TaskOutstandingDao; import com.todoroo.astrid.dao.UserDao; import com.todoroo.astrid.data.Metadata; import com.todoroo.astrid.service.AddOnService; @@ -32,6 +36,9 @@ public final class PluginServices { @Autowired TaskService taskService; + @Autowired + TaskDao taskDao; + @Autowired Database database; @@ -47,12 +54,21 @@ public final class PluginServices { @Autowired TagDataService tagDataService; + @Autowired + TagDataDao tagDataDao; + @Autowired StoreObjectDao storeObjectDao; @Autowired UserDao userDao; + @Autowired + TaskOutstandingDao taskOutstandingDao; + + @Autowired + TagOutstandingDao tagOutstandingDao; + private static PluginServices instance; static { @@ -79,10 +95,26 @@ public final class PluginServices { return getInstance().taskService; } + public static TaskDao getTaskDao() { + return getInstance().taskDao; + } + public static TagDataService getTagDataService() { return getInstance().tagDataService; } + public static TagDataDao getTagDataDao() { + return getInstance().tagDataDao; + } + + public static TaskOutstandingDao getTaskOutstandingDao() { + return getInstance().taskOutstandingDao; + } + + public static TagOutstandingDao getTagOutstandingDao() { + return getInstance().tagOutstandingDao; + } + public static ExceptionService getExceptionService() { return getInstance().exceptionService; }