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()));
}
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();
}
}
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));
}
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;
}
Aggregations