use of org.hisp.dhis.query.Query in project dhis2-core by dhis2.
the class DataElementOperandController method getObjectList.
@GetMapping
@SuppressWarnings("unchecked")
@ResponseBody
public RootNode getObjectList(@RequestParam Map<String, String> rpParameters, OrderParams orderParams) throws QueryParserException {
Schema schema = schemaService.getDynamicSchema(DataElementOperand.class);
List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
List<String> filters = Lists.newArrayList(contextService.getParameterValues("filter"));
List<Order> orders = orderParams.getOrders(schema);
if (fields.isEmpty()) {
fields.addAll(Preset.ALL.getFields());
}
WebOptions options = new WebOptions(rpParameters);
WebMetadata metadata = new WebMetadata();
List<DataElementOperand> dataElementOperands;
if (options.isTrue("persisted")) {
dataElementOperands = Lists.newArrayList(manager.getAll(DataElementOperand.class));
} else {
boolean totals = options.isTrue("totals");
String deg = CollectionUtils.popStartsWith(filters, "dataElement.dataElementGroups.id:eq:");
deg = deg != null ? deg.substring("dataElement.dataElementGroups.id:eq:".length()) : null;
String ds = options.get("dataSet");
if (deg != null) {
DataElementGroup dataElementGroup = manager.get(DataElementGroup.class, deg);
dataElementOperands = dataElementCategoryService.getOperands(dataElementGroup.getMembers(), totals);
} else if (ds != null) {
DataSet dataSet = manager.get(DataSet.class, ds);
dataElementOperands = dataElementCategoryService.getOperands(dataSet, totals);
} else {
List<DataElement> dataElements = new ArrayList<>(manager.getAllSorted(DataElement.class));
dataElementOperands = dataElementCategoryService.getOperands(dataElements, totals);
}
}
Query query = queryService.getQueryFromUrl(DataElementOperand.class, filters, orders, options.getRootJunction());
query.setDefaultOrder();
query.setObjects(dataElementOperands);
dataElementOperands = (List<DataElementOperand>) queryService.query(query);
Pager pager = metadata.getPager();
if (options.hasPaging() && pager == null) {
pager = new Pager(options.getPage(), dataElementOperands.size(), options.getPageSize());
linkService.generatePagerLinks(pager, DataElementOperand.class);
dataElementOperands = PagerUtils.pageCollection(dataElementOperands, pager);
}
RootNode rootNode = NodeUtils.createMetadata();
if (pager != null) {
rootNode.addChild(NodeUtils.createPager(pager));
}
rootNode.addChild(fieldFilterService.filter(DataElementOperand.class, dataElementOperands, fields));
return rootNode;
}
use of org.hisp.dhis.query.Query in project dhis2-core by dhis2.
the class ProgramController method getEntityList.
@Override
@SuppressWarnings("unchecked")
protected List<Program> getEntityList(WebMetadata metadata, WebOptions options, List<String> filters, List<Order> orders) throws QueryParserException {
Boolean userFilter = Boolean.parseBoolean(options.getOptions().get("userFilter"));
List<Program> entityList;
Query query = queryService.getQueryFromUrl(getEntityClass(), filters, orders, options.getRootJunction());
query.setDefaultOrder();
if (options.getOptions().containsKey("query")) {
entityList = Lists.newArrayList(manager.filter(getEntityClass(), options.getOptions().get("query")));
} else {
entityList = (List<Program>) queryService.query(query);
}
if (userFilter) {
Set<Program> programs = programService.getUserPrograms();
entityList.retainAll(programs);
metadata.setPager(null);
}
return entityList;
}
use of org.hisp.dhis.query.Query in project dhis2-core by dhis2.
the class ProgramDataElementController method getObjectList.
@GetMapping
@SuppressWarnings("unchecked")
@ResponseBody
public RootNode getObjectList(@RequestParam Map<String, String> rpParameters, OrderParams orderParams) throws QueryParserException {
Schema schema = schemaService.getDynamicSchema(ProgramDataElementDimensionItem.class);
List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
List<String> filters = Lists.newArrayList(contextService.getParameterValues("filter"));
List<Order> orders = orderParams.getOrders(schema);
if (fields.isEmpty()) {
fields.addAll(Preset.ALL.getFields());
}
WebOptions options = new WebOptions(rpParameters);
WebMetadata metadata = new WebMetadata();
List<ProgramDataElementDimensionItem> programDataElements;
Query query = queryService.getQueryFromUrl(ProgramDataElementDimensionItem.class, filters, orders, options.getRootJunction());
query.setDefaultOrder();
if (options.contains("program")) {
String programUid = options.get("program");
programDataElements = programService.getGeneratedProgramDataElements(programUid);
query.setObjects(programDataElements);
}
programDataElements = (List<ProgramDataElementDimensionItem>) queryService.query(query);
Pager pager = metadata.getPager();
if (options.hasPaging() && pager == null) {
pager = new Pager(options.getPage(), programDataElements.size(), options.getPageSize());
programDataElements = PagerUtils.pageCollection(programDataElements, pager);
}
RootNode rootNode = NodeUtils.createMetadata();
if (pager != null) {
rootNode.addChild(NodeUtils.createPager(pager));
}
rootNode.addChild(fieldFilterService.filter(ProgramDataElementDimensionItem.class, programDataElements, fields));
return rootNode;
}
use of org.hisp.dhis.query.Query in project dhis2-core by dhis2.
the class MetadataExportServiceTest method testMetadataExportWithCustomQueries.
@Test
public void testMetadataExportWithCustomQueries() {
DataElementGroup deg1 = createDataElementGroup('A');
DataElement de1 = createDataElement('A');
DataElement de2 = createDataElement('B');
DataElement de3 = createDataElement('C');
manager.save(de1);
manager.save(de2);
manager.save(de3);
User user = createUser('A');
manager.save(user);
deg1.addDataElement(de1);
deg1.addDataElement(de2);
deg1.addDataElement(de3);
deg1.setUser(user);
manager.save(deg1);
Query deQuery = Query.from(schemaService.getDynamicSchema(DataElement.class));
Disjunction disjunction = deQuery.disjunction();
disjunction.add(Restrictions.eq("id", de1.getUid()));
disjunction.add(Restrictions.eq("id", de2.getUid()));
deQuery.add(disjunction);
Query degQuery = Query.from(schemaService.getDynamicSchema(DataElementGroup.class));
degQuery.add(Restrictions.eq("id", "INVALID UID"));
MetadataExportParams params = new MetadataExportParams();
params.addQuery(deQuery);
params.addQuery(degQuery);
Map<Class<? extends IdentifiableObject>, List<? extends IdentifiableObject>> metadata = metadataExportService.getMetadata(params);
assertFalse(metadata.containsKey(User.class));
assertFalse(metadata.containsKey(DataElementGroup.class));
assertTrue(metadata.containsKey(DataElement.class));
assertEquals(2, metadata.get(DataElement.class).size());
}
use of org.hisp.dhis.query.Query in project dhis2-core by dhis2.
the class AbstractCrudController method getObjectInternal.
@SuppressWarnings("unchecked")
private RootNode getObjectInternal(String uid, Map<String, String> parameters, List<String> filters, List<String> fields, User user) throws Exception {
WebOptions options = new WebOptions(parameters);
List<T> entities = getEntity(uid, options);
if (entities.isEmpty()) {
throw new WebMessageException(WebMessageUtils.notFound(getEntityClass(), uid));
}
Query query = queryService.getQueryFromUrl(getEntityClass(), filters, new ArrayList<>(), options.getRootJunction());
query.setUser(user);
query.setObjects(entities);
entities = (List<T>) queryService.query(query);
handleLinksAndAccess(entities, fields, true, user);
for (T entity : entities) {
postProcessEntity(entity);
postProcessEntity(entity, options, parameters);
}
CollectionNode collectionNode = fieldFilterService.filter(getEntityClass(), entities, fields);
if (options.isTrue("useWrapper") || entities.size() > 1) {
RootNode rootNode = NodeUtils.createMetadata(collectionNode);
rootNode.getConfig().setInclusionStrategy(getInclusionStrategy(parameters.get("inclusionStrategy")));
return rootNode;
} else {
List<Node> children = collectionNode.getChildren();
RootNode rootNode;
if (!children.isEmpty()) {
rootNode = NodeUtils.createRootNode(children.get(0));
} else {
rootNode = NodeUtils.createRootNode(new ComplexNode(getSchema().getSingular()));
}
rootNode.getConfig().setInclusionStrategy(getInclusionStrategy(parameters.get("inclusionStrategy")));
return rootNode;
}
}
Aggregations