Updated MakeChanges to the new spec

pull/14/head
Sam Bosley 13 years ago
parent d45e6c575e
commit 8679052120

@ -34,20 +34,21 @@ public class MakeChanges<TYPE extends RemoteModel> extends ServerToClientMessage
@Override @Override
public void processMessage() { public void processMessage() {
JSONObject changes = json.optJSONObject("changes"); JSONArray changes = json.optJSONArray("changes");
String uuid = json.optString("uuid"); String uuid = json.optString("uuid");
if (dao != null && changes != null && !TextUtils.isEmpty(uuid)) { if (dao != null && changes != null && !TextUtils.isEmpty(uuid)) {
try { try {
TYPE model = dao.getModelClass().newInstance(); TYPE model = dao.getModelClass().newInstance();
JSONArray keys = changes.names(); if (changes.length() > 0) {
if (keys != null) { JSONChangeToPropertyVisitor visitor = new JSONChangeToPropertyVisitor(model);
JSONToPropertyVisitor visitor = new JSONToPropertyVisitor(changes, model); for (int i = 0; i < changes.length(); i++) {
for (int i = 0; i < keys.length(); i++) { JSONArray change = changes.optJSONArray(i);
String col = keys.optString(i);
if (!TextUtils.isEmpty(col)) { if (change != null) {
Property<?> property = NameMaps.serverColumnNameToLocalProperty(table, col); String column = change.optString(0);
Property<?> property = NameMaps.serverColumnNameToLocalProperty(table, column);
if (property != null) { // Unsupported property if (property != null) { // Unsupported property
property.accept(visitor, col); property.accept(visitor, change);
} }
} }
} }
@ -69,56 +70,54 @@ public class MakeChanges<TYPE extends RemoteModel> extends ServerToClientMessage
} }
private static class JSONToPropertyVisitor implements PropertyVisitor<Void, String> { private static class JSONChangeToPropertyVisitor implements PropertyVisitor<Void, JSONArray> {
private final JSONObject json;
private final AbstractModel model; private final AbstractModel model;
public JSONToPropertyVisitor(JSONObject json, AbstractModel model) { public JSONChangeToPropertyVisitor(AbstractModel model) {
this.json = json;
this.model = model; this.model = model;
} }
@Override @Override
public Void visitInteger(Property<Integer> property, String data) { public Void visitInteger(Property<Integer> property, JSONArray data) {
try { try {
int value = json.getInt(data); int value = data.getInt(1);
model.setValue((IntegerProperty) property, value); model.setValue((IntegerProperty) property, value);
} catch (JSONException e) { } 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; return null;
} }
@Override @Override
public Void visitLong(Property<Long> property, String data) { public Void visitLong(Property<Long> property, JSONArray data) {
try { try {
long value = json.getLong(data); long value = data.getLong(1);
model.setValue((LongProperty) property, value); model.setValue((LongProperty) property, value);
} catch (JSONException e) { } 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; return null;
} }
@Override @Override
public Void visitDouble(Property<Double> property, String data) { public Void visitDouble(Property<Double> property, JSONArray data) {
try { try {
double value = json.getDouble(data); double value = data.getDouble(1);
model.setValue((DoubleProperty) property, value); model.setValue((DoubleProperty) property, value);
} catch (JSONException e) { } 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; return null;
} }
@Override @Override
public Void visitString(Property<String> property, String data) { public Void visitString(Property<String> property, JSONArray data) {
try { try {
String value = json.getString(data); String value = data.getString(1);
model.setValue((StringProperty) property, value); model.setValue((StringProperty) property, value);
} catch (JSONException e) { } 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; return null;
} }

Loading…
Cancel
Save