Search in sources :

Example 36 with Property

use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.

the class DefaultQueryPlanner method getQueryPath.

@Override
public QueryPath getQueryPath(Schema schema, String path) {
    Schema curSchema = schema;
    Property curProperty = null;
    boolean persisted = true;
    List<String> alias = new ArrayList<>();
    String[] pathComponents = path.split("\\.");
    if (pathComponents.length == 0) {
        return null;
    }
    for (int idx = 0; idx < pathComponents.length; idx++) {
        String name = pathComponents[idx];
        curProperty = curSchema.getProperty(name);
        if (curProperty == null) {
            throw new RuntimeException("Invalid path property: " + name);
        }
        if (!curProperty.isPersisted()) {
            persisted = false;
        }
        if ((!curProperty.isSimple() && idx == pathComponents.length - 1)) {
            return new QueryPath(curProperty, persisted, alias.toArray(new String[] {}));
        }
        if (curProperty.isCollection()) {
            curSchema = schemaService.getDynamicSchema(curProperty.getItemKlass());
            alias.add(curProperty.getFieldName());
        } else if (!curProperty.isSimple()) {
            curSchema = schemaService.getDynamicSchema(curProperty.getKlass());
            alias.add(curProperty.getFieldName());
        } else {
            return new QueryPath(curProperty, persisted, alias.toArray(new String[] {}));
        }
    }
    return new QueryPath(curProperty, persisted, alias.toArray(new String[] {}));
}
Also used : Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList) Property(org.hisp.dhis.schema.Property)

Example 37 with Property

use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.

the class DefaultQueryPlanner method getQueryPath.

@Override
public Path<?> getQueryPath(Root<?> root, Schema schema, String path) {
    Schema curSchema = schema;
    Property curProperty;
    String[] pathComponents = path.split("\\.");
    Path<?> currentPath = root;
    if (pathComponents.length == 0) {
        return null;
    }
    for (int idx = 0; idx < pathComponents.length; idx++) {
        String name = pathComponents[idx];
        curProperty = curSchema.getProperty(name);
        if (curProperty == null) {
            throw new RuntimeException("Invalid path property: " + name);
        }
        if ((!curProperty.isSimple() && idx == pathComponents.length - 1)) {
            return root.join(curProperty.getFieldName());
        }
        if (curProperty.isCollection()) {
            currentPath = root.join(curProperty.getFieldName());
            curSchema = schemaService.getDynamicSchema(curProperty.getItemKlass());
        } else if (!curProperty.isSimple()) {
            curSchema = schemaService.getDynamicSchema(curProperty.getKlass());
            currentPath = root.join(curProperty.getFieldName());
        } else {
            return currentPath.get(curProperty.getFieldName());
        }
    }
    return currentPath;
}
Also used : Schema(org.hisp.dhis.schema.Schema) Property(org.hisp.dhis.schema.Property)

Example 38 with Property

use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.

the class DefaultPreheatService method collectObjectReferences.

@SuppressWarnings("unchecked")
private Map<Class<?>, Map<String, Map<String, Object>>> collectObjectReferences(Map<Class<?>, List<?>> objects) {
    Map<Class<?>, Map<String, Map<String, Object>>> map = new HashMap<>();
    if (objects.isEmpty()) {
        return map;
    }
    // clone objects list, we don't want to modify it
    Map<Class<?>, List<?>> targets = new HashMap<>(objects);
    collectScanTargets(targets);
    for (Class<?> objectClass : targets.keySet()) {
        Schema schema = schemaService.getDynamicSchema(objectClass);
        if (!schema.isIdentifiableObject()) {
            continue;
        }
        List<Property> properties = schema.getProperties().stream().filter(p -> p.isPersisted() && p.isOwner() && (PropertyType.REFERENCE == p.getPropertyType() || PropertyType.REFERENCE == p.getItemPropertyType())).collect(Collectors.toList());
        List<IdentifiableObject> identifiableObjects = (List<IdentifiableObject>) targets.get(objectClass);
        Map<String, Map<String, Object>> refMap = new HashMap<>();
        map.put(objectClass, refMap);
        for (IdentifiableObject object : identifiableObjects) {
            refMap.put(object.getUid(), new HashMap<>());
            properties.forEach(p -> {
                if (!p.isCollection()) {
                    IdentifiableObject reference = ReflectionUtils.invokeMethod(object, p.getGetterMethod());
                    if (reference != null) {
                        try {
                            IdentifiableObject identifiableObject = (IdentifiableObject) p.getKlass().newInstance();
                            mergeService.merge(new MergeParams<>(reference, identifiableObject));
                            refMap.get(object.getUid()).put(p.getName(), identifiableObject);
                        } catch (InstantiationException | IllegalAccessException ignored) {
                        }
                    }
                } else {
                    Collection<IdentifiableObject> refObjects = ReflectionUtils.newCollectionInstance(p.getKlass());
                    Collection<IdentifiableObject> references = ReflectionUtils.invokeMethod(object, p.getGetterMethod());
                    if (references != null) {
                        for (IdentifiableObject reference : references) {
                            if (reference == null) {
                                continue;
                            }
                            try {
                                IdentifiableObject identifiableObject = (IdentifiableObject) p.getItemKlass().newInstance();
                                mergeService.merge(new MergeParams<>(reference, identifiableObject));
                                refObjects.add(identifiableObject);
                            } catch (InstantiationException | IllegalAccessException ignored) {
                            }
                        }
                    }
                    refMap.get(object.getUid()).put(p.getCollectionName(), refObjects);
                }
            });
        }
    }
    return map;
}
Also used : CategoryDimension(org.hisp.dhis.category.CategoryDimension) ReflectionUtils(org.hisp.dhis.system.util.ReflectionUtils) BaseAnalyticalObject(org.hisp.dhis.common.BaseAnalyticalObject) Restrictions(org.hisp.dhis.query.Restrictions) PeriodService(org.hisp.dhis.period.PeriodService) MergeService(org.hisp.dhis.schema.MergeService) TrackedEntityAttributeDimension(org.hisp.dhis.trackedentity.TrackedEntityAttributeDimension) StringUtils(org.apache.commons.lang3.StringUtils) MergeParams(org.hisp.dhis.schema.MergeParams) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) Map(java.util.Map) Period(org.hisp.dhis.period.Period) Query(org.hisp.dhis.query.Query) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) UserGroup(org.hisp.dhis.user.UserGroup) Collection(java.util.Collection) Set(java.util.Set) SchemaService(org.hisp.dhis.schema.SchemaService) Collectors(java.util.stream.Collectors) QueryService(org.hisp.dhis.query.QueryService) Property(org.hisp.dhis.schema.Property) DataDimensionItem(org.hisp.dhis.common.DataDimensionItem) Sets(com.google.common.collect.Sets) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) AttributeService(org.hisp.dhis.attribute.AttributeService) UserAuthorityGroup(org.hisp.dhis.user.UserAuthorityGroup) Schema(org.hisp.dhis.schema.Schema) SharingUtils(org.hisp.dhis.util.SharingUtils) AnalyticalObject(org.hisp.dhis.common.AnalyticalObject) PropertyType(org.hisp.dhis.schema.PropertyType) DataSetElement(org.hisp.dhis.dataset.DataSetElement) HashMap(java.util.HashMap) Attribute(org.hisp.dhis.attribute.Attribute) Scope(org.springframework.context.annotation.Scope) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) Service(org.springframework.stereotype.Service) User(org.hisp.dhis.user.User) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) SystemTimer(org.hisp.dhis.commons.timer.SystemTimer) DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) HibernateProxyUtils(org.hisp.dhis.hibernate.HibernateProxyUtils) TrackedEntityProgramIndicatorDimension(org.hisp.dhis.trackedentity.TrackedEntityProgramIndicatorDimension) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) ScopedProxyMode(org.springframework.context.annotation.ScopedProxyMode) Timer(org.hisp.dhis.commons.timer.Timer) CollectionUtils(org.hisp.dhis.commons.collection.CollectionUtils) CurrentUserService(org.hisp.dhis.user.CurrentUserService) PeriodStore(org.hisp.dhis.period.PeriodStore) TrackedEntityDataElementDimension(org.hisp.dhis.trackedentity.TrackedEntityDataElementDimension) CodeGenerator(org.hisp.dhis.common.CodeGenerator) Transactional(org.springframework.transaction.annotation.Transactional) HashMap(java.util.HashMap) Schema(org.hisp.dhis.schema.Schema) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) BaseAnalyticalObject(org.hisp.dhis.common.BaseAnalyticalObject) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) AnalyticalObject(org.hisp.dhis.common.AnalyticalObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) Property(org.hisp.dhis.schema.Property)

Example 39 with Property

use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.

the class DefaultPatchService method calculateMutation.

@SuppressWarnings("unchecked")
private List<Mutation> calculateMutation(String path, Property property, Object source, Object target) {
    Object sourceValue = ReflectionUtils.invokeMethod(source, property.getGetterMethod());
    Object targetValue = ReflectionUtils.invokeMethod(target, property.getGetterMethod());
    List<Mutation> mutations = new ArrayList<>();
    if (sourceValue == null && targetValue == null) {
        return mutations;
    }
    if (targetValue == null || sourceValue == null) {
        return Lists.newArrayList(new Mutation(path, targetValue));
    }
    if (property.isCollection() && property.isIdentifiableObject() && !property.isEmbeddedObject()) {
        Collection<Object> addCollection = ReflectionUtils.newCollectionInstance(property.getKlass());
        Collection<Object> sourceCollection = ((Collection<Object>) sourceValue).stream().filter(Objects::nonNull).map(o -> ((IdentifiableObject) o).getUid()).collect(Collectors.toList());
        Collection<Object> targetCollection = ((Collection<Object>) targetValue).stream().filter(Objects::nonNull).map(o -> ((IdentifiableObject) o).getUid()).collect(Collectors.toList());
        for (Object o : targetCollection) {
            if (!sourceCollection.contains(o)) {
                addCollection.add(o);
            } else {
                sourceCollection.remove(o);
            }
        }
        if (!addCollection.isEmpty()) {
            mutations.add(new Mutation(path, addCollection));
        }
        Collection<Object> delCollection = ReflectionUtils.newCollectionInstance(property.getKlass());
        delCollection.addAll(sourceCollection);
        if (!delCollection.isEmpty()) {
            mutations.add(new Mutation(path, delCollection, Mutation.Operation.DELETION));
        }
    } else if (property.isCollection() && !property.isEmbeddedObject() && !property.isIdentifiableObject()) {
        List<Object> sourceCollection = new ArrayList<>((Collection<Object>) sourceValue);
        Collection<Object> targetCollection = (Collection<Object>) targetValue;
        Collection<Object> addCollection = ReflectionUtils.newCollectionInstance(property.getKlass());
        for (Object o : targetCollection) {
            if (!sourceCollection.contains(o)) {
                addCollection.add(o);
            } else {
                sourceCollection.remove(o);
            }
        }
        if (!addCollection.isEmpty()) {
            mutations.add(new Mutation(path, addCollection));
        }
        Collection<Object> delCollection = ReflectionUtils.newCollectionInstance(property.getKlass());
        delCollection.addAll(sourceCollection);
        if (!delCollection.isEmpty()) {
            mutations.add(new Mutation(path, delCollection, Mutation.Operation.DELETION));
        }
    } else if (property.isSimple() || property.isEmbeddedObject()) {
        if (!targetValue.equals(sourceValue)) {
            return Lists.newArrayList(new Mutation(path, targetValue));
        }
    }
    return mutations;
}
Also used : ReflectionUtils(org.hisp.dhis.system.util.ReflectionUtils) Date(java.util.Date) Restrictions(org.hisp.dhis.query.Restrictions) RenderService(org.hisp.dhis.render.RenderService) Autowired(org.springframework.beans.factory.annotation.Autowired) ArrayList(java.util.ArrayList) Enums(com.google.common.base.Enums) SystemService(org.hisp.dhis.system.SystemService) Lists(com.google.common.collect.Lists) Service(org.springframework.stereotype.Service) Optional(com.google.common.base.Optional) JsonNode(com.fasterxml.jackson.databind.JsonNode) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) HibernateProxyUtils(org.hisp.dhis.hibernate.HibernateProxyUtils) Query(org.hisp.dhis.query.Query) Collection(java.util.Collection) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) SchemaService(org.hisp.dhis.schema.SchemaService) Collectors(java.util.stream.Collectors) QueryService(org.hisp.dhis.query.QueryService) Property(org.hisp.dhis.schema.Property) Objects(java.util.Objects) List(java.util.List) CurrentUserService(org.hisp.dhis.user.CurrentUserService) Schema(org.hisp.dhis.schema.Schema) DateUtils(org.hisp.dhis.util.DateUtils) Transactional(org.springframework.transaction.annotation.Transactional) ArrayList(java.util.ArrayList) Objects(java.util.Objects) Collection(java.util.Collection) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) ArrayList(java.util.ArrayList) List(java.util.List) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 40 with Property

use of org.hisp.dhis.schema.Property in project dhis2-core by dhis2.

the class DefaultPatchService method applyMutation.

private void applyMutation(Mutation mutation, Schema schema, Object target) {
    String path = mutation.getPath();
    String[] paths = path.split("\\.");
    Schema currentSchema = schema;
    Property currentProperty = null;
    Object currentTarget = target;
    for (int i = 0; i < paths.length; i++) {
        if (!currentSchema.haveProperty(paths[i])) {
            return;
        }
        currentProperty = currentSchema.getProperty(paths[i]);
        if (currentProperty == null) {
            return;
        }
        if ((currentProperty.isSimple() && !currentProperty.isCollection()) && i != (paths.length - 1)) {
            return;
        }
        if (currentProperty.isCollection()) {
            currentSchema = schemaService.getDynamicSchema(currentProperty.getItemKlass());
        } else {
            currentSchema = schemaService.getDynamicSchema(currentProperty.getKlass());
        }
        if (i < (paths.length - 1)) {
            currentTarget = ReflectionUtils.invokeMethod(currentTarget, currentProperty.getGetterMethod());
        }
    }
    if (currentSchema != null && currentProperty != null) {
        applyMutation(mutation, currentProperty, currentTarget);
    }
}
Also used : Schema(org.hisp.dhis.schema.Schema) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Property(org.hisp.dhis.schema.Property)

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