From 742e53ea5452282c4d58eec3c08f6102d136e733 Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Thu, 15 Nov 2012 15:27:50 -0800 Subject: [PATCH] Refactored ClientToServerMessage JSON serialization --- .../astrid/actfm/sync/messages/BriefMe.java | 18 +++++-------- .../actfm/sync/messages/ChangesHappened.java | 26 +++++++------------ .../sync/messages/ClientToServerMessage.java | 26 ++++++++++++++++--- .../sync/messages/RequestDoubleCheck.java | 17 +++++------- 4 files changed, 46 insertions(+), 41 deletions(-) diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/BriefMe.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/BriefMe.java index e52582333..99979cf8f 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/BriefMe.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/BriefMe.java @@ -17,17 +17,13 @@ public class BriefMe extends ClientToServerMessage getChanges() { @@ -66,14 +60,14 @@ public class ChangesHappened { protected final Class modelClass; - protected final Table table; + protected final String table; protected final long id; protected final String uuid; protected final long pushedAt; @@ -24,7 +25,8 @@ public abstract class ClientToServerMessage { public ClientToServerMessage(Class modelClass, String uuid, long pushedAt) { this.modelClass = modelClass; - this.table = DaoReflectionHelpers.getStaticFieldByReflection(modelClass, Table.class, "TABLE"); + Table tableClass = DaoReflectionHelpers.getStaticFieldByReflection(modelClass, Table.class, "TABLE"); + this.table = NameMaps.getServerNameForTable(tableClass); this.uuid = uuid; this.pushedAt = pushedAt; this.id = AbstractModel.NO_ID; @@ -33,7 +35,8 @@ public abstract class ClientToServerMessage { public ClientToServerMessage(long id, Class modelClass, RemoteModelDao modelDao) { this.id = id; this.modelClass = modelClass; - this.table = DaoReflectionHelpers.getStaticFieldByReflection(modelClass, Table.class, "TABLE"); + Table tableClass = DaoReflectionHelpers.getStaticFieldByReflection(modelClass, Table.class, "TABLE"); + this.table = NameMaps.getServerNameForTable(tableClass); TYPE entity = getEntity(id, modelDao); if (entity == null) { @@ -57,6 +60,21 @@ public abstract class ClientToServerMessage { return pushedAt; } - public abstract JSONObject serializeToJSON(); + public final JSONObject serializeToJSON() { + JSONObject json = new JSONObject(); + try { + json.put(TYPE_KEY, getTypeString()); + json.put(TABLE_KEY, table); + json.put(UUID_KEY, uuid); + json.put(PUSHED_AT_KEY, pushedAt); + serializeToJSONImpl(json); + } catch (JSONException e) { + return null; + } + return json; + } + + protected abstract void serializeToJSONImpl(JSONObject serializeTo) throws JSONException; + protected abstract String getTypeString(); } 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 54a6a6bad..21ddd11d3 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 @@ -13,15 +13,12 @@ public class RequestDoubleCheck extends ClientToServer } @Override - public JSONObject serializeToJSON() { - JSONObject json = new JSONObject(); - try { - json.put(TYPE_KEY, "RequestDoubleCheck"); //$NON-NLS-1$ - json.put(TABLE_KEY, NameMaps.getServerNameForTable(table)); - json.put(UUID_KEY, uuid); - } catch (JSONException e) { - return null; - } - return json; + protected void serializeToJSONImpl(JSONObject serializeTo) throws JSONException { + // No extras + } + + @Override + protected String getTypeString() { + return "RequestDoubleCheck"; //$NON-NLS-1$ } }