Search in sources :

Example 11 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)

Example 12 with WebOptions

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

the class AbstractCrudController method updateObjectProperty.

@RequestMapping(value = "/{uid}/{property}", method = { RequestMethod.PUT, RequestMethod.PATCH })
public void updateObjectProperty(@PathVariable("uid") String pvUid, @PathVariable("property") String pvProperty, @RequestParam Map<String, String> rpParameters, HttpServletRequest request, HttpServletResponse response) throws Exception {
    WebOptions options = new WebOptions(rpParameters);
    List<T> entities = getEntity(pvUid, options);
    if (entities.isEmpty()) {
        throw new WebMessageException(WebMessageUtils.notFound(getEntityClass(), pvUid));
    }
    if (!getSchema().haveProperty(pvProperty)) {
        throw new WebMessageException(WebMessageUtils.notFound("Property " + pvProperty + " does not exist on " + getEntityName()));
    }
    Property property = getSchema().getProperty(pvProperty);
    T persistedObject = entities.get(0);
    if (!aclService.canUpdate(currentUserService.getCurrentUser(), persistedObject)) {
        throw new UpdateAccessDeniedException("You don't have the proper permissions to update this object.");
    }
    if (!property.isWritable()) {
        throw new UpdateAccessDeniedException("This property is read-only.");
    }
    T object = deserialize(request);
    if (object == null) {
        throw new WebMessageException(WebMessageUtils.badRequest("Unknown payload format."));
    }
    Object value = property.getGetterMethod().invoke(object);
    property.getSetterMethod().invoke(persistedObject, value);
    manager.update(persistedObject);
    postPatchEntity(persistedObject);
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) Property(org.hisp.dhis.schema.Property) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 13 with WebOptions

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

the class GeoFeatureController method getGeoFeaturesJson.

// -------------------------------------------------------------------------
// Resources
// -------------------------------------------------------------------------
@RequestMapping(method = RequestMethod.GET, produces = { ContextUtils.CONTENT_TYPE_JSON, ContextUtils.CONTENT_TYPE_HTML })
public void getGeoFeaturesJson(@RequestParam String ou, @RequestParam(required = false) DisplayProperty displayProperty, @RequestParam(required = false) Date relativePeriodDate, @RequestParam(required = false) String userOrgUnit, @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 = getGeoFeatures(ou, displayProperty, relativePeriodDate, userOrgUnit, request, response, includeGroupSets, apiVersion);
    if (features == null) {
        return;
    }
    ContextUtils.setCacheControl(response, GEOFEATURE_CACHE);
    response.setContentType(MediaType.APPLICATION_JSON_VALUE);
    renderService.toJson(response.getOutputStream(), features);
}
Also used : WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) GeoFeature(org.hisp.dhis.webapi.webdomain.GeoFeature) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

WebOptions (org.hisp.dhis.webapi.webdomain.WebOptions)13 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)10 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)8 Pager (org.hisp.dhis.common.Pager)6 RootNode (org.hisp.dhis.node.types.RootNode)6 WebMetadata (org.hisp.dhis.webapi.webdomain.WebMetadata)6 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)5 ArrayList (java.util.ArrayList)3 DataElementGroup (org.hisp.dhis.dataelement.DataElementGroup)3 DataElementOperand (org.hisp.dhis.dataelement.DataElementOperand)3 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)3 Order (org.hisp.dhis.query.Order)3 Query (org.hisp.dhis.query.Query)3 Schema (org.hisp.dhis.schema.Schema)3 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)2 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)2 Property (org.hisp.dhis.schema.Property)2 User (org.hisp.dhis.user.User)2 GeoFeature (org.hisp.dhis.webapi.webdomain.GeoFeature)2 GetMapping (org.springframework.web.bind.annotation.GetMapping)2