private static String[] dateFormatStrings = {"yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd", "HH:mm:ss"};
@Override
public ORMModelInstance update(Map<String, Object> data) {
ORMPersistenceManager orm = AdaptrexServices.getPersistenceManager(this.extConfig.getFactoryName());
EntityManager em = (EntityManager) orm.getSession();
EntityTransaction tx = em.getTransaction();
EntityManagerFactory emf = em.getEntityManagerFactory();
Cache emfCache = emf.getCache();
try {
tx.begin();
for (String key : data.keySet()) {
if (key.equals("id")) {
continue;
}
Object val = data.get(key);
/*
* Get our class
*/
Class<?> clazz = this.entity.getClass();
/*
* Get info about the field we're updating
*/
Field field = null;
try {
field = clazz.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")
&& orm.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 = orm.getEntity(field.getType(), val);
}
} catch (Exception e) {
log.debug("Couldn't set " + field.getName());
continue;
}
}
/*
* Check for 1:m or n:m
*/
if (key.endsWith("Ids")) {
key = StringUtilities.pluralize(key.substring(0, key.length() - 3));
if (orm.isManyToOne(clazz, key) || orm.isManyToMany(clazz, key)) {
Class<?> fieldType = null;
try {
evictAssociated(em, entity);
} catch (Exception e) {
log.info("Couldn't evict associated entity for " + entity.getClass().getName());
}
try {
field = entity.getClass().getDeclaredField(key);
fieldType = field.getType();
List<Object> assocObjList = new ArrayList<Object>();
Class<?> itemType = null;
Type type = field.getGenericType();
if (type instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) type;
itemType = (Class<?>) pt.getActualTypeArguments()[0];
}
@SuppressWarnings("unchecked")
List<Object> listItemIds = (List<Object>) StringUtilities.fromJson((String) val, List.class);
for (Object listItemId : listItemIds) {
assocObjList.add(orm.getEntity(itemType, listItemId));
}
if (fieldType.getSimpleName().equals("Set")) {
val = new HashSet<Object>(assocObjList);
} else {