Example usage for org.springframework.validation FieldError FieldError

List of usage examples for org.springframework.validation FieldError FieldError

Introduction

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

Prototype

public FieldError(String objectName, String field, @Nullable Object rejectedValue, boolean bindingFailure,
        @Nullable String[] codes, @Nullable Object[] arguments, @Nullable String defaultMessage) 

Source Link

Document

Create a new FieldError instance.

Usage

From source file:org.opentestsystem.authoring.testauth.config.TestAuthUtil.java

public static FieldError buildValidationFieldError(final String objectName, final String fieldName,
        final String messageKey, final String[] messageParams) {
    return new FieldError(objectName, fieldName, null, false, paramArray(messageKey), messageParams,
            messageKey);// w  ww . ja v a 2  s  .  co  m
}

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

private void validateFormPartition(final FormPartition formPartition) {
    final BindingResult bindingResult = new BeanPropertyBindingResult(formPartition, "formPartition");

    if (formPartition.getFormId() != null) {
        final Form form = this.formRepository.findOne(formPartition.getFormId());
        if (form == null) {
            final String messageCode = "formPartition.formId.notfound";
            bindingResult.addError(new FieldError("formPartition", "formId", null, false,
                    new String[] { messageCode }, new String[] { formPartition.getSegmentId() }, messageCode));
        }//from  w ww.j  a  va2 s.  c  o  m
    } else {
        final String messageCode = "formPartition.formId.required";
        bindingResult.addError(new FieldError("formPartition", "formId", null, false,
                new String[] { messageCode }, new String[] { formPartition.getSegmentId() }, messageCode));
    }

    if (formPartition.getSegmentId() == null) {
        final String messageCode = "formPartition.segmentId.required";
        bindingResult.addError(new FieldError("formPartition", "segmentId", null, false,
                new String[] { messageCode }, new String[] { formPartition.getSegmentId() }, messageCode));
    } else {
        final Segment segment = this.segmentService.getSegment(formPartition.getSegmentId());
        if (segment == null) {
            final String messageCode = "formPartition.segmentId.notfound";
            bindingResult.addError(new FieldError("formPartition", "segmentId", null, false,
                    new String[] { messageCode }, new String[] { formPartition.getSegmentId() }, messageCode));
        } else {
            if (segment.getItemSelectionAlgorithm()
                    .getItemSelectionAlgorithmType() != ItemSelectionAlgorithmType.FIXEDFORM) {
                final String messageCode = "formPartition.segmentId.notfixed";
                bindingResult.addError(
                        new FieldError("formPartition", "segmentId", null, false, new String[] { messageCode },
                                new String[] { formPartition.getSegmentId() }, messageCode));
            }
        }
    }

    if (bindingResult.hasErrors()) {
        throw ValidationHelper.convertErrorsToConstraintException(formPartition, bindingResult);
    }
}

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

private void formatReferenceIdMessage(final BindingResult bindingResult,
        final PerformanceLevel performanceLevel) {
    bindingResult.addError(new FieldError("performanceLevel", // objectName
            "blueprintReferenceId", // field
            null, // rejectedValue
            false, // bindingFailure
            paramArray("blueprintReferenceId.notfound"), // messageCode
            paramArray(// arguments
                    performanceLevel.getBlueprintReferenceType().name(),
                    performanceLevel.getBlueprintReferenceId(),
                    performanceLevel.getBlueprintReferenceType().getTitle(),
                    performanceLevel.getAssessmentId()),
            "blueprintReferenceId.notfound")); // defaultMessage

}

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

private FieldError buildFieldError(final String fieldName, final String messageKey,
        final String... messageArgs) {
    return new FieldError("reportingMeasure", fieldName, null, false, paramArray(messageKey), messageArgs,
            messageKey);/* www . ja  va2s . co  m*/
}

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(//from w  w w . jav a2 s  .  com
            new FieldError("reportingMeasure", "scoringRuleIdList", null, false, paramArray(messageKey),
                    paramArray(scoringRule == null ? "" : scoringRule.getLabel(),
                            reportingMeasure.getBlueprintReferenceType().name(),
                            reportingMeasure.getBlueprintReferenceId(), reportingMeasure.getAssessmentId()),
                    messageKey));

}

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

private FieldError buildFieldError(final String fieldName, final String messageKey,
        final String... messageArgs) {
    return new FieldError("scoringRule", fieldName, null, false, paramArray(messageKey), messageArgs,
            messageKey);//from w w  w . ja v  a2  s  .  c o  m
}

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   ww w  . j  a v a2  s. c o  m
    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");
    }//from   w ww.j a v  a2  s .  c  om
    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;
}

From source file:org.opentestsystem.delivery.testreg.persistence.criteria.NCESIDValidator.java

private List<FieldError> verifyNationwideIdentifier(final Sb11Entity sb11Entity,
        final Class<? extends Sb11Entity> clazz) {
    final List<FieldError> errors = new ArrayList<>();

    try {/*from  w  w w . j av a 2 s  . c o m*/
        final String propertyValue = getSimpleProperty(sb11Entity, "nationwideIdentifier");
        // If it is blank then not required to check for Unique
        if (!StringUtils.isEmpty(propertyValue)) {
            final String nationwideIdentifier = stripStart(propertyValue, "0");
            setProperty(sb11Entity, "nationwideIdentifier", nationwideIdentifier);

            final Sb11Entity ncesIdEntity = findByNationwideId(nationwideIdentifier,
                    sb11Entity.getFormatType());
            final Sb11Entity dbEntity = findDBEntity(sb11Entity, clazz);

            boolean existingEntity = false;
            if (ncesIdEntity != null && dbEntity != null && ncesIdEntity.getId().equals(dbEntity.getId())) {
                existingEntity = true;
            }

            if (ncesIdEntity != null && !existingEntity) {
                final String label = getFieldLabel(sb11Entity, "nationwideIdentifier");
                errors.add(new FieldError(sb11Entity.getFormatType().name(), label, nationwideIdentifier, false,
                        null, null, getHumanReadableName(label) + " already exists in DB and cannot be added"));
            }
        }
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        throw new RuntimeException(e);
    }

    return errors;
}

From source file:org.opentestsystem.delivery.testreg.persistence.criteria.Sb11FormatValidatorInvoker.java

private boolean processConstraintViolations(final FormatType format,
        final Set<ConstraintViolation<TestRegistrationBase>> violations, final Errors errors) {
    boolean hasErrors = false;
    for (ConstraintViolation<TestRegistrationBase> violation : violations) {
        ((BindingResult) errors).addError(new FieldError(format.name(), violation.getPropertyPath().toString(),
                getInvalidValue(violation), false, null, null, violation.getMessage()));
        hasErrors = true;/*from  w w  w  .j  a  v a  2 s.  c om*/
    }
    return hasErrors;
}