/*
* Update Entity
*/
private static Object updateEntity(AdaptrexSession session, Model model, ModelData modelData) {
AdaptrexPersistence persistence = session.getPersistence();
Object entity = model.getEntity();
try {
Class<?> clazz = entity.getClass();
Map<String, Object> data = modelData.getData();
for (String key : data.keySet()) {
if (key.equals("id")) {
continue;
}
Object val = data.get(key);
/*
* Get info about the field we're updating
*/
Field field = null;
try {
field = entity.getClass().getDeclaredField(key);
} catch (Exception e) {
}
/*
* If we don't have a field with the current name, check for a foreign entity
*/
if (key.endsWith("Id") && persistence.isManyToOne(clazz, key.substring(0, key.length() - 2))) {
key = key.substring(0, key.length() - 2);
try {
field = entity.getClass().getDeclaredField(key);
if (field != null && val != null) {
val = persistence.getEntity(session, field.getType(), (Integer) val);
}
} catch (Exception e) {
continue;
}
}
if (field == null) {
continue;
}
String typeName = field.getType().getSimpleName().toLowerCase();
/*
* Handle Date Fields
*/
if (typeName.equals("date")) {
for (String formatString : dateFormatStrings) {
try {
val = new SimpleDateFormat(formatString, Locale.ENGLISH).parse((String) val);
break;
} catch (Exception e) {}
}
}
try {
Method getter = clazz.getMethod("set" + StringUtilities.capitalize(key), field.getType());
getter.invoke(entity, val);
} catch (Exception e) {
}
}
} catch (Exception e) {
}
Object e = persistence.saveEntity(session, entity);
return e;
}