Search in sources :

Example 1 with BasicEntityRecord

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

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

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

the class InstanceServiceTest method shouldReturnInstancesFromTrash.

@Test
public void shouldReturnInstancesFromTrash() {
    mockDataService();
    mockSampleFields();
    mockEntity();
    QueryParams queryParams = new QueryParams(1, 10);
    when(trashService.getInstancesFromTrash(anyString(), eq(queryParams))).thenReturn(sampleCollection());
    List<BasicEntityRecord> records = instanceService.getTrashRecords(ENTITY_ID, queryParams);
    verify(trashService).getInstancesFromTrash(anyString(), eq(queryParams));
    assertNotNull(records);
    assertEquals(records.size(), 1);
    // Make sure all fields that were in the instance are still available
    assertEquals(records.get(0).getFields().size(), 5);
    // should not perform update when username is null or blank
    verify(userPreferencesService, never()).updateGridSize(anyLong(), anyString(), anyInt());
}
Also used : QueryParams(org.motechproject.mds.query.QueryParams) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) Test(org.junit.Test)

Example 4 with BasicEntityRecord

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

the class InstanceServiceTest method shouldReturnRelatedInstances.

@Test
public void shouldReturnRelatedInstances() {
    mockDataService();
    mockAnotherEntity();
    mockEntity();
    mockSampleFields();
    mockAnotherEntityFields();
    mockTestClassEntity();
    mockTestClassService();
    mockTestClassFields();
    when(serviceForAnotherSample.findById(INSTANCE_ID)).thenReturn(sampleForRelationshipTesting());
    QueryParams queryParams = new QueryParams(1, 2, new Order(Constants.Util.ID_FIELD_NAME, Order.Direction.ASC));
    Records<BasicEntityRecord> records = instanceService.getRelatedFieldValue(ANOTHER_ENTITY_ID, INSTANCE_ID, "testClasses", new RelationshipsUpdate(), queryParams);
    assertNotNull(records);
    // page 1
    assertEquals(Integer.valueOf(1), records.getPage());
    // 2 pages total
    assertEquals(Integer.valueOf(2), records.getTotal());
    // 3 records total
    assertEquals(Integer.valueOf(3), records.getRecords());
    assertEquals(asList(1L, 2L), extract(records.getRows(), on(BasicEntityRecord.class).getFieldByName("id").getValue()));
    RelationshipsUpdate filter = new RelationshipsUpdate();
    filter.setRemovedIds(Arrays.asList(1L, 2L));
    filter.setAddedIds(Arrays.asList(50L));
    when(testClassMotechDataService.findByIds(filter.getAddedIds())).thenReturn(Arrays.asList(new TestClass(50)));
    records = instanceService.getRelatedFieldValue(ANOTHER_ENTITY_ID, INSTANCE_ID, "testClasses", filter, queryParams);
    assertNotNull(records);
    // page 1
    assertEquals(Integer.valueOf(1), records.getPage());
    // 1 page total
    assertEquals(Integer.valueOf(1), records.getTotal());
    // 2 records total
    assertEquals(Integer.valueOf(2), records.getRecords());
    // 1L and 2L removed, 50L added
    assertEquals(asList(3L, 50L), extract(records.getRows(), on(BasicEntityRecord.class).getFieldByName("id").getValue()));
}
Also used : Order(org.motechproject.mds.util.Order) RelationshipsUpdate(org.motechproject.mds.web.domain.RelationshipsUpdate) QueryParams(org.motechproject.mds.query.QueryParams) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) Test(org.junit.Test)

Example 5 with BasicEntityRecord

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

the class InstanceServiceImpl method convertToBasicHistoryRecord.

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

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