From 65f74e745674af4d868c04174554a97243bb9410 Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Thu, 15 Nov 2012 15:09:21 -0800 Subject: [PATCH] Implementation of MakeChanges --- .../actfm/sync/messages/MakeChanges.java | 90 ++++++++++++++++++- .../sync/messages/ServerToClientMessage.java | 16 +++- 2 files changed, 101 insertions(+), 5 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 7c3490766..6274a6eae 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 @@ -1,17 +1,99 @@ package com.todoroo.astrid.actfm.sync.messages; +import org.json.JSONArray; +import org.json.JSONException; import org.json.JSONObject; -public class MakeChanges extends ServerToClientMessage { +import android.text.TextUtils; +import android.util.Log; - public MakeChanges(JSONObject json) { +import com.todoroo.andlib.data.AbstractModel; +import com.todoroo.andlib.data.Property; +import com.todoroo.andlib.data.Property.DoubleProperty; +import com.todoroo.andlib.data.Property.IntegerProperty; +import com.todoroo.andlib.data.Property.LongProperty; +import com.todoroo.andlib.data.Property.StringProperty; +import com.todoroo.astrid.dao.RemoteModelDao; +import com.todoroo.astrid.data.RemoteModel; + +@SuppressWarnings("nls") +public class MakeChanges extends ServerToClientMessage { + + private static final String ERROR_TAG = "actfm-make-changes"; + + private final RemoteModelDao dao; + private final String table; + + public MakeChanges(JSONObject json, RemoteModelDao dao) { super(json); - throw new RuntimeException("No constructor for MakeChanges implemented"); //$NON-NLS-1$ + table = json.optString("table"); + this.dao = dao; } @Override public void processMessage() { - // TODO Auto-generated method stub + JSONObject changes = json.optJSONObject("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) { + for (int i = 0; i < keys.length(); i++) { + String col = keys.optString(i); + if (!TextUtils.isEmpty(col)) { + Property property = NameMaps.serverColumnNameToLocalProperty(table, col); + if (property != null) { // Unsupported property + setPropertyFromJSON(model, property, changes, col); + } + } + } + } + + 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 + dao.createNew(model); + } + } + + } catch (IllegalAccessException e) { + Log.e(ERROR_TAG, "Error instantiating model for MakeChanges", e); + } catch (InstantiationException e) { + Log.e(ERROR_TAG, "Error instantiating model for MakeChanges", e); + } + } + } + + private static void setPropertyFromJSON(AbstractModel model, Property property, JSONObject json, String jsonKey) { + if (property instanceof LongProperty) { + try { + long value = json.getLong(jsonKey); + model.setValue((LongProperty) property, value); + } catch (JSONException e) { + Log.e(ERROR_TAG, "Error reading long from JSON " + json + " with key " + jsonKey, e); + } + } else if (property instanceof StringProperty) { + try { + String value = json.getString(jsonKey); + model.setValue((StringProperty) property, value); + } catch (JSONException e) { + Log.e(ERROR_TAG, "Error reading string from JSON " + json + " with key " + jsonKey, e); + } + } else if (property instanceof IntegerProperty) { + try { + int value = json.getInt(jsonKey); + model.setValue((IntegerProperty) property, value); + } catch (JSONException e) { + Log.e(ERROR_TAG, "Error reading int from JSON " + json + " with key " + jsonKey, e); + } + } else if (property instanceof DoubleProperty) { + try { + double value = json.getDouble(jsonKey); + model.setValue((DoubleProperty) property, value); + } catch (JSONException e) { + Log.e(ERROR_TAG, "Error reading double from JSON " + json + " with key " + jsonKey, e); + } + } } } diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ServerToClientMessage.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ServerToClientMessage.java index 47f21ab48..b2133df86 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ServerToClientMessage.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ServerToClientMessage.java @@ -2,6 +2,10 @@ package com.todoroo.astrid.actfm.sync.messages; import org.json.JSONObject; +import com.todoroo.astrid.core.PluginServices; +import com.todoroo.astrid.data.TagData; +import com.todoroo.astrid.data.Task; + @SuppressWarnings("nls") public abstract class ServerToClientMessage { @@ -21,7 +25,7 @@ public abstract class ServerToClientMessage { public static ServerToClientMessage instantiateMessage(JSONObject json) { String type = json.optString("type"); if (TYPE_MAKE_CHANGES.equals(type)) - return new MakeChanges(json); + return instantiateMakeChanges(json); else if (TYPE_ACKNOWLEDGE_CHANGE.equals(type)) return new AcknowledgeChange(json); else if (TYPE_DOUBLE_CHECK.equals(json)) @@ -32,4 +36,14 @@ public abstract class ServerToClientMessage { return null; } + private static MakeChanges instantiateMakeChanges(JSONObject json) { + String table = json.optString("table"); + if (NameMaps.SERVER_TABLE_TASKS.equals(table)) + return new MakeChanges(json, PluginServices.getTaskDao()); + else if (NameMaps.SERVER_TABLE_TAGS.equals(table)) + return new MakeChanges(json, PluginServices.getTagDataDao()); + else + return null; + } + }