Search in sources :

Example 1 with Preset

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

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

the class DefaultFieldFilterService method init.

@PostConstruct
public void init() {
    ImmutableMap.Builder<String, Preset> presetBuilder = ImmutableMap.builder();
    for (Preset preset : Preset.values()) {
        presetBuilder.put(preset.getName(), preset);
    }
    presets = presetBuilder.build();
    ImmutableMap.Builder<String, NodeTransformer> transformerBuilder = ImmutableMap.builder();
    for (NodeTransformer transformer : nodeTransformers) {
        transformerBuilder.put(transformer.name(), transformer);
    }
    transformers = transformerBuilder.build();
}
Also used : NodeTransformer(org.hisp.dhis.node.NodeTransformer) Preset(org.hisp.dhis.node.Preset) ImmutableMap(com.google.common.collect.ImmutableMap) PostConstruct(javax.annotation.PostConstruct)

Aggregations

ImmutableMap (com.google.common.collect.ImmutableMap)2 PostConstruct (javax.annotation.PostConstruct)2 NodeTransformer (org.hisp.dhis.node.NodeTransformer)2 Preset (org.hisp.dhis.node.Preset)2 Joiner (com.google.common.base.Joiner)1 Lists (com.google.common.collect.Lists)1 Sets (com.google.common.collect.Sets)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Log (org.apache.commons.logging.Log)1 LogFactory (org.apache.commons.logging.LogFactory)1 EmbeddedObject (org.hisp.dhis.common.EmbeddedObject)1 AbstractNode (org.hisp.dhis.node.AbstractNode)1 Node (org.hisp.dhis.node.Node)1 CollectionNode (org.hisp.dhis.node.types.CollectionNode)1