org.opentestsystem.authoring.testauth.validation.PerformanceLevelValueValidator.java Source code

Java tutorial

Introduction

Here is the source code for org.opentestsystem.authoring.testauth.validation.PerformanceLevelValueValidator.java

Source

/*******************************************************************************
 * Educational Online Test Delivery System
 * Copyright (c) 2013 American Institutes for Research
 * 
 * Distributed under the AIR Open Source License, Version 1.0
 * See accompanying file AIR-License-1_0.txt or at
 * http://www.smarterapp.org/documents/American_Institutes_for_Research_Open_Source_Software_License.pdf
 ******************************************************************************/
package org.opentestsystem.authoring.testauth.validation;

import java.math.BigDecimal;

import org.apache.commons.lang.StringUtils;
import org.opentestsystem.authoring.testauth.domain.PerformanceLevelValue;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;

@Component
public class PerformanceLevelValueValidator extends AbstractDomainValidator {
    private static final String PERFORMANCE_LEVEL_VALUE_ROOT = "performanceLevel.performanceLevelValues.";
    private static final String SCALED_LO = "scaledLo";
    private static final String SCALED_HI = "scaledHi";

    @Override
    public boolean supports(final Class<?> clazz) {
        return PerformanceLevelValue.class.equals(clazz);
    }

    @Override
    public void validate(final Object obj, final Errors errors) {
        final PerformanceLevelValue performanceLevelValue = (PerformanceLevelValue) obj;

        if (performanceLevelValue != null) {
            BigDecimal scaledLoValue = null;
            BigDecimal scaledHiValue = null;
            if (StringUtils.isNotEmpty(performanceLevelValue.getScaledLo())) {
                try {
                    scaledLoValue = new BigDecimal(performanceLevelValue.getScaledLo());
                    performanceLevelValue.setScaledLo(scaledLoValue.toPlainString());
                    if (scaledLoValue.compareTo(BigDecimal.ZERO) < 0) {
                        rejectValue(errors, SCALED_LO, getErrorMessageRoot() + SCALED_LO + MSG_MIN);
                    }
                } catch (final NumberFormatException e) {
                    rejectValue(errors, SCALED_LO, getErrorMessageRoot() + SCALED_LO + MSG_INVALID);
                }

            }
            if (StringUtils.isNotEmpty(performanceLevelValue.getScaledHi())) {
                try {
                    scaledHiValue = new BigDecimal(performanceLevelValue.getScaledHi());
                    performanceLevelValue.setScaledHi(scaledHiValue.toPlainString());
                    if (scaledHiValue.compareTo(BigDecimal.ZERO) < 0) {
                        rejectValue(errors, SCALED_HI, getErrorMessageRoot() + SCALED_HI + MSG_MIN);
                    }
                } catch (final NumberFormatException e) {
                    rejectValue(errors, SCALED_HI, getErrorMessageRoot() + SCALED_HI + MSG_INVALID);
                }
            }
            if (scaledLoValue != null && scaledHiValue != null) {
                if (scaledLoValue.compareTo(scaledHiValue) >= 0) {
                    rejectValue(errors, SCALED_LO, getErrorMessageRoot() + SCALED_LO + MSG_LESS_THAN_MAX);
                }
            }
        }
    }

    @Override
    protected String getErrorMessageRoot() {
        return PERFORMANCE_LEVEL_VALUE_ROOT;
    }

}