Java tutorial
/*$Id: ValueLevelValidator.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 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; /** * ValueLevelValidator for validating float values. * * @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.ValueLevelValidator") //@Table(name = "FIELD_VALUELEVEL_VALIDATOR", schema = DbSchemaNameConst.QUESTIONNAIRE_DB_SCHEMA_NAME) //@PrimaryKeyJoinColumn(name="id") @DiscriminatorValue("ValueLevelValidator") @Cache(usage = CacheConcurrencyStrategy.READ_ONLY) public class ValueLevelValidator extends FieldValidator { private static final Log logger = (Log) LogFactory.getLog(FieldValidator.class); private BigDecimal lessThan; private BigDecimal greaterThan; private BigDecimal percentage; public ValueLevelValidator() { } /** * Initialize validator with arguments. Expects two or three arguments. * @param arguments */ public void initialize(String[] arguments) { if (arguments.length == 2) { lessThan = new BigDecimal(arguments[0]); greaterThan = new BigDecimal(arguments[1]); } else if (arguments.length == 3) { lessThan = new BigDecimal(arguments[0]); greaterThan = new BigDecimal(arguments[1]); percentage = new BigDecimal(arguments[2]); } else { throw new IllegalArgumentException( "Expected one argument, stringLength. Format is e.g. StringLength(60)."); } } public boolean supports(Class clazz) { logger.info("Execute supports"); return BigDecimalFieldData.class.isAssignableFrom(clazz); } public void validate(FieldData fieldData, Errors errors) { BigDecimal bigDecimalValue = ((BigDecimalFieldData) fieldData).getBigDecimalValue(); if (bigDecimalValue == null) { logger.debug("Value " + fieldData + " for field " + fieldData.getCode() + " validated OK"); } else { if (bigDecimalValue.compareTo(lessThan) < 0) { errors.rejectValue(getFieldName(fieldData), "validation.must.be.less.than", getErrorMessageArguments(bigDecimalValue), "Int value for post " + fieldData.getCode() + " is " + bigDecimalValue + " it it has to be less than " + lessThan); } else if (bigDecimalValue.compareTo(greaterThan) > 0) { errors.rejectValue(getFieldName(fieldData), "validation.must.be.more.than", getErrorMessageArguments(bigDecimalValue), "Int value for post " + fieldData.getCode() + " is " + bigDecimalValue + " it it has to be greater than " + greaterThan); } else { logger.debug("Value " + fieldData + " for field " + fieldData.getCode() + " validated OK"); } } } public BigDecimal getLessThan() { return lessThan; } public BigDecimal getGreaterThan() { return greaterThan; } public void setLessThan(BigDecimal lessThan) { this.lessThan = lessThan; } public void setGreaterThan(BigDecimal greaterThan) { this.greaterThan = greaterThan; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } if (!super.equals(o)) { return false; } ValueLevelValidator that = (ValueLevelValidator) o; if (greaterThan != null ? !greaterThan.equals(that.greaterThan) : that.greaterThan != null) { return false; } if (lessThan != null ? !lessThan.equals(that.lessThan) : that.lessThan != null) { return false; } return true; } @Override public int hashCode() { int result = super.hashCode(); result = 31 * result + (lessThan != null ? lessThan.hashCode() : 0); result = 31 * result + (greaterThan != null ? greaterThan.hashCode() : 0); return result; } }