Search in sources :

Example 1 with Records

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

the class InstanceController method getInstances.

@RequestMapping(value = "/entities/{entityId}/instances", method = RequestMethod.POST)
@ResponseBody
public Records<BasicEntityRecord> getInstances(@PathVariable Long entityId, GridSettings settings) throws IOException {
    String lookup = settings.getLookup();
    String filterStr = settings.getFilter();
    Map<String, Object> fieldMap = getFields(settings);
    QueryParams queryParams = QueryParamsBuilder.buildQueryParams(settings, fieldMap);
    List<BasicEntityRecord> entityRecords;
    long recordCount;
    if (StringUtils.isNotBlank(lookup)) {
        entityRecords = instanceService.getEntityRecordsFromLookup(entityId, lookup, fieldMap, queryParams);
        recordCount = instanceService.countRecordsByLookup(entityId, lookup, fieldMap);
    } else if (filterSet(filterStr)) {
        Filters filters = new Filters(objectMapper.readValue(filterStr, Filter[].class));
        filters.setMultiselect(instanceService.getEntityFields(entityId));
        entityRecords = instanceService.getEntityRecordsWithFilter(entityId, filters, queryParams);
        recordCount = instanceService.countRecordsWithFilters(entityId, filters);
    } else {
        entityRecords = instanceService.getEntityRecords(entityId, queryParams);
        recordCount = instanceService.countRecords(entityId);
    }
    int rowCount = (int) Math.ceil(recordCount / (double) queryParams.getPageSize());
    Records<BasicEntityRecord> records = new Records<>(queryParams.getPage(), rowCount, (int) recordCount, entityRecords);
    processFieldsForUI(records);
    return records;
}
Also used : Filters(org.motechproject.mds.filter.Filters) Filter(org.motechproject.mds.filter.Filter) JsonObject(com.google.gson.JsonObject) QueryParams(org.motechproject.mds.query.QueryParams) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) Records(org.motechproject.mds.web.domain.Records) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with Records

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

the class InstanceController method getTrash.

@RequestMapping(value = "/entities/{entityId}/trash", method = RequestMethod.GET)
@ResponseBody
public Records<BasicEntityRecord> getTrash(@PathVariable Long entityId, GridSettings settings) {
    QueryParams queryParams = QueryParamsBuilder.buildQueryParams(settings);
    List<BasicEntityRecord> trashRecordsList = instanceService.getTrashRecords(entityId, queryParams);
    long recordCount = instanceService.countTrashRecords(entityId);
    int rowCount = (int) Math.ceil(recordCount / (double) queryParams.getPageSize());
    Records<BasicEntityRecord> records = new Records<>(queryParams.getPage(), rowCount, (int) recordCount, trashRecordsList);
    processFieldsForUI(records);
    return records;
}
Also used : QueryParams(org.motechproject.mds.query.QueryParams) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) Records(org.motechproject.mds.web.domain.Records) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with Records

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

the class InstanceController method getHistory.

@RequestMapping(value = "/instances/{entityId}/{instanceId}/history", method = RequestMethod.GET)
@ResponseBody
public Records<BasicHistoryRecord> getHistory(@PathVariable Long entityId, @PathVariable Long instanceId, GridSettings settings) {
    QueryParams queryParams = QueryParamsBuilder.buildQueryParams(settings);
    List<BasicHistoryRecord> historyRecordsList = instanceService.getInstanceHistory(entityId, instanceId, queryParams);
    long recordCount = instanceService.countHistoryRecords(entityId, instanceId);
    int rowCount = (int) Math.ceil(recordCount / (double) queryParams.getPageSize());
    Records<BasicHistoryRecord> records = new Records<>(queryParams.getPage(), rowCount, (int) recordCount, historyRecordsList);
    processFieldsForUIinHistoryRecords(records);
    return records;
}
Also used : QueryParams(org.motechproject.mds.query.QueryParams) BasicHistoryRecord(org.motechproject.mds.web.domain.BasicHistoryRecord) Records(org.motechproject.mds.web.domain.Records) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 4 with Records

use of org.motechproject.mds.web.domain.Records 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 5 with Records

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

the class InstanceControllerTest method shouldRetrieveRelatedFieldValues.

@Test
public void shouldRetrieveRelatedFieldValues() throws Exception {
    Records<BasicEntityRecord> records = new Records<>(2, 5, 7, recordsList());
    when(instanceService.getRelatedFieldValue(eq(1L), eq(6L), eq("relField"), any(RelationshipsUpdate.class), any(QueryParams.class))).thenReturn(records);
    controller.perform(post("/instances/1/instance/6/relField?rows=5&page=2&sortColumn=age&sortDirection=desc")).andExpect(status().isOk()).andExpect(content().type(RestTestUtil.JSON_UTF8)).andExpect(content().string(new ObjectMapper().writeValueAsString(records)));
    ArgumentCaptor<QueryParams> captor = ArgumentCaptor.forClass(QueryParams.class);
    verify(instanceService).getRelatedFieldValue(eq(1L), eq(6L), eq("relField"), any(RelationshipsUpdate.class), captor.capture());
    QueryParams queryParams = captor.getValue();
    // check query params
    assertNotNull(queryParams);
    assertEquals(Integer.valueOf(5), queryParams.getPageSize());
    assertEquals(Integer.valueOf(2), queryParams.getPage());
    assertNotNull(queryParams.getOrderList());
    assertEquals(2, queryParams.getOrderList().size());
    assertEquals("age", queryParams.getOrderList().get(0).getField());
    assertEquals(Order.Direction.DESC, queryParams.getOrderList().get(0).getDirection());
    assertEquals(Constants.Util.ID_FIELD_NAME, queryParams.getOrderList().get(1).getField());
    assertEquals(Order.Direction.ASC, queryParams.getOrderList().get(1).getDirection());
}
Also used : RelationshipsUpdate(org.motechproject.mds.web.domain.RelationshipsUpdate) QueryParams(org.motechproject.mds.query.QueryParams) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) Records(org.motechproject.mds.web.domain.Records) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.junit.Test)

Aggregations

Records (org.motechproject.mds.web.domain.Records)5 QueryParams (org.motechproject.mds.query.QueryParams)4 BasicEntityRecord (org.motechproject.mds.web.domain.BasicEntityRecord)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 JsonObject (com.google.gson.JsonObject)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1 Predicate (java.util.function.Predicate)1 CannotCompileException (javassist.CannotCompileException)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1 Test (org.junit.Test)1 EntityDto (org.motechproject.mds.dto.EntityDto)1 FieldDto (org.motechproject.mds.dto.FieldDto)1 FieldNotFoundException (org.motechproject.mds.exception.field.FieldNotFoundException)1 ObjectNotFoundException (org.motechproject.mds.exception.object.ObjectNotFoundException)1 ObjectReadException (org.motechproject.mds.exception.object.ObjectReadException)1 Filter (org.motechproject.mds.filter.Filter)1