Java tutorial
/*$Id: PositiveValueValidator.java 15303 2010-03-07 21:36:42Z jens $*/ /* **************************************************************************** * * * (c) Copyright 2009 ABM-utvikling * * * * This program is free software; you can redistribute it and/or modify it * * under the terms of the GNU General Public License as published by the * * Free Software Foundation; either version 2 of the License, or (at your * * option) any later version. * * * * This program is distributed in the hope that it will be useful, but * * WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * * Public License for more details. http://www.gnu.org/licenses/gpl.html * * * **************************************************************************** */ package no.abmu.questionnaire.domain.validators; import java.math.BigDecimal; import javax.persistence.DiscriminatorValue; import javax.persistence.Entity; import no.abmu.questionnaire.domain.data.BigDecimalFieldData; import no.abmu.questionnaire.domain.data.FieldData; import no.abmu.questionnaire.domain.data.LongFieldData; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; import org.springframework.validation.Errors; /* * @hibernate.joined-subclass table="FINANCE_POST_POSITIVE_VALIDATOR" * @hibernate.joined-subclass-key column="id" * @hibernate.cache usage="nonstrict-read-write" */ /** * Validator for checking that numbers are positive. * * @author Aase Mestad * @author $Author: jens $ * @version $Rev: 15303 $ * @date $Date: 2010-03-07 22:36:42 +0100 (Sun, 07 Mar 2010) $ * @copyright ABM-Utvikling */ @SuppressWarnings("serial") @Entity(name = "no.abmu.questionnaire.domain.validators.PositiveValueValidator") //@Table(name = "FIELD_POSITIVE_VALIDATOR", schema = DbSchemaNameConst.QUESTIONNAIRE_DB_SCHEMA_NAME) //@PrimaryKeyJoinColumn(name="id") @DiscriminatorValue("PositiveValueValidator") @Cache(usage = CacheConcurrencyStrategy.READ_ONLY) public class PositiveValueValidator extends FieldValidator { private static final Log logger = (Log) LogFactory.getLog(PositiveValueValidator.class); public boolean supports(Class clazz) { // if (logger.isDebugEnabled()) { // logger.debug("Execute supports on class name='" + clazz.getName() + "'"); // } if (clazz.getName().equals(LongFieldData.class.getName())) { return LongFieldData.class.isAssignableFrom(clazz); } else if (clazz.getName().equals(BigDecimalFieldData.class.getName())) { return BigDecimalFieldData.class.isAssignableFrom(clazz); } logger.warn("PositiveValueValidator does not support Class '" + clazz.getName() + "'"); return false; } public void validate(FieldData fieldData, Errors errors) { // logger.debug("Execute validate on fieldData class " + fieldData.getClass().getName()); if (fieldData.getClass().getName().equals(LongFieldData.class.getName())) { LongFieldData longFieldData = (LongFieldData) fieldData; validateLongFieldData(longFieldData, errors); } else if (fieldData.getClass().getName().equals(BigDecimalFieldData.class.getName())) { BigDecimalFieldData bigDecimalFieldData = (BigDecimalFieldData) fieldData; validateBigDecimalFieldData(bigDecimalFieldData, errors); } else { logger.warn("FieldData with code=[" + fieldData.getCode() + "] unknown fieldData class=[" + fieldData.getClass().getName() + "] for validator " + this); } } @Override public void initialize(String[] arguments) { if (arguments.length > 0) { throw new IllegalArgumentException("Expected no arguments."); } } // public String toString() { // return this.getClass().getName(); // } private void validateLongFieldData(LongFieldData fieldData, Errors errors) { Long longFieldValue = fieldData.getValue(); if (longFieldValue != null && longFieldValue < 0) { errors.rejectValue(getFieldName(fieldData), "validation.must.be.positive", "Long value for post " + fieldData.getCode() + " is " + longFieldValue + " which is less than 0"); if (logger.isDebugEnabled()) { logger.debug("Value [" + longFieldValue + "] for field with code=[" + fieldData.getCode() + "] FAILED VALIDATION"); } } else { if (logger.isDebugEnabled()) { logger.debug("Value [" + longFieldValue + "] for field with code=[" + fieldData.getCode() + "] validated OK"); } } } private void validateBigDecimalFieldData(BigDecimalFieldData fieldData, Errors errors) { BigDecimal bigDecimalValue = fieldData.getBigDecimalValue(); if (bigDecimalValue != null && bigDecimalValue.signum() < 0) { errors.rejectValue(getFieldName(fieldData), "validation.must.be.positive", "Float value for post " + fieldData.getCode() + " is " + bigDecimalValue + " which is less than 0"); if (logger.isDebugEnabled()) { logger.debug("Value [" + bigDecimalValue + "] for field with code=[" + fieldData.getCode() + "] FAILED VALIDATION"); } } else { if (logger.isDebugEnabled()) { logger.debug("Value [" + bigDecimalValue + "] for field with code=[" + fieldData.getCode() + "] validated OK"); } } } }