From af034a4089b09291dc41675d4eb0c3b99c47c218 Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Wed, 14 Nov 2012 16:17:32 -0800 Subject: [PATCH] Started working on the classes to support column name mapping --- .../andlib/utility/AndroidUtilities.java | 19 +++ .../actfm/sync/messages/ChangesHappened.java | 9 +- .../astrid/actfm/sync/messages/NameMaps.java | 111 ++++++++++++++++++ 3 files changed, 137 insertions(+), 2 deletions(-) diff --git a/api/src/com/todoroo/andlib/utility/AndroidUtilities.java b/api/src/com/todoroo/andlib/utility/AndroidUtilities.java index 5339eebe1..bc37243b6 100644 --- a/api/src/com/todoroo/andlib/utility/AndroidUtilities.java +++ b/api/src/com/todoroo/andlib/utility/AndroidUtilities.java @@ -22,8 +22,10 @@ import java.net.URLConnection; import java.security.MessageDigest; import java.util.Arrays; import java.util.Comparator; +import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; +import java.util.Set; import android.app.Activity; import android.content.BroadcastReceiver; @@ -792,6 +794,23 @@ public class AndroidUtilities { return dest; } + /** + * Returns a map where the keys are the values of the map argument + * and the values are the corresponding keys. Use at your own + * risk if your map is not 1-to-1! + * @param map + * @return + */ + public static Map reverseMap(Map map) { + HashMap reversed = new HashMap(); + + Set> entries = map.entrySet(); + for (Entry entry : entries) { + reversed.put(entry.getValue(), entry.getKey()); + } + return reversed; + } + /** * Capitalize the first character * @param string 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 600b110c7..cec17f953 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 @@ -45,7 +45,7 @@ public class ChangesHappened TABLE_LOCAL_TO_SERVER; + private static final Map TABLE_SERVER_TO_LOCAL; + + static { + // Hardcoded local tables mapped to corresponding server names + TABLE_LOCAL_TO_SERVER = new HashMap(); + TABLE_LOCAL_TO_SERVER.put(Task.TABLE, "tasks"); + TABLE_LOCAL_TO_SERVER.put(TagData.TABLE, "tags"); + TABLE_LOCAL_TO_SERVER.put(User.TABLE, "users"); + + // Reverse the mapping to construct the server to local map + TABLE_SERVER_TO_LOCAL = AndroidUtilities.reverseMap(TABLE_LOCAL_TO_SERVER); + } + + public static String getServerNameForTable(Table table) { + return TABLE_LOCAL_TO_SERVER.get(table); + } + + public static Table getLocalTableForServerName(String serverName) { + return TABLE_SERVER_TO_LOCAL.get(serverName); + } + + + // -------------------------------- + // ---- Column name mappings ------- + // -------------------------------- + private static final Map TASK_COLUMNS_LOCAL_TO_SERVER; + private static final Map TASK_COLUMNS_SERVER_TO_LOCAL; + + static { + // Hardcoded local columns mapped to corresponding server names + 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"); // TODO: NOT CORRECT + TASK_COLUMNS_LOCAL_TO_SERVER.put(Task.CREATOR_ID.name, "creator_id"); + TASK_COLUMNS_LOCAL_TO_SERVER.put(Task.SHARED_WITH.name, "shared_with"); //TODO: NOT CORRECT + 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); + } + + private static final Map TAG_DATA_COLUMNS_LOCAL_TO_SERVER; + private static final Map TAG_DATA_COLUMNS_SERVER_TO_LOCAL; + + static { + // Hardcoded local columns mapped to corresponding server names + 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 + + // Reverse the mapping to construct the server to local map + TAG_DATA_COLUMNS_SERVER_TO_LOCAL = AndroidUtilities.reverseMap(TAG_DATA_COLUMNS_LOCAL_TO_SERVER); + } + + public static String localColumnNameToServerColumnName(Table table, String localColumn) { + Map map = null; + if (table == Task.TABLE) + map = TASK_COLUMNS_LOCAL_TO_SERVER; + else if (table == TagData.TABLE) + map = TAG_DATA_COLUMNS_LOCAL_TO_SERVER; + + if (map == null) + return null; + + 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; + + if (map == null) + return null; + + return map.get(serverColumn); + } + }