From 8679052120f432419e2dbb1c585f9ec6d1561e8c Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Fri, 16 Nov 2012 16:17:26 -0800 Subject: [PATCH] Updated MakeChanges to the new spec --- .../actfm/sync/messages/MakeChanges.java | 49 +++++++++---------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/MakeChanges.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/MakeChanges.java index 0faa9a615..c6b63889c 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/MakeChanges.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/MakeChanges.java @@ -34,20 +34,21 @@ public class MakeChanges extends ServerToClientMessage @Override public void processMessage() { - JSONObject changes = json.optJSONObject("changes"); + JSONArray changes = json.optJSONArray("changes"); String uuid = json.optString("uuid"); if (dao != null && changes != null && !TextUtils.isEmpty(uuid)) { try { TYPE model = dao.getModelClass().newInstance(); - JSONArray keys = changes.names(); - if (keys != null) { - JSONToPropertyVisitor visitor = new JSONToPropertyVisitor(changes, model); - for (int i = 0; i < keys.length(); i++) { - String col = keys.optString(i); - if (!TextUtils.isEmpty(col)) { - Property property = NameMaps.serverColumnNameToLocalProperty(table, col); + 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, col); + property.accept(visitor, change); } } } @@ -69,56 +70,54 @@ public class MakeChanges extends ServerToClientMessage } - private static class JSONToPropertyVisitor implements PropertyVisitor { + private static class JSONChangeToPropertyVisitor implements PropertyVisitor { - private final JSONObject json; private final AbstractModel model; - public JSONToPropertyVisitor(JSONObject json, AbstractModel model) { - this.json = json; + public JSONChangeToPropertyVisitor(AbstractModel model) { this.model = model; } @Override - public Void visitInteger(Property property, String data) { + public Void visitInteger(Property property, JSONArray data) { try { - int value = json.getInt(data); + int value = data.getInt(1); model.setValue((IntegerProperty) property, value); } catch (JSONException e) { - Log.e(ERROR_TAG, "Error reading int from JSON " + json + " with key " + data, e); + Log.e(ERROR_TAG, "Error reading int value from JSON " + data, e); } return null; } @Override - public Void visitLong(Property property, String data) { + public Void visitLong(Property property, JSONArray data) { try { - long value = json.getLong(data); + long value = data.getLong(1); model.setValue((LongProperty) property, value); } catch (JSONException e) { - Log.e(ERROR_TAG, "Error reading long from JSON " + json + " with key " + data, e); + Log.e(ERROR_TAG, "Error reading long value from JSON " + data, e); } return null; } @Override - public Void visitDouble(Property property, String data) { + public Void visitDouble(Property property, JSONArray data) { try { - double value = json.getDouble(data); + double value = data.getDouble(1); model.setValue((DoubleProperty) property, value); } catch (JSONException e) { - Log.e(ERROR_TAG, "Error reading double from JSON " + json + " with key " + data, e); + Log.e(ERROR_TAG, "Error reading double value from JSON " + data, e); } return null; } @Override - public Void visitString(Property property, String data) { + public Void visitString(Property property, JSONArray data) { try { - String value = json.getString(data); + String value = data.getString(1); model.setValue((StringProperty) property, value); } catch (JSONException e) { - Log.e(ERROR_TAG, "Error reading string from JSON " + json + " with key " + data, e); + Log.e(ERROR_TAG, "Error reading string value from JSON " + data, e); } return null; }