Search in sources :

Example 16 with Node

use of org.openforis.idm.model.Node in project collect by openforis.

the class AbstractExpression method iterateMultiple.

protected void iterateMultiple(Node<?> contextNode, Node<?> thisNode, NodeVisitor visitor) throws InvalidExpressionException {
    try {
        JXPathContext jxPathContext = createJXPathContext(contextNode, thisNode);
        Iterator<?> pointers = compiledExpression.iteratePointers(jxPathContext);
        while (pointers.hasNext()) {
            Object item = pointers.next();
            if (item instanceof ModelNodePointer) {
                ModelNodePointer modelNodePointer = (ModelNodePointer) item;
                Object ptrNode = modelNodePointer.getNode();
                if (ptrNode != null && ptrNode instanceof Node) {
                    Node<?> node = (Node<?>) ptrNode;
                    visitor.visit(node, node.getIndex());
                }
            } else if (item instanceof VariablePointer && ((VariablePointer) item).getName().equals(THIS)) {
                visitor.visit(thisNode, thisNode.getIndex());
            }
        // ignore node pointer if it's a NullPointer
        }
    } catch (IllegalArgumentException e) {
        throw new InvalidExpressionException(e.getMessage(), this.compiledExpression.toString());
    } catch (JXPathInvalidSyntaxException e) {
        throw new InvalidExpressionException(e.getMessage());
    }
}
Also used : JXPathInvalidSyntaxException(org.apache.commons.jxpath.JXPathInvalidSyntaxException) ModelJXPathContext(org.openforis.idm.model.expression.internal.ModelJXPathContext) JXPathContext(org.apache.commons.jxpath.JXPathContext) VariablePointer(org.apache.commons.jxpath.ri.model.VariablePointer) Node(org.openforis.idm.model.Node) ModelNodePointer(org.openforis.idm.model.expression.internal.ModelNodePointer)

Example 17 with Node

use of org.openforis.idm.model.Node in project collect by openforis.

the class PathElement method evaluate.

@Override
public List<Node<?>> evaluate(Record context) {
    Entity root = context.getRootEntity();
    if (root.getName().equals(name) && (index == null || index == 1)) {
        List<Node<?>> nodes = new ArrayList<Node<?>>(1);
        nodes.add(root);
        return Collections.unmodifiableList(nodes);
    } else {
        return Collections.emptyList();
    }
}
Also used : Entity(org.openforis.idm.model.Entity) Node(org.openforis.idm.model.Node) ArrayList(java.util.ArrayList)

Example 18 with Node

use of org.openforis.idm.model.Node in project collect by openforis.

the class CodeColumnProvider method extractValues.

@Override
public List<String> extractValues(Node<?> axis) {
    List<String> values = super.extractValues(axis);
    if (hasExpandedItems) {
        List<Node<?>> attributes = extractNodes(axis);
        for (CodeListItem item : expandedItems) {
            CodeAttribute attr = findAttributeByCode(attributes, item.getCode());
            values.add(Boolean.valueOf(attr != null).toString());
            if (item.isQualifiable()) {
                values.add(attr == null ? "" : attr.getValue().getQualifier());
            }
        }
    }
    return values;
}
Also used : CodeAttribute(org.openforis.idm.model.CodeAttribute) Node(org.openforis.idm.model.Node) CodeListItem(org.openforis.idm.metamodel.CodeListItem)

Example 19 with Node

use of org.openforis.idm.model.Node in project collect by openforis.

the class RecordUpdater method afterAttributeInsertOrUpdate.

private NodeChangeSet afterAttributeInsertOrUpdate(NodeChangeMap changeMap, Attribute<?, ?> attribute) {
    attribute.updateSummaryInfo();
    Record record = attribute.getRecord();
    NodePointer attributeNodePointer = new NodePointer(attribute);
    List<Attribute<?, ?>> updatedAttributes = new ArrayList<Attribute<?, ?>>();
    // calculated attributes
    List<Attribute<?, ?>> updatedCalculatedAttributes = recalculateDependentCalculatedAttributes(attribute);
    updatedAttributes.addAll(updatedCalculatedAttributes);
    changeMap.addValueChanges(updatedCalculatedAttributes);
    // dependent code attributes
    if (attribute instanceof CodeAttribute && clearDependentCodeAttributes) {
        Set<CodeAttribute> updatedCodeAttributes = clearDependentCodeAttributes(attribute);
        updatedAttributes.addAll(updatedCodeAttributes);
        changeMap.addValueChanges(updatedCodeAttributes);
    }
    if (validateAfterUpdate) {
        // relevance
        Collection<Node<?>> nodesToCheckRelevanceFor = new ArrayList<Node<?>>(updatedAttributes);
        nodesToCheckRelevanceFor.add(attribute);
        List<NodePointer> relevanceToUpdate = record.determineRelevanceDependentNodes(nodesToCheckRelevanceFor);
        RelevanceUpdater relevanceUpdater = new RelevanceUpdater(relevanceToUpdate);
        Set<NodePointer> updatedRelevancePointers = relevanceUpdater.update();
        Set<Node<?>> updatedRelevanceNodes = pointersToNodes(updatedRelevancePointers);
        // apply default values to relevant nodes (if not applied yet)
        for (Node<?> updatedRelevanceNode : updatedRelevanceNodes) {
            if (updatedRelevanceNode instanceof Attribute) {
                Attribute<?, ?> updatedRelevanceAttr = (Attribute<?, ?>) updatedRelevanceNode;
                if (!updatedRelevanceAttr.getDefinition().isCalculated()) {
                    if (updatedRelevanceAttr.isEmpty() || isDefaultValueApplied(updatedRelevanceAttr)) {
                        Value appliedValue = applyInitialValue(updatedRelevanceAttr);
                        if (appliedValue != null) {
                            updatedAttributes.add((Attribute<?, ?>) updatedRelevanceNode);
                        }
                    }
                }
            }
        }
        changeMap.addRelevanceChanges(updatedRelevancePointers);
        if (clearNotRelevantAttributes) {
            Set<Attribute<?, ?>> noMoreRelevantAttributes = retainNotRelevantAttributes(updatedRelevanceNodes);
            Set<Attribute<?, ?>> clearedAttributes = clearUserSpecifiedAttributes(noMoreRelevantAttributes);
            updatedAttributes.addAll(clearedAttributes);
            changeMap.addValueChanges(clearedAttributes);
        }
        // min count
        Collection<NodePointer> pointersToCheckMinCountFor = new HashSet<NodePointer>(updatedRelevancePointers);
        pointersToCheckMinCountFor.add(attributeNodePointer);
        pointersToCheckMinCountFor.addAll(nodesToPointers(updatedAttributes));
        Collection<NodePointer> minCountPointersToUpdate = record.determineMinCountDependentNodes(pointersToCheckMinCountFor);
        Collection<NodePointer> updatedMinCountPointers = updateMinCount(minCountPointersToUpdate);
        changeMap.addMinCountChanges(updatedMinCountPointers);
        // max count
        Collection<NodePointer> pointersToCheckMaxCountFor = new HashSet<NodePointer>(updatedRelevancePointers);
        pointersToCheckMaxCountFor.add(attributeNodePointer);
        pointersToCheckMaxCountFor.addAll(nodesToPointers(updatedAttributes));
        Collection<NodePointer> maxCountPointersToUpdate = record.determineMaxCountDependentNodes(pointersToCheckMaxCountFor);
        Collection<NodePointer> updatedMaxCountPointers = updateMaxCount(maxCountPointersToUpdate);
        changeMap.addMaxCountChanges(updatedMaxCountPointers);
        Set<NodePointer> updatedCardinalityPointers = new HashSet<NodePointer>(updatedMinCountPointers);
        updatedCardinalityPointers.addAll(updatedMaxCountPointers);
        // validate cardinality
        List<NodePointer> ancestorsAndSelfPointers = getAncestorsAndSelfPointers(attribute);
        Set<NodePointer> pointersToValidateCardinalityFor = new HashSet<NodePointer>(updatedMinCountPointers.size() + updatedMaxCountPointers.size() + updatedRelevancePointers.size() + ancestorsAndSelfPointers.size());
        pointersToValidateCardinalityFor.addAll(nodesToPointers(updatedAttributes));
        pointersToValidateCardinalityFor.addAll(updatedMinCountPointers);
        pointersToValidateCardinalityFor.addAll(updatedMaxCountPointers);
        pointersToValidateCardinalityFor.addAll(updatedRelevancePointers);
        // validate cardinality on ancestor node pointers because we are considering empty nodes as missing nodes
        pointersToValidateCardinalityFor.addAll(ancestorsAndSelfPointers);
        validateCardinality(record, pointersToValidateCardinalityFor, changeMap);
        // validate attributes
        Set<Node<?>> nodesToCheckValidationFor = new HashSet<Node<?>>(updatedAttributes);
        nodesToCheckValidationFor.add(attribute);
        nodesToCheckValidationFor.addAll(updatedRelevanceNodes);
        nodesToCheckValidationFor.addAll(pointersToNodes(updatedCardinalityPointers));
        Set<Attribute<?, ?>> attributesToRevalidate = record.determineValidationDependentNodes(nodesToCheckValidationFor);
        validateAttributes(record, attributesToRevalidate, changeMap);
    }
    return changeMap;
}
Also used : CodeAttribute(org.openforis.idm.model.CodeAttribute) Attribute(org.openforis.idm.model.Attribute) BooleanAttribute(org.openforis.idm.model.BooleanAttribute) Node(org.openforis.idm.model.Node) ArrayList(java.util.ArrayList) NodePointer(org.openforis.idm.model.NodePointer) CodeAttribute(org.openforis.idm.model.CodeAttribute) Value(org.openforis.idm.model.Value) BooleanValue(org.openforis.idm.model.BooleanValue) Record(org.openforis.idm.model.Record) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 20 with Node

use of org.openforis.idm.model.Node in project collect by openforis.

the class RecordUpdater method moveNode.

public void moveNode(CollectRecord record, int nodeId, int index) {
    Node<?> node = record.getNodeByInternalId(nodeId);
    Entity parent = node.getParent();
    List<Node<?>> siblings = parent.getChildren(node.getDefinition());
    int oldIndex = siblings.indexOf(node);
    parent.move(node.getDefinition(), oldIndex, index);
}
Also used : Entity(org.openforis.idm.model.Entity) Node(org.openforis.idm.model.Node)

Aggregations

Node (org.openforis.idm.model.Node)37 Entity (org.openforis.idm.model.Entity)19 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)11 AbstractTest (org.openforis.idm.AbstractTest)11 NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)8 CollectRecord (org.openforis.collect.model.CollectRecord)6 RealAttribute (org.openforis.idm.model.RealAttribute)6 Attribute (org.openforis.idm.model.Attribute)5 CodeAttribute (org.openforis.idm.model.CodeAttribute)5 Code (org.openforis.idm.model.Code)4 Record (org.openforis.idm.model.Record)4 HashSet (java.util.HashSet)3 LinkedHashSet (java.util.LinkedHashSet)3 XPathDataQueryEvaluator (org.openforis.collect.datacleansing.xpath.XPathDataQueryEvaluator)3 CollectRecordSummary (org.openforis.collect.model.CollectRecordSummary)3 CollectSurvey (org.openforis.collect.model.CollectSurvey)3 RecordFilter (org.openforis.collect.model.RecordFilter)3 AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)3 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)3