1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springmodules.validation.bean.conf;
18
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.apache.commons.collections.CollectionUtils;
25 import org.springframework.validation.Validator;
26 import org.springmodules.validation.bean.rule.ValidationRule;
27 import org.springmodules.validation.validator.CompoundValidator;
28
29 /**
30 * A default implementation of {@link org.springmodules.validation.bean.conf.BeanValidationConfiguration}.
31 *
32 * @author Uri Boness
33 */
34 public class DefaultBeanValidationConfiguration implements MutableBeanValidationConfiguration {
35
36
37 private List cascadeValidations;
38
39
40 private List globalRules;
41
42
43 private Map rulesByProperty;
44
45
46 private CompoundValidator customValidator;
47
48 /**
49 * Constructs a new DefaultBeanValidationConfiguration with no rules, validatable properties, or custom validator.
50 */
51 public DefaultBeanValidationConfiguration() {
52 cascadeValidations = new ArrayList();
53 globalRules = new ArrayList();
54 rulesByProperty = new HashMap();
55 customValidator = new CompoundValidator();
56 }
57
58 /**
59 * @see org.springmodules.validation.bean.conf.BeanValidationConfiguration#getGlobalRules()
60 */
61 public ValidationRule[] getGlobalRules() {
62 return (ValidationRule[]) globalRules.toArray(new ValidationRule[globalRules.size()]);
63 }
64
65 /**
66 * Sets the global rules for this configuration.
67 *
68 * @param globalRules The global rules for this configuration
69 */
70 public void setGlobalRules(ValidationRule[] globalRules) {
71 List rules = new ArrayList();
72 CollectionUtils.addAll(rules, globalRules);
73 this.globalRules = rules;
74 }
75
76 /**
77 * Adds the given rule as global rules for this configuration.
78 *
79 * @param globalRule The rule to be added as global rules for this configuration.
80 */
81 public void addGlobalRule(ValidationRule globalRule) {
82 globalRules.add(globalRule);
83 }
84
85 /**
86 * Adds the given rules as global rules for this configuration.
87 *
88 * @param globalRules The rules to be added as global rules for this configuration.
89 */
90 public void addGlobalRules(ValidationRule[] globalRules) {
91 CollectionUtils.addAll(this.globalRules, globalRules);
92 }
93
94 /**
95 * @see org.springmodules.validation.bean.conf.BeanValidationConfiguration#getPropertyRules(String).
96 */
97 public ValidationRule[] getPropertyRules(String propertyName) {
98 List rules = (List) rulesByProperty.get(propertyName);
99 if (rules == null || rules.isEmpty()) {
100 return new ValidationRule[0];
101 }
102 return (ValidationRule[]) rules.toArray(new ValidationRule[rules.size()]);
103 }
104
105 /**
106 * @see org.springmodules.validation.bean.conf.BeanValidationConfiguration#getValidatedProperties()
107 */
108 public String[] getValidatedProperties() {
109 return (String[]) rulesByProperty.keySet().toArray(new String[rulesByProperty.size()]);
110 }
111
112 /**
113 * Sets the property validation rules for the given property.
114 *
115 * @param propertyName The name of the property.
116 * @param rules The validation rules for the given property.
117 */
118 public void setPropertyRules(String propertyName, ValidationRule[] rules) {
119 List ruleList = new ArrayList();
120 CollectionUtils.addAll(ruleList, rules);
121 rulesByProperty.put(propertyName, ruleList);
122 }
123
124 /**
125 * Adds the given validation rule to the given property.
126 *
127 * @param propertyName The name of the property.
128 * @param rule The validation rule to be added to the given property.
129 */
130 public void addPropertyRule(String propertyName, ValidationRule rule) {
131 List rules = (List) rulesByProperty.get(propertyName);
132 if (rules == null) {
133 rules = new ArrayList();
134 rulesByProperty.put(propertyName, rules);
135 }
136 rules.add(rule);
137 }
138
139 /**
140 * Adds the given validation rules to the given property.
141 *
142 * @param propertyName The name of the property.
143 * @param extraRules The extra validation rules that will be added to the given property.
144 */
145 public void addPropertyRules(String propertyName, ValidationRule[] extraRules) {
146 List rules = (List) rulesByProperty.get(propertyName);
147 if (rules == null) {
148 rules = new ArrayList();
149 rulesByProperty.put(propertyName, rules);
150 }
151 CollectionUtils.addAll(rules, extraRules);
152 }
153
154 /**
155 * @see org.springmodules.validation.bean.conf.BeanValidationConfiguration#getCustomValidator()
156 */
157 public Validator getCustomValidator() {
158 return customValidator;
159 }
160
161 /**
162 * @see org.springmodules.validation.bean.conf.BeanValidationConfiguration#getCascadeValidations()
163 */
164 public CascadeValidation[] getCascadeValidations() {
165 return (CascadeValidation[]) cascadeValidations.toArray(new CascadeValidation[cascadeValidations.size()]);
166 }
167
168 /**
169 * Sets the custom validator for this configuration.
170 *
171 * @param validator The custom validator for this configuration.
172 */
173 public void setCustomValidator(Validator validator) {
174 setCustomValidators(new Validator[]{validator});
175 }
176
177 /**
178 * Sets the custom validator for this configuration. The custom validator will be compound with the given
179 * validators.
180 *
181 * @param validators The validators that will make the custom validator of this configuration.
182 */
183 public void setCustomValidators(Validator[] validators) {
184 customValidator = new CompoundValidator(validators);
185 }
186
187 /**
188 * Adds the given validator to the custom validator of this configuration.
189 *
190 * @param validator The validator to be added to the custom validator of this configuration.
191 */
192 public void addCustomValidator(Validator validator) {
193 customValidator.addValidator(validator);
194 }
195
196 /**
197 * Adds the given validators to the custom validator of this configuration.
198 *
199 * @param validators The validators to be added to the custom validator of this configuration.
200 */
201 public void addCustomValidators(Validator[] validators) {
202 customValidator.addValidators(validators);
203 }
204
205 /**
206 * Sets the cascade validations of this configuration.
207 * <p/>
208 * param cascadeValidations The cascade validations of this configuration.
209 */
210 public void setCascadeValidations(CascadeValidation[] cascadeValidations) {
211 this.cascadeValidations = new ArrayList();
212 for (int i = 0; i < cascadeValidations.length; i++) {
213 this.cascadeValidations.add(cascadeValidations[i]);
214 }
215 }
216
217 /**
218 * Adds the given cascade validation to this configuration.
219 *
220 * @param cascadeValidation The cascase validation to be added to this configuration.
221 */
222 public void addCascadeValidation(CascadeValidation cascadeValidation) {
223 addCascadeValidations(new CascadeValidation[]{cascadeValidation});
224 }
225
226 /**
227 * Adds the given cascade validations to this configuration.
228 *
229 * @param cascadeValidations The cascade validation to be added to this configuration.
230 */
231 public void addCascadeValidations(CascadeValidation[] cascadeValidations) {
232 for (int i = 0; i < cascadeValidations.length; i++) {
233 if (!this.cascadeValidations.contains(cascadeValidations[i])) {
234 this.cascadeValidations.add(cascadeValidations[i]);
235 }
236 }
237 }
238
239 }