use of org.motechproject.mds.exception.object.ObjectCreateException 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);
}
}
}
Aggregations