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);
}
}
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;
}
}
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);
}
}
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);
}
}
}
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);
}
}
Aggregations