com.surveypanel.form.validation.NumericValidator.java Source code

Java tutorial

Introduction

Here is the source code for com.surveypanel.form.validation.NumericValidator.java

Source

/*
* SurveyPanel
* Copyright (C) 2009 Serge Tan Panza
* All rights reserved.
* License: GNU/GPL License v3 , see LICENSE.txt
* SurveyPanel is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
* 
*/
package com.surveypanel.form.validation;

import java.util.Locale;
import java.util.Map;

import org.apache.commons.validator.GenericValidator;

import com.surveypanel.form.FormData;
import com.surveypanel.form.QuestionImpl;

/**
 * @author stanpanza
 *
 */
public class NumericValidator extends Validator {

    private final static String INT = "int";
    private final static String FLOAT = "float";
    private final static String DOUBLE = "double";

    @Override
    public void validate(QuestionImpl question, FormData formData, Locale locale, Map<String, String> config,
            ValidationResult validationResult) throws Exception {
        String errorMsg = config.containsKey("errorKey") ? config.get("errorKey") : "form.error.numeric";
        Integer maximum = 0;
        Integer minimum = 0;
        String numericType = config.containsKey("type") ? config.get("type") : "int";

        String error_range = config.containsKey("error_range") ? config.get("error_range") : "form.error.range";

        String min = config.get("min");
        String max = config.get("max");

        if (max != null)
            maximum = Integer.parseInt(max);
        if (min != null)
            minimum = Integer.parseInt(min);

        if (!question.isMulti()) {
            Object value = formData.getValue();
            if (value instanceof String) {
                boolean isRightType = false;
                boolean isInRange = false;
                boolean checkRange = maximum > 0 || minimum > 0;
                if (numericType.equals(INT)) {
                    isRightType = GenericValidator.isInt((String) value);
                    if (isRightType) {
                        isInRange = checkRange ? GenericValidator.isInRange((Integer) value, minimum, maximum)
                                : true;
                    }
                } else if (numericType.equals(FLOAT)) {
                    isRightType = GenericValidator.isFloat((String) value);
                    if (isRightType) {
                        isInRange = checkRange ? GenericValidator.isInRange((Float) value, minimum, maximum) : true;
                    }
                } else if (numericType.equals(DOUBLE)) {
                    isRightType = GenericValidator.isDouble((String) value);
                    if (isRightType) {
                        isInRange = checkRange ? GenericValidator.isInRange((Double) value, minimum, maximum)
                                : true;
                    }
                }

                if (!isRightType) {
                    validationResult
                            .addError(qDefinition.getText(errorMsg, new Object[] { question.getName() }, locale));
                } else if (!isInRange) {
                    validationResult.addError(qDefinition.getText(error_range,
                            new Object[] { question.getName(), minimum, maximum }, locale));
                }
            }
        }
    }

}