Search in sources :

Example 1 with ComparisonCheck

use of org.openforis.idm.metamodel.validation.ComparisonCheck in project collect by openforis.

the class ValidationMessageBuilder method getMessageArgs.

protected String[] getMessageArgs(Attribute<?, ?> attribute, ValidationResult validationResult, Locale locale) {
    ValidationRule<?> validator = validationResult.getValidator();
    if (validator instanceof ComparisonCheck) {
        ComparisonCheck check = (ComparisonCheck) validator;
        ArrayList<String> args = new ArrayList<String>();
        String labelText = getPrettyLabelText(attribute.getDefinition(), locale);
        args.add(labelText);
        Map<String, String> expressions = new HashMap<String, String>();
        expressions.put("lt", check.getLessThanExpression());
        expressions.put("lte", check.getLessThanOrEqualsExpression());
        expressions.put("gt", check.getGreaterThanExpression());
        expressions.put("gte", check.getGreaterThanOrEqualsExpression());
        for (String key : expressions.keySet()) {
            String expression = expressions.get(key);
            if (expression != null) {
                String argPart1 = key;
                String argPart2 = getComparisonCheckMessageArg(attribute, expression, locale);
                String arg = StringUtils.join(argPart1, MULTIPLE_MESSAGE_ARGS_SEPARATOR, argPart2);
                args.add(arg);
            }
        }
        return args.toArray(new String[args.size()]);
    } else if (validator instanceof EntityKeyValidator) {
        EntityDefinition parentDefn = attribute.getDefinition().getParentEntityDefinition();
        String parentLabel = getPrettyLabelText(parentDefn, locale.getLanguage());
        List<AttributeDefinition> keyDefns = parentDefn.getKeyAttributeDefinitions();
        List<String> keyDefnLabels = new ArrayList<String>(keyDefns.size());
        for (AttributeDefinition keyDefn : keyDefns) {
            keyDefnLabels.add(getPrettyLabelText(keyDefn, locale.getLanguage()));
        }
        // TODO localize separator
        return new String[] { parentLabel, StringUtils.join(keyDefnLabels, ", ") };
    } else {
        return null;
    }
}
Also used : EntityDefinition(org.openforis.idm.metamodel.EntityDefinition) HashMap(java.util.HashMap) ComparisonCheck(org.openforis.idm.metamodel.validation.ComparisonCheck) ArrayList(java.util.ArrayList) AttributeDefinition(org.openforis.idm.metamodel.AttributeDefinition) EntityKeyValidator(org.openforis.idm.metamodel.validation.EntityKeyValidator) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with ComparisonCheck

use of org.openforis.idm.metamodel.validation.ComparisonCheck 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 3 with ComparisonCheck

use of org.openforis.idm.metamodel.validation.ComparisonCheck in project collect by openforis.

the class ValidationMessageBuilder method getMessageKey.

protected String getMessageKey(Attribute<?, ?> attribute, ValidationResult validationResult) {
    String key = null;
    ValidationRule<?> validator = validationResult.getValidator();
    if (validator instanceof CodeValidator) {
        key = "validation.codeError";
    } else if (validator instanceof CodeParentValidator) {
        key = "validation.codeParentError";
    } else if (validator instanceof ComparisonCheck) {
        key = "validation.comparisonError";
    } else if (validator instanceof CoordinateValidator) {
        if (attribute.isFilled()) {
            key = "validation.coordinateError";
        } else {
            key = "validation.incompleteCoordinateError";
        }
    } else if (validator instanceof DateValidator) {
        if (attribute.isFilled()) {
            key = "validation.dateError";
        } else {
            key = "validation.incompleteDateError";
        }
    } else if (validator instanceof DistanceCheck) {
        key = "validation.distanceError";
    } else if (validator instanceof EntityKeyValidator) {
        key = "validation.entityKeyUniquenessError";
    } else if (validator instanceof ExternalCodeValidator) {
        key = "validation.externalCodeError";
    } else if (validator instanceof IntegerRangeValidator) {
        key = "validation.integerRangeError";
    } else if (validator instanceof PatternCheck) {
        key = "validation.patternError";
    } else if (validator instanceof RealRangeValidator) {
        key = "validation.realRangeError";
    } else if (validator instanceof RecordKeyUniquenessValidator) {
        key = "validation.recordKeyUniquenessError";
    } else if (validator instanceof SpecifiedValidator) {
        if (validationResult.getFlag() == ValidationResultFlag.ERROR) {
            key = "validation.specifiedError";
        } else {
            key = "validation.requiredField";
        }
    } else if (validator instanceof TimeValidator) {
        if (attribute.isFilled()) {
            key = "validation.timeError";
        } else {
            key = "validation.incompleteTimeError";
        }
    } else if (validator instanceof TaxonVernacularLanguageValidator) {
        TaxonAttribute taxonAttr = (TaxonAttribute) attribute;
        if (StringUtils.isNotBlank(taxonAttr.getVernacularName()) && StringUtils.isBlank(taxonAttr.getLanguageCode())) {
            key = "validator.taxon.missingVernacularLanguage";
        } else {
            key = "validator.taxon.vernacularLanguageNotRequired";
        }
    } else if (validator instanceof UniquenessCheck) {
        key = "validation.uniquenessError";
    } else if (validator instanceof NumberValueUnitValidator || validator instanceof NumericRangeUnitValidator) {
        key = "validation.unitNotSpecifiedError";
    }
    return key;
}
Also used : DateValidator(org.openforis.idm.metamodel.validation.DateValidator) IntegerRangeValidator(org.openforis.idm.metamodel.validation.IntegerRangeValidator) TimeValidator(org.openforis.idm.metamodel.validation.TimeValidator) TaxonVernacularLanguageValidator(org.openforis.idm.metamodel.validation.TaxonVernacularLanguageValidator) NumberValueUnitValidator(org.openforis.idm.metamodel.validation.NumberValueUnitValidator) ExternalCodeValidator(org.openforis.idm.metamodel.validation.ExternalCodeValidator) CodeValidator(org.openforis.idm.metamodel.validation.CodeValidator) ComparisonCheck(org.openforis.idm.metamodel.validation.ComparisonCheck) EntityKeyValidator(org.openforis.idm.metamodel.validation.EntityKeyValidator) NumericRangeUnitValidator(org.openforis.idm.metamodel.validation.NumericRangeUnitValidator) PatternCheck(org.openforis.idm.metamodel.validation.PatternCheck) RealRangeValidator(org.openforis.idm.metamodel.validation.RealRangeValidator) TaxonAttribute(org.openforis.idm.model.TaxonAttribute) DistanceCheck(org.openforis.idm.metamodel.validation.DistanceCheck) ExternalCodeValidator(org.openforis.idm.metamodel.validation.ExternalCodeValidator) UniquenessCheck(org.openforis.idm.metamodel.validation.UniquenessCheck) CodeParentValidator(org.openforis.idm.metamodel.validation.CodeParentValidator) CoordinateValidator(org.openforis.idm.metamodel.validation.CoordinateValidator)

Example 4 with ComparisonCheck

use of org.openforis.idm.metamodel.validation.ComparisonCheck in project collect by openforis.

the class StateDependencyMap method registerDependencies.

private void registerDependencies(AttributeDefinition attributeDefinition) {
    List<AttributeDefault> attributeDefaults = attributeDefinition.getAttributeDefaults();
    for (AttributeDefault attributeDefault : attributeDefaults) {
        registerDependencies(attributeDefinition, attributeDefault.getCondition(), defaultValueDependencies);
        registerDependencies(attributeDefinition, attributeDefault.getExpression(), defaultValueDependencies);
    }
    List<Check> checks = attributeDefinition.getChecks();
    for (Check check : checks) {
        registerDependencies(attributeDefinition, check.getCondition(), checkDependencies);
        if (check instanceof ComparisonCheck) {
            registerDependencies(attributeDefinition, ((ComparisonCheck) check).getEqualsExpression(), checkDependencies);
            registerDependencies(attributeDefinition, ((ComparisonCheck) check).getLessThanExpression(), checkDependencies);
            registerDependencies(attributeDefinition, ((ComparisonCheck) check).getLessThanOrEqualsExpression(), checkDependencies);
            registerDependencies(attributeDefinition, ((ComparisonCheck) check).getGreaterThanExpression(), checkDependencies);
            registerDependencies(attributeDefinition, ((ComparisonCheck) check).getGreaterThanOrEqualsExpression(), checkDependencies);
        } else if (check instanceof CustomCheck) {
            registerDependencies(attributeDefinition, ((CustomCheck) check).getExpression(), checkDependencies);
        } else if (check instanceof DistanceCheck) {
            registerDependencies(attributeDefinition, ((DistanceCheck) check).getDestinationPointExpression(), checkDependencies);
            registerDependencies(attributeDefinition, ((DistanceCheck) check).getMaxDistanceExpression(), checkDependencies);
            registerDependencies(attributeDefinition, ((DistanceCheck) check).getMinDistanceExpression(), checkDependencies);
            registerDependencies(attributeDefinition, ((DistanceCheck) check).getSourcePointExpression(), checkDependencies);
        } else if (check instanceof UniquenessCheck) {
            registerDependencies(attributeDefinition, ((UniquenessCheck) check).getExpression(), checkDependencies);
        }
    }
}
Also used : CustomCheck(org.openforis.idm.metamodel.validation.CustomCheck) DistanceCheck(org.openforis.idm.metamodel.validation.DistanceCheck) UniquenessCheck(org.openforis.idm.metamodel.validation.UniquenessCheck) AttributeDefault(org.openforis.idm.metamodel.AttributeDefault) CustomCheck(org.openforis.idm.metamodel.validation.CustomCheck) UniquenessCheck(org.openforis.idm.metamodel.validation.UniquenessCheck) Check(org.openforis.idm.metamodel.validation.Check) DistanceCheck(org.openforis.idm.metamodel.validation.DistanceCheck) ComparisonCheck(org.openforis.idm.metamodel.validation.ComparisonCheck) ComparisonCheck(org.openforis.idm.metamodel.validation.ComparisonCheck)

Aggregations

ComparisonCheck (org.openforis.idm.metamodel.validation.ComparisonCheck)4 DistanceCheck (org.openforis.idm.metamodel.validation.DistanceCheck)3 UniquenessCheck (org.openforis.idm.metamodel.validation.UniquenessCheck)3 ArrayList (java.util.ArrayList)2 CustomCheck (org.openforis.idm.metamodel.validation.CustomCheck)2 EntityKeyValidator (org.openforis.idm.metamodel.validation.EntityKeyValidator)2 PatternCheck (org.openforis.idm.metamodel.validation.PatternCheck)2 HashMap (java.util.HashMap)1 List (java.util.List)1 AttributeDefault (org.openforis.idm.metamodel.AttributeDefault)1 AttributeDefinition (org.openforis.idm.metamodel.AttributeDefinition)1 EntityDefinition (org.openforis.idm.metamodel.EntityDefinition)1 ExpressionValidationResult (org.openforis.idm.metamodel.expression.ExpressionValidator.ExpressionValidationResult)1 Check (org.openforis.idm.metamodel.validation.Check)1 CodeParentValidator (org.openforis.idm.metamodel.validation.CodeParentValidator)1 CodeValidator (org.openforis.idm.metamodel.validation.CodeValidator)1 CoordinateValidator (org.openforis.idm.metamodel.validation.CoordinateValidator)1 DateValidator (org.openforis.idm.metamodel.validation.DateValidator)1 ExternalCodeValidator (org.openforis.idm.metamodel.validation.ExternalCodeValidator)1 IntegerRangeValidator (org.openforis.idm.metamodel.validation.IntegerRangeValidator)1