GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / src / commons / org / codehaus / groovy / grails / validation / MaxConstraint.java
bloba07f96f4a1aa10fe31599d03c17dbbb6858cdd63
1 /* Copyright 2004-2005 Graeme Rocher
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 package org.codehaus.groovy.grails.validation;
17 import org.codehaus.groovy.grails.commons.GrailsClassUtils;
18 import org.springframework.validation.Errors;
20 /**
21 * A Constraint that implements a maximum value constraint
23 * @author Graeme Rocher
24 * @since 0.4
25 * <p/>
26 * Created: Jan 19, 2007
27 * Time: 8:19:14 AM
29 class MaxConstraint extends AbstractConstraint {
32 private Comparable maxValue;
34 /**
35 * @return Returns the maxValue.
37 public Comparable getMaxValue() {
38 return maxValue;
42 /* (non-Javadoc)
43 * @see org.codehaus.groovy.grails.validation.Constraint#supports(java.lang.Class)
45 public boolean supports(Class type) {
46 return type != null && (Comparable.class.isAssignableFrom(type) ||
47 GrailsClassUtils.isAssignableOrConvertibleFrom(Number.class, type));
51 /* (non-Javadoc)
52 * @see org.codehaus.groovy.grails.validation.ConstrainedProperty.AbstractConstraint#setParameter(java.lang.Object)
54 public void setParameter(Object constraintParameter) {
55 if(constraintParameter == null) {
56 throw new IllegalArgumentException("Parameter for constraint ["+ConstrainedProperty.MAX_CONSTRAINT+"] of property ["+constraintPropertyName+"] of class ["+constraintOwningClass+"] cannot be null");
58 if(!(constraintParameter instanceof Comparable) && (!constraintParameter.getClass().isPrimitive()))
59 throw new IllegalArgumentException("Parameter for constraint ["+ConstrainedProperty.MAX_CONSTRAINT+"] of property ["+constraintPropertyName+"] of class ["+constraintOwningClass+"] must implement the interface [java.lang.Comparable]");
61 Class propertyClass = GrailsClassUtils.getPropertyType( constraintOwningClass, constraintPropertyName );
62 if(!GrailsClassUtils.isAssignableOrConvertibleFrom( constraintParameter.getClass(),propertyClass ))
63 throw new IllegalArgumentException("Parameter for constraint ["+ConstrainedProperty.MAX_CONSTRAINT+"] of property ["+constraintPropertyName+"] of class ["+constraintOwningClass+"] must be the same type as property: [" + propertyClass.getName() + "]");
65 this.maxValue = (Comparable)constraintParameter;
66 super.setParameter(constraintParameter);
69 public String getName() {
70 return ConstrainedProperty.MAX_CONSTRAINT;
74 protected void processValidate(Object target, Object propertyValue, Errors errors) {
75 if(maxValue.compareTo(propertyValue) < 0) {
76 Object[] args = new Object[] { constraintPropertyName, constraintOwningClass, propertyValue, maxValue };
77 super.rejectValue(target,errors,ConstrainedProperty.DEFAULT_INVALID_MAX_MESSAGE_CODE,ConstrainedProperty.MAX_CONSTRAINT + ConstrainedProperty.EXCEEDED_SUFFIX,args);