private Object buildRelatedInstances( MotechDataService service, Class<?> parameterType, Object fieldValue) throws IllegalAccessException, InstantiationException { Object parsedValue = null; if (Collection.class.isAssignableFrom(parameterType)) { if (Set.class.isAssignableFrom(parameterType)) { parsedValue = new HashSet(); } else if (List.class.isAssignableFrom(parameterType)) { parsedValue = new ArrayList(); } else { parsedValue = new ArrayList(); } for (Object object : (Collection) fieldValue) { if (isFromUI(object)) { ((Collection) parsedValue) .add(findRelatedObjectById(((Map) object).get(ID_FIELD_NAME), service)); } else if (isHistoricalObject(object)) { String currentVersion = HistoryTrashClassHelper.currentVersion(object.getClass()); ((Collection) parsedValue) .add( findRelatedObjectById( PropertyUtil.safeGetProperty(object, currentVersion), service)); } } } else { if (isFromUI(fieldValue)) { parsedValue = findRelatedObjectById(((Map) fieldValue).get(ID_FIELD_NAME), service); } else if (isHistoricalObject(fieldValue)) { String currentVersion = HistoryTrashClassHelper.currentVersion(fieldValue.getClass()); parsedValue = findRelatedObjectById( PropertyUtil.safeGetProperty(fieldValue, currentVersion), service); } } return parsedValue; }
private HistoryRecord convertToHistoryRecord( Object object, EntityDto entity, Long instanceId, MotechDataService service) { Long entityId = entity.getId(); EntityRecord entityRecord = instanceToRecord(object, entity, entityService.getEntityFields(entityId), service); Long historyInstanceSchemaVersion = (Long) PropertyUtil.safeGetProperty( object, HistoryTrashClassHelper.schemaVersion(object.getClass())); Long currentSchemaVersion = entityService.getCurrentSchemaVersion(entity.getClassName()); return new HistoryRecord( entityRecord.getId(), instanceId, historyInstanceSchemaVersion.equals(currentSchemaVersion), entityRecord.getFields()); }