Search in sources :

Example 6 with ExpressionEvaluator

use of org.openforis.idm.model.expression.ExpressionEvaluator in project collect by openforis.

the class RecordUpdater method calculateMinCount.

private int calculateMinCount(Entity entity, NodeDefinition defn) {
    String expression = defn.getMinCountExpression();
    if (!entity.isRelevant(defn) || StringUtils.isBlank(expression)) {
        return 0;
    }
    if (defn.getFixedMinCount() != null) {
        return defn.getFixedMinCount();
    }
    try {
        SurveyContext surveyContext = defn.getSurvey().getContext();
        ExpressionEvaluator expressionEvaluator = surveyContext.getExpressionEvaluator();
        Number value = expressionEvaluator.evaluateNumericValue(entity, null, expression);
        return value == null ? 0 : value.intValue();
    } catch (InvalidExpressionException e) {
        throw new IdmInterpretationError("Error evaluating required expression", e);
    }
}
Also used : IdmInterpretationError(org.openforis.idm.metamodel.IdmInterpretationError) InvalidExpressionException(org.openforis.idm.model.expression.InvalidExpressionException) ExpressionEvaluator(org.openforis.idm.model.expression.ExpressionEvaluator) SurveyContext(org.openforis.idm.metamodel.SurveyContext)

Example 7 with ExpressionEvaluator

use of org.openforis.idm.model.expression.ExpressionEvaluator in project collect by openforis.

the class DescendantNodeFilter method accept.

@Override
public boolean accept(Node<?> node) {
    NodeDefinition nodeDef = node.getDefinition();
    if (!(nodeDef instanceof EntityDefinition) || !descendantAttributeDefinition.isDescendantOf((EntityDefinition) nodeDef)) {
        return false;
    }
    Record record = node.getRecord();
    SurveyContext surveyContext = record.getSurveyContext();
    ExpressionEvaluator expressionEvaluator = surveyContext.getExpressionEvaluator();
    List<Node<?>> attributes = record.findNodesByPath(descendantAttributeDefinition.getPath());
    for (Node<?> attribute : attributes) {
        try {
            Entity parentEntity = attribute.getParent();
            if (parentEntity == node && expressionEvaluator.evaluateBoolean(parentEntity, attribute, descendantAttributeCondition)) {
                return true;
            }
        } catch (InvalidExpressionException e) {
            throw new RuntimeException(e);
        }
    }
    return false;
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) Entity(org.openforis.idm.model.Entity) InvalidExpressionException(org.openforis.idm.model.expression.InvalidExpressionException) Node(org.openforis.idm.model.Node) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) Record(org.openforis.idm.model.Record) ExpressionEvaluator(org.openforis.idm.model.expression.ExpressionEvaluator) SurveyContext(org.openforis.idm.metamodel.SurveyContext)

Example 8 with ExpressionEvaluator

use of org.openforis.idm.model.expression.ExpressionEvaluator in project collect by openforis.

the class ValidationMessageBuilder method getComparisonCheckMessageArg.

protected String getComparisonCheckMessageArg(Attribute<?, ?> attribute, String expression, Locale locale) {
    if (StringUtils.isNotBlank(expression)) {
        String result = expression;
        Survey survey = attribute.getSurvey();
        Schema schema = survey.getSchema();
        SurveyContext surveyContext = survey.getContext();
        ExpressionEvaluator expressionEvaluator = surveyContext.getExpressionEvaluator();
        try {
            Entity parentEntity = attribute.getParent();
            EntityDefinition parentDefinition = parentEntity.getDefinition();
            Set<String> referencedPaths = expressionEvaluator.determineReferencedPaths(expression);
            for (String path : referencedPaths) {
                String absolutePath = parentDefinition.getPath() + PATH_SEPARATOR + path;
                NodeDefinition nodeDefinition = schema.getDefinitionByPath(absolutePath);
                String label = getPrettyLabelText(nodeDefinition, locale);
                result = result.replaceAll(nodeDefinition.getName(), label);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    } else {
        return expression;
    }
}
Also used : Entity(org.openforis.idm.model.Entity) EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) Survey(org.openforis.idm.metamodel.Survey) Schema(org.openforis.idm.metamodel.Schema) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) ExpressionEvaluator(org.openforis.idm.model.expression.ExpressionEvaluator) SurveyContext(org.openforis.idm.metamodel.SurveyContext)

Example 9 with ExpressionEvaluator

use of org.openforis.idm.model.expression.ExpressionEvaluator in project collect by openforis.

the class XPathDataQueryEvaluator method evaluate.

@Override
public List<Node<?>> evaluate(CollectRecord record) {
    List<Node<?>> result = new ArrayList<Node<?>>();
    String condition = query.getConditions();
    AttributeDefinition attrDef = query.getAttributeDefinition();
    SurveyContext surveyContext = record.getSurveyContext();
    ExpressionEvaluator expressionEvaluator = surveyContext.getExpressionEvaluator();
    List<Node<?>> nodes = record.findNodesByPath(attrDef.getPath());
    for (Node<?> node : nodes) {
        try {
            if (expressionEvaluator.evaluateBoolean(node.getParent(), node, condition)) {
                result.add(node);
            }
        } catch (InvalidExpressionException e) {
            throw new RuntimeException(e);
        }
    }
    return result;
}
Also used : InvalidExpressionException(org.openforis.idm.model.expression.InvalidExpressionException) Node(org.openforis.idm.model.Node) ArrayList(java.util.ArrayList) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) ExpressionEvaluator(org.openforis.idm.model.expression.ExpressionEvaluator) SurveyContext(org.openforis.idm.metamodel.SurveyContext)

Example 10 with ExpressionEvaluator

use of org.openforis.idm.model.expression.ExpressionEvaluator in project collect by openforis.

the class Check method getExpressionEvaluator.

private ExpressionEvaluator getExpressionEvaluator(Attribute<?, ?> context) {
    Record record = context.getRecord();
    SurveyContext surveyContext = record.getSurveyContext();
    ExpressionEvaluator evaluator = surveyContext.getExpressionEvaluator();
    return evaluator;
}
Also used : Record(org.openforis.idm.model.Record) ExpressionEvaluator(org.openforis.idm.model.expression.ExpressionEvaluator) SurveyContext(org.openforis.idm.metamodel.SurveyContext)

Aggregations

ExpressionEvaluator (org.openforis.idm.model.expression.ExpressionEvaluator)14 InvalidExpressionException (org.openforis.idm.model.expression.InvalidExpressionException)10 SurveyContext (org.openforis.idm.metamodel.SurveyContext)9 IdmInterpretationError (org.openforis.idm.metamodel.IdmInterpretationError)5 Node (org.openforis.idm.model.Node)3 Record (org.openforis.idm.model.Record)3 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)2 NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)2 Survey (org.openforis.idm.metamodel.Survey)2 Entity (org.openforis.idm.model.Entity)2 FileNotFoundException (java.io.FileNotFoundException)1 ArrayList (java.util.ArrayList)1 CodeListImportException (org.openforis.collect.io.exception.CodeListImportException)1 CollectSurvey (org.openforis.collect.model.CollectSurvey)1 CoordinateOperations (org.openforis.idm.geospatial.CoordinateOperations)1 AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)1 Schema (org.openforis.idm.metamodel.Schema)1 IdmlParseException (org.openforis.idm.metamodel.xml.IdmlParseException)1 Attribute (org.openforis.idm.model.Attribute)1 CodeAttribute (org.openforis.idm.model.CodeAttribute)1