Refactored ClientToServer message to be an abstract class holding information common to all ClientToServer messages

pull/14/head
Sam Bosley 12 years ago
parent 189acd1367
commit 42ef792707

@ -1,18 +1,15 @@
package com.todoroo.astrid.actfm.sync.messages;
import com.todoroo.astrid.dao.RemoteModelDao;
import com.todoroo.astrid.data.RemoteModel;
public class BriefMe<TYPE extends RemoteModel> implements ClientToServerMessage {
public class BriefMe<TYPE extends RemoteModel> extends ClientToServerMessage<TYPE> {
private final Class<? extends RemoteModel> modelClass;
private final String uuid;
private long pushedAt; // TODO: Populate and use
public BriefMe(TYPE entity) {
this.modelClass = entity.getClass();
this.uuid = entity.getValue(RemoteModel.UUID_PROPERTY);
public BriefMe(long id, Class<TYPE> modelClass, RemoteModelDao<TYPE> modelDao) {
super(id, modelClass, modelDao);
}
@Override
public void sendMessage() {
// Send message
}

@ -17,33 +17,23 @@ import com.todoroo.astrid.data.OutstandingEntry;
import com.todoroo.astrid.data.RemoteModel;
@SuppressWarnings("nls")
public class ChangesHappened<TYPE extends RemoteModel, OE extends OutstandingEntry<TYPE>> implements ClientToServerMessage {
public class ChangesHappened<TYPE extends RemoteModel, OE extends OutstandingEntry<TYPE>> extends ClientToServerMessage<TYPE> {
private final Class<? extends RemoteModel> modelClass;
private final Class<OE> outstandingClass;
private final long id;
private final String uuid;
private final List<OE> changes;
private final long pushedAt;
public ChangesHappened(long id, Class<TYPE> modelClass, RemoteModelDao<TYPE> modelDao,
OutstandingEntryDao<OE> outstandingDao) {
this.modelClass = modelClass;
super(id, modelClass, modelDao);
this.outstandingClass = getOutstandingClass(modelClass);
this.id = id;
this.changes = new ArrayList<OE>();
TYPE entity = getEntity(id, modelDao);
if (entity == null) {
this.uuid = RemoteModel.NO_UUID;
this.pushedAt = 0;
} else {
this.uuid = entity.getValue(RemoteModel.UUID_PROPERTY);
this.pushedAt = entity.getValue(RemoteModel.PUSHED_AT_PROPERTY);
if (!RemoteModel.NO_UUID.equals(uuid))
populateChanges(outstandingDao);
}
}
@Override
public void sendMessage() {
// Process changes list and send to server
}
@ -56,18 +46,6 @@ public class ChangesHappened<TYPE extends RemoteModel, OE extends OutstandingEnt
return changes.size();
}
public String getUUID() {
return uuid;
}
public long getPushedAt() {
return pushedAt;
}
private TYPE getEntity(long localId, RemoteModelDao<TYPE> modelDao) {
return modelDao.fetch(localId, RemoteModel.UUID_PROPERTY, RemoteModel.PUSHED_AT_PROPERTY);
}
private void populateChanges(OutstandingEntryDao<OE> outstandingDao) {
TodorooCursor<OE> cursor = outstandingDao.query(Query.select(getModelProperties(outstandingClass))
.where(OutstandingEntry.ENTITY_ID_PROPERTY.eq(id)).orderBy(Order.asc(OutstandingEntry.CREATED_AT_PROPERTY)));

@ -1,7 +1,41 @@
package com.todoroo.astrid.actfm.sync.messages;
public interface ClientToServerMessage {
import com.todoroo.astrid.dao.RemoteModelDao;
import com.todoroo.astrid.data.RemoteModel;
public void sendMessage();
public abstract class ClientToServerMessage<TYPE extends RemoteModel> {
protected final Class<TYPE> modelClass;
protected final long id;
protected final String uuid;
protected final long pushedAt;
public ClientToServerMessage(long id, Class<TYPE> modelClass, RemoteModelDao<TYPE> modelDao) {
this.id = id;
this.modelClass = modelClass;
TYPE entity = getEntity(id, modelDao);
if (entity == null) {
this.uuid = RemoteModel.NO_UUID;
this.pushedAt = 0;
} else {
this.uuid = entity.getValue(RemoteModel.UUID_PROPERTY);
this.pushedAt = entity.getValue(RemoteModel.PUSHED_AT_PROPERTY);
}
}
private TYPE getEntity(long localId, RemoteModelDao<TYPE> modelDao) {
return modelDao.fetch(localId, RemoteModel.UUID_PROPERTY, RemoteModel.PUSHED_AT_PROPERTY);
}
public final String getUUID() {
return uuid;
}
public final long getPushedAt() {
return pushedAt;
}
public abstract void sendMessage();
}

@ -1,17 +1,15 @@
package com.todoroo.astrid.actfm.sync.messages;
import com.todoroo.astrid.dao.RemoteModelDao;
import com.todoroo.astrid.data.RemoteModel;
public class RequestDoubleCheck<TYPE extends RemoteModel> implements ClientToServerMessage {
public class RequestDoubleCheck<TYPE extends RemoteModel> extends ClientToServerMessage<TYPE> {
private final Class<? extends RemoteModel> modelClass;
private final String uuid;
public RequestDoubleCheck(TYPE entity) {
this.modelClass = entity.getClass();
this.uuid = entity.getValue(RemoteModel.UUID_PROPERTY);
public RequestDoubleCheck(long id, Class<TYPE> modelClass, RemoteModelDao<TYPE> modelDao) {
super(id, modelClass, modelDao);
}
@Override
public void sendMessage() {
// Send message
}

@ -3,31 +3,25 @@ package com.todoroo.astrid.sync;
import android.text.TextUtils;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.sql.Criterion;
import com.todoroo.andlib.sql.Query;
import com.todoroo.astrid.dao.TagDataDao;
import com.todoroo.astrid.dao.TagOutstandingDao;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.dao.TaskOutstandingDao;
import com.todoroo.astrid.data.RemoteModel;
import com.todoroo.astrid.data.TagData;
import com.todoroo.astrid.data.TagOutstanding;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.data.TaskOutstanding;
import com.todoroo.astrid.test.DatabaseTestCase;
public class SyncModelTest extends NewSyncTestCase {
public void testCreateTaskMakesUuid() {
Task task = createTask();
assertTrue(task.getValue(Task.REMOTE_ID) != 0);
assertFalse(RemoteModel.NO_UUID.equals(task.getValue(Task.UUID)));
assertFalse(TextUtils.isEmpty(task.getValue(Task.PROOF_TEXT)));
}
public void testCreateTagMakesUuid() {
TagData tag = createTagData();
assertTrue(tag.getValue(TagData.REMOTE_ID) != 0);
assertFalse(RemoteModel.NO_UUID.equals(tag.getValue(TagData.UUID)));
assertFalse(TextUtils.isEmpty(tag.getValue(TagData.PROOF_TEXT)));
}

Loading…
Cancel
Save