Search in sources :

Example 21 with SimpleNode

use of org.hisp.dhis.node.types.SimpleNode in project dhis2-core by dhis2.

the class DefaultFieldFilterService method buildNode.

private AbstractNode buildNode(final FieldMap fieldMap, final Object klassInstance, final String namespace) {
    final ComplexNode complexNode = new ComplexNode(decapitalize(klassInstance.getClass().getSimpleName()));
    complexNode.setNamespace(namespace);
    for (final String fieldKey : fieldMap.keySet()) {
        try {
            final String originalName = org.apache.commons.lang3.StringUtils.substringBefore(fieldKey, "~");
            final String rename = org.apache.commons.lang3.StringUtils.substringBetween(fieldKey, "(", ")");
            final Field field = klassInstance.getClass().getDeclaredField(originalName);
            // NOSONAR
            field.setAccessible(true);
            final Object value = ReflectionUtils.invokeGetterMethod(originalName, klassInstance);
            if (org.apache.commons.lang3.StringUtils.isNotBlank(rename)) {
                complexNode.addChild(new SimpleNode(rename, value));
            } else {
                complexNode.addChild(new SimpleNode(originalName, value));
            }
        } catch (NoSuchFieldException e) {
            log.warn("Error reading attribute", e);
        }
    }
    return complexNode;
}
Also used : Field(java.lang.reflect.Field) ComplexNode(org.hisp.dhis.node.types.ComplexNode) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) SimpleNode(org.hisp.dhis.node.types.SimpleNode)

Example 22 with SimpleNode

use of org.hisp.dhis.node.types.SimpleNode 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 23 with SimpleNode

use of org.hisp.dhis.node.types.SimpleNode 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 24 with SimpleNode

use of org.hisp.dhis.node.types.SimpleNode in project dhis2-core by dhis2.

the class NodeUtils method createPager.

public static Node createPager(Pager pager) {
    ComplexNode pagerNode = new ComplexNode("pager");
    pagerNode.setMetadata(true);
    pagerNode.addChild(new SimpleNode("page", pager.getPage()));
    pagerNode.addChild(new SimpleNode("pageCount", pager.getPageCount()));
    pagerNode.addChild(new SimpleNode("total", pager.getTotal()));
    pagerNode.addChild(new SimpleNode("pageSize", pager.getPageSize()));
    pagerNode.addChild(new SimpleNode("nextPage", pager.getNextPage()));
    pagerNode.addChild(new SimpleNode("prevPage", pager.getPrevPage()));
    return pagerNode;
}
Also used : ComplexNode(org.hisp.dhis.node.types.ComplexNode) SimpleNode(org.hisp.dhis.node.types.SimpleNode)

Example 25 with SimpleNode

use of org.hisp.dhis.node.types.SimpleNode in project dhis2-core by dhis2.

the class AbstractNodeTest method createComplexNode.

private ComplexNode createComplexNode(String node1) {
    ComplexNode complexNode1;
    List<Node> children1 = new ArrayList<>();
    children1.add(new SimpleNode("id", node1));
    complexNode1 = new ComplexNode("dataElement");
    complexNode1.setMetadata(false);
    complexNode1.setChildren(children1);
    return complexNode1;
}
Also used : ComplexNode(org.hisp.dhis.node.types.ComplexNode) SimpleNode(org.hisp.dhis.node.types.SimpleNode) ComplexNode(org.hisp.dhis.node.types.ComplexNode) RootNode(org.hisp.dhis.node.types.RootNode) ArrayList(java.util.ArrayList) SimpleNode(org.hisp.dhis.node.types.SimpleNode)

Aggregations

SimpleNode (org.hisp.dhis.node.types.SimpleNode)57 CollectionNode (org.hisp.dhis.node.types.CollectionNode)38 RootNode (org.hisp.dhis.node.types.RootNode)36 ComplexNode (org.hisp.dhis.node.types.ComplexNode)26 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)21 User (org.hisp.dhis.user.User)18 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)17 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)12 Node (org.hisp.dhis.node.Node)10 Property (org.hisp.dhis.schema.Property)10 MessageConversation (org.hisp.dhis.webapi.webdomain.MessageConversation)8 ArrayList (java.util.ArrayList)7 DeleteAccessDeniedException (org.hisp.dhis.hibernate.exception.DeleteAccessDeniedException)7 CurrentUser (org.hisp.dhis.user.CurrentUser)7 Test (org.junit.jupiter.api.Test)7 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)6 PostMapping (org.springframework.web.bind.annotation.PostMapping)5 CategoryOption (org.hisp.dhis.category.CategoryOption)4 EmbeddedObject (org.hisp.dhis.common.EmbeddedObject)4 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)4