Fixes to MakeChanges

pull/14/head
Sam Bosley 12 years ago
parent 3d25aca158
commit 01b3035cf2

@ -145,6 +145,7 @@ public class ActFmSyncThread {
}
if (messageBatch.isEmpty() && timeForBackgroundSync()) {
Flags.checkAndClear(Flags.BG_SYNC);
messageBatch.add(BriefMe.instantiateBriefMeForClass(Task.class, NameMaps.PUSHED_AT_TASKS));
messageBatch.add(BriefMe.instantiateBriefMeForClass(TagData.class, NameMaps.PUSHED_AT_TAGS));
}
@ -207,7 +208,7 @@ public class ActFmSyncThread {
}
private boolean timeForBackgroundSync() {
return Flags.checkAndClear(Flags.BG_SYNC);
return Flags.check(Flags.BG_SYNC);
}
private boolean checkForToken() {

@ -1,6 +1,7 @@
package com.todoroo.astrid.actfm.sync.messages;
import org.json.JSONArray;
import java.util.Iterator;
import org.json.JSONException;
import org.json.JSONObject;
@ -35,29 +36,25 @@ public class MakeChanges<TYPE extends RemoteModel> extends ServerToClientMessage
@Override
public void processMessage() {
JSONArray changes = json.optJSONArray("changes");
JSONObject changes = json.optJSONObject("changes");
String uuid = json.optString("uuid");
if (changes != null && !TextUtils.isEmpty(uuid)) {
if (dao != null) { // Model case
if (dao != null) {
try {
TYPE model = dao.getModelClass().newInstance();
if (changes.length() > 0) {
JSONChangeToPropertyVisitor visitor = new JSONChangeToPropertyVisitor(model);
for (int i = 0; i < changes.length(); i++) {
JSONArray change = changes.optJSONArray(i);
if (change != null) {
String column = change.optString(0);
Property<?> property = NameMaps.serverColumnNameToLocalProperty(table, column);
if (property != null) { // Unsupported property
property.accept(visitor, change);
}
}
JSONChangeToPropertyVisitor visitor = new JSONChangeToPropertyVisitor(model, changes);
Iterator<String> keys = changes.keys();
while (keys.hasNext()) {
String column = keys.next();
Property<?> property = NameMaps.serverColumnNameToLocalProperty(table, column);
if (property != null) { // Unsupported property
property.accept(visitor, column);
}
}
if (model.getSetValues().size() > 0) {
if (dao.update(RemoteModel.UUID_PROPERTY.eq(uuid), model) <= 0) { // If update doesn't update rows, create a new model
model.putTransitory(SyncFlags.ACTFM_SUPPRESS_OUTSTANDING_ENTRIES, true);
if (dao.update(RemoteModel.UUID_PROPERTY.eq(uuid), model) <= 0) { // If update doesn't update rows. create a new model
model.putTransitory(SyncFlags.ACTFM_SUPPRESS_OUTSTANDING_ENTRIES, true);
dao.createNew(model);
}
@ -68,10 +65,9 @@ public class MakeChanges<TYPE extends RemoteModel> extends ServerToClientMessage
} catch (InstantiationException e) {
Log.e(ERROR_TAG, "Error instantiating model for MakeChanges", e);
}
} else if (NameMaps.TABLE_ID_PUSHED_AT.equals(table)) { // pushed_at case
JSONArray change = changes.optJSONArray(0);
if (change != null && change.optString(0).equals(NameMaps.TABLE_ID_PUSHED_AT)) {
long pushedAt = change.optLong(1);
} else if (NameMaps.TABLE_ID_PUSHED_AT.equals(table)) {
long pushedAt = changes.optLong(NameMaps.TABLE_ID_PUSHED_AT);
if (pushedAt > 0) {
String pushedAtKey = null;
if (NameMaps.TABLE_ID_TASKS.equals(uuid))
pushedAtKey = NameMaps.PUSHED_AT_TASKS;
@ -87,57 +83,65 @@ public class MakeChanges<TYPE extends RemoteModel> extends ServerToClientMessage
}
private static class JSONChangeToPropertyVisitor implements PropertyVisitor<Void, JSONArray> {
private static class JSONChangeToPropertyVisitor implements PropertyVisitor<Void, String> {
private final AbstractModel model;
private final JSONObject data;
public JSONChangeToPropertyVisitor(AbstractModel model) {
public JSONChangeToPropertyVisitor(AbstractModel model, JSONObject data) {
this.model = model;
this.data = data;
}
@Override
public Void visitInteger(Property<Integer> property, JSONArray data) {
public Void visitInteger(Property<Integer> property, String key) {
try {
int value = data.getInt(1);
int value = data.getInt(key);
model.setValue((IntegerProperty) property, value);
} catch (JSONException e) {
Log.e(ERROR_TAG, "Error reading int value from JSON " + data, e);
Log.e(ERROR_TAG, "Error reading int value with key " + key + " from JSON " + data, e);
}
return null;
}
@Override
public Void visitLong(Property<Long> property, JSONArray data) {
public Void visitLong(Property<Long> property, String key) {
try {
long value = data.getLong(1);
long value = data.getLong(key);
model.setValue((LongProperty) property, value);
} catch (JSONException e) {
Log.e(ERROR_TAG, "Error reading long value from JSON " + data, e);
Log.e(ERROR_TAG, "Error reading long value with key " + key + " from JSON " + data, e);
}
return null;
}
@Override
public Void visitDouble(Property<Double> property, JSONArray data) {
public Void visitDouble(Property<Double> property, String key) {
try {
double value = data.getDouble(1);
double value = data.getDouble(key);
model.setValue((DoubleProperty) property, value);
} catch (JSONException e) {
Log.e(ERROR_TAG, "Error reading double value from JSON " + data, e);
Log.e(ERROR_TAG, "Error reading double value with key " + key + " from JSON " + data, e);
}
return null;
}
@Override
public Void visitString(Property<String> property, JSONArray data) {
public Void visitString(Property<String> property, String key) {
try {
String value = data.getString(1);
String value = data.getString(key);
model.setValue((StringProperty) property, value);
} catch (JSONException e) {
Log.e(ERROR_TAG, "Error reading string value from JSON " + data, e);
try {
JSONObject object = data.getJSONObject(key);
if (object != null)
model.setValue((StringProperty) property, object.toString());
} catch (JSONException e2) {
Log.e(ERROR_TAG, "Error reading JSON value with key " + key + " from JSON " + data, e);
}
Log.e(ERROR_TAG, "Error reading string value with key " + key + " from JSON " + data, e);
}
return null;
}
}
}

@ -36,18 +36,11 @@ public class SyncMessageTest extends NewSyncTestCase {
JSONObject makeChanges = new JSONObject();
makeChanges.put("type", ServerToClientMessage.TYPE_MAKE_CHANGES);
makeChanges.put("table", NameMaps.TABLE_ID_TASKS);
JSONArray changes = new JSONArray();
JSONArray change1 = new JSONArray();
change1.put("title");
change1.put(MAKE_CHANGES_TITLE);
JSONObject changes = new JSONObject();
changes.put("title", MAKE_CHANGES_TITLE);
changes.put("importance", Task.IMPORTANCE_DO_OR_DIE);
JSONArray change2 = new JSONArray();
change2.put("importance");
change2.put(Task.IMPORTANCE_DO_OR_DIE);
changes.put(change1);
changes.put(change2);
makeChanges.put("changes", changes);
return makeChanges;
}
@ -58,11 +51,8 @@ public class SyncMessageTest extends NewSyncTestCase {
makeChanges.put("table", NameMaps.TABLE_ID_PUSHED_AT);
makeChanges.put("uuid", NameMaps.TABLE_ID_TASKS);
JSONArray changes = new JSONArray();
JSONArray change1 = new JSONArray();
change1.put("pushed_at");
change1.put(date);
changes.put(change1);
JSONObject changes = new JSONObject();
changes.put("pushed_at", date);
makeChanges.put("changes", changes);
return makeChanges;

Loading…
Cancel
Save