Search in sources :

Example 1 with ComplexNode

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

the class DefaultFieldFilterService method filter.

@Override
public ComplexNode filter(Object object, List<String> fieldList) {
    Assert.notNull(object, "Object cannot be null");
    CollectionNode collectionNode = filter(object.getClass(), Lists.newArrayList(object), fieldList);
    if (collectionNode.getChildren().size() > 0) {
        return (ComplexNode) collectionNode.getChildren().get(0);
    }
    return null;
}
Also used : ComplexNode(org.hisp.dhis.node.types.ComplexNode) CollectionNode(org.hisp.dhis.node.types.CollectionNode)

Example 2 with ComplexNode

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

the class DefaultFieldFilterService method buildNode.

private AbstractNode buildNode(FieldMap fieldMap, Class<?> klass, Object object, String nodeName) {
    Schema schema = schemaService.getDynamicSchema(klass);
    ComplexNode complexNode = new ComplexNode(nodeName);
    complexNode.setNamespace(schema.getNamespace());
    if (object == null) {
        return new SimpleNode(schema.getName(), null);
    }
    updateFields(fieldMap, schema.getKlass());
    for (String fieldKey : fieldMap.keySet()) {
        AbstractNode child;
        Property property = schema.getProperty(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());
        Schema propertySchema = schemaService.getDynamicSchema(property.getKlass());
        FieldMap fieldValue = fieldMap.get(fieldKey);
        if (returnValue == null && property.isCollection()) {
            continue;
        }
        if (property.isCollection()) {
            updateFields(fieldValue, property.getItemKlass());
        } else {
            updateFields(fieldValue, property.getKlass());
        }
        if (fieldValue.isEmpty()) {
            List<String> fields = Preset.defaultAssociationPreset().getFields();
            if (property.isCollection()) {
                Collection<?> collection = (Collection<?>) returnValue;
                child = new CollectionNode(property.getCollectionName());
                child.setNamespace(property.getNamespace());
                if (property.isIdentifiableObject() && isProperIdObject(property.getItemKlass())) {
                    for (Object collectionObject : collection) {
                        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);
                        if (!node.getChildren().isEmpty()) {
                            child.addChild(node);
                        }
                    }
                } else {
                    if (collection != null) {
                        for (Object collectionObject : collection) {
                            SimpleNode simpleNode = child.addChild(new SimpleNode(property.getName(), collectionObject));
                            simpleNode.setProperty(property);
                        }
                    }
                }
            } else if (property.isIdentifiableObject() && isProperIdObject(property.getKlass())) {
                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), property.getKlass(), returnValue);
                }
            }
        } else {
            if (property.isCollection()) {
                child = new CollectionNode(property.getCollectionName());
                child.setNamespace(property.getNamespace());
                for (Object collectionObject : (Collection<?>) returnValue) {
                    Node node = buildNode(fieldValue, property.getItemKlass(), collectionObject, property.getName());
                    if (!node.getChildren().isEmpty()) {
                        child.addChild(node);
                    }
                }
            } else {
                child = buildNode(fieldValue, property.getKlass(), returnValue);
            }
        }
        if (child != null) {
            child.setName(fieldKey);
            child.setProperty(property);
            // TODO fix ugly hack, will be replaced by custom field serializer/deserializer
            if (child.isSimple() && PeriodType.class.isInstance((((SimpleNode) child).getValue()))) {
                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) AbstractNode(org.hisp.dhis.node.AbstractNode) ComplexNode(org.hisp.dhis.node.types.ComplexNode) Schema(org.hisp.dhis.schema.Schema) CollectionNode(org.hisp.dhis.node.types.CollectionNode) AbstractNode(org.hisp.dhis.node.AbstractNode) Node(org.hisp.dhis.node.Node) SimpleNode(org.hisp.dhis.node.types.SimpleNode) ComplexNode(org.hisp.dhis.node.types.ComplexNode) CollectionNode(org.hisp.dhis.node.types.CollectionNode) SimpleNode(org.hisp.dhis.node.types.SimpleNode) Collection(java.util.Collection) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) Property(org.hisp.dhis.schema.Property)

Example 3 with ComplexNode

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

the class IndexController method createRootNode.

private RootNode createRootNode() {
    RootNode rootNode = NodeUtils.createMetadata();
    CollectionNode collectionNode = rootNode.addChild(new CollectionNode("resources"));
    for (Schema schema : schemaService.getSchemas()) {
        if (schema.haveApiEndpoint()) {
            ComplexNode complexNode = collectionNode.addChild(new ComplexNode("resource"));
            // TODO add i18n to this
            complexNode.addChild(new SimpleNode("displayName", beautify(schema.getPlural())));
            complexNode.addChild(new SimpleNode("singular", schema.getSingular()));
            complexNode.addChild(new SimpleNode("plural", schema.getPlural()));
            complexNode.addChild(new SimpleNode("href", contextService.getApiPath() + schema.getRelativeApiEndpoint()));
        }
    }
    return rootNode;
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) ComplexNode(org.hisp.dhis.node.types.ComplexNode) Schema(org.hisp.dhis.schema.Schema) CollectionNode(org.hisp.dhis.node.types.CollectionNode) SimpleNode(org.hisp.dhis.node.types.SimpleNode)

Example 4 with ComplexNode

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

the class ExcelNodeSerializer method startWriteRootNode.

@Override
protected void startWriteRootNode(RootNode rootNode) throws Exception {
    XSSFCreationHelper creationHelper = workbook.getCreationHelper();
    int rowIdx = 1;
    for (Node collectionNode : rootNode.getChildren()) {
        if (collectionNode.isCollection()) {
            for (Node complexNode : collectionNode.getChildren()) {
                XSSFRow row = sheet.createRow(rowIdx++);
                int cellIdx = 0;
                for (Node node : complexNode.getChildren()) {
                    if (node.isSimple()) {
                        XSSFCell cell = row.createCell(cellIdx++);
                        cell.setCellValue(getValue((SimpleNode) node));
                        if (node.haveProperty() && PropertyType.URL.equals(node.getProperty().getPropertyType())) {
                            XSSFHyperlink hyperlink = creationHelper.createHyperlink(HyperlinkType.URL);
                            hyperlink.setAddress(getValue((SimpleNode) node));
                            hyperlink.setLabel(getValue((SimpleNode) node));
                            cell.setHyperlink(hyperlink);
                        } else if (node.haveProperty() && PropertyType.EMAIL.equals(node.getProperty().getPropertyType())) {
                            XSSFHyperlink hyperlink = creationHelper.createHyperlink(HyperlinkType.EMAIL);
                            hyperlink.setAddress(getValue((SimpleNode) node));
                            hyperlink.setLabel(getValue((SimpleNode) node));
                            cell.setHyperlink(hyperlink);
                        }
                    }
                }
            }
        }
    }
}
Also used : XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFCreationHelper(org.apache.poi.xssf.usermodel.XSSFCreationHelper) CollectionNode(org.hisp.dhis.node.types.CollectionNode) Node(org.hisp.dhis.node.Node) SimpleNode(org.hisp.dhis.node.types.SimpleNode) ComplexNode(org.hisp.dhis.node.types.ComplexNode) RootNode(org.hisp.dhis.node.types.RootNode) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) SimpleNode(org.hisp.dhis.node.types.SimpleNode) XSSFHyperlink(org.apache.poi.xssf.usermodel.XSSFHyperlink)

Example 5 with ComplexNode

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

the class DefaultMetadataVersionService method getMetadataVersionsAsNode.

@Override
public RootNode getMetadataVersionsAsNode(List<MetadataVersion> versions) {
    RootNode rootNode = NodeUtils.createRootNode("metadataversions");
    CollectionNode collectionNode = new CollectionNode("metadataversions", true);
    rootNode.addChild(collectionNode);
    for (MetadataVersion version : versions) {
        ComplexNode complexNode = new ComplexNode("");
        complexNode.addChild(new SimpleNode("name", version.getName()));
        complexNode.addChild(new SimpleNode("type", version.getType()));
        complexNode.addChild(new SimpleNode("created", version.getCreated()));
        complexNode.addChild(new SimpleNode("id", version.getUid()));
        complexNode.addChild(new SimpleNode("importdate", version.getImportDate()));
        complexNode.addChild(new SimpleNode("hashCode", version.getHashCode()));
        collectionNode.addChild(complexNode);
    }
    return rootNode;
}
Also used : MetadataVersion(org.hisp.dhis.metadata.version.MetadataVersion) RootNode(org.hisp.dhis.node.types.RootNode) ComplexNode(org.hisp.dhis.node.types.ComplexNode) CollectionNode(org.hisp.dhis.node.types.CollectionNode) SimpleNode(org.hisp.dhis.node.types.SimpleNode)

Aggregations

ComplexNode (org.hisp.dhis.node.types.ComplexNode)33 SimpleNode (org.hisp.dhis.node.types.SimpleNode)23 CollectionNode (org.hisp.dhis.node.types.CollectionNode)21 RootNode (org.hisp.dhis.node.types.RootNode)15 Test (org.junit.jupiter.api.Test)9 ArrayList (java.util.ArrayList)7 DhisSpringTest (org.hisp.dhis.DhisSpringTest)5 EmbeddedObject (org.hisp.dhis.common.EmbeddedObject)5 Node (org.hisp.dhis.node.Node)5 CategoryOption (org.hisp.dhis.category.CategoryOption)4 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)4 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)4 MetadataVersion (org.hisp.dhis.metadata.version.MetadataVersion)4 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)4 Schema (org.hisp.dhis.schema.Schema)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 FieldFilterParams (org.hisp.dhis.fieldfilter.FieldFilterParams)3 Property (org.hisp.dhis.schema.Property)3 Collection (java.util.Collection)2 Category (org.hisp.dhis.category.Category)2