Search in sources :

Example 11 with Query

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;
}
Also used : Order(org.hisp.dhis.query.Order) DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) RootNode(org.hisp.dhis.node.types.RootNode) Query(org.hisp.dhis.query.Query) DataSet(org.hisp.dhis.dataset.DataSet) Schema(org.hisp.dhis.schema.Schema) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) WebMetadata(org.hisp.dhis.webapi.webdomain.WebMetadata) Pager(org.hisp.dhis.common.Pager) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) ArrayList(java.util.ArrayList) List(java.util.List) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 12 with Query

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;
}
Also used : Program(org.hisp.dhis.program.Program) Query(org.hisp.dhis.query.Query)

Example 13 with Query

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;
}
Also used : Order(org.hisp.dhis.query.Order) RootNode(org.hisp.dhis.node.types.RootNode) ProgramDataElementDimensionItem(org.hisp.dhis.program.ProgramDataElementDimensionItem) Query(org.hisp.dhis.query.Query) Schema(org.hisp.dhis.schema.Schema) Pager(org.hisp.dhis.common.Pager) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) WebMetadata(org.hisp.dhis.webapi.webdomain.WebMetadata) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 14 with Query

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());
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) Disjunction(org.hisp.dhis.query.Disjunction) User(org.hisp.dhis.user.User) Query(org.hisp.dhis.query.Query) MetadataExportParams(org.hisp.dhis.dxf2.metadata.MetadataExportParams) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) List(java.util.List) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 15 with Query

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;
    }
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) Query(org.hisp.dhis.query.Query) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) ComplexNode(org.hisp.dhis.node.types.ComplexNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) SimpleNode(org.hisp.dhis.node.types.SimpleNode) ComplexNode(org.hisp.dhis.node.types.ComplexNode) RootNode(org.hisp.dhis.node.types.RootNode) CollectionNode(org.hisp.dhis.node.types.CollectionNode) Node(org.hisp.dhis.node.Node) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) CollectionNode(org.hisp.dhis.node.types.CollectionNode)

Aggregations

Query (org.hisp.dhis.query.Query)16 List (java.util.List)8 ArrayList (java.util.ArrayList)6 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)5 Pager (org.hisp.dhis.common.Pager)4 RootNode (org.hisp.dhis.node.types.RootNode)4 Schema (org.hisp.dhis.schema.Schema)4 User (org.hisp.dhis.user.User)4 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)4 HashMap (java.util.HashMap)3 WebOptions (org.hisp.dhis.webapi.webdomain.WebOptions)3 Map (java.util.Map)2 SystemTimer (org.hisp.dhis.commons.timer.SystemTimer)2 Timer (org.hisp.dhis.commons.timer.Timer)2 DataElementOperand (org.hisp.dhis.dataelement.DataElementOperand)2 OrderParams (org.hisp.dhis.dxf2.common.OrderParams)2 Order (org.hisp.dhis.query.Order)2 WebMetadata (org.hisp.dhis.webapi.webdomain.WebMetadata)2 GetMapping (org.springframework.web.bind.annotation.GetMapping)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1