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 13 years ago
parent a2e897083e
commit 7879d517b0

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

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

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

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

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

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

Loading…
Cancel
Save