Refactored ClientToServerMessage JSON serialization

pull/14/head
Sam Bosley 13 years ago
parent e479f7a3ff
commit 742e53ea54

@ -17,17 +17,13 @@ public class BriefMe<TYPE extends RemoteModel> extends ClientToServerMessage<TYP
} }
@Override @Override
public JSONObject serializeToJSON() { protected void serializeToJSONImpl(JSONObject serializeTo) throws JSONException {
JSONObject json = new JSONObject(); // No extras
try {
json.put(TYPE_KEY, "BriefMe"); //$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;
} }
return json;
@Override
protected String getTypeString() {
return "BriefMe"; //$NON-NLS-1$
} }
} }

@ -42,20 +42,14 @@ public class ChangesHappened<TYPE extends RemoteModel, OE extends OutstandingEnt
} }
@Override @Override
public JSONObject serializeToJSON() { protected void serializeToJSONImpl(JSONObject serializeTo) throws JSONException {
// Process changes list and serialize to JSON // Process changes list and serialize to JSON
JSONObject json = new JSONObject(); serializeTo.put(CHANGES_KEY, changesToJSON());
try {
String serverTable = NameMaps.getServerNameForTable(table);
json.put(TYPE_KEY, "ChangesHappened");
json.put(TABLE_KEY, serverTable);
json.put(UUID_KEY, uuid);
json.put(PUSHED_AT_KEY, pushedAt);
json.put(CHANGES_KEY, changesToJSON(serverTable));
} catch (JSONException e) {
return null;
} }
return json;
@Override
protected String getTypeString() {
return "ChangesHappened";
} }
public List<OE> getChanges() { public List<OE> getChanges() {
@ -66,14 +60,14 @@ public class ChangesHappened<TYPE extends RemoteModel, OE extends OutstandingEnt
return changes.size(); return changes.size();
} }
private JSONArray changesToJSON(String tableString) { private JSONArray changesToJSON() {
JSONArray array = new JSONArray(); JSONArray array = new JSONArray();
for (OE change : changes) { for (OE change : changes) {
try { try {
String localColumn = change.getValue(OutstandingEntry.COLUMN_STRING_PROPERTY); String localColumn = change.getValue(OutstandingEntry.COLUMN_STRING_PROPERTY);
String serverColumn = NameMaps.localColumnNameToServerColumnName(tableString, localColumn); String serverColumn = NameMaps.localColumnNameToServerColumnName(table, localColumn);
if (serverColumn == null) if (serverColumn == null)
throw new RuntimeException("No server column found for local column " + localColumn + " in table " + tableString); throw new RuntimeException("No server column found for local column " + localColumn + " in table " + table);
JSONObject changeJson = new JSONObject(); JSONObject changeJson = new JSONObject();
changeJson.put("id", change.getId()); changeJson.put("id", change.getId());

@ -1,5 +1,6 @@
package com.todoroo.astrid.actfm.sync.messages; package com.todoroo.astrid.actfm.sync.messages;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import com.todoroo.andlib.data.AbstractModel; import com.todoroo.andlib.data.AbstractModel;
@ -12,7 +13,7 @@ import com.todoroo.astrid.data.RemoteModel;
public abstract class ClientToServerMessage<TYPE extends RemoteModel> { public abstract class ClientToServerMessage<TYPE extends RemoteModel> {
protected final Class<TYPE> modelClass; protected final Class<TYPE> modelClass;
protected final Table table; protected final String table;
protected final long id; protected final long id;
protected final String uuid; protected final String uuid;
protected final long pushedAt; protected final long pushedAt;
@ -24,7 +25,8 @@ public abstract class ClientToServerMessage<TYPE extends RemoteModel> {
public ClientToServerMessage(Class<TYPE> modelClass, String uuid, long pushedAt) { public ClientToServerMessage(Class<TYPE> modelClass, String uuid, long pushedAt) {
this.modelClass = modelClass; 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.uuid = uuid;
this.pushedAt = pushedAt; this.pushedAt = pushedAt;
this.id = AbstractModel.NO_ID; this.id = AbstractModel.NO_ID;
@ -33,7 +35,8 @@ public abstract class ClientToServerMessage<TYPE extends RemoteModel> {
public ClientToServerMessage(long id, Class<TYPE> modelClass, RemoteModelDao<TYPE> modelDao) { public ClientToServerMessage(long id, Class<TYPE> modelClass, RemoteModelDao<TYPE> modelDao) {
this.id = id; this.id = id;
this.modelClass = modelClass; 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); TYPE entity = getEntity(id, modelDao);
if (entity == null) { if (entity == null) {
@ -57,6 +60,21 @@ public abstract class ClientToServerMessage<TYPE extends RemoteModel> {
return pushedAt; 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();
} }

@ -13,15 +13,12 @@ public class RequestDoubleCheck<TYPE extends RemoteModel> extends ClientToServer
} }
@Override @Override
public JSONObject serializeToJSON() { protected void serializeToJSONImpl(JSONObject serializeTo) throws JSONException {
JSONObject json = new JSONObject(); // No extras
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;
@Override
protected String getTypeString() {
return "RequestDoubleCheck"; //$NON-NLS-1$
} }
} }

Loading…
Cancel
Save