Search in sources :

Example 6 with EntityRecord

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

the class InstanceServiceTest method shouldUpdateGridSize.

@Test
public void shouldUpdateGridSize() {
    setUpSecurityContext();
    EntityDto entityDto = new EntityDto();
    entityDto.setReadOnlySecurityMode(null);
    entityDto.setSecurityMode(null);
    entityDto.setClassName(TestSample.class.getName());
    EntityRecord entityRecord = new EntityRecord(ENTITY_ID + 1, null, new ArrayList<>());
    when(entityService.getEntity(ENTITY_ID + 1)).thenReturn(entityDto);
    mockDataService();
    instanceService.getEntityRecords(entityRecord.getId(), new QueryParams(1, 100));
    verify(entityService).getEntityFieldsForUI(ENTITY_ID + 1);
    verify(userPreferencesService).updateGridSize(ENTITY_ID + 1, "motech", 100);
}
Also used : BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) EntityRecord(org.motechproject.mds.web.domain.EntityRecord) EntityDto(org.motechproject.mds.dto.EntityDto) QueryParams(org.motechproject.mds.query.QueryParams) Test(org.junit.Test)

Example 7 with EntityRecord

use of org.motechproject.mds.web.domain.EntityRecord 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);
}
Also used : EntityRecord(org.motechproject.mds.web.domain.EntityRecord) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) ArrayList(java.util.ArrayList) BasicFieldRecord(org.motechproject.mds.web.domain.BasicFieldRecord) FieldRecord(org.motechproject.mds.web.domain.FieldRecord) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 8 with EntityRecord

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

the class InstanceServiceImpl method parseRelationshipCollection.

private Object parseRelationshipCollection(MotechDataService service, Class<? extends Collection> parameterType, Class<?> argumentType, Collection existingRelatedInstances, RelationshipsUpdate relationshipsUpdate, List<FieldDto> entityFields) throws InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchMethodException, CannotCompileException, NoSuchFieldException {
    Collection relatedInstances;
    Class<?> collectionImplementation = TypeHelper.suggestCollectionImplementation(parameterType);
    relatedInstances = (Collection) (collectionImplementation == null ? new ArrayList() : collectionImplementation.newInstance());
    // Add already related instances, provided any exist
    if (existingRelatedInstances != null) {
        relatedInstances.addAll(existingRelatedInstances);
    }
    // Add new, existing relations, based on the passed IDs
    relatedInstances.addAll(service.findByIds(relationshipsUpdate.getAddedIds()));
    // Remove existing relations if their ID is on the list
    relatedInstances.removeIf(new Predicate() {

        @Override
        public boolean test(Object o) {
            return relationshipsUpdate.getRemovedIds().contains(PropertyUtil.safeGetProperty(o, Constants.Util.ID_FIELD_NAME));
        }
    });
    // Add new relations, that do not exist in db yet
    for (EntityRecord record : relationshipsUpdate.getAddedNewRecords()) {
        relatedInstances.add(newInstanceFromEntityRecord(argumentType, entityFields, record.getFields(), service));
    }
    return relatedInstances;
}
Also used : EntityRecord(org.motechproject.mds.web.domain.EntityRecord) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) ArrayList(java.util.ArrayList) Collection(java.util.Collection) Predicate(java.util.function.Predicate)

Example 9 with EntityRecord

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

the class InstanceServiceImpl method convertToHistoryRecord.

private HistoryRecord convertToHistoryRecord(Object object, EntityDto entity, Long instanceId, MotechDataService service) {
    Long entityId = entity.getId();
    List<FieldDto> fields = getEntityFields(entityId);
    Map<String, List<FieldDto>> relatedEntitiesFields = getRelatedEntitiesFields(fields);
    EntityRecord entityRecord = instanceToRecord(object, entity, entityService.getEntityFieldsForUI(entityId), service, EntityType.HISTORY, relatedEntitiesFields);
    Long historyInstanceSchemaVersion = (Long) PropertyUtil.safeGetProperty(object, HistoryTrashClassHelper.historySchemaVersion(object.getClass()));
    Long currentSchemaVersion = entityService.getCurrentSchemaVersion(entity.getClassName());
    return new HistoryRecord(entityRecord.getId(), instanceId, historyInstanceSchemaVersion.equals(currentSchemaVersion), entityRecord.getFields());
}
Also used : EntityRecord(org.motechproject.mds.web.domain.EntityRecord) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) ArrayList(java.util.ArrayList) List(java.util.List) BasicHistoryRecord(org.motechproject.mds.web.domain.BasicHistoryRecord) HistoryRecord(org.motechproject.mds.web.domain.HistoryRecord) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 10 with EntityRecord

use of org.motechproject.mds.web.domain.EntityRecord 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)

Aggregations

BasicEntityRecord (org.motechproject.mds.web.domain.BasicEntityRecord)27 EntityRecord (org.motechproject.mds.web.domain.EntityRecord)27 Test (org.junit.Test)16 BasicFieldRecord (org.motechproject.mds.web.domain.BasicFieldRecord)15 FieldRecord (org.motechproject.mds.web.domain.FieldRecord)15 EntityDto (org.motechproject.mds.dto.EntityDto)11 ArrayList (java.util.ArrayList)5 FieldDto (org.motechproject.mds.dto.FieldDto)5 Matchers.anyString (org.mockito.Matchers.anyString)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 List (java.util.List)3 MotechDataService (org.motechproject.mds.service.MotechDataService)3 RelationshipsUpdate (org.motechproject.mds.web.domain.RelationshipsUpdate)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Collection (java.util.Collection)2 Predicate (java.util.function.Predicate)2 CannotCompileException (javassist.CannotCompileException)2 FieldNotFoundException (org.motechproject.mds.exception.field.FieldNotFoundException)2 ObjectNotFoundException (org.motechproject.mds.exception.object.ObjectNotFoundException)2