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;
}
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;
}
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());
}
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()));
}
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());
}
Aggregations