Search in sources :

Example 1 with BulkJsonPatches

use of org.hisp.dhis.jsonpatch.BulkJsonPatches in project dhis2-core by dhis2.

the class BulkPatchManagerTest method testApplyPatchesOk.

@Test
void testApplyPatchesOk() throws IOException {
    final BulkJsonPatches bulkJsonPatch = loadPatch("bulk_sharing_patches.json", BulkJsonPatches.class);
    BulkPatchParameters patchParameters = BulkPatchParameters.builder().validators(BulkPatchValidatorFactory.SHARING).build();
    List<IdentifiableObject> patchedObjects = patchManager.applyPatches(bulkJsonPatch, patchParameters);
    assertEquals(3, patchedObjects.size());
    assertFalse(aclService.canRead(userA, dataSetA));
    assertFalse(aclService.canRead(userB, dataSetA));
    IdentifiableObject patchedDataElementA = patchedObjects.stream().filter(de -> de.getUid().equals(dataElementA.getUid())).findFirst().get();
    IdentifiableObject patchedDataElementB = patchedObjects.stream().filter(de -> de.getUid().equals(dataElementB.getUid())).findFirst().get();
    IdentifiableObject patchedDataSetA = patchedObjects.stream().filter(de -> de.getUid().equals(dataSetA.getUid())).findFirst().get();
    assertTrue(aclService.canRead(userA, patchedDataElementA));
    assertTrue(aclService.canRead(userB, patchedDataElementB));
    assertTrue(aclService.canRead(userA, patchedDataSetA));
    assertTrue(aclService.canRead(userB, patchedDataSetA));
    assertFalse(aclService.canRead(userC, patchedDataElementA));
    assertFalse(aclService.canRead(userC, patchedDataSetA));
}
Also used : BulkPatchParameters(org.hisp.dhis.jsonpatch.BulkPatchParameters) BulkJsonPatches(org.hisp.dhis.jsonpatch.BulkJsonPatches) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 2 with BulkJsonPatches

use of org.hisp.dhis.jsonpatch.BulkJsonPatches 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

IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)2 BulkJsonPatches (org.hisp.dhis.jsonpatch.BulkJsonPatches)2 BulkPatchParameters (org.hisp.dhis.jsonpatch.BulkPatchParameters)2 List (java.util.List)1 DhisSpringTest (org.hisp.dhis.DhisSpringTest)1 MetadataImportParams (org.hisp.dhis.dxf2.metadata.MetadataImportParams)1 ImportReport (org.hisp.dhis.dxf2.metadata.feedback.ImportReport)1 Test (org.junit.jupiter.api.Test)1 PatchMapping (org.springframework.web.bind.annotation.PatchMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1