Search in sources :

Example 1 with ExpressionValidationResult

use of org.openforis.idm.metamodel.expression.ExpressionValidator.ExpressionValidationResult in project collect by openforis.

the class PatternCheckFormValidator method validateExpression.

private boolean validateExpression(ValidationContext ctx) {
    if (!validateRequired(ctx, REGULAR_EXPRESSION_FIELD)) {
        return false;
    }
    ExpressionValidator expressionValidator = getExpressionValidator(ctx);
    String expression = getValue(ctx, REGULAR_EXPRESSION_FIELD);
    ExpressionValidationResult result = expressionValidator.validateRegularExpression(expression);
    if (result.isError()) {
        String message = Strings.firstNotBlank(result.getDetailedMessage(), result.getMessage());
        addInvalidMessage(ctx, REGULAR_EXPRESSION_FIELD, getMessage(INVALID_EXPRESSION_MESSAGE_KEY, message));
        return false;
    }
    return true;
}
Also used : ExpressionValidator(org.openforis.idm.metamodel.expression.ExpressionValidator) ExpressionValidationResult(org.openforis.idm.metamodel.expression.ExpressionValidator.ExpressionValidationResult)

Example 2 with ExpressionValidationResult

use of org.openforis.idm.metamodel.expression.ExpressionValidator.ExpressionValidationResult in project collect by openforis.

the class UniquenessCheckFormValidator method validateExpression.

private boolean validateExpression(ValidationContext ctx) {
    if (validateRequired(ctx, EXPRESSION_FIELD)) {
        ExpressionValidator expressionValidator = getExpressionValidator(ctx);
        NodeDefinition contextNode = getContextNode(ctx);
        String expression = getValue(ctx, EXPRESSION_FIELD);
        ExpressionValidationResult result = expressionValidator.validateUniquenessExpression(contextNode.getParentDefinition(), contextNode, expression);
        if (result.isError()) {
            String message = Strings.firstNotBlank(result.getDetailedMessage(), result.getMessage());
            addInvalidMessage(ctx, EXPRESSION_FIELD, getMessage(INVALID_EXPRESSION_MESSAGE_KEY, message));
            return false;
        }
    }
    return true;
}
Also used : NodeDefinition(org.openforis.idm.metamodel.NodeDefinition) ExpressionValidator(org.openforis.idm.metamodel.expression.ExpressionValidator) ExpressionValidationResult(org.openforis.idm.metamodel.expression.ExpressionValidator.ExpressionValidationResult)

Example 3 with ExpressionValidationResult

use of org.openforis.idm.metamodel.expression.ExpressionValidator.ExpressionValidationResult in project collect by openforis.

the class ExpressionValidatorTest method validate.

private ExpressionValidationResult validate(Survey survey, String contextNodeDefPath, String expression) {
    ExpressionValidator validator = new ExpressionValidator(survey.getContext().getExpressionFactory());
    ExpressionValidationResult result = validator.validateValueExpression(survey.getSchema().getDefinitionByPath(contextNodeDefPath), expression);
    return result;
}
Also used : ExpressionValidationResult(org.openforis.idm.metamodel.expression.ExpressionValidator.ExpressionValidationResult)

Example 4 with ExpressionValidationResult

use of org.openforis.idm.metamodel.expression.ExpressionValidator.ExpressionValidationResult in project collect by openforis.

the class SurveyValidator method validateCheck.

private List<SurveyValidationResult> validateCheck(AttributeDefinition node, Check<?> check) {
    List<SurveyValidationResult> results = new ArrayList<SurveyValidationResult>();
    // validate condition expression
    addBooleanExpressionValidationResult(results, node, check.getCondition(), "survey.validation.check.error.invalid_condition_expression");
    if (check instanceof ComparisonCheck) {
        addBooleanExpressionValidationResult(results, node, ((ComparisonCheck) check).getExpression(), "survey.validation.check.comparison.error.invalid_comparison_expression");
    } else if (check instanceof CustomCheck) {
        addBooleanExpressionValidationResult(results, node, ((CustomCheck) check).getExpression(), "survey.validation.check.custom.error.error.invalid_custom_expression");
    } else if (check instanceof DistanceCheck) {
        // validate source point
        addValueExpressionValidationResult(results, node, ((DistanceCheck) check).getSourcePointExpression(), "survey.validation.check.distance.error.invalid_source_point_expression");
        // validate destination point
        addValueExpressionValidationResult(results, node, ((DistanceCheck) check).getDestinationPointExpression(), "survey.validation.check.distance.error.invalid_destination_point_expression");
        // validate min distance
        addValueExpressionValidationResult(results, node, ((DistanceCheck) check).getMinDistanceExpression(), "survey.validation.check.distance.error.invalid_min_distance_expression");
        // validate max distance
        addValueExpressionValidationResult(results, node, ((DistanceCheck) check).getMaxDistanceExpression(), "survey.validation.check.distance.error.invalid_max_distance_expression");
    } else if (check instanceof PatternCheck) {
        String regEx = ((PatternCheck) check).getRegularExpression();
        if (StringUtils.isNotBlank(regEx)) {
            ExpressionValidationResult result = expressionValidator.validateRegularExpression(regEx);
            if (result.isError()) {
                results.add(new SurveyValidationResult(node.getPath(), "survey.validation.check.pattern.error.invalid_pattern_expression", result.getMessage()));
            }
        }
    } else if (check instanceof UniquenessCheck) {
        String expression = ((UniquenessCheck) check).getExpression();
        if (StringUtils.isNotBlank(expression)) {
            ExpressionValidationResult result = expressionValidator.validateUniquenessExpression(node.getParentEntityDefinition(), node, expression);
            if (result.isError()) {
                results.add(new SurveyValidationResult(node.getPath(), "survey.validation.check.uniqueness.error.invalid_uniqueness_expression", result.getMessage()));
            }
        }
    }
    return results;
}
Also used : PatternCheck(org.openforis.idm.metamodel.validation.PatternCheck) CustomCheck(org.openforis.idm.metamodel.validation.CustomCheck) DistanceCheck(org.openforis.idm.metamodel.validation.DistanceCheck) UniquenessCheck(org.openforis.idm.metamodel.validation.UniquenessCheck) ArrayList(java.util.ArrayList) ComparisonCheck(org.openforis.idm.metamodel.validation.ComparisonCheck) ExpressionValidationResult(org.openforis.idm.metamodel.expression.ExpressionValidator.ExpressionValidationResult)

Example 5 with ExpressionValidationResult

use of org.openforis.idm.metamodel.expression.ExpressionValidator.ExpressionValidationResult in project collect by openforis.

the class SimpleValidator method validateExpression.

protected boolean validateExpression(Errors errors, NodeDefinition contextNodeDef, NodeDefinition thisNodeDef, String field, String expression, ExpressionType type) {
    ExpressionValidationResult result = expressionValidator.validateExpression(type, contextNodeDef, thisNodeDef, expression);
    if (result.isError()) {
        String errorCode = "validation.invalid_expression";
        String validationMessage = StringUtils.defaultString(result.getDetailedMessage(), result.getMessage());
        String[] errorMessageArgs = new String[] { validationMessage };
        String defaultMessage = messageSource.getMessage(errorCode, errorMessageArgs, Locale.ENGLISH);
        errors.rejectValue(field, errorCode, defaultMessage);
    }
    return result.isOk();
}
Also used : ExpressionValidationResult(org.openforis.idm.metamodel.expression.ExpressionValidator.ExpressionValidationResult)

Aggregations

ExpressionValidationResult (org.openforis.idm.metamodel.expression.ExpressionValidator.ExpressionValidationResult)7 ExpressionValidator (org.openforis.idm.metamodel.expression.ExpressionValidator)3 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 JXPathCompiledExpression (org.apache.commons.jxpath.ri.JXPathCompiledExpression)1 Expression (org.apache.commons.jxpath.ri.compiler.Expression)1 Operation (org.apache.commons.jxpath.ri.compiler.Operation)1 NodeDefinition (org.openforis.idm.metamodel.NodeDefinition)1 ComparisonCheck (org.openforis.idm.metamodel.validation.ComparisonCheck)1 CustomCheck (org.openforis.idm.metamodel.validation.CustomCheck)1 DistanceCheck (org.openforis.idm.metamodel.validation.DistanceCheck)1 PatternCheck (org.openforis.idm.metamodel.validation.PatternCheck)1 UniquenessCheck (org.openforis.idm.metamodel.validation.UniquenessCheck)1