Search in sources :

Example 26 with Property

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()));
}
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 27 with Property

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;
}
Also used : GistLogic.isPersistentCollectionField(org.hisp.dhis.gist.GistLogic.isPersistentCollectionField) Field(org.hisp.dhis.gist.GistQuery.Field) GistLogic.isPersistentReferenceField(org.hisp.dhis.gist.GistLogic.isPersistentReferenceField) GistLogic.isIncludedField(org.hisp.dhis.gist.GistLogic.isIncludedField) ArrayList(java.util.ArrayList) PrimaryKeyObject(org.hisp.dhis.common.PrimaryKeyObject) Property(org.hisp.dhis.schema.Property)

Example 28 with Property

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;
}
Also used : GistLogic.isPersistentCollectionField(org.hisp.dhis.gist.GistLogic.isPersistentCollectionField) Field(org.hisp.dhis.gist.GistQuery.Field) GistLogic.isPersistentReferenceField(org.hisp.dhis.gist.GistLogic.isPersistentReferenceField) GistLogic.isIncludedField(org.hisp.dhis.gist.GistLogic.isIncludedField) ArrayList(java.util.ArrayList) Property(org.hisp.dhis.schema.Property)

Example 29 with Property

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();
        }
    });
}
Also used : Schema(org.hisp.dhis.schema.Schema) Property(org.hisp.dhis.schema.Property) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 30 with Property

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;
}
Also used : TypeReport(org.hisp.dhis.feedback.TypeReport) Property(org.hisp.dhis.schema.Property) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Property (org.hisp.dhis.schema.Property)126 Schema (org.hisp.dhis.schema.Schema)69 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)36 ArrayList (java.util.ArrayList)32 HashMap (java.util.HashMap)26 Collection (java.util.Collection)21 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)21 List (java.util.List)20 Map (java.util.Map)16 Test (org.junit.jupiter.api.Test)16 Attribute (org.hisp.dhis.attribute.Attribute)14 ReflectionUtils (org.hisp.dhis.system.util.ReflectionUtils)14 Collectors (java.util.stream.Collectors)13 User (org.hisp.dhis.user.User)13 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)12 EmbeddedObject (org.hisp.dhis.common.EmbeddedObject)12 SimpleNode (org.hisp.dhis.node.types.SimpleNode)12 Query (org.hisp.dhis.query.Query)12 SchemaService (org.hisp.dhis.schema.SchemaService)12 Transactional (org.springframework.transaction.annotation.Transactional)12