|
|
|
@ -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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|