Fixed a bug when resolving uuid conflicts in make changes, refactored some JSON-to-model code into static helper methods for future use

pull/14/head
Sam Bosley 13 years ago
parent 2e8639d252
commit f6e33e38dd

@ -46,14 +46,8 @@ public class MakeChanges<TYPE extends RemoteModel> extends ServerToClientMessage
this.dao = dao; this.dao = dao;
} }
@Override public static <T extends RemoteModel> T changesToModel(RemoteModelDao<T> dao, JSONObject changes, String table) throws IllegalAccessException, InstantiationException {
public void processMessage() { T model = dao.getModelClass().newInstance();
JSONObject changes = json.optJSONObject("changes");
String uuid = json.optString("uuid");
if (changes != null && !TextUtils.isEmpty(uuid)) {
if (dao != null) {
try {
TYPE model = dao.getModelClass().newInstance();
JSONChangeToPropertyVisitor visitor = new JSONChangeToPropertyVisitor(model, changes); JSONChangeToPropertyVisitor visitor = new JSONChangeToPropertyVisitor(model, changes);
Iterator<String> keys = changes.keys(); Iterator<String> keys = changes.keys();
while (keys.hasNext()) { while (keys.hasNext()) {
@ -63,6 +57,33 @@ public class MakeChanges<TYPE extends RemoteModel> extends ServerToClientMessage
property.accept(visitor, column); property.accept(visitor, column);
} }
} }
return model;
}
public static <T extends RemoteModel> void saveOrUpdateModelAfterChanges(RemoteModelDao<T> dao, T model, String oldUuid, String uuid) {
Criterion uuidCriterion;
if (oldUuid == null)
uuidCriterion = RemoteModel.UUID_PROPERTY.eq(uuid);
else
uuidCriterion = RemoteModel.UUID_PROPERTY.eq(oldUuid);
if (model.getSetValues() != null && model.getSetValues().size() > 0) {
model.putTransitory(SyncFlags.ACTFM_SUPPRESS_OUTSTANDING_ENTRIES, true);
if (dao.update(uuidCriterion, model) <= 0) { // If update doesn't update rows. create a new model
model.putTransitory(SyncFlags.ACTFM_SUPPRESS_OUTSTANDING_ENTRIES, true);
dao.createNew(model);
}
}
}
@Override
public void processMessage() {
JSONObject changes = json.optJSONObject("changes");
String uuid = json.optString("uuid");
if (changes != null && !TextUtils.isEmpty(uuid)) {
if (dao != null) {
try {
TYPE model = changesToModel(dao, changes, table);
StringProperty uuidProperty = (StringProperty) NameMaps.serverColumnNameToLocalProperty(table, "uuid"); StringProperty uuidProperty = (StringProperty) NameMaps.serverColumnNameToLocalProperty(table, "uuid");
String oldUuid = null; // For indicating that a uuid collision has occurred String oldUuid = null; // For indicating that a uuid collision has occurred
@ -76,13 +97,7 @@ public class MakeChanges<TYPE extends RemoteModel> extends ServerToClientMessage
if (model.getSetValues() != null && !model.getSetValues().containsKey(uuidProperty.name)) if (model.getSetValues() != null && !model.getSetValues().containsKey(uuidProperty.name))
model.setValue(uuidProperty, uuid); model.setValue(uuidProperty, uuid);
if (model.getSetValues() != null && model.getSetValues().size() > 0) { saveOrUpdateModelAfterChanges(dao, model, oldUuid, uuid);
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);
}
}
afterSaveChanges(changes, model, uuid, oldUuid); afterSaveChanges(changes, model, uuid, oldUuid);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {

Loading…
Cancel
Save