Implement generic version of ChangesHappened message, as well as stubs for other ClientToServer messages

pull/14/head
Sam Bosley 12 years ago
parent 826064bfc3
commit 5998e066c1

@ -33,7 +33,7 @@ public final class TagData extends RemoteModel {
public static final Table TABLE = new Table("tagdata", TagData.class);
/** model class for entries in the outstanding table */
public static final Class<? extends AbstractModel> OUTSTANDING_MODEL = TagOutstanding.class;
public static final Class<? extends OutstandingEntry<TagData>> OUTSTANDING_MODEL = TagOutstanding.class;
/** content uri for this model */
public static final Uri CONTENT_URI = Uri.parse("content://" + AstridApiConstants.PACKAGE + "/" +

@ -38,7 +38,7 @@ public final class Task extends RemoteModel {
public static final Table TABLE = new Table("tasks", Task.class);
/** model class for entries in the outstanding table */
public static final Class<? extends AbstractModel> OUTSTANDING_MODEL = TaskOutstanding.class;
public static final Class<? extends OutstandingEntry<Task>> OUTSTANDING_MODEL = TaskOutstanding.class;
/** content uri for this model */
public static final Uri CONTENT_URI = Uri.parse("content://" + AstridApiConstants.PACKAGE + "/" +

@ -1,5 +1,20 @@
package com.todoroo.astrid.actfm.sync.messages;
public class BriefMe implements ClientToServerMessage {
import com.todoroo.astrid.data.RemoteModel;
public class BriefMe<TYPE extends RemoteModel> implements ClientToServerMessage {
private final Class<? extends RemoteModel> modelClass;
private final long uuid;
private long pushedAt; // TODO: Populate and use
public BriefMe(TYPE entity) {
this.modelClass = entity.getClass();
this.uuid = entity.getValue(RemoteModel.REMOTE_ID_PROPERTY);
}
public void send() {
// Send message
}
}

@ -1,24 +1,109 @@
package com.todoroo.astrid.actfm.sync.messages;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import android.util.Log;
import com.todoroo.andlib.data.AbstractModel;
import com.todoroo.andlib.data.Property;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.sql.Order;
import com.todoroo.andlib.sql.Query;
import com.todoroo.astrid.dao.OutstandingEntryDao;
import com.todoroo.astrid.data.OutstandingEntry;
import com.todoroo.astrid.data.RemoteModel;
@SuppressWarnings("nls")
public class ChangesHappened<TYPE extends RemoteModel> implements ClientToServerMessage {
private final Class<? extends RemoteModel> modelClass;
private final Class<? extends OutstandingEntry<TYPE>> outstandingClass;
private final Property<?>[] outstandingProperties;
private final long id;
private final long uuid;
private final List<OutstandingEntry<TYPE>> changes;
private long pushedAt;
private final List<Change> changes;
private long pushedAt; // TODO: Populate and use
private final OutstandingEntryDao<OutstandingEntry<TYPE>> dao;
public ChangesHappened(TYPE entity) {
public ChangesHappened(TYPE entity, OutstandingEntryDao<OutstandingEntry<TYPE>> dao) {
this.modelClass = entity.getClass();
this.outstandingClass = getOutstandingClass(modelClass);
this.outstandingProperties = getModelProperties(outstandingClass);
this.id = entity.getId();
this.uuid = entity.getValue(RemoteModel.REMOTE_ID_PROPERTY);
this.changes = new ArrayList<OutstandingEntry<TYPE>>();
this.changes = new ArrayList<Change>();
this.dao = dao;
populateChanges();
}
public void send() {
// Process changes list and send to server
}
private class Change {
public final long uuid;
public final OutstandingEntry<TYPE> outstandingEntry;
public Change(long uuid, OutstandingEntry<TYPE> outstandingEntry) {
this.uuid = uuid;
this.outstandingEntry = outstandingEntry;
}
}
private void populateChanges() {
TodorooCursor<OutstandingEntry<TYPE>> cursor = dao.query(Query.select(outstandingProperties)
.where(OutstandingEntry.ENTITY_ID_PROPERTY.eq(id)).orderBy(Order.asc(OutstandingEntry.CREATED_AT_PROPERTY)));
try {
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
try {
OutstandingEntry<TYPE> instance = outstandingClass.newInstance();
instance.readPropertiesFromCursor(cursor);
changes.add(new Change(uuid, instance));
} catch (IllegalAccessException e) {
Log.e("ChangesHappened", "Error instantiating outstanding model class", e);
} catch (InstantiationException e2) {
Log.e("ChangesHappened", "Error instantiating outstanding model class", e2);
}
}
} finally {
cursor.close();
}
}
private Class<? extends OutstandingEntry<TYPE>> getOutstandingClass(Class<? extends RemoteModel> model) {
try {
Field outstandingField = model.getField("OUTSTANDING_MODEL");
Class<? extends OutstandingEntry<TYPE>> outstanding = (Class<? extends OutstandingEntry<TYPE>>) outstandingField.get(null);
if (outstanding == null) {
throw new RuntimeException("OUTSTANDING_MODEL field for class " + model.getName() + " is null");
}
return outstanding;
} catch (NoSuchFieldException e) {
throw new RuntimeException("Class " + model.getName() + " does not declare an OUTSTANDING_MODEL field");
} catch (IllegalAccessException e2) {
throw new RuntimeException("OUTSTANDING_MODEL field for class " + model.getName() + " is not accessible");
} catch (ClassCastException e3) {
throw new RuntimeException("OUTSTANDING_MODEL field for class " + model.getName() + " is not of the correct type");
}
}
private Property<?>[] getModelProperties(Class<? extends AbstractModel> model) {
try {
Field propertiesField = model.getField("PROPERTIES");
Property<?>[] properties = (Property<?>[]) propertiesField.get(null);
if (properties == null) {
throw new RuntimeException("PROPERTIES field for class " + model.getName() + " is null");
}
return properties;
} catch (NoSuchFieldException e) {
throw new RuntimeException("Class " + model.getName() + " does not declare an PROPERTIES field");
} catch (IllegalAccessException e2) {
throw new RuntimeException("PROPERTIES field for class " + model.getName() + " is not accessible");
} catch (ClassCastException e3) {
throw new RuntimeException("PROPERTIES field for class " + model.getName() + " is not of the correct type");
}
}
}

@ -2,4 +2,6 @@ package com.todoroo.astrid.actfm.sync.messages;
public interface ClientToServerMessage {
public void send();
}

@ -1,5 +1,18 @@
package com.todoroo.astrid.actfm.sync.messages;
public class RequestDoubleCheck implements ClientToServerMessage {
import com.todoroo.astrid.data.RemoteModel;
public class RequestDoubleCheck<TYPE extends RemoteModel> implements ClientToServerMessage {
private final Class<? extends RemoteModel> modelClass;
private final long uuid;
public RequestDoubleCheck(TYPE entity) {
this.modelClass = entity.getClass();
this.uuid = entity.getValue(RemoteModel.REMOTE_ID_PROPERTY);
}
public void send() {
// Send message
}
}

Loading…
Cancel
Save