use of org.hisp.dhis.schema.Property 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.schema.Property in project dhis2-core by dhis2.
the class GistPlanner method withCollectionItemPropertyAsTransformation.
private List<Field> withCollectionItemPropertyAsTransformation(List<Field> fields) {
List<Field> mapped = new ArrayList<>();
for (Field f : fields) {
String path = f.getPropertyPath();
if (!isNonNestedPath(path) && context.resolveMandatory(parentPath(path)).isCollection()) {
String parentPath = parentPath(path);
String propertyName = path.substring(path.lastIndexOf('.') + 1);
Property collection = context.resolveMandatory(parentPath);
if ("id".equals(propertyName) && PrimaryKeyObject.class.isAssignableFrom(collection.getItemKlass())) {
mapped.add(f.withPropertyPath(parentPath).withAlias(path).withTransformation(Transform.IDS));
} else {
mapped.add(Field.builder().propertyPath(parentPath).alias(path).transformation(Transform.PLUCK).transformationArgument(propertyName).build());
}
} else {
mapped.add(f);
}
}
return mapped;
}
use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class GistPlanner method withEndpointsField.
private List<Field> withEndpointsField(List<Field> fields) {
if (!query.isReferences()) {
return fields;
}
boolean hasReferences = fields.stream().anyMatch(field -> {
if (field.isAttribute()) {
return false;
}
Property p = context.resolveMandatory(field.getPropertyPath());
return isPersistentReferenceField(p) && p.isIdentifiableObject() || isPersistentCollectionField(p);
});
if (!hasReferences) {
return fields;
}
ArrayList<Field> extended = new ArrayList<>(fields);
extended.add(new Field(Field.REFS_PATH, Transform.NONE).withAlias("apiEndpoints"));
return extended;
}
use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class DefaultCollectionService method addNonOwnedCollectionItems.
private void addNonOwnedCollectionItems(IdentifiableObject object, Property property, Collection<String> itemCodes, TypeReport report) {
Schema owningSchema = schemaService.getDynamicSchema(property.getItemKlass());
Property owningProperty = owningSchema.propertyByRole(property.getOwningRole());
updateCollectionItems(property, itemCodes, report, ErrorCode.E1108, item -> {
Collection<IdentifiableObject> collection = getCollection(item, owningProperty);
if (!collection.contains(object)) {
validateAndThrowErrors(() -> schemaValidator.validateProperty(property, object));
collection.add(object);
manager.update(item);
report.getStats().incUpdated();
} else {
report.getStats().incIgnored();
}
});
}
use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.
the class DefaultCollectionService method delCollectionItems.
@Override
@Transactional
public TypeReport delCollectionItems(IdentifiableObject object, String propertyName, Collection<? extends IdentifiableObject> objects) throws Exception {
Property property = validateUpdate(object, propertyName, "Only identifiable object collections can be removed from.");
Collection<String> itemCodes = getItemCodes(objects);
if (itemCodes.isEmpty()) {
return TypeReport.empty(property.getItemKlass());
}
TypeReport report = new TypeReport(property.getItemKlass());
manager.refresh(object);
if (property.isOwner()) {
delOwnedCollectionItems(object, property, itemCodes, report);
} else {
delNonOwnedCollectionItems(object, property, itemCodes, report);
}
validateAndThrowErrors(() -> schemaValidator.validateProperty(property, object));
manager.update(object);
dbmsManager.clearSession();
cacheManager.clearCache();
return report;
}
Aggregations