Search in sources :

Example 1 with JsonPatchException

use of org.hisp.dhis.commons.jackson.jsonpatch.JsonPatchException 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 JsonPatchException

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

the class BulkPatchManager method applySafely.

/**
 * Try to apply given {@link JsonPatch} to given entity by calling
 * {@link JsonPatchManager#apply}.
 * <p>
 * If there is an error, add it to the given {@link ObjectReport} list and
 * return {@link Optional#empty()}.
 */
private Optional<IdentifiableObject> applySafely(JsonPatch patch, IdentifiableObject entity, BulkPatchParameters parameters, Consumer<Optional<IdentifiableObject>> entityConsumer) {
    try {
        Optional<IdentifiableObject> patched = Optional.ofNullable(jsonPatchManager.apply(patch, entity));
        entityConsumer.accept(patched);
        return patched;
    } catch (JsonPatchException e) {
        parameters.addTypeReport(createTypeReport(entity.getClass(), entity.getUid(), new ErrorReport(entity.getClass(), ErrorCode.E6003, e.getMessage())));
        return Optional.empty();
    }
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) JsonPatchException(org.hisp.dhis.commons.jackson.jsonpatch.JsonPatchException) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject)

Example 3 with JsonPatchException

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

the class DefaultMetadataWorkflowService method acceptUpdate.

private ImportReport acceptUpdate(MetadataProposal proposal, ObjectBundleMode mode) {
    JsonPatch patch = mapJsonChangeToObject(proposal.getChange(), JsonPatch.class);
    Class<? extends IdentifiableObject> objType = proposal.getTarget().getType();
    if (patch == null) {
        return createJsonErrorReport(proposal);
    }
    IdentifiableObject patched = null;
    try {
        patched = patchManager.apply(patch, objectManager.get(objType, proposal.getTargetId()));
    } catch (JsonPatchException ex) {
        log.error("Failed to apply proposed object update: " + proposal.getChange(), ex);
        return createJsonErrorReport(proposal);
    }
    return importService.importMetadata(createImportParams(mode, ImportStrategy.UPDATE, patched));
}
Also used : JsonPatchException(org.hisp.dhis.commons.jackson.jsonpatch.JsonPatchException) JsonPatch(org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 4 with JsonPatchException

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

the class AddOperation method apply.

@Override
public JsonNode apply(JsonNode node) throws JsonPatchException {
    if (path == JsonPointer.empty()) {
        return value;
    }
    final JsonNode parentNode = node.at(path.head());
    final String rawToken = path.last().getMatchingProperty();
    if (parentNode.isMissingNode()) {
        throw new JsonPatchException("Path does not exist: " + path);
    }
    if (!parentNode.isContainerNode()) {
        throw new JsonPatchException("Parent node is not a container, unable to proceed");
    }
    if (parentNode.isObject()) {
        ((ObjectNode) parentNode).set(rawToken, value);
    } else if (parentNode.isArray()) {
        final ArrayNode target = (ArrayNode) node.at(path.head());
        // array.
        if (rawToken.equals(END_OF_ARRAY)) {
            target.add(value);
            return node;
        }
        final int index = getArrayIndex(rawToken, target.size());
        target.insert(index, value);
    }
    return node;
}
Also used : JsonPatchException(org.hisp.dhis.commons.jackson.jsonpatch.JsonPatchException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Aggregations

JsonPatchException (org.hisp.dhis.commons.jackson.jsonpatch.JsonPatchException)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)2 JsonPatch (org.hisp.dhis.commons.jackson.jsonpatch.JsonPatch)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)1 ErrorReport (org.hisp.dhis.feedback.ErrorReport)1 Property (org.hisp.dhis.schema.Property)1 Schema (org.hisp.dhis.schema.Schema)1 SchemaService (org.hisp.dhis.schema.SchemaService)1 ReflectionUtils (org.hisp.dhis.system.util.ReflectionUtils)1 JsonUtils.jsonToObject (org.hisp.dhis.util.JsonUtils.jsonToObject)1 Service (org.springframework.stereotype.Service)1 Transactional (org.springframework.transaction.annotation.Transactional)1