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