@Override public void revertInstanceFromTrash(Long entityId, Long instanceId) { EntityDto entity = getEntity(entityId); validateCredentials(entity); validateNonEditableProperty(entity); MotechDataService service = getServiceForEntity(entity); Object trash = service.findTrashInstanceById(instanceId, entityId); List<FieldRecord> fieldRecords = new LinkedList<>(); try { for (FieldDto field : entityService.getEntityFields(entity.getId())) { if (ID_FIELD_NAME.equalsIgnoreCase(field.getBasic().getDisplayName())) { continue; } Field f = FieldUtils.getField( trash.getClass(), StringUtils.uncapitalize(field.getBasic().getName()), true); FieldRecord record = new FieldRecord(field); record.setValue(f.get(trash)); fieldRecords.add(record); } Class<?> entityClass = getEntityClass(entity); Object newInstance = entityClass.newInstance(); updateFields(newInstance, fieldRecords, service, null); service.revertFromTrash(newInstance, trash); } catch (Exception e) { throw new RevertFromTrashException(entity.getName(), instanceId, e); } }
private void updateFields( Object instance, List<FieldRecord> fieldRecords, MotechDataService service, Long deleteValueFieldId, boolean retainId) throws NoSuchMethodException, ClassNotFoundException, CannotCompileException, InstantiationException, IllegalAccessException, NoSuchFieldException { for (FieldRecord fieldRecord : fieldRecords) { if (!(retainId && ID_FIELD_NAME.equals(fieldRecord.getName())) && !fieldRecord.getType().isRelationship()) { setProperty(instance, fieldRecord, service, deleteValueFieldId); } else if (fieldRecord.getType().isRelationship()) { setRelationProperty(instance, fieldRecord); } } }