Search in sources :

Example 11 with Value

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

the class RecordUpdater method applyInitialValues.

private List<Attribute<?, ?>> applyInitialValues(Entity entity) {
    final List<Attribute<?, ?>> updatedAttributes = new ArrayList<Attribute<?, ?>>();
    entity.traverse(new NodeVisitor() {

        public void visit(Node<?> node, int idx) {
            if (node instanceof Attribute && node.isEmpty()) {
                Attribute<?, ?> attr = (Attribute<?, ?>) node;
                Value value = applyInitialValue(attr);
                if (value != null) {
                    updatedAttributes.add(attr);
                }
            }
        }
    });
    return updatedAttributes;
}
Also used : CodeAttribute(org.openforis.idm.model.CodeAttribute) Attribute(org.openforis.idm.model.Attribute) BooleanAttribute(org.openforis.idm.model.BooleanAttribute) ArrayList(java.util.ArrayList) Value(org.openforis.idm.model.Value) BooleanValue(org.openforis.idm.model.BooleanValue) NodeVisitor(org.openforis.idm.model.NodeVisitor)

Example 12 with Value

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

the class RecordUpdater method recalculateValues.

@SuppressWarnings({ "rawtypes", "unchecked" })
private List<Attribute<?, ?>> recalculateValues(List<Attribute<?, ?>> attributesToRecalculate) {
    List<Attribute<?, ?>> updatedAttributes = new ArrayList<Attribute<?, ?>>();
    for (Attribute calcAttr : attributesToRecalculate) {
        Value previousValue = calcAttr.getValue();
        Value newValue = recalculateValue(calcAttr);
        if (!((previousValue == newValue) || (previousValue != null && previousValue.equals(newValue)))) {
            calcAttr.setValue(newValue);
            calcAttr.updateSummaryInfo();
            updatedAttributes.add(calcAttr);
        }
    }
    return updatedAttributes;
}
Also used : CodeAttribute(org.openforis.idm.model.CodeAttribute) Attribute(org.openforis.idm.model.Attribute) BooleanAttribute(org.openforis.idm.model.BooleanAttribute) ArrayList(java.util.ArrayList) Value(org.openforis.idm.model.Value) BooleanValue(org.openforis.idm.model.BooleanValue)

Example 13 with Value

use of org.openforis.idm.model.Value 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 14 with Value

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

the class CSVWriterDataQueryResultItemProcessor method process.

@Override
public void process(DataQueryResultItem item) {
    List<String> lineValues = new ArrayList<String>();
    lineValues.addAll(item.getRecordKeyValues());
    lineValues.add(item.extractNodePath());
    AttributeDefinition attrDef = item.getAttributeDefinition();
    Value value = item.extractAttributeValue();
    Map<String, Object> valueMap = value == null ? null : value.toMap();
    List<String> fieldNames = attrDef.getFieldNames();
    for (String fieldName : fieldNames) {
        Object fieldValue = valueMap == null ? null : valueMap.get(fieldName);
        lineValues.add(fieldValue == null ? "" : fieldValue.toString());
    }
    csvWriter.writeNext(lineValues);
}
Also used : ArrayList(java.util.ArrayList) Value(org.openforis.idm.model.Value) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition)

Example 15 with Value

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

the class AttributeUpdateRequestProxy method toAttributeUpdateRequest.

@Override
@SuppressWarnings("unchecked")
public AttributeUpdateRequest<?> toAttributeUpdateRequest(CodeListManager codeListManager, RecordSessionManager sessionManager, CollectRecord record) {
    AttributeUpdateRequest<Value> opts = new NodeUpdateRequest.AttributeUpdateRequest<Value>();
    Attribute<?, ?> attribute = (Attribute<?, ?>) record.getNodeByInternalId(nodeId);
    opts.setAttribute((Attribute<?, Value>) attribute);
    Value parsedValue;
    if (attribute instanceof FileAttribute) {
        parsedValue = parseFileAttributeValue(sessionManager, record, nodeId, value);
    } else if (value == null) {
        parsedValue = null;
    } else {
        Entity parentEntity = attribute.getParent();
        String attributeName = attribute.getName();
        parsedValue = parseCompositeAttributeValue(codeListManager, parentEntity, attributeName, value);
    }
    opts.setValue(parsedValue);
    opts.setSymbol(symbol);
    return opts;
}
Also used : Entity(org.openforis.idm.model.Entity) AttributeUpdateRequest(org.openforis.collect.remoting.service.NodeUpdateRequest.AttributeUpdateRequest) Attribute(org.openforis.idm.model.Attribute) FileAttribute(org.openforis.idm.model.FileAttribute) Value(org.openforis.idm.model.Value) FileAttribute(org.openforis.idm.model.FileAttribute)

Aggregations

Value (org.openforis.idm.model.Value)26 AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)9 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)8 AbstractValue (org.openforis.idm.model.AbstractValue)6 Attribute (org.openforis.idm.model.Attribute)6 BooleanValue (org.openforis.idm.model.BooleanValue)6 ArrayList (java.util.ArrayList)5 Entity (org.openforis.idm.model.Entity)5 NodeChangeSet (org.openforis.collect.model.NodeChangeSet)4 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)4 BooleanAttribute (org.openforis.idm.model.BooleanAttribute)4 EventProducer (org.openforis.collect.event.EventProducer)3 RecordEvent (org.openforis.collect.event.RecordEvent)3 CollectRecord (org.openforis.collect.model.CollectRecord)3 CollectSurvey (org.openforis.collect.model.CollectSurvey)3 CodeAttribute (org.openforis.idm.model.CodeAttribute)3 EntityKeysIdentifier (org.openforis.collect.io.data.DataLine.EntityKeysIdentifier)2 EntityPositionIdentifier (org.openforis.collect.io.data.DataLine.EntityPositionIdentifier)2 CollectRecordSummary (org.openforis.collect.model.CollectRecordSummary)2 BooleanAttributeDefinition (org.openforis.idm.metamodel.BooleanAttributeDefinition)2