Search in sources :

Example 1 with JsonPatch

use of org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch in project dhis2-core by dhis2.

the class JsonPatchManager method apply.

/**
 * Applies a JSON patch to any valid jackson java object. It uses
 * valueToTree to convert from the object into a tree like node structure,
 * and this is where the patch will be applied. This means that any property
 * renaming etc will be followed.
 *
 * @param patch JsonPatch object with the operations it should apply.
 * @param object Jackson Object to apply the patch to.
 * @return New instance of the object with the patch applied.
 */
@Transactional
@SuppressWarnings("unchecked")
public <T> T apply(JsonPatch patch, T object) throws JsonPatchException {
    if (patch == null || object == null) {
        return null;
    }
    Schema schema = schemaService.getSchema(object.getClass());
    JsonNode node = jsonMapper.valueToTree(object);
    // since valueToTree does not properly handle our deeply nested classes,
    // we need to make another trip to make sure all collections are
    // correctly made into json nodes.
    handleCollectionUpdates(object, schema, (ObjectNode) node);
    node = patch.apply(node);
    return (T) jsonToObject(node, object.getClass(), jsonMapper, ex -> new JsonPatchException(ex.getMessage()));
}
Also used : JsonPatch(org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch) ReflectionUtils(org.hisp.dhis.system.util.ReflectionUtils) JsonUtils.jsonToObject(org.hisp.dhis.util.JsonUtils.jsonToObject) Service(org.springframework.stereotype.Service) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Schema(org.hisp.dhis.schema.Schema) SchemaService(org.hisp.dhis.schema.SchemaService) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonPatchException(org.hisp.dhis.commons.jackson.jsonpatch.JsonPatchException) Property(org.hisp.dhis.schema.Property) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Transactional(org.springframework.transaction.annotation.Transactional) JsonPatchException(org.hisp.dhis.commons.jackson.jsonpatch.JsonPatchException) Schema(org.hisp.dhis.schema.Schema) JsonNode(com.fasterxml.jackson.databind.JsonNode) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with JsonPatch

use of org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch in project dhis2-core by dhis2.

the class BulkPatchValidatorService method checkPatchObject.

/**
 * Validate given ID and {@link JsonPatch} by using
 * {@link org.hisp.dhis.jsonpatch.validator.BulkPatchValidator} from given
 * {@link BulkPatchParameters}.
 * <p>
 * The valid {@link IdentifiableObject} and {@link JsonPatch} will be added
 * to given {@link PatchBundle}
 *
 * @return {@link ObjectReport} contains {@link ErrorReport} if any.
 */
private ObjectReport checkPatchObject(Schema schema, String id, JsonPatch jsonPatch, BulkPatchParameters patchParams, PatchBundle bundle) {
    ObjectReport objectReport = new ObjectReport(schema.getKlass(), 0, id);
    IdentifiableObject entity = manager.get((Class<? extends IdentifiableObject>) schema.getKlass(), id);
    BulkPatchValidateParams bulkPatchValidateParams = BulkPatchValidateParams.builder().schema(schema).jsonPatch(jsonPatch).id(id).entity(entity).build();
    patchParams.getValidators().forEach(validator -> validator.validate(bulkPatchValidateParams, error -> objectReport.addErrorReport(error)));
    if (objectReport.hasErrorReports()) {
        return objectReport;
    }
    bundle.addEntity(id, entity, jsonPatch);
    return objectReport;
}
Also used : IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) ErrorReport(org.hisp.dhis.feedback.ErrorReport) SchemaService(org.hisp.dhis.schema.SchemaService) JsonPatchException(org.hisp.dhis.commons.jackson.jsonpatch.JsonPatchException) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) TypeReport(org.hisp.dhis.feedback.TypeReport) JsonPatch(org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch) List(java.util.List) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) Service(org.springframework.stereotype.Service) ErrorCode(org.hisp.dhis.feedback.ErrorCode) Schema(org.hisp.dhis.schema.Schema) BulkPatchValidateParams(org.hisp.dhis.jsonpatch.validator.BulkPatchValidateParams) AllArgsConstructor(lombok.AllArgsConstructor) ObjectReport(org.hisp.dhis.feedback.ObjectReport) ObjectReport(org.hisp.dhis.feedback.ObjectReport) BulkPatchValidateParams(org.hisp.dhis.jsonpatch.validator.BulkPatchValidateParams) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 3 with JsonPatch

use of org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch in project dhis2-core by dhis2.

the class BulkPatchValidatorService method validate.

/**
 * Validate {@link org.hisp.dhis.jsonpatch.BulkJsonPatch} with by using
 * validators from given {@link org.hisp.dhis.jsonpatch.BulkPatchParameters}
 *
 * @param bulkJsonPatch {@link org.hisp.dhis.jsonpatch.BulkJsonPatch} for
 *        validating
 * @param patchParameters
 *        {@link org.hisp.dhis.jsonpatch.BulkPatchParameters} contains
 *        validators
 * @return {@link org.hisp.dhis.jsonpatch.PatchBundle} contains validated
 *         {@link IdentifiableObject} and {@link JsonPatch}
 */
public PatchBundle validate(BulkJsonPatch bulkJsonPatch, BulkPatchParameters patchParameters) {
    PatchBundle bundle = new PatchBundle();
    Schema schema = schemaService.getSchemaByPluralName(bulkJsonPatch.getClassName());
    if (schema == null) {
        patchParameters.addTypeReport(createTypeReport(new ErrorReport(JsonPatchException.class, ErrorCode.E6002, bulkJsonPatch.getClassName())));
        return bundle;
    }
    List<ObjectReport> objectReports = new ArrayList<>();
    objectReports.addAll(bulkJsonPatch.getIds().stream().map(id -> checkPatchObject(schema, id, bulkJsonPatch.getPatch(), patchParameters, bundle)).filter(objectReport -> objectReport.hasErrorReports()).collect(Collectors.toList()));
    if (!objectReports.isEmpty()) {
        TypeReport typeReport = new TypeReport(schema.getKlass());
        typeReport.setObjectReports(objectReports);
        patchParameters.addTypeReport(typeReport);
    }
    return bundle;
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) ErrorReport(org.hisp.dhis.feedback.ErrorReport) SchemaService(org.hisp.dhis.schema.SchemaService) JsonPatchException(org.hisp.dhis.commons.jackson.jsonpatch.JsonPatchException) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) TypeReport(org.hisp.dhis.feedback.TypeReport) JsonPatch(org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch) List(java.util.List) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) Service(org.springframework.stereotype.Service) ErrorCode(org.hisp.dhis.feedback.ErrorCode) Schema(org.hisp.dhis.schema.Schema) BulkPatchValidateParams(org.hisp.dhis.jsonpatch.validator.BulkPatchValidateParams) AllArgsConstructor(lombok.AllArgsConstructor) ObjectReport(org.hisp.dhis.feedback.ObjectReport) TypeReport(org.hisp.dhis.feedback.TypeReport) Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList) ObjectReport(org.hisp.dhis.feedback.ObjectReport)

Example 4 with JsonPatch

use of org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch in project dhis2-core by dhis2.

the class BulkPatchValidatorService method validate.

/**
 * Validate {@link org.hisp.dhis.jsonpatch.BulkJsonPatches} with by using
 * validators from given {@link BulkPatchParameters}
 *
 * @param patches {@link org.hisp.dhis.jsonpatch.BulkJsonPatches} for
 *        validating
 * @param patchParameters {@link BulkPatchParameters} contains validators
 * @return {@link PatchBundle} contains validated {@link IdentifiableObject}
 *         and {@link JsonPatch}
 */
public PatchBundle validate(BulkJsonPatches patches, BulkPatchParameters patchParameters) {
    PatchBundle bundle = new PatchBundle();
    for (String className : patches.getClassNames()) {
        Schema schema = schemaService.getSchemaByPluralName(className);
        if (schema == null) {
            patchParameters.addTypeReport(createTypeReport(new ErrorReport(JsonPatchException.class, ErrorCode.E6002, className)));
            continue;
        }
        List<ObjectReport> objectReports = new ArrayList<>();
        objectReports.addAll(patches.get(className).entrySet().stream().map(entry -> checkPatchObject(schema, entry.getKey(), entry.getValue(), patchParameters, bundle)).filter(objectReport -> objectReport.hasErrorReports()).collect(Collectors.toList()));
        if (!objectReports.isEmpty()) {
            TypeReport typeReport = new TypeReport(schema.getKlass());
            typeReport.setObjectReports(objectReports);
            patchParameters.addTypeReport(typeReport);
        }
    }
    return bundle;
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) ErrorReport(org.hisp.dhis.feedback.ErrorReport) SchemaService(org.hisp.dhis.schema.SchemaService) JsonPatchException(org.hisp.dhis.commons.jackson.jsonpatch.JsonPatchException) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) TypeReport(org.hisp.dhis.feedback.TypeReport) JsonPatch(org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch) List(java.util.List) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) Service(org.springframework.stereotype.Service) ErrorCode(org.hisp.dhis.feedback.ErrorCode) Schema(org.hisp.dhis.schema.Schema) BulkPatchValidateParams(org.hisp.dhis.jsonpatch.validator.BulkPatchValidateParams) AllArgsConstructor(lombok.AllArgsConstructor) ObjectReport(org.hisp.dhis.feedback.ObjectReport) TypeReport(org.hisp.dhis.feedback.TypeReport) Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList) ObjectReport(org.hisp.dhis.feedback.ObjectReport)

Example 5 with JsonPatch

use of org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch in project dhis2-core by dhis2.

the class JsonPatchManagerTest method testSimpleAddPatchNoPersist.

@Test
void testSimpleAddPatchNoPersist() throws Exception {
    Constant constant = createConstant('A', 1.0d);
    assertEquals("ConstantA", constant.getName());
    assertEquals(constant.getValue(), 0, 1.0d);
    JsonPatch patch = jsonMapper.readValue("[" + "{\"op\": \"add\", \"path\": \"/name\", \"value\": \"updated\"}," + "{\"op\": \"add\", \"path\": \"/value\", \"value\": 5.0}" + "]", JsonPatch.class);
    assertNotNull(patch);
    Constant patchedConstant = jsonPatchManager.apply(patch, constant);
    assertEquals("ConstantA", constant.getName());
    assertEquals(constant.getValue(), 0, 1.0d);
    assertEquals("updated", patchedConstant.getName());
    assertEquals(patchedConstant.getValue(), 0, 5.0d);
}
Also used : Constant(org.hisp.dhis.constant.Constant) JsonPatch(org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch) Test(org.junit.jupiter.api.Test)

Aggregations

JsonPatch (org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch)26 Test (org.junit.jupiter.api.Test)20 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)10 JsonNode (com.fasterxml.jackson.databind.JsonNode)8 JsonPatchException (org.hisp.dhis.commons.jackson.jsonpatch.JsonPatchException)5 List (java.util.List)4 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)4 Schema (org.hisp.dhis.schema.Schema)4 SchemaService (org.hisp.dhis.schema.SchemaService)4 Service (org.springframework.stereotype.Service)4 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)3 ArrayList (java.util.ArrayList)3 Collectors (java.util.stream.Collectors)3 AllArgsConstructor (lombok.AllArgsConstructor)3 IdentifiableObjectManager (org.hisp.dhis.common.IdentifiableObjectManager)3 DataElement (org.hisp.dhis.dataelement.DataElement)3 ErrorCode (org.hisp.dhis.feedback.ErrorCode)3 ErrorReport (org.hisp.dhis.feedback.ErrorReport)3 ObjectReport (org.hisp.dhis.feedback.ObjectReport)3 TypeReport (org.hisp.dhis.feedback.TypeReport)3