use of org.motechproject.mds.exception.object.ObjectUpdateException in project motech by motech.
the class InstanceServiceImpl method saveInstance.
@Override
@Transactional
public Object saveInstance(EntityRecord entityRecord, Long deleteValueFieldId) {
EntityDto entity = getEntity(entityRecord.getEntitySchemaId());
validateCredentials(entity);
validateNonEditableProperty(entity);
List<FieldDto> entityFields = getEntityFields(entityRecord.getEntitySchemaId());
try {
MotechDataService service = getServiceForEntity(entity);
Class<?> entityClass = getEntityClass(entity);
boolean newObject = entityRecord.getId() == null;
Object instance;
if (newObject) {
instance = newInstanceFromEntityRecord(entityClass, entityFields, entityRecord.getFields(), service);
return service.create(instance);
} else {
instance = service.retrieve(ID_FIELD_NAME, entityRecord.getId());
if (instance == null) {
throw new ObjectNotFoundException(entity.getName(), entityRecord.getId());
}
updateFields(instance, entityRecord.getFields(), service, deleteValueFieldId, true);
return service.update(instance);
}
} catch (Exception e) {
if (entityRecord.getId() == null) {
throw new ObjectCreateException(entity.getName(), e);
} else {
throw new ObjectUpdateException(entity.getName(), entityRecord.getId(), e);
}
}
}
use of org.motechproject.mds.exception.object.ObjectUpdateException in project motech by motech.
the class DefaultMotechDataService method revertFromTrash.
@Override
@Transactional
public T revertFromTrash(Long trashId) {
validateCredentials();
Object trashRecord = trashService.findTrashById(trashId, getClassType().getName());
if (trashRecord == null) {
throw new TrashInstanceNotFoundException(getClassType().getName(), trashId);
}
verifySchemaVersion(trashRecord, trashId, true);
try {
T newInstance = getClassType().newInstance();
copyValuesFromRecord(newInstance, trashRecord);
newInstance = create(newInstance);
trashService.removeFromTrash(trashRecord);
return newInstance;
} catch (InstantiationException | IllegalAccessException e) {
throw new ObjectUpdateException(trashRecord.getClass().getName(), trashId, e);
}
}
Aggregations