Example usage for org.springframework.validation BindingResult addError

List of usage examples for org.springframework.validation BindingResult addError

Introduction

In this page you can find the example usage for org.springframework.validation BindingResult addError.

Prototype

void addError(ObjectError error);

Source Link

Document

Add a custom ObjectError or FieldError to the errors list.

Usage

From source file:org.opentestsystem.authoring.testauth.service.impl.ReportingMeasureServiceImpl.java

private void validateReportingMeasure(final ReportingMeasure reportingMeasure) {
    final BindingResult bindingResult = new BeanPropertyBindingResult(reportingMeasure, "reportingMeasure");
    if (reportingMeasure.getBlueprintReferenceType() != null
            && reportingMeasure.getBlueprintReferenceId() != null) {
        switch (reportingMeasure.getBlueprintReferenceType()) {
        case TEST:
            if (!reportingMeasure.getBlueprintReferenceId().equals(reportingMeasure.getAssessmentId())) {
                formatReferenceIdMessage(bindingResult, reportingMeasure, BPREF_ID_NOT_FOUND);
            }//from  w  w  w.  j  av a  2  s . c  o  m
            reportingMeasure.setBlueprintDenotationType(null);
            break;
        case SEGMENT:
            final Segment segment = this.segmentService.getSegment(reportingMeasure.getBlueprintReferenceId());
            if (segment == null) {
                formatReferenceIdMessage(bindingResult, reportingMeasure, BPREF_ID_NOT_FOUND);
            }
            reportingMeasure.setBlueprintDenotationType(null);
            break;
        case AFFINITY_GROUP:
            final AffinityGroup affinityGroup = this.affinityGroupService
                    .getAffinityGroup(reportingMeasure.getBlueprintReferenceId());
            if (affinityGroup == null) {
                formatReferenceIdMessage(bindingResult, reportingMeasure, BPREF_ID_NOT_FOUND);
            }
            reportingMeasure.setBlueprintDenotationType(null);
            break;
        case STANDARD:
            if (reportingMeasure.getBlueprintDenotationType() == null) {
                // missing denotation type message
                bindingResult.addError(buildFieldError("blueprintDenotationType", BP_DENOTATION_REQUIRED,
                        paramArray(reportingMeasure.getBlueprintReferenceType().name(),
                                reportingMeasure.getBlueprintReferenceType().getTitle())));
            } else {
                if (reportingMeasure.getBlueprintDenotationType() == BlueprintDenotationType.STANDARD_KEY) {
                    // ensure ID is actual active BP Element
                    final BlueprintElement blueprintElement = reportingMeasure.getBlueprintReferenceId() == null
                            ? null
                            : this.blueprintElementRepository
                                    .findOne(reportingMeasure.getBlueprintReferenceId());
                    if (blueprintElement == null) {
                        formatReferenceIdMessage(bindingResult, reportingMeasure, BPREF_ID_NOT_FOUND);
                    } else if (!blueprintElement.isActive()) {
                        formatReferenceIdMessage(bindingResult, reportingMeasure, BPREF_NOT_ACTIVE);
                    } else if (!StringUtils.equals(blueprintElement.getAssessmentId(),
                            reportingMeasure.getAssessmentId())) {
                        formatReferenceIdMessage(bindingResult, reportingMeasure, BPREF_ASSESSMENTID_MISMATCH);
                    }
                } else if (reportingMeasure.getBlueprintDenotationType() == BlueprintDenotationType.LEVEL) {
                    // ensure level choice is valid
                    if (!findDistinctActiveBlueprintLevelsByAssessmentId(reportingMeasure.getAssessmentId())
                            .contains(reportingMeasure.getBlueprintReferenceId())) {
                        formatReferenceIdMessage(bindingResult, reportingMeasure,
                                "reportingMeasure.blueprintReferenceId.level.invalid");
                    }
                } else if (reportingMeasure
                        .getBlueprintDenotationType() == BlueprintDenotationType.LEAF_NODES) {
                    // ensure ID field is empty/null
                    if (reportingMeasure.getBlueprintReferenceId() != null) {
                        formatReferenceIdMessage(bindingResult, reportingMeasure,
                                "reportingMeasure.blueprintReferenceId.notempty");
                    }
                } else {
                    bindingResult.addError(buildFieldError("blueprintDenotationType", BP_DENOTATION_REQUIRED,
                            paramArray(reportingMeasure.getBlueprintReferenceType().name(),
                                    reportingMeasure.getBlueprintReferenceType().getTitle())));
                }
            }
            break;
        default:
            // do nothing, cannot enter here if prior validation passed
            break;
        }
        populateScoringRules(reportingMeasure);
        if (CollectionUtils.isEmpty(reportingMeasure.getScoringRuleIdList()) || reportingMeasure
                .getScoringRuleIdList().size() > reportingMeasure.getScoringRuleList().size()) {
            formatScoringRuleIdMessage(bindingResult, reportingMeasure, null,
                    "reportingMeasure.scoringRuleIdList.invalid");
        }
        for (final ScoringRule scoringRule : reportingMeasure.getScoringRuleList()) {
            if (!scoringRule.getBlueprintReferenceId().equals(reportingMeasure.getBlueprintReferenceId())
                    || !scoringRule.getBlueprintReferenceType()
                            .equals(reportingMeasure.getBlueprintReferenceType())) {
                formatScoringRuleIdMessage(bindingResult, reportingMeasure, scoringRule,
                        "reportingMeasure.scoringRuleIdList.mismatch");
            }
        }
    }
    if (bindingResult.hasErrors()) {
        throw ValidationHelper.convertErrorsToConstraintException(reportingMeasure, bindingResult);
    }
}

From source file:org.opentestsystem.authoring.testauth.service.impl.ReportingMeasureServiceImpl.java

private void formatReferenceIdMessage(final BindingResult bindingResult,
        final ReportingMeasure reportingMeasure, final String messageKey) {
    bindingResult.addError(buildFieldError("blueprintReferenceId", messageKey,
            reportingMeasure.getBlueprintReferenceType().getTitle()));
}

From source file:org.opentestsystem.authoring.testauth.service.impl.ReportingMeasureServiceImpl.java

private void formatScoringRuleIdMessage(final BindingResult bindingResult,
        final ReportingMeasure reportingMeasure, final ScoringRule scoringRule, final String messageKey) {
    bindingResult.addError(
            new FieldError("reportingMeasure", "scoringRuleIdList", null, false, paramArray(messageKey),
                    paramArray(scoringRule == null ? "" : scoringRule.getLabel(),
                            reportingMeasure.getBlueprintReferenceType().name(),
                            reportingMeasure.getBlueprintReferenceId(), reportingMeasure.getAssessmentId()),
                    messageKey));/*from w w  w .j a va  2  s .  com*/

}

From source file:org.opentestsystem.authoring.testauth.service.impl.ScoringRuleHelper.java

protected void validateScoringRule(final ScoringRule scoringRule) {
    populateComputationRule(scoringRule);
    final BindingResult bindingResult = new BeanPropertyBindingResult(scoringRule, "scoringRule");
    this.scoringRuleValidator.validate(scoringRule, bindingResult);
    if (!bindingResult.hasErrors()) {
        switch (scoringRule.getBlueprintReferenceType()) {
        case TEST:
            if (!StringUtils.equals(scoringRule.getBlueprintReferenceId(), scoringRule.getAssessmentId())) {
                formatReferenceIdMessage(bindingResult, scoringRule, BPREF_ID_NOT_FOUND);
            }//ww w .  j  av a 2 s .co m
            scoringRule.setBlueprintDenotationType(null);
            break;
        case SEGMENT:
            final Segment segment = scoringRule.getBlueprintReferenceId() == null ? null
                    : this.segmentService.getSegment(scoringRule.getBlueprintReferenceId());
            if (segment == null
                    || !StringUtils.equals(segment.getAssessmentId(), scoringRule.getAssessmentId())) {
                formatReferenceIdMessage(bindingResult, scoringRule, BPREF_ID_NOT_FOUND);
            } else if (!StringUtils.equals(segment.getAssessmentId(), scoringRule.getAssessmentId())) {
                formatReferenceIdMessage(bindingResult, scoringRule, BPREF_ASSESSMENTID_MISMATCH);
            }
            scoringRule.setBlueprintDenotationType(null);
            break;
        case AFFINITY_GROUP:
            final AffinityGroup affinityGroup = scoringRule.getBlueprintReferenceId() == null ? null
                    : this.affinityGroupService.getAffinityGroup(scoringRule.getBlueprintReferenceId());
            if (affinityGroup == null) {
                formatReferenceIdMessage(bindingResult, scoringRule, BPREF_ID_NOT_FOUND);
            } else if (!affinityGroup.isActive()) {
                formatReferenceIdMessage(bindingResult, scoringRule, BPREF_NOT_ACTIVE);
            } else if (!StringUtils.equals(affinityGroup.getAssessmentId(), scoringRule.getAssessmentId())) {
                formatReferenceIdMessage(bindingResult, scoringRule, BPREF_ASSESSMENTID_MISMATCH);
            }
            scoringRule.setBlueprintDenotationType(null);
            break;
        case STANDARD:
            if (scoringRule.getBlueprintDenotationType() == null) {
                // missing denotation type message
                bindingResult.addError(buildFieldError("blueprintDenotationType", BP_DENOTATION_REQUIRED,
                        paramArray(scoringRule.getBlueprintReferenceType().name(),
                                scoringRule.getBlueprintReferenceType().getTitle())));
            } else {
                if (scoringRule.getBlueprintDenotationType() == BlueprintDenotationType.STANDARD_KEY) {
                    // ensure ID is actual active BP Element
                    final BlueprintElement blueprintElement = scoringRule.getBlueprintReferenceId() == null
                            ? null
                            : this.blueprintElementService
                                    .getBlueprintElement(scoringRule.getBlueprintReferenceId());
                    if (blueprintElement == null) {
                        formatReferenceIdMessage(bindingResult, scoringRule, BPREF_ID_NOT_FOUND);
                    } else if (!blueprintElement.isActive()) {
                        formatReferenceIdMessage(bindingResult, scoringRule, BPREF_NOT_ACTIVE);
                    } else if (!StringUtils.equals(blueprintElement.getAssessmentId(),
                            scoringRule.getAssessmentId())) {
                        formatReferenceIdMessage(bindingResult, scoringRule, BPREF_ASSESSMENTID_MISMATCH);
                    }
                } else if (scoringRule.getBlueprintDenotationType() == BlueprintDenotationType.LEVEL) {
                    // ensure level choice is valid
                    if (!findDistinctActiveBlueprintLevelsByAssessmentId(scoringRule.getAssessmentId()).keySet()
                            .contains(scoringRule.getBlueprintReferenceId())) {
                        formatReferenceIdMessage(bindingResult, scoringRule,
                                "scoringRule.blueprintReferenceId.level.invalid");
                    }
                } else if (scoringRule.getBlueprintDenotationType() == BlueprintDenotationType.LEAF_NODES) {
                    // ensure ID field is empty/null
                    if (scoringRule.getBlueprintReferenceId() != null) {
                        formatReferenceIdMessage(bindingResult, scoringRule,
                                "scoringRule.blueprintReferenceId.notempty");
                    }
                } else {
                    bindingResult.addError(buildFieldError("blueprintDenotationType", BP_DENOTATION_REQUIRED,
                            paramArray(scoringRule.getBlueprintReferenceType().name(),
                                    scoringRule.getBlueprintReferenceType().getTitle())));
                }
            }
            break;
        default:
            // do nothing, cannot enter here if prior validation passed
            break;
        }

        validateFileIdsExist(bindingResult, scoringRule);

        validateOrderValue(bindingResult, scoringRule);
        if (scoringRule.getComputationRule()
                .getConversionTableType() == ConversionTableType.VALUE_STANDARD_ERROR) {
            validateConversionTableFileLengthsMatch(bindingResult, scoringRule);
        }
    }
    if (bindingResult.hasErrors()) {
        throw ValidationHelper.convertErrorsToConstraintException(scoringRule, bindingResult);
    }
}

From source file:org.opentestsystem.authoring.testauth.service.impl.ScoringRuleHelper.java

private void formatReferenceIdMessage(final BindingResult bindingResult, final ScoringRule scoringRule,
        final String messageKey) {
    bindingResult.addError(buildFieldError("blueprintReferenceId", messageKey,
            scoringRule.getBlueprintReferenceType().getTitle()));
}

From source file:org.opentestsystem.authoring.testauth.service.impl.ScoringRuleHelper.java

private void validateOrderValue(final BindingResult bindingResult, final ScoringRule scoringRule) {
    final ScoringRuleSearchRequest searchRequest = new ScoringRuleSearchRequest(ImmutableMap.of("assessmentId",
            paramArray(scoringRule.getAssessmentId()), "pageSize", paramArray("5000")));
    if (searchRequest.isValid()) {
        final SearchResponse<ScoringRule> searchResponse = this.scoringRuleRepository.search(searchRequest);
        final int sizeToCompare = scoringRule.getId() == null ? searchResponse.getSearchResults().size() + 1
                : searchResponse.getSearchResults().size();
        if (sizeToCompare < scoringRule.getOrder()) {
            bindingResult.addError(
                    buildFieldError("order", "scoringRule.order.exceed", String.valueOf(sizeToCompare)));
        }/*from  w w  w  .  ja  v a2s .  co  m*/
    }
}

From source file:org.opentestsystem.authoring.testauth.service.impl.ScoringRuleHelper.java

private void validateFileIdsExist(final BindingResult bindingResult, final ScoringRule scoringRule) {
    if (scoringRule != null && scoringRule.getValueConversionTableGridFsId() != null) {
        final GridFSDBFile gridFSDBFile = this.gridFsRepository
                .getById(scoringRule.getValueConversionTableGridFsId());
        if (gridFSDBFile == null) {
            bindingResult.addError(buildFieldError("valueConversionTableGridFsId",
                    "scoringRule.valueConversionTableGridFsId.required"));
        }//from  www  . j  a va 2 s. com
    }
    if (scoringRule != null && scoringRule.getStandardErrorConversionTableGridFsId() != null) {
        final GridFSDBFile gridFSDBFile = this.gridFsRepository
                .getById(scoringRule.getStandardErrorConversionTableGridFsId());
        if (gridFSDBFile == null) {
            bindingResult.addError(buildFieldError("standardErrorConversionTableGridFsId",
                    "scoringRule.standardErrorConversionTableGridFsId.required"));
        }
    }
}

From source file:org.opentestsystem.authoring.testauth.service.impl.ScoringRuleHelper.java

private void validateConversionTableFileLengthsMatch(final BindingResult bindingResult,
        final ScoringRule scoringRule) {
    if (scoringRule != null && scoringRule.getValueConversionTableGridFsId() != null
            && scoringRule.getStandardErrorConversionTableGridFsId() != null) {
        final GridFSDBFile valueFile = this.gridFsRepository
                .getById(scoringRule.getValueConversionTableGridFsId());
        final ByteArrayOutputStream valueBaos = new ByteArrayOutputStream();
        try {//from   w ww .j a va2s  . c om
            valueFile.writeTo(valueBaos);
        } catch (final IOException e) {
            bindingResult.addError(buildFieldError("valueConversionTableGridFsId",
                    "scoringRule.valueConversionTableGridFsId.required"));
        }
        final int valueListSize = validateConversionListFile(valueBaos.toByteArray(), scoringRule.getLabel(),
                "VALUE", false).size();

        final GridFSDBFile errorFile = this.gridFsRepository
                .getById(scoringRule.getStandardErrorConversionTableGridFsId());
        final ByteArrayOutputStream errorBaos = new ByteArrayOutputStream();
        try {
            errorFile.writeTo(errorBaos);
        } catch (final IOException e) {
            bindingResult.addError(buildFieldError("standardErrorConversionTableGridFsId",
                    "scoringRule.standardErrorConversionTableGridFsId.required"));
        }
        final int errorListSize = validateConversionListFile(errorBaos.toByteArray(), scoringRule.getLabel(),
                "STANDARD_ERROR", false).size();

        if (valueListSize != errorListSize) {
            bindingResult.addError(buildFieldError("standardErrorConversionTableGridFsId",
                    "scoringRule.standardErrorConversionTableGridFsId.length",
                    paramArray(String.valueOf(valueListSize), String.valueOf(errorListSize))));
        }
    }
}

From source file:org.opentestsystem.authoring.testauth.service.impl.SegmentServiceImpl.java

@Override
public Segment createSegment(final Segment segment) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Saving segment");
    }//from w  w w  .j  a v  a2 s.  c om
    checkForLockedAssessment(segment.getAssessmentId());
    if (segment == null || StringUtils.isNotBlank(segment.getId())) {
        throw new LocalizedException("segment.invalid.id");
    }

    final BindingResult bindingResult = new BeanPropertyBindingResult(segment, "segment");
    if (findSegmentListByAssessmentId(segment.getAssessmentId()).size() + 1 != segment.getPosition()
            || segment.getPosition() > SEGMENT_MAX_POSITION || segment.getPosition() < SEGMENT_MIN_POSITION) {
        bindingResult.addError(
                new FieldError("segment", "position", null, false, paramArray("segment.invalid.position"),
                        paramArray(String.valueOf(segment.getPosition())), "segment.invalid.position"));
    }
    Segment savedSegment = null;

    validateSegment(bindingResult, segment);

    try {
        savedSegment = this.segmentRepository.save(segment);

        loadReferenceData(savedSegment);
    } catch (final DuplicateKeyException dke) {
        throw new LocalizedException("segment.already.exists", paramArray(segment.getLabel()), dke);
    }
    this.blueprintElementService.addBlueprintElementValueGroup(savedSegment.getAssessmentId(),
            savedSegment.getId(), null);
    this.affinityGroupService.addAffinityGroupValueGroup(savedSegment.getAssessmentId(), savedSegment.getId());
    return savedSegment;
}

From source file:org.opentestsystem.authoring.testauth.service.impl.SegmentServiceImpl.java

@Override
public Segment updateSegment(final Segment segment) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Updating segment");
    }/* w  ww  .  ja  v  a  2 s .co  m*/
    checkForLockedAssessment(segment.getAssessmentId());

    if (segment == null || StringUtils.isBlank(segment.getId())) {
        throw new LocalizedException("segment.invalid.id");
    }
    final BindingResult bindingResult = new BeanPropertyBindingResult(segment, "segment");
    if (findSegmentListByAssessmentId(segment.getAssessmentId()).size() < segment.getPosition()
            || segment.getPosition() > SEGMENT_MAX_POSITION || segment.getPosition() < SEGMENT_MIN_POSITION) {
        bindingResult.addError(
                new FieldError("segment", "position", null, false, paramArray("segment.invalid.position"),
                        paramArray(String.valueOf(segment.getPosition())), "segment.invalid.position"));
    }

    final String existingSegmentItemSelectionAlgorithmId = this.segmentRepository.findOne(segment.getId())
            .getItemSelectionAlgorithmId();
    final boolean isNewItemSelectionAlgorithm = !StringUtils.equals(existingSegmentItemSelectionAlgorithmId,
            segment.getItemSelectionAlgorithmId());
    if (isNewItemSelectionAlgorithm) {
        final ItemSelectionAlgorithm existingItemSelectionAlgorithm = this.itemSelectionAlgorithmService
                .getItemSelectionAlgorithm(existingSegmentItemSelectionAlgorithmId);
        final ItemSelectionAlgorithm newItemSelectionAlgorithm = this.itemSelectionAlgorithmService
                .getItemSelectionAlgorithm(segment.getItemSelectionAlgorithmId());
        if (existingItemSelectionAlgorithm.getItemSelectionAlgorithmType() != newItemSelectionAlgorithm
                .getItemSelectionAlgorithmType()) {
            bindingResult.addError(buildValidationFieldError("segment", "itemSelectionAlgorithmId",
                    "segment.cannot.alter.itemSelectionAlgorithm.type",
                    paramArray(existingItemSelectionAlgorithm.getItemSelectionAlgorithmType().name())));
        }
    }

    validateSegment(bindingResult, segment);

    segmentPositionResequencing(segment, true);

    Segment savedSegment = null;

    try {
        savedSegment = this.segmentRepository.save(segment);

        // if ISA has changed, update blueprint values
        if (isNewItemSelectionAlgorithm) {
            this.blueprintElementService.updateBlueprintElementItemSelectionParameters(
                    savedSegment.getAssessmentId(), savedSegment.getId());
            this.affinityGroupService.updateAffinityGroupItemSelectionParameters(savedSegment.getAssessmentId(),
                    savedSegment.getId());
        }

        loadReferenceData(savedSegment);
    } catch (final DuplicateKeyException dke) {
        throw new LocalizedException("segment.already.exists", paramArray(segment.getLabel()), dke);
    }
    return savedSegment;
}