Some refactoring, serializing client to server messages to JSON

pull/14/head
Sam Bosley 13 years ago
parent 8feff285cd
commit bf7ba3d7c4

@ -1,5 +1,7 @@
package com.todoroo.astrid.actfm.sync.messages; package com.todoroo.astrid.actfm.sync.messages;
import org.json.JSONObject;
import com.todoroo.astrid.dao.RemoteModelDao; import com.todoroo.astrid.dao.RemoteModelDao;
import com.todoroo.astrid.data.RemoteModel; import com.todoroo.astrid.data.RemoteModel;
@ -14,8 +16,8 @@ public class BriefMe<TYPE extends RemoteModel> extends ClientToServerMessage<TYP
} }
@Override @Override
public void sendMessage() { public JSONObject serializeToJSON() {
// Send message return null;
} }
} }

@ -1,9 +1,12 @@
package com.todoroo.astrid.actfm.sync.messages; package com.todoroo.astrid.actfm.sync.messages;
import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log; import android.util.Log;
import com.todoroo.andlib.data.AbstractModel; import com.todoroo.andlib.data.AbstractModel;
@ -11,6 +14,7 @@ import com.todoroo.andlib.data.Property;
import com.todoroo.andlib.data.TodorooCursor; import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.sql.Order; import com.todoroo.andlib.sql.Order;
import com.todoroo.andlib.sql.Query; import com.todoroo.andlib.sql.Query;
import com.todoroo.astrid.dao.DaoReflectionHelpers;
import com.todoroo.astrid.dao.OutstandingEntryDao; import com.todoroo.astrid.dao.OutstandingEntryDao;
import com.todoroo.astrid.dao.RemoteModelDao; import com.todoroo.astrid.dao.RemoteModelDao;
import com.todoroo.astrid.data.OutstandingEntry; import com.todoroo.astrid.data.OutstandingEntry;
@ -22,6 +26,8 @@ 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;
public static final String CHANGES_KEY = "changes";
public ChangesHappened(long id, Class<TYPE> modelClass, RemoteModelDao<TYPE> modelDao, public ChangesHappened(long id, Class<TYPE> modelClass, RemoteModelDao<TYPE> modelDao,
OutstandingEntryDao<OE> outstandingDao) { OutstandingEntryDao<OE> outstandingDao) {
super(id, modelClass, modelDao); super(id, modelClass, modelDao);
@ -34,8 +40,19 @@ public class ChangesHappened<TYPE extends RemoteModel, OE extends OutstandingEnt
} }
@Override @Override
public void sendMessage() { public JSONObject serializeToJSON() {
// Process changes list and send to server // Process changes list and serialize to JSON
JSONObject json = new JSONObject();
try {
json.put(TYPE_KEY, "ChangesHappened");
json.put(TABLE_KEY, table.name);
json.put(UUID_KEY, uuid);
json.put(PUSHED_AT_KEY, pushedAt);
json.put(CHANGES_KEY, changesToJSON());
} catch (JSONException e) {
return null;
}
return json;
} }
public List<OE> getChanges() { public List<OE> getChanges() {
@ -46,6 +63,23 @@ public class ChangesHappened<TYPE extends RemoteModel, OE extends OutstandingEnt
return changes.size(); return changes.size();
} }
private JSONArray changesToJSON() {
JSONArray array = new JSONArray();
for (OE change : changes) {
try {
JSONObject changeJson = new JSONObject();
changeJson.put("id", change.getId());
changeJson.put("column", change.getValue(OutstandingEntry.COLUMN_STRING_PROPERTY));
changeJson.put("value", change.getValue(OutstandingEntry.VALUE_STRING_PROPERTY));
array.put(changeJson);
} catch (JSONException e) {
//
}
}
return array;
}
private void populateChanges(OutstandingEntryDao<OE> outstandingDao) { private void populateChanges(OutstandingEntryDao<OE> outstandingDao) {
TodorooCursor<OE> cursor = outstandingDao.query(Query.select(getModelProperties(outstandingClass)) TodorooCursor<OE> cursor = outstandingDao.query(Query.select(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)));
@ -67,34 +101,10 @@ public class ChangesHappened<TYPE extends RemoteModel, OE extends OutstandingEnt
} }
private Class<OE> getOutstandingClass(Class<? extends RemoteModel> model) { private Class<OE> getOutstandingClass(Class<? extends RemoteModel> model) {
try { return DaoReflectionHelpers.getStaticFieldByReflection(model, Class.class, "OUTSTANDING_MODEL");
return (Class<OE>) getStaticFieldByReflection(model, "OUTSTANDING_MODEL");
} catch (ClassCastException e) {
throw new RuntimeException("OUTSTANDING_MODEL field for class " + model.getName() + " is not of the correct type");
}
} }
private Property<?>[] getModelProperties(Class<? extends AbstractModel> model) { private Property<?>[] getModelProperties(Class<? extends AbstractModel> model) {
try { return DaoReflectionHelpers.getStaticFieldByReflection(model, Property[].class, "PROPERTIES");
return (Property<?>[]) getStaticFieldByReflection(model, "PROPERTIES");
} catch (ClassCastException e) {
throw new RuntimeException("PROPERTIES field for class " + model.getName() + " is not of the correct type");
}
} }
private Object getStaticFieldByReflection(Class<?> cls, String fieldName) {
try {
Field field = cls.getField(fieldName);
Object obj = field.get(null);
if (obj == null) {
throw new RuntimeException(fieldName + " field for class " + cls.getName() + " is null");
}
return obj;
} catch (NoSuchFieldException e) {
throw new RuntimeException("Class " + cls.getName() + " does not declare field " + fieldName);
} catch (IllegalAccessException e2) {
throw new RuntimeException(fieldName + " field for class " + cls.getName() + " is not accessible");
}
}
} }

@ -1,18 +1,30 @@
package com.todoroo.astrid.actfm.sync.messages; package com.todoroo.astrid.actfm.sync.messages;
import org.json.JSONObject;
import com.todoroo.andlib.data.AbstractModel; import com.todoroo.andlib.data.AbstractModel;
import com.todoroo.andlib.data.Table;
import com.todoroo.astrid.dao.DaoReflectionHelpers;
import com.todoroo.astrid.dao.RemoteModelDao; import com.todoroo.astrid.dao.RemoteModelDao;
import com.todoroo.astrid.data.RemoteModel; import com.todoroo.astrid.data.RemoteModel;
@SuppressWarnings("nls")
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 long id; protected final long id;
protected final String uuid; protected final String uuid;
protected final long pushedAt; protected final long pushedAt;
public static final String TYPE_KEY = "type";
public static final String TABLE_KEY = "table";
public static final String UUID_KEY = "uuid";
public static final String PUSHED_AT_KEY = "pushed_at";
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");
this.uuid = uuid; this.uuid = uuid;
this.pushedAt = pushedAt; this.pushedAt = pushedAt;
this.id = AbstractModel.NO_ID; this.id = AbstractModel.NO_ID;
@ -21,6 +33,7 @@ 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");
TYPE entity = getEntity(id, modelDao); TYPE entity = getEntity(id, modelDao);
if (entity == null) { if (entity == null) {
@ -44,6 +57,6 @@ public abstract class ClientToServerMessage<TYPE extends RemoteModel> {
return pushedAt; return pushedAt;
} }
public abstract void sendMessage(); public abstract JSONObject serializeToJSON();
} }

@ -0,0 +1,5 @@
package com.todoroo.astrid.actfm.sync.messages;
public class NameMaps {
}

@ -1,5 +1,7 @@
package com.todoroo.astrid.actfm.sync.messages; package com.todoroo.astrid.actfm.sync.messages;
import org.json.JSONObject;
import com.todoroo.astrid.dao.RemoteModelDao; import com.todoroo.astrid.dao.RemoteModelDao;
import com.todoroo.astrid.data.RemoteModel; import com.todoroo.astrid.data.RemoteModel;
@ -10,7 +12,7 @@ public class RequestDoubleCheck<TYPE extends RemoteModel> extends ClientToServer
} }
@Override @Override
public void sendMessage() { public JSONObject serializeToJSON() {
// Send message return null;
} }
} }

@ -0,0 +1,25 @@
package com.todoroo.astrid.dao;
import java.lang.reflect.Field;
@SuppressWarnings("nls")
public class DaoReflectionHelpers {
public static <T> T getStaticFieldByReflection(Class<?> cls, Class<T> cast, String fieldName) {
try {
Field field = cls.getField(fieldName);
Object obj = field.get(null);
if (obj == null) {
throw new RuntimeException(fieldName + " field for class " + cls.getName() + " is null");
}
return cast.cast(obj);
} catch (NoSuchFieldException e) {
throw new RuntimeException("Class " + cls.getName() + " does not declare field " + fieldName);
} catch (IllegalAccessException e2) {
throw new RuntimeException(fieldName + " field for class " + cls.getName() + " is not accessible");
} catch (ClassCastException e3) {
throw new RuntimeException(fieldName + " field for class " + cls.getName() + " cannot be cast to type " + cast.getName());
}
}
}

@ -16,17 +16,17 @@ import com.todoroo.astrid.data.Metadata;
import com.todoroo.astrid.data.RemoteModel; import com.todoroo.astrid.data.RemoteModel;
import com.todoroo.astrid.data.TagData; import com.todoroo.astrid.data.TagData;
import com.todoroo.astrid.data.Task; import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.tags.Astrid44SyncMigrator; import com.todoroo.astrid.tags.AstridNewSyncMigrator;
import com.todoroo.astrid.tags.TagMetadata; import com.todoroo.astrid.tags.TagMetadata;
public class Astrid44MigrationTest extends NewSyncTestCase { public class AstridNewSyncMigrationTest extends NewSyncTestCase {
@Autowired @Autowired
private MetadataDao metadataDao; private MetadataDao metadataDao;
public void testAstrid44Migration() { public void testAstrid44Migration() {
setupOldDatabase(); setupOldDatabase();
new Astrid44SyncMigrator().performMigration(); new AstridNewSyncMigrator().performMigration();
assertAllModelsHaveUUID(); assertAllModelsHaveUUID();
assertAllTagsHaveTagData(); assertAllTagsHaveTagData();
assertAllMetadataHasAllFields(); assertAllMetadataHasAllFields();
Loading…
Cancel
Save