Search in sources :

Example 1 with InvalidExpressionException

use of org.openforis.idm.model.expression.InvalidExpressionException 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 InvalidExpressionException

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

the class DistanceCheck method evaluateAttributeValueExpression.

private <T extends Object> T evaluateAttributeValueExpression(String expression, CoordinateAttribute thisNode) {
    if (expression == null) {
        return null;
    }
    try {
        ExpressionEvaluator expressionEvaluator = getExpressionEvaluator(thisNode);
        T result = expressionEvaluator.evaluateAttributeValue(thisNode.getParent(), thisNode, thisNode.getDefinition(), expression);
        return result;
    } catch (InvalidExpressionException e) {
        if (LOG.isWarnEnabled()) {
            LOG.warn(String.format("[survey %s: coordinate attribute: %s] Unable to evaluate expression %s" + thisNode.getSurvey().getName(), thisNode.getPath(), expression), e);
        }
        return null;
    }
}
Also used : InvalidExpressionException(org.openforis.idm.model.expression.InvalidExpressionException) ExpressionEvaluator(org.openforis.idm.model.expression.ExpressionEvaluator)

Example 3 with InvalidExpressionException

use of org.openforis.idm.model.expression.InvalidExpressionException 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 4 with InvalidExpressionException

use of org.openforis.idm.model.expression.InvalidExpressionException 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 5 with InvalidExpressionException

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

the class MaxCountValidatorTest method updateMaxCount.

// protected boolean containsMaxCountError(List<ValidationResult> errors, String name) {
// for (ValidationResult result : errors) {
// ValidationRule validator = result.getValidator();
// if (validator instanceof MaxCountValidator) {
// MaxCountValidator v = (MaxCountValidator) validator;
// NodeDefinition nodeDefinition = v.getNodeDefinition();
// if (nodeDefinition.getName().equals(name)) {
// return true;
// }
// }
// }
// return false;
// }
private void updateMaxCount(Entity entity, String childName) {
    NodeDefinition childDef = entity.getDefinition().getChildDefinition(childName);
    try {
        String expr = childDef.getMaxCountExpression();
        Integer count = null;
        if (StringUtils.isNotBlank(expr)) {
            Number val = expressionEvaluator.evaluateNumericValue(entity, null, expr);
            count = val.intValue();
        } else if (!childDef.isMultiple()) {
            count = 1;
        }
        entity.setMaxCount(childDef, count);
    } catch (InvalidExpressionException e) {
        throw new RuntimeException(e);
    }
}
Also used : InvalidExpressionException(org.openforis.idm.model.expression.InvalidExpressionException) NodeDefinition(org.openforis.idm.metamodel.NodeDefinition)

Aggregations

InvalidExpressionException (org.openforis.idm.model.expression.InvalidExpressionException)16 ExpressionEvaluator (org.openforis.idm.model.expression.ExpressionEvaluator)10 IdmInterpretationError (org.openforis.idm.metamodel.IdmInterpretationError)6 SurveyContext (org.openforis.idm.metamodel.SurveyContext)6 AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)3 NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)3 Node (org.openforis.idm.model.Node)3 AttributeDefault (org.openforis.idm.metamodel.AttributeDefault)2 BooleanAttributeDefinition (org.openforis.idm.metamodel.BooleanAttributeDefinition)2 CodeAttributeDefinition (org.openforis.idm.metamodel.CodeAttributeDefinition)2 Record (org.openforis.idm.model.Record)2 Value (org.openforis.idm.model.Value)2 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 DataTransformation (org.openforis.collect.io.data.csv.DataTransformation)1 ModelCsvWriter (org.openforis.collect.io.data.csv.ModelCsvWriter)1 CollectRecord (org.openforis.collect.model.CollectRecord)1