Search in sources :

Example 31 with WebOptions

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

the class AbstractCrudController method partialUpdateObject.

// --------------------------------------------------------------------------
// OLD PATCH
// --------------------------------------------------------------------------
@PatchMapping(value = "/{uid}")
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void partialUpdateObject(@PathVariable("uid") String pvUid, @RequestParam Map<String, String> rpParameters, @CurrentUser User currentUser, HttpServletRequest request) throws Exception {
    WebOptions options = new WebOptions(rpParameters);
    List<T> entities = getEntity(pvUid, options);
    if (entities.isEmpty()) {
        throw new WebMessageException(notFound(getEntityClass(), pvUid));
    }
    T persistedObject = entities.get(0);
    if (!aclService.canUpdate(currentUser, persistedObject)) {
        throw new UpdateAccessDeniedException("You don't have the proper permissions to update this object.");
    }
    Patch patch = diff(request);
    prePatchEntity(persistedObject);
    patchService.apply(patch, persistedObject);
    validateAndThrowErrors(() -> schemaValidator.validate(persistedObject));
    manager.update(persistedObject);
    postPatchEntity(persistedObject);
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) BulkJsonPatch(org.hisp.dhis.jsonpatch.BulkJsonPatch) Patch(org.hisp.dhis.patch.Patch) JsonPatch(org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) PatchMapping(org.springframework.web.bind.annotation.PatchMapping)

Example 32 with WebOptions

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

the class LockExceptionController method getLockExceptions.

// -------------------------------------------------------------------------
// Resources
// -------------------------------------------------------------------------
@GetMapping(produces = ContextUtils.CONTENT_TYPE_JSON)
@ResponseBody
public RootNode getLockExceptions(@RequestParam(required = false) String key, @RequestParam Map<String, String> rpParameters, HttpServletRequest request, HttpServletResponse response) throws WebMessageException {
    List<String> filters = Lists.newArrayList(contextService.getParameterValues("filter"));
    List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
    if (fields.isEmpty()) {
        fields.addAll(Preset.ALL.getFields());
    }
    List<LockException> lockExceptions = new ArrayList<>();
    if (key != null) {
        LockException lockException = dataSetService.getLockException(MathUtils.parseInt(key));
        if (lockException == null) {
            throw new WebMessageException(notFound("Cannot find LockException with key: " + key));
        }
        lockExceptions.add(lockException);
    } else if (!filters.isEmpty()) {
        lockExceptions = dataSetService.filterLockExceptions(filters);
    } else {
        lockExceptions = dataSetService.getAllLockExceptions();
    }
    WebOptions options = new WebOptions(rpParameters);
    WebMetadata metadata = new WebMetadata();
    Pager pager = metadata.getPager();
    if (options.hasPaging() && pager == null) {
        pager = new Pager(options.getPage(), lockExceptions.size(), options.getPageSize());
        lockExceptions = PagerUtils.pageCollection(lockExceptions, pager);
    }
    RootNode rootNode = NodeUtils.createMetadata();
    if (pager != null) {
        rootNode.addChild(NodeUtils.createPager(pager));
    }
    I18nFormat format = this.i18nManager.getI18nFormat();
    for (LockException lockException : lockExceptions) {
        lockException.getPeriod().setName(format.formatPeriod(lockException.getPeriod()));
    }
    rootNode.addChild(fieldFilterService.toCollectionNode(LockException.class, new FieldFilterParams(lockExceptions, fields)));
    return rootNode;
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) LockException(org.hisp.dhis.dataset.LockException) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) Pager(org.hisp.dhis.common.Pager) ArrayList(java.util.ArrayList) FieldFilterParams(org.hisp.dhis.fieldfilter.FieldFilterParams) I18nFormat(org.hisp.dhis.i18n.I18nFormat) 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 33 with WebOptions

use of org.hisp.dhis.webapi.webdomain.WebOptions 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;
}
Also used : Order(org.hisp.dhis.query.Order) RootNode(org.hisp.dhis.node.types.RootNode) Query(org.hisp.dhis.query.Query) AbstractNode(org.hisp.dhis.node.AbstractNode) CollectionNode(org.hisp.dhis.node.types.CollectionNode) AbstractNode(org.hisp.dhis.node.AbstractNode) Node(org.hisp.dhis.node.Node) RootNode(org.hisp.dhis.node.types.RootNode) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) CollectionNode(org.hisp.dhis.node.types.CollectionNode) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) FieldFilterParams(org.hisp.dhis.fieldfilter.FieldFilterParams) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 34 with WebOptions

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

the class DataItemQueryController method getDimensionalItems.

/**
 * Based on the informed arguments, this method will read the URL and based
 * on the give params will retrieve the respective data items.
 *
 * @param currentUser the logged user
 * @param urlParameters the request url params
 * @param orderParams the request order params
 * @return the complete root node
 */
private ResponseEntity<RootNode> getDimensionalItems(final User currentUser, final Map<String, String> urlParameters, final OrderParams orderParams) {
    // Defining the input params.
    final Set<String> fields = newHashSet(contextService.getParameterValues(FIELDS));
    final Set<String> filters = newHashSet(contextService.getParameterValues(FILTER));
    final WebOptions options = new WebOptions(urlParameters);
    if (fields.isEmpty()) {
        fields.addAll(Preset.ALL.getFields());
    }
    checkNamesAndOperators(filters);
    checkOrderParams(orderParams.getOrders());
    // Extracting the target entities to be queried.
    final Set<Class<? extends BaseIdentifiableObject>> targetEntities = dataItemServiceFacade.extractTargetEntities(filters);
    // Checking if the user can read all the target entities.
    checkAuthorization(currentUser, targetEntities);
    // Retrieving the data items based on the input params.
    final List<DataItem> dimensionalItems = dataItemServiceFacade.retrieveDataItemEntities(targetEntities, filters, options, orderParams);
    // Creating the response node.
    final RootNode rootNode = createMetadata();
    responseHandler.addResultsToNode(rootNode, dimensionalItems, fields);
    responseHandler.addPaginationToNode(rootNode, targetEntities, currentUser, options, filters);
    return new ResponseEntity<>(rootNode, OK);
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) ResponseEntity(org.springframework.http.ResponseEntity) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) DataItem(org.hisp.dhis.dataitem.DataItem) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions)

Example 35 with WebOptions

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

the class DataElementGroupController method getOperandsByQuery.

@GetMapping("/{uid}/operands/query/{q}")
public String getOperandsByQuery(@PathVariable("uid") String uid, @PathVariable("q") String q, @RequestParam Map<String, String> parameters, TranslateParams translateParams, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
    WebOptions options = new WebOptions(parameters);
    setUserContext(translateParams);
    List<DataElementGroup> dataElementGroups = getEntity(uid, NO_WEB_OPTIONS);
    if (dataElementGroups.isEmpty()) {
        throw new WebMessageException(notFound("DataElementGroup not found for uid: " + uid));
    }
    WebMetadata metadata = new WebMetadata();
    List<DataElementOperand> dataElementOperands = Lists.newArrayList();
    for (DataElementOperand dataElementOperand : dataElementCategoryService.getOperands(dataElementGroups.get(0).getMembers())) {
        if (dataElementOperand.getDisplayName().toLowerCase().contains(q.toLowerCase())) {
            dataElementOperands.add(dataElementOperand);
        }
    }
    metadata.setDataElementOperands(dataElementOperands);
    if (options.hasPaging()) {
        Pager pager = new Pager(options.getPage(), dataElementOperands.size(), options.getPageSize());
        metadata.setPager(pager);
        dataElementOperands = PagerUtils.pageCollection(dataElementOperands, pager);
    }
    metadata.setDataElementOperands(dataElementOperands);
    linkService.generateLinks(metadata, false);
    model.addAttribute("model", metadata);
    model.addAttribute("viewClass", options.getViewClass("basic"));
    return StringUtils.uncapitalize(getEntitySimpleName());
}
Also used : DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) Pager(org.hisp.dhis.common.Pager) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) WebMetadata(org.hisp.dhis.webapi.webdomain.WebMetadata) GetMapping(org.springframework.web.bind.annotation.GetMapping)

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