use of org.hisp.dhis.node.Node in project dhis2-core by dhis2.
the class DimensionController method getItems.
@SuppressWarnings("unchecked")
@GetMapping("/{uid}/items")
@ResponseBody
public RootNode getItems(@PathVariable String uid, @RequestParam Map<String, String> parameters, OrderParams orderParams) throws QueryParserException {
List<String> fields = newArrayList(contextService.getParameterValues("fields"));
List<String> filters = newArrayList(contextService.getParameterValues("filter"));
List<Order> orders = orderParams.getOrders(getSchema(DimensionalItemObject.class));
WebOptions options = new WebOptions(parameters);
if (fields.isEmpty()) {
fields.addAll(Preset.defaultPreset().getFields());
}
// This is the base list used in this flow. It contains only items
// allowed to the current user.
List<DimensionalItemObject> readableItems = dimensionService.getCanReadDimensionItems(uid);
// This is needed for two reasons:
// 1) We are doing in-memory paging;
// 2) We have to count all items respecting the filtering.
Query queryForCount = queryService.getQueryFromUrl(DimensionalItemObject.class, filters, orders);
queryForCount.setObjects(readableItems);
List<DimensionalItemObject> forCountItems = (List<DimensionalItemObject>) queryService.query(queryForCount);
Query query = queryService.getQueryFromUrl(DimensionalItemObject.class, filters, orders, getPaginationData(options));
query.setObjects(readableItems);
query.setDefaultOrder();
List<DimensionalItemObject> paginatedItems = (List<DimensionalItemObject>) queryService.query(query);
RootNode rootNode = NodeUtils.createMetadata();
CollectionNode collectionNode = rootNode.addChild(fieldFilterService.toCollectionNode(DimensionalItemObject.class, new FieldFilterParams(paginatedItems, fields)));
collectionNode.setName("items");
for (Node node : collectionNode.getChildren()) {
((AbstractNode) node).setName("item");
}
// Adding pagination elements to the root node.
final int totalOfItems = isNotEmpty(forCountItems) ? forCountItems.size() : 0;
dimensionItemPageHandler.addPaginationToNodeIfEnabled(rootNode, options, uid, totalOfItems);
return rootNode;
}
Aggregations