Search in sources :

Example 1 with IdmInterpretationError

use of org.openforis.idm.metamodel.IdmInterpretationError in project collect by openforis.

the class CustomCheck method evaluate.

@Override
public ValidationResultFlag evaluate(Attribute<?, ?> node) {
    try {
        ExpressionEvaluator expressionEvaluator = node.getSurvey().getContext().getExpressionEvaluator();
        boolean valid = expressionEvaluator.evaluateBoolean(node.getParent(), node, getExpression());
        return ValidationResultFlag.valueOf(valid, this.getFlag());
    } catch (InvalidExpressionException e) {
        throw new IdmInterpretationError("Error evaluating custom check", e);
    }
}
Also used : IdmInterpretationError(org.openforis.idm.metamodel.IdmInterpretationError) InvalidExpressionException(org.openforis.idm.model.expression.InvalidExpressionException) ExpressionEvaluator(org.openforis.idm.model.expression.ExpressionEvaluator)

Example 2 with IdmInterpretationError

use of org.openforis.idm.metamodel.IdmInterpretationError in project collect by openforis.

the class UniquenessCheck method evaluate.

@Override
public ValidationResultFlag evaluate(final Attribute<?, ?> attribute) {
    try {
        SurveyContext recordContext = attribute.getRecord().getSurveyContext();
        ExpressionEvaluator expressionEvaluator = recordContext.getExpressionEvaluator();
        Node<?> duplicateNode = expressionEvaluator.findNode(attribute.getParent(), attribute, expression, new Predicate<Node<?>>() {

            public boolean evaluate(Node<?> node) {
                if (node instanceof Attribute && node != attribute) {
                    Value value = ((Attribute<?, ?>) node).getValue();
                    if (value != null && value.equals(attribute.getValue())) {
                        return true;
                    }
                }
                return false;
            }
        });
        boolean unique = duplicateNode == null;
        return ValidationResultFlag.valueOf(unique, this.getFlag());
    } catch (InvalidExpressionException e) {
        throw new IdmInterpretationError("Error evaluating uniqueness check", e);
    }
}
Also used : IdmInterpretationError(org.openforis.idm.metamodel.IdmInterpretationError) Attribute(org.openforis.idm.model.Attribute) InvalidExpressionException(org.openforis.idm.model.expression.InvalidExpressionException) Node(org.openforis.idm.model.Node) Value(org.openforis.idm.model.Value) ExpressionEvaluator(org.openforis.idm.model.expression.ExpressionEvaluator) SurveyContext(org.openforis.idm.metamodel.SurveyContext)

Example 3 with IdmInterpretationError

use of org.openforis.idm.metamodel.IdmInterpretationError in project collect by openforis.

the class Check method getMessageWithEvaluatedExpressions.

public String getMessageWithEvaluatedExpressions(Attribute<?, ?> context, String preferredLanguage) {
    Survey survey = context.getSurvey();
    String message;
    if (preferredLanguage == null || survey.isDefaultLanguage(preferredLanguage)) {
        message = getMessage(survey.getDefaultLanguage());
    } else {
        message = getMessageInPreferredLanguage(survey, preferredLanguage);
    }
    if (StringUtils.isBlank(message)) {
        return null;
    } else {
        try {
            StringBuffer sb = new StringBuffer();
            Matcher matcher = MESSAGE_NESTED_EXPRESSION_PATTERN.matcher(message);
            while (matcher.find()) {
                String expr = matcher.group(1);
                Object val = getExpressionEvaluator(context).evaluateValue(context.getParent(), context, expr);
                String replacement = val == null ? "" : val.toString();
                matcher.appendReplacement(sb, replacement);
            }
            matcher.appendTail(sb);
            return sb.toString();
        } catch (InvalidExpressionException e) {
            throw new IdmInterpretationError("Unable to evaluate condition " + condition, e);
        }
    }
}
Also used : IdmInterpretationError(org.openforis.idm.metamodel.IdmInterpretationError) Survey(org.openforis.idm.metamodel.Survey) Matcher(java.util.regex.Matcher) InvalidExpressionException(org.openforis.idm.model.expression.InvalidExpressionException)

Example 4 with IdmInterpretationError

use of org.openforis.idm.metamodel.IdmInterpretationError in project collect by openforis.

the class RecordUpdater method calculateMaxCount.

private int calculateMaxCount(Entity entity, NodeDefinition defn) {
    String expression = defn.getMaxCountExpression();
    if (!entity.isRelevant(defn) || StringUtils.isBlank(expression)) {
        return defn.isMultiple() ? Integer.MAX_VALUE : 1;
    }
    if (defn.getFixedMaxCount() != null) {
        return defn.getFixedMaxCount();
    }
    try {
        SurveyContext surveyContext = defn.getSurvey().getContext();
        ExpressionEvaluator expressionEvaluator = surveyContext.getExpressionEvaluator();
        Number value = expressionEvaluator.evaluateNumericValue(entity, null, expression);
        return value == null ? Integer.MAX_VALUE : 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 5 with IdmInterpretationError

use of org.openforis.idm.metamodel.IdmInterpretationError 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)

Aggregations

IdmInterpretationError (org.openforis.idm.metamodel.IdmInterpretationError)6 InvalidExpressionException (org.openforis.idm.model.expression.InvalidExpressionException)6 ExpressionEvaluator (org.openforis.idm.model.expression.ExpressionEvaluator)5 SurveyContext (org.openforis.idm.metamodel.SurveyContext)4 Matcher (java.util.regex.Matcher)1 Survey (org.openforis.idm.metamodel.Survey)1 Attribute (org.openforis.idm.model.Attribute)1 Node (org.openforis.idm.model.Node)1 Record (org.openforis.idm.model.Record)1 Value (org.openforis.idm.model.Value)1