use of org.motechproject.mds.exception.object.ObjectReadException in project motech by motech.
the class InstanceServiceImpl method getRelatedFieldValue.
@Override
public Records<BasicEntityRecord> getRelatedFieldValue(Long entityId, Long instanceId, String fieldName, RelationshipsUpdate filter, QueryParams queryParams) {
try {
// first get the entity
EntityDto entity = getEntity(entityId);
validateCredentials(entity);
List<FieldDto> fields = getEntityFields(entityId);
String entityName = entity.getName();
MotechDataService service = getServiceForEntity(entity);
// then the related entity
FieldDto relatedField = findFieldByName(fields, fieldName);
if (relatedField == null) {
throw new FieldNotFoundException(entity.getClassName(), fieldName);
}
String relatedClass = relatedField.getMetadataValue(Constants.MetadataKeys.RELATED_CLASS);
if (StringUtils.isBlank(relatedClass)) {
throw new IllegalArgumentException("Field " + fieldName + " in entity " + entity.getClassName() + " is not a related field");
}
// these will be used for building the records
EntityDto relatedEntity = getEntity(relatedClass);
List<FieldDto> relatedFields = getEntityFields(relatedEntity.getId());
MotechDataService relatedDataService = getServiceForEntity(relatedEntity);
Collection relatedAsColl = new ArrayList<>();
// If the relationship already exists, fetch instances and use correct type
if (instanceId != null) {
// get the instance of the original entity
Object instance = service.findById(instanceId);
if (instance == null) {
throw new ObjectNotFoundException(entityName, instanceId);
}
// the value of the related field
relatedAsColl = TypeHelper.asCollection(PropertyUtil.getProperty(instance, fieldName));
}
relatedAsColl.addAll(relatedDataService.findByIds(filter.getAddedIds()));
List<Long> updatedInstancesIds = new ArrayList<>();
for (EntityRecord record : filter.getAddedNewRecords()) {
Integer id = (Integer) record.getFieldByName(Constants.Util.ID_FIELD_NAME).getValue();
if (id != null && id > 0) {
updatedInstancesIds.add(id.longValue());
}
}
relatedAsColl.removeIf(new Predicate() {
@Override
public boolean test(Object o) {
Long objectId = (Long) PropertyUtil.safeGetProperty(o, Constants.Util.ID_FIELD_NAME);
return filter.getRemovedIds().contains(objectId) || updatedInstancesIds.contains(objectId);
}
});
for (EntityRecord record : filter.getAddedNewRecords()) {
relatedAsColl.add(newInstanceFromEntityRecord(getEntityClass(relatedEntity), relatedFields, record.getFields(), relatedDataService));
}
// apply pagination ordering (currently in memory)
List filtered = InMemoryQueryFilter.filter(relatedAsColl, queryParams);
// convert the instance to a grid-friendly form
List<BasicEntityRecord> entityRecords = instancesToBasicRecords(filtered, relatedEntity, relatedFields, relatedDataService, EntityType.STANDARD);
// counts for the grid
int recordCount = relatedAsColl.size();
int rowCount = (int) Math.ceil(recordCount / (double) queryParams.getPageSize());
// package as records
return new Records<>(queryParams.getPage(), rowCount, recordCount, entityRecords);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | IllegalArgumentException | ClassNotFoundException | CannotCompileException | InstantiationException | NoSuchFieldException e) {
throw new ObjectReadException(entityId, e);
}
}
use of org.motechproject.mds.exception.object.ObjectReadException in project motech by motech.
the class InstanceServiceImpl method instanceToRecord.
private <T extends BasicEntityRecord> T instanceToRecord(Object instance, EntityDto entityDto, List<FieldDto> fields, MotechDataService service, EntityType entityType, Class<T> clazz, Map<String, List<FieldDto>> relatedEntitiesFields) {
if (instance == null) {
return null;
}
try {
List fieldRecords = new ArrayList<>();
boolean basic = BasicEntityRecord.class.equals(clazz);
for (FieldDto field : fields) {
if (entityType != EntityType.STANDARD && field.isVersionField()) {
continue;
}
Object value = getProperty(instance, field, service);
Object displayValue = DisplayHelper.getDisplayValueForField(field, value, MAX_LENGTH);
value = parseValueForDisplay(value, relatedEntitiesFields.get(field.getMetadata(Constants.MetadataKeys.RELATED_CLASS)));
BasicFieldRecord fieldRecord = basic ? new BasicFieldRecord(field) : new FieldRecord(field);
fieldRecord.setValue(value);
fieldRecord.setDisplayValue(displayValue);
fieldRecords.add(fieldRecord);
}
Number id = (Number) PropertyUtil.safeGetProperty(instance, ID_FIELD_NAME);
Long parsedId = id == null ? null : id.longValue();
return (T) (basic ? new BasicEntityRecord(parsedId, fieldRecords) : new EntityRecord(parsedId, entityDto.getId(), fieldRecords));
} catch (Exception e) {
throw new ObjectReadException(entityDto.getName(), e);
}
}
use of org.motechproject.mds.exception.object.ObjectReadException in project motech by motech.
the class InstanceServiceImpl method getInstanceValueAsRelatedField.
@Override
public FieldRecord getInstanceValueAsRelatedField(Long entityId, Long fieldId, Long instanceId) {
validateCredentialsForReading(getEntity(entityId));
try {
FieldRecord fieldRecord;
FieldDto field = entityService.getEntityFieldById(entityId, fieldId);
MotechDataService service = DataServiceHelper.getDataService(bundleContext, field.getMetadata(RELATED_CLASS).getValue());
Object instance = service.findById(instanceId);
if (instance == null) {
throw new ObjectNotFoundException(service.getClassType().getName(), instanceId);
}
List<FieldDto> relatedEntityFields = getEntityFieldsByClassName(field.getMetadata(RELATED_CLASS).getValue());
fieldRecord = new FieldRecord(field);
fieldRecord.setValue(parseValueForDisplay(instance, relatedEntityFields));
fieldRecord.setDisplayValue(instance.toString());
return fieldRecord;
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
throw new ObjectReadException(entityId, e);
}
}
Aggregations