Search in sources :

Example 21 with Schema

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

the class DefaultFieldFilterService method updateFields.

private void updateFields(FieldMap fieldMap, Class<?> klass, boolean expandOnly) {
    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();
            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 : CollectionNode(org.hisp.dhis.node.types.CollectionNode) ReflectionUtils(org.hisp.dhis.system.util.ReflectionUtils) Autowired(org.springframework.beans.factory.annotation.Autowired) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) UserCredentials(org.hisp.dhis.user.UserCredentials) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) NodeTransformer(org.hisp.dhis.node.NodeTransformer) Lists(com.google.common.collect.Lists) AbstractNode(org.hisp.dhis.node.AbstractNode) Matcher(java.util.regex.Matcher) Preset(org.hisp.dhis.node.Preset) ImmutableMap(com.google.common.collect.ImmutableMap) Node(org.hisp.dhis.node.Node) Collection(java.util.Collection) Set(java.util.Set) SchemaService(org.hisp.dhis.schema.SchemaService) Property(org.hisp.dhis.schema.Property) Sets(com.google.common.collect.Sets) SimpleNode(org.hisp.dhis.node.types.SimpleNode) List(java.util.List) ComplexNode(org.hisp.dhis.node.types.ComplexNode) PostConstruct(javax.annotation.PostConstruct) PeriodType(org.hisp.dhis.period.PeriodType) Log(org.apache.commons.logging.Log) Schema(org.hisp.dhis.schema.Schema) LogFactory(org.apache.commons.logging.LogFactory) Pattern(java.util.regex.Pattern) Joiner(com.google.common.base.Joiner) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) 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) ArrayList(java.util.ArrayList) List(java.util.List) Property(org.hisp.dhis.schema.Property)

Example 22 with Schema

use of org.hisp.dhis.schema.Schema 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;
    }
    ComplexNode complexNode = new ComplexNode(currentProperty.getName());
    complexNode.setNamespace(currentProperty.getNamespace());
    complexNode.setProperty(currentProperty);
    Schema schema;
    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) Property(org.hisp.dhis.schema.Property) SimpleNode(org.hisp.dhis.node.types.SimpleNode)

Example 23 with Schema

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

the class DefaultFieldFilterService method filter.

@Override
public CollectionNode filter(Class<?> klass, List<?> objects, List<String> fieldList) {
    String fields = fieldList == null ? "" : Joiner.on(",").join(fieldList);
    Schema rootSchema = schemaService.getDynamicSchema(klass);
    CollectionNode collectionNode = new CollectionNode(rootSchema.getCollectionName());
    collectionNode.setNamespace(rootSchema.getNamespace());
    if (objects == null || objects.isEmpty()) {
        return collectionNode;
    }
    FieldMap fieldMap = new FieldMap();
    Schema schema = schemaService.getDynamicSchema(objects.get(0).getClass());
    if (StringUtils.isEmpty(fields)) {
        for (Property property : schema.getProperties()) {
            fieldMap.put(property.getName(), new FieldMap());
        }
    } else {
        fieldMap = fieldParser.parse(fields);
    }
    final FieldMap finalFieldMap = fieldMap;
    objects.forEach(object -> collectionNode.addChild(buildNode(finalFieldMap, klass, object)));
    return collectionNode;
}
Also used : Schema(org.hisp.dhis.schema.Schema) CollectionNode(org.hisp.dhis.node.types.CollectionNode) Property(org.hisp.dhis.schema.Property)

Example 24 with Schema

use of org.hisp.dhis.schema.Schema 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 25 with Schema

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

the class HibernateMinMaxDataElementStore method parseFilter.

private Criteria parseFilter(Criteria criteria, List<String> filters) {
    Conjunction conjunction = Restrictions.conjunction();
    Schema schema = schemaService.getDynamicSchema(MinMaxDataElement.class);
    if (!filters.isEmpty()) {
        for (String filter : filters) {
            String[] split = filter.split(":");
            if (split.length != 3) {
                throw new QueryParserException("Invalid filter: " + filter);
            }
            QueryPath queryPath = queryPlanner.getQueryPath(schema, split[0]);
            Property property = queryParser.getProperty(schema, split[0]);
            Criterion restriction = getRestriction(property, queryPath.getPath(), split[1], split[2]);
            if (restriction != null) {
                conjunction.add(restriction);
                if (queryPath.haveAlias()) {
                    for (String alias : queryPath.getAlias()) {
                        criteria.createAlias(alias, alias);
                    }
                }
            }
        }
    }
    criteria.add(conjunction);
    return criteria;
}
Also used : QueryPath(org.hisp.dhis.query.planner.QueryPath) QueryParserException(org.hisp.dhis.query.QueryParserException) Criterion(org.hibernate.criterion.Criterion) Schema(org.hisp.dhis.schema.Schema) Conjunction(org.hibernate.criterion.Conjunction) Property(org.hisp.dhis.schema.Property)

Aggregations

Schema (org.hisp.dhis.schema.Schema)149 Authority (org.hisp.dhis.security.Authority)65 Property (org.hisp.dhis.schema.Property)29 ArrayList (java.util.ArrayList)20 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)20 Test (org.junit.Test)16 Collection (java.util.Collection)14 List (java.util.List)13 HashMap (java.util.HashMap)12 DhisSpringTest (org.hisp.dhis.DhisSpringTest)12 EmbeddedObject (org.hisp.dhis.common.EmbeddedObject)12 Map (java.util.Map)10 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)10 User (org.hisp.dhis.user.User)10 AnalyticalObject (org.hisp.dhis.common.AnalyticalObject)9 BaseAnalyticalObject (org.hisp.dhis.common.BaseAnalyticalObject)9 UserCredentials (org.hisp.dhis.user.UserCredentials)9 HashSet (java.util.HashSet)8 Set (java.util.Set)8 Log (org.apache.commons.logging.Log)8