First attempt at making tag_removed work

pull/14/head
Sam Bosley 13 years ago
parent 3b8c27af31
commit 3b3e490823

@ -15,6 +15,7 @@ import android.util.Log;
import com.todoroo.andlib.service.Autowired; import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.service.ContextManager; import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.service.DependencyInjectionService; import com.todoroo.andlib.service.DependencyInjectionService;
import com.todoroo.andlib.utility.AndroidUtilities;
import com.todoroo.andlib.utility.DateUtilities; import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.astrid.actfm.sync.messages.BriefMe; import com.todoroo.astrid.actfm.sync.messages.BriefMe;
import com.todoroo.astrid.actfm.sync.messages.ClientToServerMessage; import com.todoroo.astrid.actfm.sync.messages.ClientToServerMessage;
@ -132,6 +133,7 @@ public class ActFmSyncThread {
while ((pendingMessages.isEmpty() && !timeForBackgroundSync()) || !actFmPreferenceService.isLoggedIn()) { while ((pendingMessages.isEmpty() && !timeForBackgroundSync()) || !actFmPreferenceService.isLoggedIn()) {
try { try {
monitor.wait(); monitor.wait();
AndroidUtilities.sleepDeep(500L); // Wait briefly for large database operations to finish (e.g. adding a task with several tags may trigger a message before all saves are done--fix this?)
} catch (InterruptedException e) { } catch (InterruptedException e) {
// Ignored // Ignored
} }
@ -188,7 +190,9 @@ public class ActFmSyncThread {
} }
} }
} }
// replayOutstandingChanges(); JSONArray errors = response.optJSONArray("errors");
boolean errorsExist = (errors != null && errors.length() > 0);
replayOutstandingChanges(errorsExist);
} }
batchSize = Math.min(batchSize, messageBatch.size()) * 2; batchSize = Math.min(batchSize, messageBatch.size()) * 2;
@ -212,9 +216,9 @@ public class ActFmSyncThread {
// Reapplies changes still in the outstanding tables to the local database // Reapplies changes still in the outstanding tables to the local database
// Called after a batch has finished processing // Called after a batch has finished processing
private void replayOutstandingChanges() { private void replayOutstandingChanges(boolean afterErrors) {
new ReplayOutstandingEntries<Task, TaskOutstanding>(Task.class, NameMaps.TABLE_ID_TASKS, taskDao, taskOutstandingDao).execute(); new ReplayOutstandingEntries<Task, TaskOutstanding>(Task.class, NameMaps.TABLE_ID_TASKS, taskDao, taskOutstandingDao, afterErrors).execute();
new ReplayOutstandingEntries<TagData, TagOutstanding>(TagData.class, NameMaps.TABLE_ID_TAGS, tagDataDao, tagOutstandingDao).execute(); new ReplayOutstandingEntries<TagData, TagOutstanding>(TagData.class, NameMaps.TABLE_ID_TAGS, tagDataDao, tagOutstandingDao, afterErrors).execute();
} }
private boolean timeForBackgroundSync() { private boolean timeForBackgroundSync() {

@ -160,15 +160,25 @@ public class MakeChanges<TYPE extends RemoteModel> extends ServerToClientMessage
super(model, changes, uuid); super(model, changes, uuid);
} }
@SuppressWarnings("null")
@Override @Override
public void performChanges() { public void performChanges() {
JSONArray addTags = changes.optJSONArray("tag_added"); JSONArray addTags = changes.optJSONArray("tag_added");
if (addTags != null && addTags.length() > 0) { JSONArray removeTags = changes.optJSONArray("tag_removed");
boolean tagsAdded = (addTags != null && addTags.length() > 0);
boolean tagsRemoved = (removeTags != null && removeTags.length() > 0);
if (!tagsAdded && !tagsRemoved)
return;
long localId;
if (!model.isSaved()) { // We don't have the local task id if (!model.isSaved()) { // We don't have the local task id
long localId = dao.localIdFromUuid(uuid); localId = dao.localIdFromUuid(uuid);
model.setId(localId); model.setId(localId);
} else {
localId = model.getId();
} }
if (tagsAdded) {
if (model.isSaved()) { if (model.isSaved()) {
TagService tagService = TagService.getInstance(); TagService tagService = TagService.getInstance();
for (int i = 0; i < addTags.length(); i++) { for (int i = 0; i < addTags.length(); i++) {
@ -182,8 +192,7 @@ public class MakeChanges<TYPE extends RemoteModel> extends ServerToClientMessage
} }
} }
JSONArray removeTags = changes.optJSONArray("tag_removed"); if (tagsRemoved) {
if (removeTags != null) {
ArrayList<String> toRemove = new ArrayList<String>(removeTags.length()); ArrayList<String> toRemove = new ArrayList<String>(removeTags.length());
for (int i = 0; i < removeTags.length(); i++) { for (int i = 0; i < removeTags.length(); i++) {
try { try {
@ -193,7 +202,7 @@ public class MakeChanges<TYPE extends RemoteModel> extends ServerToClientMessage
// //
} }
} }
TagService.getInstance().deleteLinks(uuid, toRemove.toArray(new String[toRemove.size()])); TagService.getInstance().deleteLinks(localId, uuid, toRemove.toArray(new String[toRemove.size()]));
} }
} }

@ -24,13 +24,15 @@ public class ReplayOutstandingEntries<T extends RemoteModel, OE extends Outstand
private final String table; private final String table;
private final RemoteModelDao<T> dao; private final RemoteModelDao<T> dao;
private final OutstandingEntryDao<OE> outstandingDao; private final OutstandingEntryDao<OE> outstandingDao;
private final boolean afterErrors;
public ReplayOutstandingEntries(Class<T> modelClass, String table, RemoteModelDao<T> dao, OutstandingEntryDao<OE> outstandingDao) { public ReplayOutstandingEntries(Class<T> modelClass, String table, RemoteModelDao<T> dao, OutstandingEntryDao<OE> outstandingDao, boolean afterErrors) {
this.modelClass = modelClass; this.modelClass = modelClass;
this.outstandingClass = DaoReflectionHelpers.getOutstandingClass(modelClass); this.outstandingClass = DaoReflectionHelpers.getOutstandingClass(modelClass);
this.table = table; this.table = table;
this.dao = dao; this.dao = dao;
this.outstandingDao = outstandingDao; this.outstandingDao = outstandingDao;
this.afterErrors = afterErrors;
} }
public void execute() { public void execute() {
@ -65,13 +67,12 @@ public class ReplayOutstandingEntries<T extends RemoteModel, OE extends Outstand
String column = instance.getValue(OutstandingEntry.COLUMN_STRING_PROPERTY); String column = instance.getValue(OutstandingEntry.COLUMN_STRING_PROPERTY);
Property<?> property = NameMaps.localColumnNameToProperty(table, column); Property<?> property = NameMaps.localColumnNameToProperty(table, column);
if (property == null)
throw new RuntimeException("No local property found for local column " + column + " in table " + table);
// set values to model // set values to model
if (property != null)
property.accept(visitor, instance); property.accept(visitor, instance);
} }
if (afterErrors)
model.putTransitory(SyncFlags.ACTFM_SUPPRESS_OUTSTANDING_ENTRIES, true); model.putTransitory(SyncFlags.ACTFM_SUPPRESS_OUTSTANDING_ENTRIES, true);
dao.saveExisting(model); dao.saveExisting(model);

@ -300,8 +300,9 @@ public final class TagService {
* @param taskUuid * @param taskUuid
* @param tagUuid * @param tagUuid
*/ */
public void deleteLink(String taskUuid, String tagUuid) { public void deleteLink(long taskId, String taskUuid, String tagUuid) {
Metadata deleteTemplate = new Metadata(); Metadata deleteTemplate = new Metadata();
deleteTemplate.setValue(Metadata.TASK, taskId); // Need this for recording changes in outstanding table
deleteTemplate.setValue(Metadata.DELETION_DATE, DateUtilities.now()); deleteTemplate.setValue(Metadata.DELETION_DATE, DateUtilities.now());
metadataDao.update(Criterion.and(MetadataCriteria.withKey(TagMetadata.KEY), Metadata.DELETION_DATE.eq(0), metadataDao.update(Criterion.and(MetadataCriteria.withKey(TagMetadata.KEY), Metadata.DELETION_DATE.eq(0),
TagMetadata.TASK_UUID.eq(taskUuid), TagMetadata.TAG_UUID.eq(tagUuid)), deleteTemplate); TagMetadata.TASK_UUID.eq(taskUuid), TagMetadata.TAG_UUID.eq(tagUuid)), deleteTemplate);
@ -312,8 +313,9 @@ public final class TagService {
* @param taskUuid * @param taskUuid
* @param tagUuids * @param tagUuids
*/ */
public void deleteLinks(String taskUuid, String[] tagUuids) { public void deleteLinks(long taskId, String taskUuid, String[] tagUuids) {
Metadata deleteTemplate = new Metadata(); Metadata deleteTemplate = new Metadata();
deleteTemplate.setValue(Metadata.TASK, taskId); // Need this for recording changes in outstanding table
deleteTemplate.setValue(Metadata.DELETION_DATE, DateUtilities.now()); deleteTemplate.setValue(Metadata.DELETION_DATE, DateUtilities.now());
if (tagUuids != null) { if (tagUuids != null) {
// TODO: We have this as a loop until I can figure out how to make update with multiple rows record outstanding // TODO: We have this as a loop until I can figure out how to make update with multiple rows record outstanding
@ -513,7 +515,7 @@ public final class TagService {
// Mark as deleted links that don't exist anymore // Mark as deleted links that don't exist anymore
Metadata deletedLinkTemplate = new Metadata(); Metadata deletedLinkTemplate = new Metadata();
deletedLinkTemplate.setValue(Metadata.DELETION_DATE, DateUtilities.now()); deletedLinkTemplate.setValue(Metadata.DELETION_DATE, DateUtilities.now());
deleteLinks(taskUuid, existingLinks.toArray(new String[existingLinks.size()])); deleteLinks(taskId, taskUuid, existingLinks.toArray(new String[existingLinks.size()]));
return true; return true;
} }

Loading…
Cancel
Save