View Javadoc

1   /*
2    * Copyright 2004-2009 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springmodules.validation.valang.predicates;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Iterator;
22  import java.util.Map;
23  
24  import org.apache.commons.collections.Predicate;
25  import org.springframework.beans.BeanWrapper;
26  import org.springframework.beans.BeanWrapperImpl;
27  import org.springframework.util.Assert;
28  import org.springframework.util.CollectionUtils;
29  import org.springframework.util.StringUtils;
30  import org.springframework.validation.Errors;
31  import org.springmodules.validation.valang.functions.Function;
32  
33  
34  /**
35   * <p>Validation rule implementation that will validate a target
36   * bean an return an error message is the validation fails.
37   *
38   * @author Steven Devijver
39   * @since 23-04-2005
40   */
41  public class BasicValidationRule implements ValidationRule {
42  
43      private final Predicate predicate;
44      private final Predicate wherePredicate;
45      private final String defaultPropertyName;
46      private final String errorMessage;
47      private final String errorKey;
48      private final Collection<Function> errorArgs;
49  
50      /**
51       * Constructor.
52       * 
53       * @param       field           Default property/field name.
54       * @param       predicate       Predicate used for comparison.
55       * @param       errorMessage    Default error message.
56       */
57      public BasicValidationRule(String defaultPropertyName, Predicate predicate, String errorMessage) {
58          Assert.notNull(defaultPropertyName, "Field parameter must not be null.");
59          Assert.notNull(predicate, "Predicate parameter must not be null.");
60          Assert.notNull(errorMessage, "Error message parameter must not be null.");
61  
62          this.defaultPropertyName = defaultPropertyName;
63          this.predicate = predicate;
64          this.wherePredicate = null;
65          this.errorMessage = errorMessage;
66          this.errorKey = null;
67          this.errorArgs = null;
68      }
69  
70      /**
71       * Constructor.
72       * 
73       * @param       field               Default property/field name.
74       * @param       predicate           Predicate used for comparison.
75       * @param       wherePredicate      Where predicate used for whether or not validation should be performed.
76       * @param       errorKey            Message resource key for the error.
77       * @param       errorMessage        Default error message.
78       * @param       errorArgs           Error message argument.
79       */
80      public BasicValidationRule(String defaultPropertyName, Predicate predicate, Predicate wherePredicate,
81                                 String errorKey, String errorMessage, Collection<Function> errorArgs) {
82          Assert.notNull(defaultPropertyName, "Field parameter must not be null.");
83          Assert.notNull(predicate, "Predicate parameter must not be null.");
84          Assert.notNull(errorMessage, "Error message parameter must not be null.");
85  
86          this.defaultPropertyName = defaultPropertyName;
87          this.predicate = predicate;
88          this.wherePredicate = wherePredicate;
89          this.errorMessage = errorMessage;
90          this.errorKey = errorKey;
91          this.errorArgs = errorArgs;
92          
93      }
94  
95      /**
96       * Gets predicate.
97       */
98      public Predicate getPredicate() {
99          return this.predicate;
100     }
101 
102 //    /**
103 //     * Sets predicate.
104 //     */
105 //    private void setPredicate(Predicate predicate) {
106 //        Assert.notNull(predicate, "Predicate parameter must not be null.");
107 //
108 //        this.predicate = predicate;
109 //    }
110 
111     /**
112      * Gets error message.
113      */
114     public String getErrorMessage() {
115         return this.errorMessage;
116     }
117 
118 //    /**
119 //     * Sets error message.
120 //     */
121 //    private void setErrorMessage(String errorMessage) {
122 //        Assert.notNull(errorMessage, "Error message parameter must not be null.");
123 //
124 //        this.errorMessage = errorMessage;
125 //    }
126 
127     /**
128      * Gets field.
129      */
130     public String getField() {
131         return this.defaultPropertyName;
132     }
133 
134 //    /**
135 //     * Sets field.
136 //     */
137 //    private void setField(String field) {
138 //        Assert.notNull(field, "Field parameter must not be null.");
139 //
140 //        this.defaultPropertyName = field;
141 //    }
142 
143 //    private void setErrorKey(String errorKey) {
144 //        this.errorKey = errorKey;
145 //    }
146 
147     /**
148      * Gets error key.
149      */
150     public String getErrorKey() {
151         return this.errorKey;
152     }
153 
154 //    private void setErrorArgs(Collection errorArgs) {
155 //        this.errorArgs = errorArgs;
156 //    }
157 
158     /**
159      * Get error args.
160      */
161     public Collection<Function> getErrorArgs() {
162         return this.errorArgs;
163     }
164 
165     /**
166      * Validates bean and records any errors.
167      */
168     public void validate(Object target, Errors errors) {
169         // if the where predicate isn't null continue, 
170         // otherwise only continue evaluate where predicate evaluates to true
171         if ((wherePredicate != null ? wherePredicate.evaluate(target) : true)) {
172             if (!getPredicate().evaluate(target)) {
173                 // Take into account error key and error args for localization
174                 if (StringUtils.hasLength(getErrorKey())) {
175                     if (!CollectionUtils.isEmpty(getErrorArgs())) {
176                         Collection<String> tmpColl = new ArrayList<String>();
177 
178                         for (Function errorArgFunction : getErrorArgs()) {
179                             tmpColl.add(errorArgFunction.getResult(target).toString());
180                         }
181 
182                         errors.rejectValue(getField(), getErrorKey(), tmpColl.toArray(), getErrorMessage());
183                     } else {
184                         errors.rejectValue(getField(), getErrorKey(), getErrorMessage());
185                     }
186                 } else {
187                     errors.rejectValue(getField(), getField(), getErrorMessage());
188                 }    
189             }
190         }
191     }
192 
193 
194 }