Search in sources :

Example 36 with RootNode

use of org.hisp.dhis.node.types.RootNode in project dhis2-core by dhis2.

the class EventController method getEvents.

// -------------------------------------------------------------------------
// Object Read
// -------------------------------------------------------------------------
@GetMapping
@ResponseBody
public RootNode getEvents(EventCriteria eventCriteria, @RequestParam Map<String, String> parameters, Model model, HttpServletResponse response, HttpServletRequest request) {
    WebOptions options = new WebOptions(parameters);
    List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
    if (fields.isEmpty()) {
        fields.add("event,uid,program,programType,status,assignedUser,orgUnit,orgUnitName,eventDate,orgUnit,orgUnitName,created,lastUpdated,followup");
    }
    EventSearchParams params = requestToSearchParamsMapper.map(eventCriteria);
    setParamBasedOnFieldParameters(params, fields);
    Events events = eventService.getEvents(params);
    if (hasHref(fields, eventCriteria.getSkipEventId())) {
        events.getEvents().forEach(e -> e.setHref(ContextUtils.getRootPath(request) + RESOURCE_PATH + "/" + e.getEvent()));
    }
    if (!eventCriteria.isSkipMeta() && params.getProgram() != null) {
        events.setMetaData(getMetaData(params.getProgram()));
    }
    model.addAttribute("model", events);
    model.addAttribute("viewClass", options.getViewClass("detailed"));
    RootNode rootNode = NodeUtils.createMetadata();
    addPager(params, events, rootNode);
    if (!StringUtils.isEmpty(eventCriteria.getAttachment())) {
        response.addHeader(ContextUtils.HEADER_CONTENT_DISPOSITION, "attachment; filename=" + eventCriteria.getAttachment());
        response.addHeader(ContextUtils.HEADER_CONTENT_TRANSFER_ENCODING, "binary");
    }
    rootNode.addChild(fieldFilterService.toCollectionNode(Event.class, new FieldFilterParams(events.getEvents(), fields)));
    return rootNode;
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) Events(org.hisp.dhis.dxf2.events.event.Events) EventSearchParams(org.hisp.dhis.dxf2.events.event.EventSearchParams) Event(org.hisp.dhis.dxf2.events.event.Event) FieldFilterParams(org.hisp.dhis.fieldfilter.FieldFilterParams) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 37 with RootNode

use of org.hisp.dhis.node.types.RootNode in project dhis2-core by dhis2.

the class DimensionController method getRecommendedDimensions.

@GetMapping("/recommendations")
@ResponseBody
public RootNode getRecommendedDimensions(@RequestParam Set<String> dimension) {
    List<String> fields = newArrayList(contextService.getParameterValues("fields"));
    DataQueryRequest request = DataQueryRequest.newBuilder().dimension(dimension).build();
    if (fields.isEmpty()) {
        fields.addAll(Preset.defaultPreset().getFields());
    }
    List<DimensionalObject> dimensions = analyticsDimensionService.getRecommendedDimensions(request);
    RootNode rootNode = NodeUtils.createMetadata();
    rootNode.addChild(fieldFilterService.toCollectionNode(getEntityClass(), new FieldFilterParams(dimensions, fields)));
    return rootNode;
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) FieldFilterParams(org.hisp.dhis.fieldfilter.FieldFilterParams) DataQueryRequest(org.hisp.dhis.common.DataQueryRequest) DimensionalObject(org.hisp.dhis.common.DimensionalObject) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 38 with RootNode

use of org.hisp.dhis.node.types.RootNode in project dhis2-core by dhis2.

the class DimensionController method getDimensionConstraints.

@GetMapping("/constraints")
@ResponseBody
public RootNode getDimensionConstraints(@RequestParam(value = "links", defaultValue = "true", required = false) Boolean links) {
    List<String> fields = newArrayList(contextService.getParameterValues("fields"));
    List<DimensionalObject> dimensionConstraints = dimensionService.getDimensionConstraints();
    if (links) {
        linkService.generateLinks(dimensionConstraints, false);
    }
    RootNode rootNode = NodeUtils.createMetadata();
    rootNode.addChild(fieldFilterService.toCollectionNode(getEntityClass(), new FieldFilterParams(dimensionConstraints, fields)));
    return rootNode;
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) FieldFilterParams(org.hisp.dhis.fieldfilter.FieldFilterParams) DimensionalObject(org.hisp.dhis.common.DimensionalObject) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 39 with RootNode

use of org.hisp.dhis.node.types.RootNode in project dhis2-core by dhis2.

the class ResponseHandler method addPaginationToNode.

/**
 * This method takes care of the pagination link and their respective
 * attributes. It will count the number of results available and base on the
 * WebOptions will calculate the pagination output.
 *
 * @param rootNode the node where the the pagination will be attached to
 * @param targetEntities the list of classes which requires pagination
 * @param currentUser the current logged user
 * @param options holds the pagination definitions
 * @param filters the query filters used in the count query
 */
void addPaginationToNode(final RootNode rootNode, final Set<Class<? extends BaseIdentifiableObject>> targetEntities, final User currentUser, final WebOptions options, final Set<String> filters) {
    if (options.hasPaging() && isNotEmpty(targetEntities)) {
        // Defining query params map and setting common params.
        final MapSqlParameterSource paramsMap = new MapSqlParameterSource().addValue(QueryParam.USER_UID, currentUser.getUid());
        setFilteringParams(filters, options, paramsMap, currentUser);
        final AtomicLong count = new AtomicLong();
        // Counting and summing up the results for each entity.
        count.addAndGet(pageCountingCache.get(createPageCountingCacheKey(currentUser, targetEntities, filters, options), p -> countEntityRowsTotal(targetEntities, options, paramsMap)));
        final Pager pager = new Pager(options.getPage(), count.get(), options.getPageSize());
        linkService.generatePagerLinks(pager, API_RESOURCE_PATH);
        rootNode.addChild(createPager(pager));
    }
}
Also used : WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) FilteringHelper.setFilteringParams(org.hisp.dhis.webapi.controller.dataitem.helper.FilteringHelper.setFilteringParams) CollectionNode(org.hisp.dhis.node.types.CollectionNode) QueryExecutor(org.hisp.dhis.dataitem.query.QueryExecutor) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) DXF_2_0(org.hisp.dhis.common.DxfNamespaces.DXF_2_0) String.join(java.lang.String.join) User(org.hisp.dhis.user.User) LinkService(org.hisp.dhis.webapi.service.LinkService) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) Pager(org.hisp.dhis.common.Pager) FieldFilterService(org.hisp.dhis.fieldfilter.FieldFilterService) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) DataItem(org.hisp.dhis.dataitem.DataItem) Set(java.util.Set) CacheProvider(org.hisp.dhis.cache.CacheProvider) QueryParam(org.hisp.dhis.dataitem.query.shared.QueryParam) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Component(org.springframework.stereotype.Component) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) CollectionUtils.isNotEmpty(org.apache.commons.collections4.CollectionUtils.isNotEmpty) API_RESOURCE_PATH(org.hisp.dhis.webapi.controller.dataitem.DataItemQueryController.API_RESOURCE_PATH) FieldFilterParams(org.hisp.dhis.fieldfilter.FieldFilterParams) NodeUtils.createPager(org.hisp.dhis.node.NodeUtils.createPager) Cache(org.hisp.dhis.cache.Cache) RootNode(org.hisp.dhis.node.types.RootNode) AtomicLong(java.util.concurrent.atomic.AtomicLong) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) Pager(org.hisp.dhis.common.Pager) NodeUtils.createPager(org.hisp.dhis.node.NodeUtils.createPager)

Example 40 with RootNode

use of org.hisp.dhis.node.types.RootNode in project dhis2-core by dhis2.

the class AbstractFullReadOnlyController method getObjectList.

// --------------------------------------------------------------------------
// GET Full
// --------------------------------------------------------------------------
@GetMapping
@ResponseBody
public RootNode getObjectList(@RequestParam Map<String, String> rpParameters, OrderParams orderParams, HttpServletResponse response, @CurrentUser User currentUser) throws QueryParserException {
    List<Order> orders = orderParams.getOrders(getSchema());
    List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
    List<String> filters = Lists.newArrayList(contextService.getParameterValues("filter"));
    forceFiltering(filters);
    if (fields.isEmpty()) {
        fields.addAll(Preset.defaultPreset().getFields());
    }
    WebOptions options = new WebOptions(rpParameters);
    WebMetadata metadata = new WebMetadata();
    if (!aclService.canRead(currentUser, getEntityClass())) {
        throw new ReadAccessDeniedException("You don't have the proper permissions to read objects of this type.");
    }
    List<T> entities = getEntityList(metadata, options, filters, orders);
    Pager pager = metadata.getPager();
    if (options.hasPaging() && pager == null) {
        long totalCount;
        if (options.getOptions().containsKey("query")) {
            totalCount = entities.size();
            long skip = (long) (options.getPage() - 1) * options.getPageSize();
            entities = entities.stream().skip(skip).limit(options.getPageSize()).collect(toList());
        } else {
            totalCount = countTotal(options, filters, orders);
        }
        pager = new Pager(options.getPage(), totalCount, options.getPageSize());
    }
    postProcessResponseEntities(entities, options, rpParameters);
    handleLinksAndAccess(entities, fields, false);
    handleAttributeValues(entities, fields);
    linkService.generatePagerLinks(pager, getEntityClass());
    RootNode rootNode = NodeUtils.createMetadata();
    rootNode.getConfig().setInclusionStrategy(getInclusionStrategy(rpParameters.get("inclusionStrategy")));
    if (pager != null) {
        rootNode.addChild(NodeUtils.createPager(pager));
    }
    FieldFilterParams defaults1 = new FieldFilterParams(entities, fields, Defaults.valueOf(options.get("defaults", DEFAULTS)));
    CollectionNode defaults = fieldFilterService.toCollectionNode(getEntityClass(), defaults1);
    rootNode.addChild(defaults);
    cachePrivate(response);
    return rootNode;
}
Also used : Order(org.hisp.dhis.query.Order) RootNode(org.hisp.dhis.node.types.RootNode) ReadAccessDeniedException(org.hisp.dhis.hibernate.exception.ReadAccessDeniedException) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) CollectionNode(org.hisp.dhis.node.types.CollectionNode) WebMetadata(org.hisp.dhis.webapi.webdomain.WebMetadata) 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)

Aggregations

RootNode (org.hisp.dhis.node.types.RootNode)112 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)59 CollectionNode (org.hisp.dhis.node.types.CollectionNode)50 SimpleNode (org.hisp.dhis.node.types.SimpleNode)40 FieldFilterParams (org.hisp.dhis.fieldfilter.FieldFilterParams)30 GetMapping (org.springframework.web.bind.annotation.GetMapping)29 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)27 Pager (org.hisp.dhis.common.Pager)26 User (org.hisp.dhis.user.User)25 WebOptions (org.hisp.dhis.webapi.webdomain.WebOptions)24 ComplexNode (org.hisp.dhis.node.types.ComplexNode)20 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)17 Test (org.junit.jupiter.api.Test)16 ArrayList (java.util.ArrayList)15 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)13 Node (org.hisp.dhis.node.Node)11 WebMetadata (org.hisp.dhis.webapi.webdomain.WebMetadata)10 List (java.util.List)9 Query (org.hisp.dhis.query.Query)9 DataSet (org.hisp.dhis.dataset.DataSet)7