Search in sources :

Example 6 with BasicEntityRecord

use of org.motechproject.mds.web.domain.BasicEntityRecord 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);
    }
}
Also used : ArrayList(java.util.ArrayList) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) CannotCompileException(javassist.CannotCompileException) MotechDataService(org.motechproject.mds.service.MotechDataService) Predicate(java.util.function.Predicate) EntityDto(org.motechproject.mds.dto.EntityDto) ArrayList(java.util.ArrayList) List(java.util.List) Records(org.motechproject.mds.web.domain.Records) FieldDto(org.motechproject.mds.dto.FieldDto) FieldNotFoundException(org.motechproject.mds.exception.field.FieldNotFoundException) ObjectReadException(org.motechproject.mds.exception.object.ObjectReadException) InvocationTargetException(java.lang.reflect.InvocationTargetException) EntityRecord(org.motechproject.mds.web.domain.EntityRecord) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) ObjectNotFoundException(org.motechproject.mds.exception.object.ObjectNotFoundException) Collection(java.util.Collection)

Example 7 with BasicEntityRecord

use of org.motechproject.mds.web.domain.BasicEntityRecord in project motech by motech.

the class InstanceServiceImpl method instancesToBasicRecords.

private List<BasicEntityRecord> instancesToBasicRecords(Collection instances, EntityDto entity, List<FieldDto> fields, MotechDataService service, EntityType entityType) {
    List<BasicEntityRecord> records = new ArrayList<>();
    Map<String, List<FieldDto>> relatedEntitiesFields = getRelatedEntitiesFields(fields);
    for (Object instance : instances) {
        BasicEntityRecord record = instanceToBasicRecord(instance, entity, fields, service, entityType, relatedEntitiesFields);
        records.add(record);
    }
    return records;
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord)

Example 8 with BasicEntityRecord

use of org.motechproject.mds.web.domain.BasicEntityRecord 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);
    }
}
Also used : ArrayList(java.util.ArrayList) BasicFieldRecord(org.motechproject.mds.web.domain.BasicFieldRecord) FieldRecord(org.motechproject.mds.web.domain.FieldRecord) ObjectReadException(org.motechproject.mds.exception.object.ObjectReadException) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) ObjectReadException(org.motechproject.mds.exception.object.ObjectReadException) FieldReadOnlyException(org.motechproject.mds.exception.field.FieldReadOnlyException) InvocationTargetException(java.lang.reflect.InvocationTargetException) LookupNotFoundException(org.motechproject.mds.exception.lookup.LookupNotFoundException) ObjectUpdateException(org.motechproject.mds.exception.object.ObjectUpdateException) LookupExecutorException(org.motechproject.mds.exception.lookup.LookupExecutorException) SecurityException(org.motechproject.mds.exception.object.SecurityException) EntityNotFoundException(org.motechproject.mds.exception.entity.EntityNotFoundException) LookupExecutionException(org.motechproject.mds.exception.lookup.LookupExecutionException) ObjectCreateException(org.motechproject.mds.exception.object.ObjectCreateException) FieldNotFoundException(org.motechproject.mds.exception.field.FieldNotFoundException) ObjectNotFoundException(org.motechproject.mds.exception.object.ObjectNotFoundException) InstanceNotFoundException(javax.management.InstanceNotFoundException) EntityInstancesNonEditableException(org.motechproject.mds.exception.entity.EntityInstancesNonEditableException) CannotCompileException(javassist.CannotCompileException) BasicFieldRecord(org.motechproject.mds.web.domain.BasicFieldRecord) EntityRecord(org.motechproject.mds.web.domain.EntityRecord) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) ArrayList(java.util.ArrayList) List(java.util.List) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 9 with BasicEntityRecord

use of org.motechproject.mds.web.domain.BasicEntityRecord in project motech by motech.

the class InstanceServiceImpl method getEntityRecordsFromLookup.

@Override
public List<BasicEntityRecord> getEntityRecordsFromLookup(Long entityId, String lookupName, Map<String, Object> lookupMap, QueryParams queryParams) {
    EntityDto entity = getEntity(entityId);
    validateCredentialsForReading(entity);
    LookupDto lookup = getLookupByName(entityId, lookupName);
    List<FieldDto> fields = entityService.getEntityFieldsForUI(entityId);
    Map<String, FieldDto> fieldMap = entityService.getLookupFieldsMapping(entityId, lookupName);
    MotechDataService service = getServiceForEntity(entity);
    try {
        LookupExecutor lookupExecutor = new LookupExecutor(service, lookup, fieldMap);
        Object result = lookupExecutor.execute(lookupMap, queryParams);
        if (lookup.isSingleObjectReturn()) {
            BasicEntityRecord record = instanceToBasicRecord(result, entity, fields, service, EntityType.STANDARD, getRelatedEntitiesFields(fields));
            return (record == null) ? new ArrayList<BasicEntityRecord>() : Collections.singletonList(record);
        } else {
            List instances = (List) result;
            return instancesToBasicRecords(instances, entity, fields, service, EntityType.STANDARD);
        }
    } catch (LookupExecutorException e) {
        if (e.getMessageKey() != null) {
            throw new LookupExecutionException(e, e.getMessageKey());
        } else {
            throw new LookupExecutionException(e, LOOKUP_EXCEPTION_MESSAGE_KEY);
        }
    } catch (RuntimeException e) {
        throw new LookupExecutionException(e, LOOKUP_EXCEPTION_MESSAGE_KEY);
    }
}
Also used : LookupDto(org.motechproject.mds.dto.LookupDto) LookupExecutor(org.motechproject.mds.lookup.LookupExecutor) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) MotechDataService(org.motechproject.mds.service.MotechDataService) EntityDto(org.motechproject.mds.dto.EntityDto) ArrayList(java.util.ArrayList) List(java.util.List) LookupExecutionException(org.motechproject.mds.exception.lookup.LookupExecutionException) FieldDto(org.motechproject.mds.dto.FieldDto) LookupExecutorException(org.motechproject.mds.exception.lookup.LookupExecutorException)

Example 10 with BasicEntityRecord

use of org.motechproject.mds.web.domain.BasicEntityRecord in project motech by motech.

the class InstanceControllerTest method testRecordAsEntityRecord.

private BasicEntityRecord testRecordAsEntityRecord(String name, int val, long id) {
    BasicFieldRecord nameField = new FieldRecord("name", name, TypeDto.STRING);
    BasicFieldRecord valField = new FieldRecord("val", val, TypeDto.INTEGER);
    return new BasicEntityRecord(id, asList(nameField, valField));
}
Also used : FieldRecord(org.motechproject.mds.web.domain.FieldRecord) BasicFieldRecord(org.motechproject.mds.web.domain.BasicFieldRecord) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) BasicFieldRecord(org.motechproject.mds.web.domain.BasicFieldRecord)

Aggregations

BasicEntityRecord (org.motechproject.mds.web.domain.BasicEntityRecord)15 Test (org.junit.Test)6 QueryParams (org.motechproject.mds.query.QueryParams)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 BasicFieldRecord (org.motechproject.mds.web.domain.BasicFieldRecord)5 FieldRecord (org.motechproject.mds.web.domain.FieldRecord)5 FieldDto (org.motechproject.mds.dto.FieldDto)4 Records (org.motechproject.mds.web.domain.Records)4 HashMap (java.util.HashMap)3 Matchers.anyString (org.mockito.Matchers.anyString)3 RelationshipsUpdate (org.motechproject.mds.web.domain.RelationshipsUpdate)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 CannotCompileException (javassist.CannotCompileException)2 EntityDto (org.motechproject.mds.dto.EntityDto)2 FieldNotFoundException (org.motechproject.mds.exception.field.FieldNotFoundException)2 LookupExecutionException (org.motechproject.mds.exception.lookup.LookupExecutionException)2 LookupExecutorException (org.motechproject.mds.exception.lookup.LookupExecutorException)2