From a4405481f7d6ac0d56d7ed6f70a4ca163bd5e6aa Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Fri, 16 Nov 2012 15:28:15 -0800 Subject: [PATCH] Use visitors when processing ChangesHappened or MakeChanges to ensure types are correct; started working on ReplayOutstandingEntries --- .../astrid/actfm/sync/ActFmSyncThread.java | 21 ++++- .../actfm/sync/messages/ChangesHappened.java | 60 +++++++++++-- .../actfm/sync/messages/MakeChanges.java | 64 +++++++++---- .../astrid/actfm/sync/messages/NameMaps.java | 90 ++++++++++--------- .../messages/ReplayOutstandingEntries.java | 36 ++++++++ .../astrid/dao/DaoReflectionHelpers.java | 12 +++ .../todoroo/astrid/sync/SyncMessageTest.java | 3 +- 7 files changed, 212 insertions(+), 74 deletions(-) create mode 100644 astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ReplayOutstandingEntries.java diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncThread.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncThread.java index fe2689f92..a4a6b11ce 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncThread.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncThread.java @@ -14,12 +14,17 @@ import com.todoroo.andlib.service.Autowired; import com.todoroo.andlib.service.DependencyInjectionService; import com.todoroo.astrid.actfm.sync.messages.BriefMe; import com.todoroo.astrid.actfm.sync.messages.ClientToServerMessage; +import com.todoroo.astrid.actfm.sync.messages.ReplayOutstandingEntries; import com.todoroo.astrid.actfm.sync.messages.ServerToClientMessage; import com.todoroo.astrid.core.PluginServices; 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.data.TagData; +import com.todoroo.astrid.data.TagOutstanding; import com.todoroo.astrid.data.Task; +import com.todoroo.astrid.data.TaskOutstanding; public class ActFmSyncThread { @@ -35,6 +40,18 @@ public class ActFmSyncThread { @Autowired private ActFmPreferenceService actFmPreferenceService; + @Autowired + private TaskDao taskDao; + + @Autowired + private TaskOutstandingDao taskOutstandingDao; + + @Autowired + private TagDataDao tagDataDao; + + @Autowired + private TagOutstandingDao tagOutstandingDao; + private String token; public static enum ModelType { @@ -172,8 +189,8 @@ public class ActFmSyncThread { // Reapplies changes still in the outstanding tables to the local database // Called after a batch has finished processing private void replayOutstandingChanges() { - // TODO: Replay existing outstanding changes - + new ReplayOutstandingEntries(Task.class, taskDao, taskOutstandingDao).execute(); + new ReplayOutstandingEntries(TagData.class, tagDataDao, tagOutstandingDao).execute(); } private boolean timeForBackgroundSync() { 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 89dd4054b..86548f1b7 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 @@ -9,8 +9,8 @@ import org.json.JSONObject; import android.util.Log; -import com.todoroo.andlib.data.AbstractModel; import com.todoroo.andlib.data.Property; +import com.todoroo.andlib.data.Property.PropertyVisitor; import com.todoroo.andlib.data.TodorooCursor; import com.todoroo.andlib.sql.Order; import com.todoroo.andlib.sql.Query; @@ -53,7 +53,7 @@ public class ChangesHappened outstandingDao) { super(id, modelClass, modelDao); - this.outstandingClass = getOutstandingClass(modelClass); + this.outstandingClass = DaoReflectionHelpers.getOutstandingClass(modelClass); this.changes = new ArrayList(); if (!RemoteModel.NO_UUID.equals(uuid)) @@ -81,17 +81,23 @@ public class ChangesHappened localProperty = NameMaps.localColumnNameToProperty(table, localColumn); + if (localProperty == null) + throw new RuntimeException("No local property found for local column " + localColumn + " in table " + table); + String serverColumn = NameMaps.localColumnNameToServerColumnName(table, localColumn); if (serverColumn == null) throw new RuntimeException("No server column found for local column " + localColumn + " in table " + table); + JSONObject changeJson = new JSONObject(); changeJson.put("id", change.getId()); changeJson.put("column", serverColumn); - changeJson.put("value", change.getValue(OutstandingEntry.VALUE_STRING_PROPERTY)); + changeJson.put("value", localProperty.accept(visitor, change)); array.put(changeJson); } catch (JSONException e) { @@ -102,7 +108,7 @@ public class ChangesHappened outstandingDao) { - TodorooCursor cursor = outstandingDao.query(Query.select(getModelProperties(outstandingClass)) + TodorooCursor cursor = outstandingDao.query(Query.select(DaoReflectionHelpers.getModelProperties(outstandingClass)) .where(OutstandingEntry.ENTITY_ID_PROPERTY.eq(id)).orderBy(Order.asc(OutstandingEntry.CREATED_AT_PROPERTY))); try { for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { @@ -121,11 +127,47 @@ public class ChangesHappened getOutstandingClass(Class model) { - return DaoReflectionHelpers.getStaticFieldByReflection(model, Class.class, "OUTSTANDING_MODEL"); - } + private class PropertyToJSONVisitor implements PropertyVisitor { + + private String getAsString(OE data) { + return data.getValue(OutstandingEntry.VALUE_STRING_PROPERTY); + } + + @Override + public Object visitInteger(Property property, OE data) { + Integer i = data.getMergedValues().getAsInteger(OutstandingEntry.VALUE_STRING_PROPERTY.name); + if (i != null) { + return i.intValue(); + } else { + return getAsString(data); + } + } + + @Override + public Object visitLong(Property property, OE data) { + Long l = data.getMergedValues().getAsLong(OutstandingEntry.VALUE_STRING_PROPERTY.name); + if (l != null) { + return l.longValue(); + } else { + return getAsString(data); + } + } + + @Override + public Object visitDouble(Property property, OE data) { + Double d = data.getMergedValues().getAsDouble(OutstandingEntry.VALUE_STRING_PROPERTY.name); + if (d != null) { + return d.doubleValue(); + } else { + return getAsString(data); + } + } + + @Override + public Object visitString(Property property, OE data) { + return getAsString(data); + } - private Property[] getModelProperties(Class model) { - return DaoReflectionHelpers.getStaticFieldByReflection(model, Property[].class, "PROPERTIES"); } + } diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/MakeChanges.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/MakeChanges.java index 6264fe0c0..0faa9a615 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/MakeChanges.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/MakeChanges.java @@ -12,6 +12,7 @@ import com.todoroo.andlib.data.Property; import com.todoroo.andlib.data.Property.DoubleProperty; import com.todoroo.andlib.data.Property.IntegerProperty; import com.todoroo.andlib.data.Property.LongProperty; +import com.todoroo.andlib.data.Property.PropertyVisitor; import com.todoroo.andlib.data.Property.StringProperty; import com.todoroo.astrid.dao.RemoteModelDao; import com.todoroo.astrid.data.RemoteModel; @@ -40,12 +41,13 @@ public class MakeChanges extends ServerToClientMessage TYPE model = dao.getModelClass().newInstance(); JSONArray keys = changes.names(); if (keys != null) { + JSONToPropertyVisitor visitor = new JSONToPropertyVisitor(changes, model); for (int i = 0; i < keys.length(); i++) { String col = keys.optString(i); if (!TextUtils.isEmpty(col)) { Property property = NameMaps.serverColumnNameToLocalProperty(table, col); if (property != null) { // Unsupported property - setPropertyFromJSON(model, property, changes, col); + property.accept(visitor, col); } } } @@ -66,36 +68,60 @@ public class MakeChanges extends ServerToClientMessage } } - private static void setPropertyFromJSON(AbstractModel model, Property property, JSONObject json, String jsonKey) { - if (property instanceof LongProperty) { + + private static class JSONToPropertyVisitor implements PropertyVisitor { + + private final JSONObject json; + private final AbstractModel model; + + public JSONToPropertyVisitor(JSONObject json, AbstractModel model) { + this.json = json; + this.model = model; + } + + @Override + public Void visitInteger(Property property, String data) { try { - long value = json.getLong(jsonKey); - model.setValue((LongProperty) property, value); + int value = json.getInt(data); + model.setValue((IntegerProperty) property, value); } catch (JSONException e) { - Log.e(ERROR_TAG, "Error reading long from JSON " + json + " with key " + jsonKey, e); + Log.e(ERROR_TAG, "Error reading int from JSON " + json + " with key " + data, e); } - } else if (property instanceof StringProperty) { + return null; + } + + @Override + public Void visitLong(Property property, String data) { try { - String value = json.getString(jsonKey); - model.setValue((StringProperty) property, value); + long value = json.getLong(data); + model.setValue((LongProperty) property, value); } catch (JSONException e) { - Log.e(ERROR_TAG, "Error reading string from JSON " + json + " with key " + jsonKey, e); + Log.e(ERROR_TAG, "Error reading long from JSON " + json + " with key " + data, e); } - } else if (property instanceof IntegerProperty) { + return null; + } + + @Override + public Void visitDouble(Property property, String data) { try { - int value = json.getInt(jsonKey); - model.setValue((IntegerProperty) property, value); + double value = json.getDouble(data); + model.setValue((DoubleProperty) property, value); } catch (JSONException e) { - Log.e(ERROR_TAG, "Error reading int from JSON " + json + " with key " + jsonKey, e); + Log.e(ERROR_TAG, "Error reading double from JSON " + json + " with key " + data, e); } - } else if (property instanceof DoubleProperty) { + return null; + } + + @Override + public Void visitString(Property property, String data) { try { - double value = json.getDouble(jsonKey); - model.setValue((DoubleProperty) property, value); + String value = json.getString(data); + model.setValue((StringProperty) property, value); } catch (JSONException e) { - Log.e(ERROR_TAG, "Error reading double from JSON " + json + " with key " + jsonKey, e); + Log.e(ERROR_TAG, "Error reading string from JSON " + json + " with key " + data, e); } + return null; } - } + } } diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/NameMaps.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/NameMaps.java index b6380fd59..f9014c7f0 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/NameMaps.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/NameMaps.java @@ -47,39 +47,44 @@ public class NameMaps { // ---- Column name mappings ------- // -------------------------------- private static final Map, String> TASK_PROPERTIES_LOCAL_TO_SERVER; + private static final Map> TASK_COLUMN_NAMES_TO_PROPERTIES; private static final Map TASK_COLUMNS_LOCAL_TO_SERVER; private static final Map> TASK_PROPERTIES_SERVER_TO_LOCAL; private static void putPropertyToServerName(Property property, String serverName, - Map, String> propertyMap, Map nameMap) { + Map, String> propertyMap, Map> localNameMap, Map serverNameMap) { propertyMap.put(property, serverName); - nameMap.put(property.name, serverName); + localNameMap.put(property.name, property); + serverNameMap.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(); - 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); - putPropertyToServerName(Task.PUSHED_AT, "pushed_at", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMNS_LOCAL_TO_SERVER); + TASK_COLUMN_NAMES_TO_PROPERTIES = new HashMap>(); + + putPropertyToServerName(Task.TITLE, "title", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMN_NAMES_TO_PROPERTIES, TASK_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(Task.IMPORTANCE, "importance", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMN_NAMES_TO_PROPERTIES, TASK_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(Task.DUE_DATE, "due", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMN_NAMES_TO_PROPERTIES, TASK_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(Task.HIDE_UNTIL, "hide_until", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMN_NAMES_TO_PROPERTIES, TASK_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(Task.COMPLETION_DATE, "completed_at", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMN_NAMES_TO_PROPERTIES, TASK_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(Task.DELETION_DATE, "deleted_at", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMN_NAMES_TO_PROPERTIES, TASK_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(Task.NOTES, "notes", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMN_NAMES_TO_PROPERTIES, TASK_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(Task.RECURRENCE, "repeat", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMN_NAMES_TO_PROPERTIES, TASK_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(Task.USER_ID, "user_id", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMN_NAMES_TO_PROPERTIES, TASK_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(Task.USER, "user", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMN_NAMES_TO_PROPERTIES, TASK_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(Task.CREATOR_ID, "creator_id", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMN_NAMES_TO_PROPERTIES, TASK_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(Task.UUID, "uuid", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMN_NAMES_TO_PROPERTIES, TASK_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(Task.PROOF_TEXT, "proof_text", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMN_NAMES_TO_PROPERTIES, TASK_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(Task.PUSHED_AT, "pushed_at", TASK_PROPERTIES_LOCAL_TO_SERVER, TASK_COLUMN_NAMES_TO_PROPERTIES, 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_COLUMN_NAMES_TO_PROPERTIES; private static final Map TAG_DATA_COLUMNS_LOCAL_TO_SERVER; private static final Map> TAG_DATA_PROPERTIES_SERVER_TO_LOCAL; @@ -87,47 +92,48 @@ public class NameMaps { // 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(); - - 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 - putPropertyToServerName(TagData.PUSHED_AT, "pushed_at", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMNS_LOCAL_TO_SERVER); + TAG_DATA_COLUMN_NAMES_TO_PROPERTIES = new HashMap>(); + + putPropertyToServerName(TagData.USER_ID, "user_id", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMN_NAMES_TO_PROPERTIES, TAG_DATA_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(TagData.USER, "user", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMN_NAMES_TO_PROPERTIES, TAG_DATA_COLUMNS_LOCAL_TO_SERVER); //TODO: NOT CORRECT + putPropertyToServerName(TagData.NAME, "name", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMN_NAMES_TO_PROPERTIES, TAG_DATA_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(TagData.PICTURE, "picture_id", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMN_NAMES_TO_PROPERTIES, TAG_DATA_COLUMNS_LOCAL_TO_SERVER); //TODO: NOT CORRECT + putPropertyToServerName(TagData.MEMBERS, "members", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMN_NAMES_TO_PROPERTIES, TAG_DATA_COLUMNS_LOCAL_TO_SERVER); //TODO: NOT CORRECT + putPropertyToServerName(TagData.CREATION_DATE, "created_at", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMN_NAMES_TO_PROPERTIES, TAG_DATA_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(TagData.DELETION_DATE, "deleted_at", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMN_NAMES_TO_PROPERTIES, TAG_DATA_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(TagData.UUID, "uuid", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMN_NAMES_TO_PROPERTIES, TAG_DATA_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(TagData.PROOF_TEXT, "proof_text", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMN_NAMES_TO_PROPERTIES, TAG_DATA_COLUMNS_LOCAL_TO_SERVER); + putPropertyToServerName(TagData.TAG_ORDERING, "tag_ordering", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMN_NAMES_TO_PROPERTIES, TAG_DATA_COLUMNS_LOCAL_TO_SERVER); //TODO: NOT CORRECT + putPropertyToServerName(TagData.PUSHED_AT, "pushed_at", TAG_DATA_PROPERTIES_LOCAL_TO_SERVER, TAG_DATA_COLUMN_NAMES_TO_PROPERTIES, TAG_DATA_COLUMNS_LOCAL_TO_SERVER); // Reverse the mapping to construct the server to local map TAG_DATA_PROPERTIES_SERVER_TO_LOCAL = AndroidUtilities.reverseMap(TAG_DATA_PROPERTIES_LOCAL_TO_SERVER); } - public static String localColumnNameToServerColumnName(String table, String localColumn) { - Map map = null; + private static B mapColumnName(String table, String col, Map taskMap, Map tagMap) { + Map map = null; if (SERVER_TABLE_TASKS.equals(table)) - map = TASK_COLUMNS_LOCAL_TO_SERVER; + map = taskMap; else if (SERVER_TABLE_TAGS.equals(table)) - map = TAG_DATA_COLUMNS_LOCAL_TO_SERVER; + map = tagMap; if (map == null) return null; - return map.get(localColumn); + return map.get(col); } - 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; + public static String localColumnNameToServerColumnName(String table, String localColumn) { + return mapColumnName(table, localColumn, TASK_COLUMNS_LOCAL_TO_SERVER, TAG_DATA_COLUMNS_LOCAL_TO_SERVER); + } - if (map == null) - return null; + public static Property localColumnNameToProperty(String table, String localColumn) { + return mapColumnName(table, localColumn, TASK_COLUMN_NAMES_TO_PROPERTIES, TAG_DATA_COLUMN_NAMES_TO_PROPERTIES); + } - return map.get(serverColumn); + public static Property serverColumnNameToLocalProperty(String table, String serverColumn) { + return mapColumnName(table, serverColumn, TASK_PROPERTIES_SERVER_TO_LOCAL, TAG_DATA_PROPERTIES_SERVER_TO_LOCAL); } + } diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ReplayOutstandingEntries.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ReplayOutstandingEntries.java new file mode 100644 index 000000000..3e0bd6db9 --- /dev/null +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ReplayOutstandingEntries.java @@ -0,0 +1,36 @@ +package com.todoroo.astrid.actfm.sync.messages; + +import com.todoroo.andlib.data.TodorooCursor; +import com.todoroo.andlib.sql.Order; +import com.todoroo.andlib.sql.Query; +import com.todoroo.astrid.dao.DaoReflectionHelpers; +import com.todoroo.astrid.dao.OutstandingEntryDao; +import com.todoroo.astrid.dao.RemoteModelDao; +import com.todoroo.astrid.data.OutstandingEntry; +import com.todoroo.astrid.data.RemoteModel; + +public class ReplayOutstandingEntries> { + + private final Class modelClass; + private final Class outstandingClass; + private final RemoteModelDao dao; + private final OutstandingEntryDao outstandingDao; + + public ReplayOutstandingEntries(Class modelClass, RemoteModelDao dao, OutstandingEntryDao outstandingDao) { + this.modelClass = modelClass; + this.outstandingClass = DaoReflectionHelpers.getOutstandingClass(modelClass); + this.dao = dao; + this.outstandingDao = outstandingDao; + } + + public void execute() { + TodorooCursor outstanding = outstandingDao.query(Query.select(DaoReflectionHelpers.getModelProperties(outstandingClass)) + .orderBy(Order.asc(OutstandingEntry.ENTITY_ID_PROPERTY), Order.asc(OutstandingEntry.CREATED_AT_PROPERTY))); + try { + // Do and apply + } finally { + outstanding.close(); + } + } + +} diff --git a/astrid/src/com/todoroo/astrid/dao/DaoReflectionHelpers.java b/astrid/src/com/todoroo/astrid/dao/DaoReflectionHelpers.java index c6b68dcfb..5a35253da 100644 --- a/astrid/src/com/todoroo/astrid/dao/DaoReflectionHelpers.java +++ b/astrid/src/com/todoroo/astrid/dao/DaoReflectionHelpers.java @@ -2,6 +2,10 @@ package com.todoroo.astrid.dao; import java.lang.reflect.Field; +import com.todoroo.andlib.data.AbstractModel; +import com.todoroo.andlib.data.Property; +import com.todoroo.astrid.data.RemoteModel; + @SuppressWarnings("nls") public class DaoReflectionHelpers { @@ -22,4 +26,12 @@ public class DaoReflectionHelpers { } } + public static Property[] getModelProperties(Class model) { + return getStaticFieldByReflection(model, Property[].class, "PROPERTIES"); + } + + public static Class getOutstandingClass(Class model) { + return DaoReflectionHelpers.getStaticFieldByReflection(model, Class.class, "OUTSTANDING_MODEL"); + } + } diff --git a/tests-sync/src/com/todoroo/astrid/sync/SyncMessageTest.java b/tests-sync/src/com/todoroo/astrid/sync/SyncMessageTest.java index ad90dc604..47e949e90 100644 --- a/tests-sync/src/com/todoroo/astrid/sync/SyncMessageTest.java +++ b/tests-sync/src/com/todoroo/astrid/sync/SyncMessageTest.java @@ -5,7 +5,6 @@ import org.json.JSONObject; import com.todoroo.astrid.actfm.sync.ActFmSyncThread.ModelType; import com.todoroo.astrid.actfm.sync.messages.ChangesHappened; -import com.todoroo.astrid.actfm.sync.messages.ClientToServerMessage; import com.todoroo.astrid.actfm.sync.messages.NameMaps; import com.todoroo.astrid.actfm.sync.messages.ServerToClientMessage; import com.todoroo.astrid.data.RemoteModel; @@ -16,7 +15,7 @@ public class SyncMessageTest extends NewSyncTestCase { public void testTaskChangesHappenedConstructor() { Task t = createTask(); try { - ChangesHappened changes = ClientToServerMessage.instantiateChangesHappened(t.getId(), ModelType.TYPE_TASK); + ChangesHappened changes = ChangesHappened.instantiateChangesHappened(t.getId(), ModelType.TYPE_TASK); assertTrue(changes.numChanges() > 0); assertFalse(RemoteModel.NO_UUID.equals(changes.getUUID())); assertEquals(t.getValue(Task.UUID), changes.getUUID());