1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
106
107
108
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
122
123
124
125
126
127 /**
128 * Gets field.
129 */
130 public String getField() {
131 return this.defaultPropertyName;
132 }
133
134 /**
135 // * Sets field.
136 // */
137
138
139
140
141
142
143
144
145
146
147 /**
148 * Gets error key.
149 */
150 public String getErrorKey() {
151 return this.errorKey;
152 }
153
154
155
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
170
171 if ((wherePredicate != null ? wherePredicate.evaluate(target) : true)) {
172 if (!getPredicate().evaluate(target)) {
173
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 }