Search in sources :

Example 21 with WebOptions

use of org.hisp.dhis.webapi.webdomain.WebOptions in project dhis2-core by dhis2.

the class PaginationUtilsTest method verifyIgnoreNegativePage.

@Test
void verifyIgnoreNegativePage() {
    Map<String, String> options = new HashMap<>();
    options.put(WebOptions.PAGING, "true");
    options.put(WebOptions.PAGE, "-2");
    options.put(WebOptions.PAGE_SIZE, "200");
    WebOptions webOptions = new WebOptions(options);
    Pagination paginationData = PaginationUtils.getPaginationData(webOptions);
    assertThat(paginationData.getFirstResult(), is(0));
    assertThat(paginationData.getSize(), is(200));
}
Also used : Pagination(org.hisp.dhis.query.Pagination) HashMap(java.util.HashMap) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) Test(org.junit.jupiter.api.Test)

Example 22 with WebOptions

use of org.hisp.dhis.webapi.webdomain.WebOptions in project dhis2-core by dhis2.

the class PaginationUtilsTest method verifyPaginationIsDisabled.

@Test
void verifyPaginationIsDisabled() {
    Map<String, String> options = new HashMap<>();
    options.put(WebOptions.PAGING, "false");
    WebOptions webOptions = new WebOptions(options);
    Pagination paginationData = PaginationUtils.getPaginationData(webOptions);
    assertThat(paginationData.getFirstResult(), is(0));
    assertThat(paginationData.getSize(), is(0));
    assertThat(paginationData.hasPagination(), is(false));
}
Also used : Pagination(org.hisp.dhis.query.Pagination) HashMap(java.util.HashMap) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) Test(org.junit.jupiter.api.Test)

Example 23 with WebOptions

use of org.hisp.dhis.webapi.webdomain.WebOptions 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, PaginationUtils.getPaginationData(options), 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.toCollectionNode(ProgramDataElementDimensionItem.class, new FieldFilterParams(programDataElements, fields)));
    return rootNode;
}
Also used : Order(org.hisp.dhis.query.Order) RootNode(org.hisp.dhis.node.types.RootNode) Query(org.hisp.dhis.query.Query) Schema(org.hisp.dhis.schema.Schema) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) WebMetadata(org.hisp.dhis.webapi.webdomain.WebMetadata) ProgramDataElementDimensionItem(org.hisp.dhis.program.ProgramDataElementDimensionItem) Pager(org.hisp.dhis.common.Pager) FieldFilterParams(org.hisp.dhis.fieldfilter.FieldFilterParams) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 24 with WebOptions

use of org.hisp.dhis.webapi.webdomain.WebOptions in project dhis2-core by dhis2.

the class GeoFeatureController method getGeoFeaturesJsonP.

@GetMapping(produces = "application/javascript")
public void getGeoFeaturesJsonP(@RequestParam(required = false) String ou, @RequestParam(required = false) String oug, @RequestParam(required = false) DisplayProperty displayProperty, @RequestParam(required = false) Date relativePeriodDate, @RequestParam(required = false) String userOrgUnit, @RequestParam(required = false) String coordinateField, @RequestParam(defaultValue = "callback") String callback, @RequestParam(defaultValue = "false", value = "includeGroupSets") boolean rpIncludeGroupSets, @RequestParam Map<String, String> parameters, DhisApiVersion apiVersion, HttpServletRequest request, HttpServletResponse response) throws IOException {
    WebOptions options = new WebOptions(parameters);
    boolean includeGroupSets = "detailed".equals(options.getViewClass()) || rpIncludeGroupSets;
    List<GeoFeature> features = geoFeatureService.getGeoFeatures(GeoFeatureService.Parameters.builder().apiVersion(apiVersion).displayProperty(displayProperty).includeGroupSets(includeGroupSets).request(request).response(response).userOrgUnit(userOrgUnit).organisationUnitGroupId(oug).relativePeriodDate(relativePeriodDate).coordinateField(coordinateField).build());
    if (features == null) {
        return;
    }
    ContextUtils.setCacheControl(response, GEOFEATURE_CACHE);
    response.setContentType("application/javascript");
    renderService.toJsonP(response.getOutputStream(), features, callback);
}
Also used : WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) GeoFeature(org.hisp.dhis.webapi.webdomain.GeoFeature) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 25 with WebOptions

use of org.hisp.dhis.webapi.webdomain.WebOptions 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

WebOptions (org.hisp.dhis.webapi.webdomain.WebOptions)45 RootNode (org.hisp.dhis.node.types.RootNode)21 Test (org.junit.jupiter.api.Test)18 Pager (org.hisp.dhis.common.Pager)16 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)14 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)13 GetMapping (org.springframework.web.bind.annotation.GetMapping)13 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)12 DataItem (org.hisp.dhis.dataitem.DataItem)10 FieldFilterParams (org.hisp.dhis.fieldfilter.FieldFilterParams)10 User (org.hisp.dhis.user.User)9 WebMetadata (org.hisp.dhis.webapi.webdomain.WebMetadata)9 List (java.util.List)8 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)7 Order (org.hisp.dhis.query.Order)7 Query (org.hisp.dhis.query.Query)7 ArrayList (java.util.ArrayList)6 CollectionNode (org.hisp.dhis.node.types.CollectionNode)6 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6 HashMap (java.util.HashMap)5