Search in sources :

Example 81 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class InstanceServiceImpl method getEntityRecords.

@Override
public List<BasicEntityRecord> getEntityRecords(Long entityId, QueryParams queryParams) {
    EntityDto entity = getEntity(entityId);
    validateCredentialsForReading(entity);
    List<FieldDto> fields = entityService.getEntityFieldsForUI(entityId);
    MotechDataService service = getServiceForEntity(entity);
    List instances = service.retrieveAll(queryParams);
    updateGridSize(entityId, queryParams);
    return instancesToBasicRecords(instances, entity, fields, service, EntityType.STANDARD);
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) ArrayList(java.util.ArrayList) List(java.util.List) MotechDataService(org.motechproject.mds.service.MotechDataService) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 82 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class InstanceServiceImpl method getTrashRecords.

@Override
public List<BasicEntityRecord> getTrashRecords(Long entityId, QueryParams queryParams) {
    EntityDto entity = getEntity(entityId);
    validateCredentialsForReading(entity);
    MotechDataService service = getServiceForEntity(entity);
    List<FieldDto> fields = entityService.getEntityFieldsForUI(entityId);
    Collection collection = trashService.getInstancesFromTrash(entity.getClassName(), queryParams);
    updateGridSize(entityId, queryParams);
    return instancesToBasicRecords(collection, entity, fields, service, EntityType.TRASH);
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) Collection(java.util.Collection) MotechDataService(org.motechproject.mds.service.MotechDataService) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 83 with FieldDto

use of org.motechproject.mds.dto.FieldDto 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 84 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class InstanceServiceImpl method getRelatedEntitiesFields.

private Map<String, List<FieldDto>> getRelatedEntitiesFields(List<FieldDto> fields) {
    Map<String, List<FieldDto>> relatedEntitiesFields = new HashMap<>();
    for (FieldDto field : fields) {
        MetadataDto entityMetadata = field.getMetadata(Constants.MetadataKeys.RELATED_CLASS);
        if (entityMetadata != null) {
            String className = entityMetadata.getValue();
            relatedEntitiesFields.put(className, getEntityFieldsByClassName(className));
        }
    }
    return relatedEntitiesFields;
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) MetadataDto(org.motechproject.mds.dto.MetadataDto) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 85 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class InstanceServiceImpl method getEntityRecordsWithFilter.

@Override
public List<BasicEntityRecord> getEntityRecordsWithFilter(Long entityId, Filters filters, QueryParams queryParams) {
    EntityDto entity = getEntity(entityId);
    validateCredentialsForReading(entity);
    List<FieldDto> fields = entityService.getEntityFieldsForUI(entityId);
    MotechDataService service = getServiceForEntity(entity);
    List instances = service.filter(filters, queryParams);
    return instancesToBasicRecords(instances, entity, fields, service, EntityType.STANDARD);
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) ArrayList(java.util.ArrayList) List(java.util.List) MotechDataService(org.motechproject.mds.service.MotechDataService) FieldDto(org.motechproject.mds.dto.FieldDto)

Aggregations

FieldDto (org.motechproject.mds.dto.FieldDto)158 Test (org.junit.Test)61 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)60 EntityDto (org.motechproject.mds.dto.EntityDto)53 ArrayList (java.util.ArrayList)47 LookupDto (org.motechproject.mds.dto.LookupDto)29 List (java.util.List)23 MetadataDto (org.motechproject.mds.dto.MetadataDto)22 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)19 TypeDto (org.motechproject.mds.dto.TypeDto)17 Method (java.lang.reflect.Method)14 MotechDataService (org.motechproject.mds.service.MotechDataService)14 HashMap (java.util.HashMap)12 Field (org.motechproject.mds.domain.Field)12 FieldTestHelper.lookupFieldDto (org.motechproject.mds.testutil.FieldTestHelper.lookupFieldDto)12 Arrays.asList (java.util.Arrays.asList)11 SettingDto (org.motechproject.mds.dto.SettingDto)11 Before (org.junit.Before)7 AdvancedSettingsDto (org.motechproject.mds.dto.AdvancedSettingsDto)7 BasicEntityRecord (org.motechproject.mds.web.domain.BasicEntityRecord)7