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
public JSONObject serializeToJSON() {
JSONObject json = new JSONObject();
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;
protected void serializeToJSONImpl(JSONObject serializeTo) throws JSONException {
// No extras
}
@Override
protected String getTypeString() {
return "BriefMe"; //$NON-NLS-1$
}
}

@ -42,20 +42,14 @@ public class ChangesHappened<TYPE extends RemoteModel, OE extends OutstandingEnt
}
@Override
public JSONObject serializeToJSON() {
protected void serializeToJSONImpl(JSONObject serializeTo) throws JSONException {
// Process changes list and serialize to JSON
JSONObject json = new JSONObject();
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;
serializeTo.put(CHANGES_KEY, changesToJSON());
}
@Override
protected String getTypeString() {
return "ChangesHappened";
}
public List<OE> getChanges() {
@ -66,14 +60,14 @@ public class ChangesHappened<TYPE extends RemoteModel, OE extends OutstandingEnt
return changes.size();
}
private JSONArray changesToJSON(String tableString) {
private JSONArray changesToJSON() {
JSONArray array = new JSONArray();
for (OE change : changes) {
try {
String localColumn = change.getValue(OutstandingEntry.COLUMN_STRING_PROPERTY);
String serverColumn = NameMaps.localColumnNameToServerColumnName(tableString, localColumn);
String serverColumn = NameMaps.localColumnNameToServerColumnName(table, localColumn);
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();
changeJson.put("id", change.getId());

@ -1,5 +1,6 @@
package com.todoroo.astrid.actfm.sync.messages;
import org.json.JSONException;
import org.json.JSONObject;
import com.todoroo.andlib.data.AbstractModel;
@ -12,7 +13,7 @@ import com.todoroo.astrid.data.RemoteModel;
public abstract class ClientToServerMessage<TYPE extends RemoteModel> {
protected final Class<TYPE> 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<TYPE extends RemoteModel> {
public ClientToServerMessage(Class<TYPE> 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<TYPE extends RemoteModel> {
public ClientToServerMessage(long id, Class<TYPE> modelClass, RemoteModelDao<TYPE> 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<TYPE extends RemoteModel> {
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
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$
}
}

Loading…
Cancel
Save