Search in sources :

Example 1 with NodeVisitor

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

the class EventProducer method produceFor.

public List<RecordEvent> produceFor(CollectRecord record, final String userName) {
    final List<RecordEvent> events = new ArrayList<RecordEvent>();
    final Integer recordId = record.getId();
    final RecordStep recordStep = record.getStep().toRecordStep();
    record.getRootEntity().traverse(new NodeVisitor() {

        public void visit(Node<? extends NodeDefinition> node, int idx) {
            NodeDefinition nodeDef = node.getDefinition();
            List<String> ancestorIds = getAncestorIds(nodeDef, node.getAncestorIds());
            EventFactory factory = new EventFactory(recordId, recordStep, ancestorIds, node, userName);
            if (node instanceof Entity) {
                events.addAll(factory.entityCreated());
            } else if (node instanceof Attribute) {
                if (nodeDef.isMultiple()) {
                    events.addAll(factory.attributeCreated());
                } else {
                    events.addAll(factory.attributeUpdated());
                }
            }
        }
    });
    return events;
}
Also used : Entity(org.openforis.idm.model.Entity) CoordinateAttribute(org.openforis.idm.model.CoordinateAttribute) CodeAttribute(org.openforis.idm.model.CodeAttribute) Attribute(org.openforis.idm.model.Attribute) DateAttribute(org.openforis.idm.model.DateAttribute) TimeAttribute(org.openforis.idm.model.TimeAttribute) NumberAttribute(org.openforis.idm.model.NumberAttribute) BooleanAttribute(org.openforis.idm.model.BooleanAttribute) TextAttribute(org.openforis.idm.model.TextAttribute) NumericRangeAttribute(org.openforis.idm.model.NumericRangeAttribute) TaxonAttribute(org.openforis.idm.model.TaxonAttribute) ArrayList(java.util.ArrayList) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) NodeVisitor(org.openforis.idm.model.NodeVisitor) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List)

Example 2 with NodeVisitor

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

the class AbstractExpression method evaluateMultiple.

/**
 * Returns a list of Node that matches the expression
 *
 * @throws InvalidExpressionException
 */
public List<Node<?>> evaluateMultiple(Node<?> contextNode, final Node<?> thisNode) throws InvalidExpressionException {
    final List<Node<?>> list = new ArrayList<Node<?>>();
    iterateMultiple(contextNode, thisNode, new NodeVisitor() {

        public void visit(Node<?> item, int index) {
            list.add(item);
        }
    });
    return list;
}
Also used : Node(org.openforis.idm.model.Node) ArrayList(java.util.ArrayList) NodeVisitor(org.openforis.idm.model.NodeVisitor)

Example 3 with NodeVisitor

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

the class RecordConverter method convertToLatestUnitStorage.

/**
 * This is a workaround: it avoids to refer to the old unit field to get data
 *
 * TODO remove this conversion or apply only for records stored using version prior to 3.0 Alpha 5
 *
 * @param survey
 * @param rootEntity
 */
protected void convertToLatestUnitStorage(CollectRecord record) {
    final CollectSurvey survey = (CollectSurvey) record.getSurvey();
    Entity rootEntity = record.getRootEntity();
    rootEntity.traverse(new NodeVisitor() {

        @Override
        public void visit(Node<? extends NodeDefinition> node, int idx) {
            if (node instanceof NumberAttribute<?, ?> || node instanceof NumericRangeAttribute<?, ?>) {
                Field<String> unitNameField;
                Field<Integer> unitField;
                if (node instanceof NumberAttribute<?, ?>) {
                    unitNameField = ((NumberAttribute<?, ?>) node).getUnitNameField();
                    unitField = ((NumberAttribute<?, ?>) node).getUnitField();
                } else {
                    unitNameField = ((NumericRangeAttribute<?, ?>) node).getUnitNameField();
                    unitField = ((NumericRangeAttribute<?, ?>) node).getUnitField();
                }
                if (unitNameField.hasData()) {
                    moveDataToNewUnitField(survey, unitNameField, unitField);
                }
            }
        }
    });
}
Also used : Entity(org.openforis.idm.model.Entity) Field(org.openforis.idm.model.Field) NumericRangeAttribute(org.openforis.idm.model.NumericRangeAttribute) NumberAttribute(org.openforis.idm.model.NumberAttribute) CollectSurvey(org.openforis.collect.model.CollectSurvey) NodeVisitor(org.openforis.idm.model.NodeVisitor)

Example 4 with NodeVisitor

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

the class RecordIndexManager method index.

protected void index(final IndexWriter indexWriter, CollectRecord record) throws RecordIndexException {
    try {
        Entity rootEntity = record.getRootEntity();
        rootEntity.traverse(new NodeVisitor() {

            @Override
            public void visit(Node<? extends NodeDefinition> node, int idx) {
                NodeDefinition defn = node.getDefinition();
                if (defn instanceof AttributeDefinition) {
                    index(indexWriter, (Attribute<?, ?>) node);
                }
            }
        });
    } catch (Exception e) {
        throw new RecordIndexException(e);
    }
}
Also used : Entity(org.openforis.idm.model.Entity) Attribute(org.openforis.idm.model.Attribute) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) ParseException(org.apache.lucene.queryParser.ParseException) IOException(java.io.IOException) NodeVisitor(org.openforis.idm.model.NodeVisitor)

Example 5 with NodeVisitor

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

Aggregations

NodeVisitor (org.openforis.idm.model.NodeVisitor)8 Entity (org.openforis.idm.model.Entity)6 ArrayList (java.util.ArrayList)5 NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)3 Attribute (org.openforis.idm.model.Attribute)3 List (java.util.List)2 AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)2 BooleanAttribute (org.openforis.idm.model.BooleanAttribute)2 CodeAttribute (org.openforis.idm.model.CodeAttribute)2 NumberAttribute (org.openforis.idm.model.NumberAttribute)2 NumericRangeAttribute (org.openforis.idm.model.NumericRangeAttribute)2 IOException (java.io.IOException)1 Arrays.asList (java.util.Arrays.asList)1 Collections.emptyList (java.util.Collections.emptyList)1 ParseException (org.apache.lucene.queryParser.ParseException)1 CollectSurvey (org.openforis.collect.model.CollectSurvey)1 BooleanAttributeDefinition (org.openforis.idm.metamodel.BooleanAttributeDefinition)1 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)1 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)1 ModelVersion (org.openforis.idm.metamodel.ModelVersion)1