Search in sources :

Example 6 with PatchMapping

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

the class AbstractCrudController method bulkSharing.

@ResponseBody
@PatchMapping(path = "/sharing", consumes = "application/json-patch+json", produces = APPLICATION_JSON_VALUE)
public WebMessage bulkSharing(@RequestParam(required = false, defaultValue = "false") boolean atomic, HttpServletRequest request) throws Exception {
    final BulkJsonPatch bulkJsonPatch = jsonMapper.readValue(request.getInputStream(), BulkJsonPatch.class);
    BulkPatchParameters patchParams = BulkPatchParameters.builder().validators(BulkPatchValidatorFactory.SHARING).build();
    List<IdentifiableObject> patchedObjects = bulkPatchManager.applyPatch(bulkJsonPatch, 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 params = importService.getParamsFromMap(parameterValuesMap);
    params.setUser(currentUserService.getCurrentUser()).setImportStrategy(ImportStrategy.UPDATE).addObjects(patchedObjects);
    ImportReport importReport = importService.importMetadata(params);
    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) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) BulkJsonPatch(org.hisp.dhis.jsonpatch.BulkJsonPatch) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) PatchMapping(org.springframework.web.bind.annotation.PatchMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 7 with PatchMapping

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

use of org.springframework.web.bind.annotation.PatchMapping in project scoold by Erudika.

the class ApiController method updateTag.

@PatchMapping("/tags/{id}")
public Tag updateTag(@PathVariable String id, HttpServletRequest req, HttpServletResponse res) {
    Map<String, Object> entity = readEntity(req);
    if (entity.isEmpty()) {
        badReq("Missing request body.");
    }
    Model model = new ExtendedModelMap();
    tagsController.rename(id, (String) entity.get("tag"), req, res, model);
    if (!model.containsAttribute("tag")) {
        res.setStatus(HttpStatus.NOT_FOUND.value());
        return null;
    }
    return (Tag) model.getAttribute("tag");
}
Also used : ExtendedModelMap(org.springframework.ui.ExtendedModelMap) Model(org.springframework.ui.Model) ParaObject(com.erudika.para.core.ParaObject) Tag(com.erudika.para.core.Tag) PatchMapping(org.springframework.web.bind.annotation.PatchMapping)

Example 9 with PatchMapping

use of org.springframework.web.bind.annotation.PatchMapping in project scoold by Erudika.

the class ApiController method updateWebhook.

@PatchMapping("/webhooks/{id}")
public Webhook updateWebhook(@PathVariable String id, HttpServletRequest req, HttpServletResponse res) {
    if (!utils.isWebhooksEnabled()) {
        res.setStatus(HttpStatus.FORBIDDEN.value());
        return null;
    }
    Webhook webhook = pc.read(id);
    if (webhook == null) {
        res.setStatus(HttpStatus.NOT_FOUND.value());
        return null;
    }
    Map<String, Object> entity = readEntity(req);
    return pc.update(ParaObjectUtils.setAnnotatedFields(webhook, entity, Locked.class));
}
Also used : Locked(com.erudika.para.core.annotations.Locked) Webhook(com.erudika.para.core.Webhook) ParaObject(com.erudika.para.core.ParaObject) PatchMapping(org.springframework.web.bind.annotation.PatchMapping)

Example 10 with PatchMapping

use of org.springframework.web.bind.annotation.PatchMapping in project open-kilda by telstra.

the class LinkController method updateLinkEnableBfd.

/**
 * Update "enable bfd" flag in the link.
 *
 * @return updated link.
 */
@ApiOperation(value = "Update \"enable bfd\" flag for the link.", response = LinkDto.class, responseContainer = "List")
@PatchMapping(path = "/links/enable-bfd", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseStatus(HttpStatus.OK)
public CompletableFuture<List<LinkDto>> updateLinkEnableBfd(@RequestBody LinkEnableBfdDto link) {
    NetworkEndpoint source = makeSourceEndpoint(new SwitchId(link.getSrcSwitch()), link.getSrcPort());
    NetworkEndpoint destination = makeDestinationEndpoint(new SwitchId(link.getDstSwitch()), link.getDstPort());
    return linkService.writeBfdProperties(source, destination, link.isEnableBfd());
}
Also used : NetworkEndpoint(org.openkilda.messaging.model.NetworkEndpoint) SwitchId(org.openkilda.model.SwitchId) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ApiOperation(io.swagger.annotations.ApiOperation) PatchMapping(org.springframework.web.bind.annotation.PatchMapping)

Aggregations

PatchMapping (org.springframework.web.bind.annotation.PatchMapping)19 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 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)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