Search in sources :

Example 1 with NodePointer

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

the class RecordUpdater method validateCardinality.

private Set<NodePointer> validateCardinality(Record record, Collection<NodePointer> pointers, NodeChangeMap changeMap) {
    Set<NodePointer> updatedPointers = new HashSet<NodePointer>();
    Validator validator = record.getSurveyContext().getValidator();
    for (NodePointer nodePointer : pointers) {
        Entity entity = nodePointer.getEntity();
        NodeDefinition childDef = nodePointer.getChildDefinition();
        ValidationResultFlag minCountResult, maxCountResult;
        if (entity.isRelevant(childDef)) {
            minCountResult = validator.validateMinCount(entity, childDef);
            maxCountResult = validator.validateMaxCount(entity, childDef);
        } else {
            minCountResult = maxCountResult = ValidationResultFlag.OK;
        }
        if (entity.getMinCountValidationResult(childDef) != minCountResult) {
            entity.setMinCountValidationResult(childDef, minCountResult);
            changeMap.addMinCountValidationResultChange(nodePointer, minCountResult);
            updatedPointers.add(nodePointer);
        }
        if (entity.getMaxCountValidationResult(childDef) != maxCountResult) {
            entity.setMaxCountValidationResult(childDef, maxCountResult);
            changeMap.addMaxCountValidationResultChange(nodePointer, maxCountResult);
            updatedPointers.add(nodePointer);
        }
    }
    return updatedPointers;
}
Also used : Entity(org.openforis.idm.model.Entity) ValidationResultFlag(org.openforis.idm.metamodel.validation.ValidationResultFlag) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) NodePointer(org.openforis.idm.model.NodePointer) Validator(org.openforis.idm.metamodel.validation.Validator) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 2 with NodePointer

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

the class RecordUpdater method getDescendantNodePointers.

private List<NodePointer> getDescendantNodePointers(Entity entity) {
    ModelVersion version = entity.getRecord().getVersion();
    List<NodePointer> pointers = new ArrayList<NodePointer>();
    EntityDefinition definition = entity.getDefinition();
    for (NodeDefinition childDef : definition.getChildDefinitionsInVersion(version)) {
        pointers.add(new NodePointer(entity, childDef));
        if (childDef instanceof EntityDefinition) {
            for (Node<?> childEntity : entity.getChildren(childDef)) {
                pointers.addAll(getDescendantNodePointers((Entity) childEntity));
            }
        }
    }
    return pointers;
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) Entity(org.openforis.idm.model.Entity) ArrayList(java.util.ArrayList) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) ModelVersion(org.openforis.idm.metamodel.ModelVersion) NodePointer(org.openforis.idm.model.NodePointer)

Example 3 with NodePointer

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

the class RecordUpdater method updateMinCount.

private Collection<NodePointer> updateMinCount(Collection<NodePointer> nodePointers) {
    List<NodePointer> updatedPointers = new ArrayList<NodePointer>();
    for (NodePointer nodePointer : nodePointers) {
        Entity entity = nodePointer.getEntity();
        NodeDefinition childDef = nodePointer.getChildDefinition();
        Integer oldCount = entity.getMinCount(childDef);
        int newCount = calculateMinCount(nodePointer);
        entity.setMinCount(childDef, newCount);
        if (oldCount == null || oldCount.intValue() != newCount) {
            updatedPointers.add(nodePointer);
        }
    }
    return updatedPointers;
}
Also used : Entity(org.openforis.idm.model.Entity) ArrayList(java.util.ArrayList) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) NodePointer(org.openforis.idm.model.NodePointer)

Example 4 with NodePointer

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

the class RecordUpdater method getChildNodePointers.

private List<NodePointer> getChildNodePointers(Entity entity) {
    ModelVersion version = entity.getRecord().getVersion();
    List<NodePointer> pointers = new ArrayList<NodePointer>();
    EntityDefinition definition = entity.getDefinition();
    for (NodeDefinition childDef : definition.getChildDefinitionsInVersion(version)) {
        pointers.add(new NodePointer(entity, childDef));
    }
    return pointers;
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) ArrayList(java.util.ArrayList) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) ModelVersion(org.openforis.idm.metamodel.ModelVersion) NodePointer(org.openforis.idm.model.NodePointer)

Example 5 with NodePointer

use of org.openforis.idm.model.NodePointer 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)

Aggregations

NodePointer (org.openforis.idm.model.NodePointer)14 ArrayList (java.util.ArrayList)7 Entity (org.openforis.idm.model.Entity)6 NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)5 HashSet (java.util.HashSet)4 LinkedHashSet (java.util.LinkedHashSet)4 Attribute (org.openforis.idm.model.Attribute)3 BooleanAttribute (org.openforis.idm.model.BooleanAttribute)3 CodeAttribute (org.openforis.idm.model.CodeAttribute)3 Node (org.openforis.idm.model.Node)3 Record (org.openforis.idm.model.Record)3 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)2 ModelVersion (org.openforis.idm.metamodel.ModelVersion)2 ValidationResultFlag (org.openforis.idm.metamodel.validation.ValidationResultFlag)1 Validator (org.openforis.idm.metamodel.validation.Validator)1 BooleanValue (org.openforis.idm.model.BooleanValue)1 Value (org.openforis.idm.model.Value)1