1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springmodules.validation.bean.rule;
18
19 import org.springmodules.validation.bean.rule.resolver.ErrorArgumentsResolver;
20 import org.springmodules.validation.bean.rule.resolver.StaticErrorArgumentsResolver;
21 import org.springmodules.validation.util.condition.Condition;
22 import org.springmodules.validation.util.condition.common.AlwaysTrueCondition;
23
24 /**
25 * The default implementation of the {@link org.springmodules.validation.bean.rule.ValidationRule} interface. This implementation uses an applicability condition
26 * to determine whether this rule is applicable on a given object (see {@link ValidationRule#isApplicable(Object)}).
27 *
28 * @author Uri Boness
29 */
30 public class DefaultValidationRule implements ValidationRule {
31
32
33 private final static Condition DEFAULT_APPLICABILITY_CONDITION = new AlwaysTrueCondition();
34
35
36 private Condition condition;
37
38
39 private Condition applicabilityCondition;
40
41
42 private String errorCode;
43
44
45 private String defalutErrorMessage;
46
47
48 private ErrorArgumentsResolver errorArgumentsResolver;
49
50 /**
51 * Empty contructor (javabean support).
52 */
53 public DefaultValidationRule() {
54 this(null, null);
55 }
56
57 /**
58 * Constructs a new DefaultValidationRule with the given condition and error code. The condition
59 * is always applicable.
60 *
61 * @param condition The condition of this validation rule.
62 * @param errorCode The error code of this validation rule.
63 */
64 public DefaultValidationRule(Condition condition, String errorCode) {
65 this(condition, DEFAULT_APPLICABILITY_CONDITION, errorCode, errorCode, new Object[0]);
66 }
67
68 /**
69 * Constructs a new DefaultValidationRule with the given condition, error code, and error arguments. The condition
70 * is always applicable.
71 *
72 * @param condition The condition of this validation rule.
73 * @param errorCode The error code of this validation rule.
74 * @param errorArguments The error arguments of this validation rule.
75 */
76 public DefaultValidationRule(Condition condition, String errorCode, Object[] errorArguments) {
77 this(condition, DEFAULT_APPLICABILITY_CONDITION, errorCode, errorCode, errorArguments);
78 }
79
80 /**
81 * Constructs a new DefaultValidationRule with the given condition and error information. The condition is always
82 * applicable.
83 *
84 * @param condition The condition of this validation rule.
85 * @param errorCode The error code of this validation rule.
86 * @param defalutErrorMessage The default error message of this validation rule.
87 * @param errorArguments The error arguments of this validation rule.
88 */
89 public DefaultValidationRule(
90 Condition condition,
91 String errorCode,
92 String defalutErrorMessage,
93 Object[] errorArguments) {
94
95 this(condition, DEFAULT_APPLICABILITY_CONDITION, errorCode, defalutErrorMessage, errorArguments);
96 }
97
98 /**
99 * Constructs a new DefaultValidationRule with given condition and error code. The applicability of this rule
100 * is determined by the given applicability condition.
101 *
102 * @param condition The condition of this validation rule.
103 * @param applicabilityCondition Determines whether this rule is applicable on a given object.
104 * @param errorCode The error code of this validation rule.
105 */
106 public DefaultValidationRule(Condition condition, Condition applicabilityCondition, String errorCode) {
107 this(condition, applicabilityCondition, errorCode, errorCode, new Object[0]);
108 }
109
110 /**
111 * Constructs a new DefaultValidationRule with given condition, error code, and error arguments. The applicability
112 * of this rule is determined by the given applicability condition.
113 *
114 * @param condition The condition of this validation rule.
115 * @param applicabilityCondition Determines whether this rule is applicable on a given object.
116 * @param errorCode The error code of this validation rule.
117 * @param errorArguments The error arguments of this validation rule.
118 */
119 public DefaultValidationRule(
120 Condition condition,
121 Condition applicabilityCondition,
122 String errorCode,
123 Object[] errorArguments) {
124
125 this(condition, applicabilityCondition, errorCode, errorCode, errorArguments);
126 }
127
128 /**
129 * Constructs a new DefaultValidationRule with given condition, error code, error arguments, and default error
130 * message. The applicability of this rule is determined by the given applicability condition.
131 *
132 * @param condition The condition of this validation rule.
133 * @param applicabilityCondition Determines whether this rule is applicable on a given object.
134 * @param errorCode The error code of this validation rule.
135 * @param defalutErrorMessage The default error message of this validation rule.
136 * @param errorArguments The error arguments of this validation rule.
137 */
138 public DefaultValidationRule(
139 Condition condition,
140 Condition applicabilityCondition,
141 String errorCode,
142 String defalutErrorMessage,
143 Object[] errorArguments) {
144
145 this(condition, applicabilityCondition, errorCode, defalutErrorMessage, new StaticErrorArgumentsResolver(errorArguments));
146 }
147
148 /**
149 * Constructs a new DefaultValidationRule with given condition, error code, error arguments resolver, and default error
150 * message. The applicability of this rule is determined by the given applicability condition.
151 *
152 * @param condition The condition of this validation rule.
153 * @param applicabilityCondition Determines whether this rule is applicable on a given object.
154 * @param errorCode The error code of this validation rule.
155 * @param defalutErrorMessage The default error message of this validation rule.
156 * @param errorArgumentsResolver The resolver that will be used to resolve the error arguments.
157 */
158 public DefaultValidationRule(
159 Condition condition,
160 Condition applicabilityCondition,
161 String errorCode,
162 String defalutErrorMessage,
163 ErrorArgumentsResolver errorArgumentsResolver) {
164
165 this.condition = condition;
166 this.applicabilityCondition = applicabilityCondition;
167 this.errorCode = errorCode;
168 this.errorArgumentsResolver = errorArgumentsResolver;
169 this.defalutErrorMessage = defalutErrorMessage;
170 }
171
172 /**
173 * see {@link ValidationRule#isApplicable(Object)}.
174 * <p/>
175 * The applicability of this validation rule is determined by the applicability condition.
176 * see {@link #getApplicabilityCondition()}.
177 */
178 public boolean isApplicable(Object obj) {
179 return applicabilityCondition.check(obj);
180 }
181
182 /**
183 * see {@link org.springmodules.validation.bean.rule.ValidationRule#getCondition()}
184 */
185 public Condition getCondition() {
186 return condition;
187 }
188
189 /**
190 * Sets the condition of this validation rule. see {@link #getCondition()}.
191 *
192 * @param condition The condition of this validation rule.
193 */
194 public void setCondition(Condition condition) {
195 this.condition = condition;
196 }
197
198 /**
199 * See {@link org.springmodules.validation.bean.rule.ValidationRule#getErrorCode()}
200 */
201 public String getErrorCode() {
202 return errorCode;
203 }
204
205 /**
206 * Sets the error code of this validation rule. see {@link #getErrorCode()}.
207 *
208 * @param errorCode The error code of this validation rule.
209 */
210 public void setErrorCode(String errorCode) {
211 this.errorCode = errorCode;
212 }
213
214 /**
215 * See {@link org.springmodules.validation.bean.rule.ValidationRule#getErrorArguments(Object)}
216 */
217 public Object[] getErrorArguments(Object obj) {
218 return errorArgumentsResolver.resolveArguments(obj);
219 }
220
221 /**
222 * Sets the arguments to attach to the error code of this validation rule. see {@link #getErrorArguments(Object)}.
223 *
224 * @param errorArguments The arguments to attach to the error code of this validation rule.
225 */
226 public void setErrorArguments(Object[] errorArguments) {
227 this.errorArgumentsResolver = new StaticErrorArgumentsResolver(errorArguments);
228 }
229
230 /**
231 * Sets the error arguments resolver to be used by this validation rule to resolve the error arguments.
232 *
233 * @param errorArgumentsResolver The given error arguments resolver.
234 */
235 public void setErrorArgumentsResolver(ErrorArgumentsResolver errorArgumentsResolver) {
236 this.errorArgumentsResolver = errorArgumentsResolver;
237 }
238
239 /**
240 * See {@link org.springmodules.validation.bean.rule.ValidationRule#getDefaultErrorMessage()}.
241 */
242 public String getDefaultErrorMessage() {
243 return defalutErrorMessage;
244 }
245
246 /**
247 * Sets the default error message to be used in case no error message is associated with the error code
248 * of this validation rule. See {@link #getDefaultErrorMessage()}.
249 *
250 * @param defalutErrorMessage The default error message of this validation rule.
251 */
252 public void setDefalutErrorMessage(String defalutErrorMessage) {
253 this.defalutErrorMessage = defalutErrorMessage;
254 }
255
256
257
258 /**
259 * Returns the applicability condition of this validation rule. This applicability condition determines whether
260 * this validation rule is applicable for a given object.
261 *
262 * @return The applicability condition of this validation rule.
263 */
264 public Condition getApplicabilityCondition() {
265 return applicabilityCondition;
266 }
267
268 /**
269 * Sets the applicability condition of this validation rule. see {@link #getApplicabilityCondition()}.
270 *
271 * @param applicabilityCondition The applicability condition of this validation rule.
272 */
273 public void setApplicabilityCondition(Condition applicabilityCondition) {
274 this.applicabilityCondition = applicabilityCondition;
275 }
276
277 }