Search in sources :

Example 51 with UID

use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.UID in project dhis2-core by dhis2.

the class AbstractFullReadOnlyController method getObjectProperty.

@GetMapping("/{uid}/{property}")
@ResponseBody
public RootNode getObjectProperty(@PathVariable("uid") String pvUid, @PathVariable("property") String pvProperty, @RequestParam Map<String, String> rpParameters, TranslateParams translateParams, @CurrentUser User currentUser, HttpServletResponse response) throws Exception {
    if (!"translations".equals(pvProperty)) {
        setUserContext(currentUser, translateParams);
    } else {
        setUserContext(null, new TranslateParams(false));
    }
    try {
        if (!aclService.canRead(currentUser, getEntityClass())) {
            throw new ReadAccessDeniedException("You don't have the proper permissions to read objects of this type.");
        }
        List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
        if (fields.isEmpty()) {
            fields.add(":all");
        }
        String fieldFilter = "[" + Joiner.on(',').join(fields) + "]";
        cachePrivate(response);
        return getObjectInternal(pvUid, rpParameters, Lists.newArrayList(), Lists.newArrayList(pvProperty + fieldFilter), currentUser);
    } finally {
        UserContext.reset();
    }
}
Also used : TranslateParams(org.hisp.dhis.dxf2.common.TranslateParams) ReadAccessDeniedException(org.hisp.dhis.hibernate.exception.ReadAccessDeniedException) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 52 with UID

use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.UID in project dhis2-core by dhis2.

the class AbstractFullReadOnlyController method getObjectInternal.

@SuppressWarnings("unchecked")
private RootNode getObjectInternal(String uid, Map<String, String> parameters, List<String> filters, List<String> fields, User currentUser) throws Exception {
    WebOptions options = new WebOptions(parameters);
    List<T> entities = getEntity(uid, options);
    if (entities.isEmpty()) {
        throw new WebMessageException(notFound(getEntityClass(), uid));
    }
    Query query = queryService.getQueryFromUrl(getEntityClass(), filters, new ArrayList<>(), getPaginationData(options), options.getRootJunction());
    query.setUser(currentUser);
    query.setObjects(entities);
    query.setDefaults(Defaults.valueOf(options.get("defaults", DEFAULTS)));
    entities = (List<T>) queryService.query(query);
    handleLinksAndAccess(entities, fields, true);
    handleAttributeValues(entities, fields);
    for (T entity : entities) {
        postProcessResponseEntity(entity, options, parameters);
    }
    CollectionNode collectionNode = fieldFilterService.toCollectionNode(getEntityClass(), new FieldFilterParams(entities, fields, Defaults.valueOf(options.get("defaults", DEFAULTS))).setUser(currentUser));
    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) 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) FieldFilterParams(org.hisp.dhis.fieldfilter.FieldFilterParams) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) CollectionNode(org.hisp.dhis.node.types.CollectionNode)

Example 53 with UID

use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.UID in project dhis2-core by dhis2.

the class AbstractCrudController method deleteObject.

// --------------------------------------------------------------------------
// DELETE
// --------------------------------------------------------------------------
@DeleteMapping(value = "/{uid}")
@ResponseBody
public WebMessage deleteObject(@PathVariable("uid") String pvUid, @CurrentUser User currentUser, HttpServletRequest request, HttpServletResponse response) throws Exception {
    List<T> objects = getEntity(pvUid);
    if (objects.isEmpty()) {
        return notFound(getEntityClass(), pvUid);
    }
    if (!aclService.canDelete(currentUser, objects.get(0))) {
        throw new DeleteAccessDeniedException("You don't have the proper permissions to delete this object.");
    }
    preDeleteEntity(objects.get(0));
    MetadataImportParams params = new MetadataImportParams().setImportReportMode(ImportReportMode.FULL).setUser(currentUser).setImportStrategy(ImportStrategy.DELETE).addObject(objects.get(0));
    ImportReport importReport = importService.importMetadata(params);
    postDeleteEntity(pvUid);
    return objectReport(importReport);
}
Also used : MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) DeleteAccessDeniedException(org.hisp.dhis.hibernate.exception.DeleteAccessDeniedException) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 54 with UID

use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.UID in project dhis2-core by dhis2.

the class AbstractCrudController method patchObject.

// --------------------------------------------------------------------------
// PATCH
// --------------------------------------------------------------------------
/**
 * Adds support for HTTP Patch using JSON Patch (RFC 6902), updated object
 * is run through normal metadata importer and internally looks like a
 * normal PUT (after the JSON Patch has been applied).
 *
 * For now we only support the official mimetype
 * "application/json-patch+json" but in future releases we might also want
 * to support "application/json" after the old patch behavior has been
 * removed.
 */
@ResponseBody
@PatchMapping(path = "/{uid}", consumes = "application/json-patch+json")
public WebMessage patchObject(@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()) {
        return notFound(getEntityClass(), pvUid);
    }
    final T persistedObject = entities.get(0);
    if (!aclService.canUpdate(currentUser, persistedObject)) {
        throw new UpdateAccessDeniedException("You don't have the proper permissions to update this object.");
    }
    manager.resetNonOwnerProperties(persistedObject);
    prePatchEntity(persistedObject);
    final JsonPatch patch = jsonMapper.readValue(request.getInputStream(), JsonPatch.class);
    final T patchedObject = jsonPatchManager.apply(patch, persistedObject);
    // we don't allow changing IDs
    ((BaseIdentifiableObject) patchedObject).setId(persistedObject.getId());
    // we don't allow changing UIDs
    ((BaseIdentifiableObject) patchedObject).setUid(persistedObject.getUid());
    // Only supports new Sharing format
    ((BaseIdentifiableObject) patchedObject).clearLegacySharingCollections();
    prePatchEntity(persistedObject, patchedObject);
    Map<String, List<String>> parameterValuesMap = contextService.getParameterValuesMap();
    if (!parameterValuesMap.containsKey("importReportMode")) {
        parameterValuesMap.put("importReportMode", Collections.singletonList("ERRORS_NOT_OWNER"));
    }
    MetadataImportParams params = importService.getParamsFromMap(parameterValuesMap);
    params.setUser(currentUser).setImportStrategy(ImportStrategy.UPDATE).addObject(patchedObject);
    ImportReport importReport = importService.importMetadata(params);
    WebMessage webMessage = objectReport(importReport);
    if (importReport.getStatus() == Status.OK) {
        T entity = manager.get(getEntityClass(), pvUid);
        postPatchEntity(entity);
    } else {
        webMessage.setStatus(Status.ERROR);
    }
    return webMessage;
}
Also used : BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) BulkJsonPatch(org.hisp.dhis.jsonpatch.BulkJsonPatch) JsonPatch(org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) PatchMapping(org.springframework.web.bind.annotation.PatchMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 55 with UID

use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.UID in project dhis2-core by dhis2.

the class OrganisationUnitSupplierTest method verifyCodeSchemaForOu.

@Test
void verifyCodeSchemaForOu() throws SQLException {
    // mock resultset data
    when(mockResultSet.getLong("organisationunitid")).thenReturn(100L);
    when(mockResultSet.getString("uid")).thenReturn("abcded");
    when(mockResultSet.getString("code")).thenReturn("CODE1");
    when(mockResultSet.getString("path")).thenReturn("/abcded");
    when(mockResultSet.getInt("hierarchylevel")).thenReturn(1);
    // create event to import
    Event event = new Event();
    event.setUid(CodeGenerator.generateUid());
    event.setOrgUnit("CODE1");
    // mock resultset extraction
    mockResultSetExtractor(mockResultSet);
    ImportOptions importOptions = ImportOptions.getDefaultImportOptions();
    importOptions.setOrgUnitIdScheme(IdScheme.CODE.name());
    Map<String, OrganisationUnit> map = subject.get(importOptions, Collections.singletonList(event));
    OrganisationUnit organisationUnit = map.get(event.getUid());
    assertThat(organisationUnit, is(notNullValue()));
    assertThat(organisationUnit.getId(), is(100L));
    assertThat(organisationUnit.getUid(), is("abcded"));
    assertThat(organisationUnit.getCode(), is("CODE1"));
    assertThat(organisationUnit.getPath(), is("/abcded"));
    assertThat(organisationUnit.getHierarchyLevel(), is(1));
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Event(org.hisp.dhis.dxf2.events.event.Event) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) Test(org.junit.jupiter.api.Test)

Aggregations

WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)92 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)52 Event (org.hisp.dhis.dxf2.events.event.Event)39 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)37 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)34 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)29 GetMapping (org.springframework.web.bind.annotation.GetMapping)28 User (org.hisp.dhis.user.User)23 Test (org.junit.jupiter.api.Test)21 HashMap (java.util.HashMap)19 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)19 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)19 InputStream (java.io.InputStream)18 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)18 ArrayList (java.util.ArrayList)17 MetadataImportParams (org.hisp.dhis.dxf2.metadata.MetadataImportParams)17 List (java.util.List)16 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)16 DataElement (org.hisp.dhis.dataelement.DataElement)15 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)15