use of org.motechproject.mds.dto.FieldDto 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.dto.FieldDto in project motech by motech.
the class InstanceServiceImpl method getInstanceFields.
@Override
public List<FieldInstanceDto> getInstanceFields(Long entityId, Long instanceId) {
EntityDto entity = entityService.getEntity(entityId);
validateCredentialsForReading(entity);
List<FieldDto> fields = entityService.getEntityFieldsForUI(entityId);
List<FieldInstanceDto> result = new ArrayList<>();
for (FieldDto field : fields) {
FieldInstanceDto fieldInstanceDto = new FieldInstanceDto(field.getId(), instanceId, field.getBasic());
result.add(fieldInstanceDto);
}
return result;
}
use of org.motechproject.mds.dto.FieldDto in project motech by motech.
the class InstanceServiceImpl method buildRelatedInstances.
private Object buildRelatedInstances(MotechDataService service, Class<?> parameterType, Class<?> argumentType, Object fieldValue, Object relatedObject) throws IllegalAccessException, InstantiationException, ClassNotFoundException, NoSuchMethodException, CannotCompileException, NoSuchFieldException {
Object parsedValue;
RelationshipsUpdate relationshipsUpdate = mapper.convertValue(fieldValue, RelationshipsUpdate.class);
EntityDto relatedEntity = getEntity(argumentType.getName());
List<FieldDto> entityFields = getEntityFields(relatedEntity.getId());
if (Collection.class.isAssignableFrom(parameterType)) {
parsedValue = parseRelationshipCollection(service, (Class<? extends Collection>) parameterType, argumentType, (Collection) relatedObject, relationshipsUpdate, entityFields);
} else {
parsedValue = parseRelationshipValue(service, argumentType, relationshipsUpdate, entityFields);
}
return parsedValue;
}
use of org.motechproject.mds.dto.FieldDto in project motech by motech.
the class InstanceServiceImpl method newInstanceFromEntityRecord.
private Object newInstanceFromEntityRecord(Class<?> entityClass, List<FieldDto> entityFields, List<FieldRecord> fields, MotechDataService service) throws IllegalAccessException, InstantiationException, ClassNotFoundException, NoSuchMethodException, CannotCompileException, NoSuchFieldException {
Object instance = entityClass.newInstance();
for (FieldDto entityField : entityFields) {
if (entityField.getType().isMap() && entityField.getBasic().getDefaultValue() != null) {
setInstanceFieldMap(instance, entityField);
}
}
updateFields(instance, fields, service, null, false);
return instance;
}
use of org.motechproject.mds.dto.FieldDto in project motech by motech.
the class InstanceServiceImpl method newInstance.
@Override
public EntityRecord newInstance(Long entityId) {
validateCredentials(getEntity(entityId));
List<FieldDto> fields = entityService.getEntityFieldsForUI(entityId);
List<FieldRecord> fieldRecords = new ArrayList<>();
for (FieldDto field : fields) {
FieldRecord fieldRecord = new FieldRecord(field);
fieldRecords.add(fieldRecord);
}
populateDefaultFields(fieldRecords);
return new EntityRecord(null, entityId, fieldRecords);
}
Aggregations