diff --git a/api/src/com/todoroo/astrid/data/TagData.java b/api/src/com/todoroo/astrid/data/TagData.java index 901bdc7b3..d0a938209 100644 --- a/api/src/com/todoroo/astrid/data/TagData.java +++ b/api/src/com/todoroo/astrid/data/TagData.java @@ -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 OUTSTANDING_MODEL = TagOutstanding.class; + public static final Class> OUTSTANDING_MODEL = TagOutstanding.class; /** content uri for this model */ public static final Uri CONTENT_URI = Uri.parse("content://" + AstridApiConstants.PACKAGE + "/" + diff --git a/api/src/com/todoroo/astrid/data/Task.java b/api/src/com/todoroo/astrid/data/Task.java index e08072a23..8358e3d3c 100644 --- a/api/src/com/todoroo/astrid/data/Task.java +++ b/api/src/com/todoroo/astrid/data/Task.java @@ -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 OUTSTANDING_MODEL = TaskOutstanding.class; + public static final Class> OUTSTANDING_MODEL = TaskOutstanding.class; /** content uri for this model */ public static final Uri CONTENT_URI = Uri.parse("content://" + AstridApiConstants.PACKAGE + "/" + diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/BriefMe.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/BriefMe.java index adaea272d..f1de7608c 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/BriefMe.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/BriefMe.java @@ -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 implements ClientToServerMessage { + + private final Class 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 + } } diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ChangesHappened.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ChangesHappened.java index b16dbf56c..57e5a0ae7 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ChangesHappened.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ChangesHappened.java @@ -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 implements ClientToServerMessage { private final Class modelClass; + private final Class> outstandingClass; + private final Property[] outstandingProperties; private final long id; private final long uuid; - private final List> changes; - private long pushedAt; + private final List changes; + private long pushedAt; // TODO: Populate and use + private final OutstandingEntryDao> dao; - public ChangesHappened(TYPE entity) { + public ChangesHappened(TYPE entity, OutstandingEntryDao> 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>(); + this.changes = new ArrayList(); + this.dao = dao; + populateChanges(); + } + + public void send() { + // Process changes list and send to server + } + + private class Change { + public final long uuid; + public final OutstandingEntry outstandingEntry; + + public Change(long uuid, OutstandingEntry outstandingEntry) { + this.uuid = uuid; + this.outstandingEntry = outstandingEntry; + } + } + + private void populateChanges() { + TodorooCursor> 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 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> getOutstandingClass(Class model) { + try { + Field outstandingField = model.getField("OUTSTANDING_MODEL"); + Class> outstanding = (Class>) 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 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"); + } } } diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ClientToServerMessage.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ClientToServerMessage.java index 4db6263ed..bf972dca8 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ClientToServerMessage.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ClientToServerMessage.java @@ -2,4 +2,6 @@ package com.todoroo.astrid.actfm.sync.messages; public interface ClientToServerMessage { + public void send(); + } diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/RequestDoubleCheck.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/RequestDoubleCheck.java index 6cf82d5a1..d95d0d673 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/RequestDoubleCheck.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/RequestDoubleCheck.java @@ -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 implements ClientToServerMessage { + + private final Class 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 + } }