Search in sources :

Example 56 with Property

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

the class DefaultFieldFilterService method updateFields.

private void updateFields(FieldMap fieldMap, Class<?> klass, boolean expandOnly) {
    if (fieldMap.isEmpty()) {
        return;
    }
    Schema schema = schemaService.getDynamicSchema(klass);
    List<String> cleanupFields = Lists.newArrayList();
    for (String fieldKey : Sets.newHashSet(fieldMap.keySet())) {
        Collection<Property> properties = schema.getReadableProperties().values();
        if ("*".equals(fieldKey)) {
            properties.stream().filter(property -> !fieldMap.containsKey(property.key())).forEach(property -> fieldMap.put(property.key(), new FieldMap()));
            cleanupFields.add(fieldKey);
        } else if (":persisted".equals(fieldKey)) {
            properties.stream().filter(property -> !fieldMap.containsKey(property.key()) && property.isPersisted()).forEach(property -> fieldMap.put(property.key(), new FieldMap()));
            cleanupFields.add(fieldKey);
        } else if (":owner".equals(fieldKey)) {
            properties.stream().filter(property -> !fieldMap.containsKey(property.key()) && property.isPersisted() && property.isOwner()).forEach(property -> fieldMap.put(property.key(), new FieldMap()));
            cleanupFields.add(fieldKey);
        } else if (fieldKey.startsWith(":")) {
            Preset preset = presets.get(fieldKey.substring(1));
            if (preset == null) {
                continue;
            }
            List<String> fields = preset.getFields();
            fields.stream().filter(field -> !fieldMap.containsKey(field)).forEach(field -> fieldMap.put(field, new FieldMap()));
            cleanupFields.add(fieldKey);
        } else if (fieldKey.startsWith("!") && !expandOnly) {
            cleanupFields.add(fieldKey);
        } else if (fieldKey.contains("::") || fieldKey.contains("|") || fieldKey.contains("~")) {
            Matcher matcher = FIELD_PATTERN.matcher(fieldKey);
            if (!matcher.find()) {
                continue;
            }
            String fieldName = matcher.group("field");
            FieldMap value = new FieldMap();
            value.putAll(fieldMap.get(fieldKey));
            matcher = TRANSFORMER_PATTERN.matcher(fieldKey);
            while (matcher.find()) {
                String nameMatch = matcher.group("name");
                String argsMatch = matcher.group("args");
                if (transformers.containsKey(nameMatch)) {
                    NodeTransformer transformer = transformers.get(nameMatch);
                    List<String> args = argsMatch == null ? new ArrayList<>() : Lists.newArrayList(argsMatch.split(";"));
                    value.getPipeline().addTransformer(transformer, args);
                }
            }
            fieldMap.put(fieldName, value);
            cleanupFields.add(fieldKey);
        }
    }
    for (String field : cleanupFields) {
        fieldMap.remove(field);
        if (!expandOnly) {
            fieldMap.remove(field.substring(1));
        }
    }
}
Also used : ReflectionUtils(org.hisp.dhis.system.util.ReflectionUtils) Preheat(org.hisp.dhis.preheat.Preheat) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) NodeTransformer(org.hisp.dhis.node.NodeTransformer) Matcher(java.util.regex.Matcher) Preset(org.hisp.dhis.node.Preset) UserService(org.hisp.dhis.user.UserService) ImmutableMap(com.google.common.collect.ImmutableMap) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) Collection(java.util.Collection) Set(java.util.Set) SchemaService(org.hisp.dhis.schema.SchemaService) Collectors(java.util.stream.Collectors) Property(org.hisp.dhis.schema.Property) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) SimpleNode(org.hisp.dhis.node.types.SimpleNode) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) ComplexNode(org.hisp.dhis.node.types.ComplexNode) AttributeService(org.hisp.dhis.attribute.AttributeService) UserGroupService(org.hisp.dhis.user.UserGroupService) UserAccess(org.hisp.dhis.user.UserAccess) CollectionUtils(org.springframework.util.CollectionUtils) Modifier(java.lang.reflect.Modifier) Cache(org.hisp.dhis.cache.Cache) PostConstruct(javax.annotation.PostConstruct) AclService(org.hisp.dhis.security.acl.AclService) Schema(org.hisp.dhis.schema.Schema) Pattern(java.util.regex.Pattern) Joiner(com.google.common.base.Joiner) AttributeValue(org.hisp.dhis.attribute.AttributeValue) CollectionNode(org.hisp.dhis.node.types.CollectionNode) PropertyTransformer(org.hisp.dhis.schema.PropertyTransformer) Attribute(org.hisp.dhis.attribute.Attribute) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AbstractNode(org.hisp.dhis.node.AbstractNode) Lists(com.google.common.collect.Lists) User(org.hisp.dhis.user.User) Nonnull(javax.annotation.Nonnull) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Access(org.hisp.dhis.security.acl.Access) HibernateProxyUtils(org.hisp.dhis.hibernate.HibernateProxyUtils) Node(org.hisp.dhis.node.Node) Field(java.lang.reflect.Field) UserGroupAccess(org.hisp.dhis.user.UserGroupAccess) Introspector.decapitalize(java.beans.Introspector.decapitalize) CacheProvider(org.hisp.dhis.cache.CacheProvider) Component(org.springframework.stereotype.Component) CurrentUserService(org.hisp.dhis.user.CurrentUserService) PeriodType(org.hisp.dhis.period.PeriodType) Collections(java.util.Collections) NodeTransformer(org.hisp.dhis.node.NodeTransformer) Matcher(java.util.regex.Matcher) Preset(org.hisp.dhis.node.Preset) Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Property(org.hisp.dhis.schema.Property)

Example 57 with Property

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

the class DefaultFieldFilterService method getProperties.

private ComplexNode getProperties(Property currentProperty, Object object, List<String> fields) {
    if (object == null) {
        return null;
    }
    // objects
    if (isBaseIdentifiableObjectIdOnly(object, fields)) {
        return createBaseIdentifiableObjectIdNode(currentProperty, object);
    }
    ComplexNode complexNode = new ComplexNode(currentProperty.getName());
    complexNode.setNamespace(currentProperty.getNamespace());
    complexNode.setProperty(currentProperty);
    Schema schema;
    if (currentProperty.hasPropertyTransformer()) {
        schema = schemaService.getDynamicSchema(HibernateProxyUtils.getRealClass(object));
    } else if (currentProperty.isCollection()) {
        schema = schemaService.getDynamicSchema(currentProperty.getItemKlass());
    } else {
        schema = schemaService.getDynamicSchema(currentProperty.getKlass());
    }
    for (String field : fields) {
        Property property = schema.getProperty(field);
        if (property == null) {
            continue;
        }
        Object returnValue = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
        SimpleNode simpleNode = new SimpleNode(field, returnValue);
        simpleNode.setAttribute(property.isAttribute());
        simpleNode.setNamespace(property.getNamespace());
        simpleNode.setProperty(property);
        complexNode.addChild(simpleNode);
    }
    return complexNode;
}
Also used : ComplexNode(org.hisp.dhis.node.types.ComplexNode) Schema(org.hisp.dhis.schema.Schema) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Property(org.hisp.dhis.schema.Property) SimpleNode(org.hisp.dhis.node.types.SimpleNode)

Example 58 with Property

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

the class DefaultFieldFilterService method buildNode.

private AbstractNode buildNode(FieldMap fieldMap, Class<?> klass, Object object, User user, String nodeName, Defaults defaults) {
    Schema schema = schemaService.getDynamicSchema(klass);
    ComplexNode complexNode = new ComplexNode(nodeName);
    complexNode.setNamespace(schema.getNamespace());
    if (object == null) {
        return new SimpleNode(schema.getName(), null);
    }
    if (shouldExclude(object, defaults)) {
        return null;
    }
    updateFields(fieldMap, schema.getKlass());
    if (fieldMap.containsKey("access") && schema.isIdentifiableObject()) {
        Access access = aclService.getAccess((IdentifiableObject) object, user);
        ((BaseIdentifiableObject) object).setAccess(access);
    }
    if (fieldMap.containsKey("attribute") && AttributeValue.class.isAssignableFrom(object.getClass())) {
        AttributeValue attributeValue = (AttributeValue) object;
        attributeValue.setAttribute(attributeService.getAttribute(attributeValue.getAttribute().getUid()));
    }
    if (UserGroupAccess.class.isAssignableFrom(object.getClass())) {
        UserGroupAccess userGroupAccess = (UserGroupAccess) object;
        userGroupAccess.setDisplayName(userGroupService.getDisplayName(userGroupAccess.getUserGroupUid()));
    }
    if (UserAccess.class.isAssignableFrom(object.getClass())) {
        UserAccess userAccess = (UserAccess) object;
        userAccess.setDisplayName(userService.getDisplayName(userAccess.getUserUid()));
    }
    for (String fieldKey : fieldMap.keySet()) {
        AbstractNode child = null;
        Property property = schema.getProperty(fieldKey);
        FieldMap fieldValue = fieldMap.get(fieldKey);
        if (property == null || !property.isReadable()) {
            // throw new FieldFilterException( fieldKey, schema );
            log.debug("Unknown field property `" + fieldKey + "`, available fields are " + schema.getPropertyMap().keySet());
            continue;
        }
        Object returnValue = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
        Class<?> propertyClass = property.getKlass();
        Schema propertySchema = schemaService.getDynamicSchema(propertyClass);
        if (property.hasPropertyTransformer()) {
            PropertyTransformer propertyTransformer = transformerCache.get(property.getPropertyTransformer().getName(), s -> {
                try {
                    return property.getPropertyTransformer().newInstance();
                } catch (InstantiationException | IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
            });
            if (returnValue != null) {
                returnValue = propertyTransformer.transform(returnValue);
                propertyClass = returnValue.getClass();
                propertySchema = schemaService.getDynamicSchema(propertyClass);
                updateFields(fieldValue, propertyTransformer.getKlass());
            }
        }
        if (returnValue != null && propertySchema.getProperties().isEmpty() && !property.isCollection() && property.getKlass().isInterface() && !property.isIdentifiableObject()) {
            // try to retrieve schema from concrete class
            propertyClass = returnValue.getClass();
            propertySchema = schemaService.getDynamicSchema(propertyClass);
        }
        if (returnValue == null && property.isCollection()) {
            continue;
        }
        if (property.isCollection()) {
            updateFields(fieldValue, property.getItemKlass());
        } else {
            updateFields(fieldValue, propertyClass);
        }
        if (fieldValue.isEmpty()) {
            List<String> fields = Preset.defaultAssociationPreset().getFields();
            if (property.isCollection()) {
                Collection<?> collection = (Collection<?>) returnValue;
                child = new CollectionNode(property.getCollectionName(), collection.size());
                child.setNamespace(property.getNamespace());
                if (property.isIdentifiableObject() && isProperIdObject(property.getItemKlass())) {
                    final boolean mayExclude = collection.isEmpty() || mayExclude(property.getItemKlass(), defaults);
                    for (Object collectionObject : collection) {
                        if (!mayExclude || !shouldExclude(collectionObject, defaults)) {
                            child.addChild(getProperties(property, collectionObject, fields));
                        }
                    }
                } else if (!property.isSimple()) {
                    FieldMap map = getFullFieldMap(schemaService.getDynamicSchema(property.getItemKlass()));
                    for (Object collectionObject : collection) {
                        Node node = buildNode(map, property.getItemKlass(), collectionObject, user, defaults);
                        if (node != null && !node.getChildren().isEmpty()) {
                            child.addChild(node);
                        }
                    }
                } else {
                    for (Object collectionObject : collection) {
                        SimpleNode simpleNode = child.addChild(new SimpleNode(property.getName(), collectionObject));
                        simpleNode.setProperty(property);
                    }
                }
            } else if (property.isIdentifiableObject() && isProperIdObject(propertyClass)) {
                if (!shouldExclude(returnValue, defaults)) {
                    child = getProperties(property, returnValue, fields);
                }
            } else {
                if (propertySchema.getProperties().isEmpty()) {
                    SimpleNode simpleNode = new SimpleNode(fieldKey, returnValue);
                    simpleNode.setAttribute(property.isAttribute());
                    simpleNode.setNamespace(property.getNamespace());
                    child = simpleNode;
                } else {
                    child = buildNode(getFullFieldMap(propertySchema), propertyClass, returnValue, user, defaults);
                }
            }
        } else {
            if (property.isCollection()) {
                child = new CollectionNode(property.getCollectionName());
                child.setNamespace(property.getNamespace());
                for (Object collectionObject : (Collection<?>) Objects.requireNonNull(returnValue)) {
                    Node node;
                    if (property.hasPropertyTransformer()) {
                        // if it has a transformer, re-get the schema (the
                        // item klass has probably changed)
                        Schema sch = schemaService.getDynamicSchema(HibernateProxyUtils.getRealClass(collectionObject));
                        node = buildNode(fieldValue, sch.getKlass(), collectionObject, user, property.getName(), defaults);
                    } else {
                        node = buildNode(fieldValue, property.getItemKlass(), collectionObject, user, property.getName(), defaults);
                    }
                    if (!Objects.requireNonNull(node).getChildren().isEmpty()) {
                        child.addChild(node);
                    }
                }
            } else {
                returnValue = handleJsonbObjectProperties(klass, propertyClass, returnValue);
                child = buildNode(fieldValue, propertyClass, returnValue, user, defaults);
            }
        }
        if (child != null) {
            child.setName(fieldKey);
            child.setProperty(property);
            // serializer/deserializer
            if (child.isSimple() && (((SimpleNode) child).getValue()) instanceof PeriodType) {
                child = new SimpleNode(child.getName(), ((PeriodType) ((SimpleNode) child).getValue()).getName());
            }
            complexNode.addChild(fieldValue.getPipeline().process(child));
        }
    }
    return complexNode;
}
Also used : PeriodType(org.hisp.dhis.period.PeriodType) AttributeValue(org.hisp.dhis.attribute.AttributeValue) AbstractNode(org.hisp.dhis.node.AbstractNode) Schema(org.hisp.dhis.schema.Schema) SimpleNode(org.hisp.dhis.node.types.SimpleNode) ComplexNode(org.hisp.dhis.node.types.ComplexNode) CollectionNode(org.hisp.dhis.node.types.CollectionNode) AbstractNode(org.hisp.dhis.node.AbstractNode) Node(org.hisp.dhis.node.Node) UserAccess(org.hisp.dhis.user.UserAccess) Access(org.hisp.dhis.security.acl.Access) UserGroupAccess(org.hisp.dhis.user.UserGroupAccess) SimpleNode(org.hisp.dhis.node.types.SimpleNode) Property(org.hisp.dhis.schema.Property) UserGroupAccess(org.hisp.dhis.user.UserGroupAccess) UserAccess(org.hisp.dhis.user.UserAccess) ComplexNode(org.hisp.dhis.node.types.ComplexNode) CollectionNode(org.hisp.dhis.node.types.CollectionNode) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) PropertyTransformer(org.hisp.dhis.schema.PropertyTransformer) Collection(java.util.Collection) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 59 with Property

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

the class IsEmptyNodeTransformer method transform.

@Override
public Node transform(Node node, List<String> args) {
    checkNotNull(node);
    checkNotNull(node.getProperty());
    Property property = node.getProperty();
    if (property.isCollection()) {
        return new SimpleNode(property.getCollectionName(), node.getChildren().isEmpty(), property.isAttribute());
    } else if (property.isSimple()) {
        return new SimpleNode(property.getName(), ObjectUtils.isEmpty(((SimpleNode) node).getValue()), property.isAttribute());
    }
    return node;
}
Also used : Property(org.hisp.dhis.schema.Property) SimpleNode(org.hisp.dhis.node.types.SimpleNode)

Example 60 with Property

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

the class IsNotEmptyNodeTransformer method transform.

@Override
public Node transform(Node node, List<String> args) {
    checkNotNull(node);
    checkNotNull(node.getProperty());
    Property property = node.getProperty();
    if (property.isCollection()) {
        return new SimpleNode(property.getCollectionName(), !node.getChildren().isEmpty(), property.isAttribute());
    } else if (property.isSimple()) {
        return new SimpleNode(property.getName(), !ObjectUtils.isEmpty(((SimpleNode) node).getValue()), property.isAttribute());
    }
    return node;
}
Also used : Property(org.hisp.dhis.schema.Property) SimpleNode(org.hisp.dhis.node.types.SimpleNode)

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