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) {
AdaptrexSession session = new AdaptrexSession();
val = session.getEntity(field.getType(), (Integer) val);
}
} catch (Exception e) {
log.warn("Error", e);
continue;
}
}
if (field == null) {
log.debug("Unable to serialize " + key);
continue;
}
String typeName = field.getType().getSimpleName().toLowerCase();
/*
* Handle Date Fields
*/
if (typeName.equals("date")) {
try {
val = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH).parse((String) val);
} catch (Exception e) {
continue;
}
}
try {
Method getter = clazz.getMethod("set" + StringUtilities.capitalize(key), field.getType());
getter.invoke(entity, val);
} catch (Exception e) {
log.warn("Error updating field: Couldn't invoke set" + StringUtilities.capitalize(key));
}
}
} catch (Exception e) {
log.warn("Error", e);
}
AdaptrexSession session = new AdaptrexSession();
Object e = session.saveEntity(entity);
session.close();
return e;
}