Search in sources :

Example 16 with PatchMapping

use of org.springframework.web.bind.annotation.PatchMapping in project dhis2-core by dhis2.

the class AbstractCrudController method updateObjectProperty.

@PatchMapping("/{uid}/{property}")
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void updateObjectProperty(@PathVariable("uid") String pvUid, @PathVariable("property") String pvProperty, @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));
    }
    if (!getSchema().haveProperty(pvProperty)) {
        throw new WebMessageException(notFound("Property " + pvProperty + " does not exist on " + getEntityName()));
    }
    Property property = getSchema().getProperty(pvProperty);
    T persistedObject = entities.get(0);
    if (!aclService.canUpdate(currentUser, 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(badRequest("Unknown payload format."));
    }
    prePatchEntity(persistedObject);
    Object value = property.getGetterMethod().invoke(object);
    property.getSetterMethod().invoke(persistedObject, value);
    Map<String, List<String>> parameterValuesMap = contextService.getParameterValuesMap();
    MetadataImportParams params = importService.getParamsFromMap(parameterValuesMap);
    params.setUser(currentUser).setImportStrategy(ImportStrategy.UPDATE).addObject(persistedObject);
    ImportReport importReport = importService.importMetadata(params);
    if (importReport.getStatus() != Status.OK) {
        throw new WebMessageException(objectReport(importReport));
    }
    postPatchEntity(persistedObject);
}
Also used : MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) SubscribableObject(org.hisp.dhis.common.SubscribableObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) Property(org.hisp.dhis.schema.Property) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) PatchMapping(org.springframework.web.bind.annotation.PatchMapping)

Example 17 with PatchMapping

use of org.springframework.web.bind.annotation.PatchMapping 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 18 with PatchMapping

use of org.springframework.web.bind.annotation.PatchMapping in project dhis2-core by dhis2.

the class MetadataImportExportController method bulkSharing.

@ResponseBody
@PatchMapping(value = "sharing", consumes = "application/json-patch+json", produces = APPLICATION_JSON_VALUE)
public WebMessage bulkSharing(@RequestParam(required = false, defaultValue = "false") boolean atomic, HttpServletRequest request) throws IOException {
    final BulkJsonPatches bulkJsonPatches = jsonMapper.readValue(request.getInputStream(), BulkJsonPatches.class);
    BulkPatchParameters patchParams = BulkPatchParameters.builder().validators(BulkPatchValidatorFactory.SHARING).build();
    List<IdentifiableObject> patchedObjects = bulkPatchManager.applyPatches(bulkJsonPatches, patchParams);
    if (patchedObjects.isEmpty() || (atomic && !patchParams.hasErrorReports())) {
        ImportReport importReport = new ImportReport();
        importReport.addTypeReports(patchParams.getTypeReports());
        importReport.setStatus(Status.ERROR);
        return importReport(importReport);
    }
    Map<String, List<String>> parameterValuesMap = contextService.getParameterValuesMap();
    MetadataImportParams importParams = metadataImportService.getParamsFromMap(parameterValuesMap);
    importParams.setUser(currentUserService.getCurrentUser()).setImportStrategy(ImportStrategy.UPDATE).setAtomicMode(atomic ? AtomicMode.ALL : AtomicMode.NONE).addObjects(patchedObjects);
    ImportReport importReport = metadataImportService.importMetadata(importParams);
    if (patchParams.hasErrorReports()) {
        importReport.addTypeReports(patchParams.getTypeReports());
        importReport.setStatus(importReport.getStatus() == Status.OK ? Status.WARNING : importReport.getStatus());
    }
    return importReport(importReport);
}
Also used : MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) BulkPatchParameters(org.hisp.dhis.jsonpatch.BulkPatchParameters) BulkJsonPatches(org.hisp.dhis.jsonpatch.BulkJsonPatches) List(java.util.List) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) PatchMapping(org.springframework.web.bind.annotation.PatchMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

PatchMapping (org.springframework.web.bind.annotation.PatchMapping)18 List (java.util.List)5 ParaObject (com.erudika.para.core.ParaObject)4 MetadataImportParams (org.hisp.dhis.dxf2.metadata.MetadataImportParams)4 ImportReport (org.hisp.dhis.dxf2.metadata.feedback.ImportReport)4 DocumentId (de.metas.ui.web.window.datatypes.DocumentId)3 Collections.singletonList (java.util.Collections.singletonList)3 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)3 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)3 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)3 BulkJsonPatch (org.hisp.dhis.jsonpatch.BulkJsonPatch)3 WebOptions (org.hisp.dhis.webapi.webdomain.WebOptions)3 ExtendedModelMap (org.springframework.ui.ExtendedModelMap)3 Model (org.springframework.ui.Model)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 Profile (com.erudika.scoold.core.Profile)2 IDocumentChangesCollector (de.metas.ui.web.window.model.IDocumentChangesCollector)2 JsonPatch (org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch)2 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)2 BulkPatchParameters (org.hisp.dhis.jsonpatch.BulkPatchParameters)2