Refactored some client to server logic so that ChangesHappened doesn't have to do a bunch of expensive stuff on instantiation

pull/14/head
Sam Bosley 12 years ago
parent a2e897083e
commit 7879d517b0

@ -23,11 +23,9 @@ public class SyncDatabaseListener<MTYPE extends RemoteModel> implements ModelUpd
@Override
public void onModelUpdated(MTYPE model) {
ChangesHappened<?, ?> ch = ChangesHappened.instantiateChangesHappened(model.getId(), modelType);
if (ch.numChanges() > 0) {
queue.add(ch);
synchronized(monitor) {
monitor.notifyAll();
}
queue.add(ch);
synchronized(monitor) {
monitor.notifyAll();
}
}

@ -23,8 +23,9 @@ public class BriefMe<TYPE extends RemoteModel> extends ClientToServerMessage<TYP
}
@Override
protected void serializeExtrasToJSON(JSONObject serializeTo) throws JSONException {
protected boolean serializeExtrasToJSON(JSONObject serializeTo) throws JSONException {
// No extras
return true;
}
@Override

@ -35,6 +35,7 @@ public class ChangesHappened<TYPE extends RemoteModel, OE extends OutstandingEnt
private final Class<OE> outstandingClass;
private final List<OE> changes;
private final OutstandingEntryDao<OE> outstandingDao;
public static final String CHANGES_KEY = "changes";
@ -56,16 +57,19 @@ public class ChangesHappened<TYPE extends RemoteModel, OE extends OutstandingEnt
super(id, modelClass, modelDao);
this.outstandingClass = DaoReflectionHelpers.getOutstandingClass(modelClass);
this.outstandingDao = outstandingDao;
this.changes = new ArrayList<OE>();
if (!RemoteModel.NO_UUID.equals(uuid))
populateChanges(outstandingDao);
}
@Override
protected void serializeExtrasToJSON(JSONObject serializeTo) throws JSONException {
protected boolean serializeExtrasToJSON(JSONObject serializeTo) throws JSONException {
// Process changes list and serialize to JSON
serializeTo.put(CHANGES_KEY, changesToJSON());
JSONArray changesJson = changesToJSON();
if (changesJson == null)
return false;
serializeTo.put(CHANGES_KEY, changesJson);
return true;
}
@Override
@ -77,11 +81,10 @@ public class ChangesHappened<TYPE extends RemoteModel, OE extends OutstandingEnt
return changes;
}
public int numChanges() {
return changes.size();
}
private JSONArray changesToJSON() {
if (!RemoteModel.NO_UUID.equals(uuid))
populateChanges();
JSONArray array = new JSONArray();
PropertyToJSONVisitor visitor = new PropertyToJSONVisitor();
for (OE change : changes) {
@ -118,7 +121,7 @@ public class ChangesHappened<TYPE extends RemoteModel, OE extends OutstandingEnt
return array;
}
private void populateChanges(OutstandingEntryDao<OE> outstandingDao) {
private void populateChanges() {
TodorooCursor<OE> cursor = outstandingDao.query(Query.select(DaoReflectionHelpers.getModelProperties(outstandingClass))
.where(OutstandingEntry.ENTITY_ID_PROPERTY.eq(id)).orderBy(Order.asc(OutstandingEntry.CREATED_AT_PROPERTY)));
try {

@ -69,13 +69,15 @@ public abstract class ClientToServerMessage<TYPE extends RemoteModel> {
json.put(UUID_KEY, uuid);
String dateValue = DateUtilities.timeToIso8601(pushedAt, true);
json.put(PUSHED_AT_KEY, dateValue != null ? dateValue : 0);
serializeExtrasToJSON(json);
if (serializeExtrasToJSON(json))
return json;
else
return null;
} catch (JSONException e) {
return null;
}
return json;
}
protected abstract void serializeExtrasToJSON(JSONObject serializeTo) throws JSONException;
protected abstract boolean serializeExtrasToJSON(JSONObject serializeTo) throws JSONException;
protected abstract String getTypeString();
}

@ -13,8 +13,9 @@ public class RequestDoubleCheck<TYPE extends RemoteModel> extends ClientToServer
}
@Override
protected void serializeExtrasToJSON(JSONObject serializeTo) throws JSONException {
protected boolean serializeExtrasToJSON(JSONObject serializeTo) throws JSONException {
// No extras
return true;
}
@Override

@ -24,7 +24,7 @@ public class SyncMessageTest extends NewSyncTestCase {
Task t = createTask();
try {
ChangesHappened<?, ?> changes = ChangesHappened.instantiateChangesHappened(t.getId(), ModelType.TYPE_TASK);
assertTrue(changes.numChanges() > 0);
assertNotNull(changes.serializeToJSON());
assertFalse(RemoteModel.NO_UUID.equals(changes.getUUID()));
assertEquals(t.getValue(Task.UUID), changes.getUUID());
} catch (Exception e) {

Loading…
Cancel
Save