Search in sources :

Example 1 with AbstractNode

use of org.hisp.dhis.node.AbstractNode 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 2 with AbstractNode

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

the class DimensionController method getItems.

@SuppressWarnings("unchecked")
@RequestMapping(value = "/{uid}/items", method = RequestMethod.GET)
@ResponseBody
public RootNode getItems(@PathVariable String uid, @RequestParam Map<String, String> parameters, Model model, HttpServletRequest request, HttpServletResponse response) throws QueryParserException {
    List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
    List<String> filters = Lists.newArrayList(contextService.getParameterValues("filter"));
    if (fields.isEmpty()) {
        fields.addAll(Preset.defaultPreset().getFields());
    }
    List<DimensionalItemObject> items = dimensionService.getCanReadDimensionItems(uid);
    Query query = queryService.getQueryFromUrl(getEntityClass(), filters, new ArrayList<>());
    query.setObjects(items);
    query.setDefaultOrder();
    items = (List<DimensionalItemObject>) queryService.query(query);
    RootNode rootNode = NodeUtils.createMetadata();
    CollectionNode collectionNode = rootNode.addChild(fieldFilterService.filter(getEntityClass(), items, fields));
    collectionNode.setName("items");
    for (Node node : collectionNode.getChildren()) {
        ((AbstractNode) node).setName("item");
    }
    return rootNode;
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) Query(org.hisp.dhis.query.Query) AbstractNode(org.hisp.dhis.node.AbstractNode) CollectionNode(org.hisp.dhis.node.types.CollectionNode) AbstractNode(org.hisp.dhis.node.AbstractNode) Node(org.hisp.dhis.node.Node) RootNode(org.hisp.dhis.node.types.RootNode) CollectionNode(org.hisp.dhis.node.types.CollectionNode) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with AbstractNode

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

the class PagingNodeTransformer method transform.

@Override
public Node transform(Node node, List<String> args) {
    checkNotNull(node);
    checkArgument(!args.isEmpty(), "paging requires at least one parameter, .e.g: property|paging(page, pageSize)");
    checkNotNull(node.getProperty());
    int page;
    int pageSize = Pager.DEFAULT_PAGE_SIZE;
    try {
        page = Integer.parseInt(args.get(0));
        if (args.size() > 1) {
            pageSize = Integer.parseInt(args.get(1));
        }
    } catch (NumberFormatException ex) {
        return node;
    }
    Property property = node.getProperty();
    if (property.isCollection()) {
        Pager pager = new Pager(page, node.getChildren().size(), pageSize);
        ((AbstractNode) node).setChildren(PagerUtils.pageCollection(node.getChildren(), pager));
        return node;
    }
    return node;
}
Also used : AbstractNode(org.hisp.dhis.node.AbstractNode) Pager(org.hisp.dhis.common.Pager) Property(org.hisp.dhis.schema.Property)

Aggregations

AbstractNode (org.hisp.dhis.node.AbstractNode)3 Node (org.hisp.dhis.node.Node)2 CollectionNode (org.hisp.dhis.node.types.CollectionNode)2 Property (org.hisp.dhis.schema.Property)2 Collection (java.util.Collection)1 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)1 EmbeddedObject (org.hisp.dhis.common.EmbeddedObject)1 Pager (org.hisp.dhis.common.Pager)1 ComplexNode (org.hisp.dhis.node.types.ComplexNode)1 RootNode (org.hisp.dhis.node.types.RootNode)1 SimpleNode (org.hisp.dhis.node.types.SimpleNode)1 PeriodType (org.hisp.dhis.period.PeriodType)1 Query (org.hisp.dhis.query.Query)1 Schema (org.hisp.dhis.schema.Schema)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1