Example usage for org.springframework.validation ValidationUtils invokeValidator

List of usage examples for org.springframework.validation ValidationUtils invokeValidator

Introduction

In this page you can find the example usage for org.springframework.validation ValidationUtils invokeValidator.

Prototype

public static void invokeValidator(Validator validator, Object target, Errors errors,
        @Nullable Object... validationHints) 

Source Link

Document

Invoke the given Validator / SmartValidator for the supplied object and Errors instance.

Usage

From source file:org.opentestsystem.authoring.testauth.validation.ScoringRuleValidator.java

@Override
public void validate(final Object obj, final Errors errors) {
    // execute JSR-303 validations (annotations)
    this.jsrValidator.validate(obj, errors);

    final ScoringRule scoringRule = (ScoringRule) obj;

    // parameterDataMap can only be evaluated if corresponding ComputationRule is passed along
    if (StringUtils.isNotBlank(scoringRule.getComputationRuleId())
            && scoringRule.getComputationRule() != null) {
        validateFileUploads(errors, scoringRule);

        if (!CollectionUtils.isEmpty(scoringRule.getParameters())) {
            // check for duplicate parameter names
            final Map<String, Collection<ScoringRuleParameter>> duplicates = Maps.filterEntries(
                    Multimaps.index(scoringRule.getParameters(), RULE_PARAMETER_TO_NAME_TRANSFORMER).asMap(),
                    PARAMETER_DUPLICATE_FILTER);
            if (!duplicates.isEmpty()) {
                rejectValue(errors, PARAMETERS,
                        getErrorMessageRoot() + PARAMETERS + MSG_PARAMETER_NAME + MSG_DUPLICATES,
                        duplicates.keySet().toString());
            }//from   w ww . j ava2  s.  c  o  m

            for (int i = 0; i < scoringRule.getParameters().size(); i++) {
                final ScoringRuleParameter scoringRuleParameter = scoringRule.getParameters().get(i);
                try {
                    errors.pushNestedPath(PARAMETERS + "[" + i + "]");
                    final ComputationRuleParameter computationRuleParameter = Iterables
                            .find(scoringRule.getComputationRule().getParameters(), FUNCTION_PARAMETER_FINDER
                                    .getInstance(scoringRuleParameter.getComputationRuleParameterName()));
                    if (computationRuleParameter == null) {
                        rejectValue(errors, PARAMETER_NAME,
                                getErrorMessageRoot() + PARAMETERS + MSG_PARAMETER_NAME + MSG_NOT_FOUND,
                                scoringRuleParameter.getComputationRuleParameterName());
                    } else {
                        // evaluate every value within the rule parameter to ensure it adheres to the constraints of the ComputationRuleParameter
                        ValidationUtils.invokeValidator(this.scoringRuleParameterValidator,
                                scoringRuleParameter, errors, computationRuleParameter);
                    }
                } catch (final NoSuchElementException e) {
                    rejectValue(errors, PARAMETER_NAME,
                            getErrorMessageRoot() + PARAMETERS + MSG_PARAMETER_NAME + MSG_NOT_FOUND,
                            scoringRuleParameter.getComputationRuleParameterName());
                } finally {
                    errors.popNestedPath();
                }
            }

            for (final ComputationRuleParameter computationRuleParameter : scoringRule.getComputationRule()
                    .getParameters()) {
                if (!Iterables.any(scoringRule.getParameters(),
                        RULE_PARAMETER_FINDER.getInstance(computationRuleParameter.getParameterName()))) {
                    rejectValue(errors, PARAMETERS,
                            getErrorMessageRoot() + PARAMETERS + MSG_PARAMETER_NAME + MSG_MISSING,
                            computationRuleParameter.getParameterName());
                }
            }
        }
    }
}