1 | |
package org.seasar.cubby.validator.impl; |
2 | |
|
3 | |
import java.util.Map; |
4 | |
|
5 | |
import org.seasar.cubby.action.Action; |
6 | |
import org.seasar.cubby.action.Validation; |
7 | |
import org.seasar.cubby.controller.CubbyConfiguration; |
8 | |
import org.seasar.cubby.util.CubbyUtils; |
9 | |
import org.seasar.cubby.validator.ActionValidator; |
10 | |
import org.seasar.cubby.validator.PropertyValidationRule; |
11 | |
import org.seasar.cubby.validator.ValidationContext; |
12 | |
import org.seasar.cubby.validator.ValidationRule; |
13 | |
import org.seasar.cubby.validator.ValidationRules; |
14 | |
import org.seasar.cubby.validator.Validator; |
15 | |
import org.seasar.framework.beans.BeanDesc; |
16 | |
import org.seasar.framework.beans.PropertyDesc; |
17 | |
import org.seasar.framework.beans.factory.BeanDescFactory; |
18 | |
|
19 | 21 | public class ActionValidatorImpl implements ActionValidator { |
20 | |
|
21 | |
private CubbyConfiguration cubbyConfiguration; |
22 | |
|
23 | |
public void setCubbyConfiguration( |
24 | |
final CubbyConfiguration cubbyConfiguration) { |
25 | 21 | this.cubbyConfiguration = cubbyConfiguration; |
26 | 21 | } |
27 | |
|
28 | |
public boolean processValidation(final Validation valid, |
29 | |
final Action action, final Map<String, Object> params, |
30 | |
final Object form, final ValidationRules rules) { |
31 | 1 | if (valid == null) { |
32 | |
return true; |
33 | |
} |
34 | 1 | validateAction(action, params, form, rules); |
35 | 1 | return action.getErrors().isEmpty(); |
36 | |
} |
37 | |
|
38 | |
@SuppressWarnings("unchecked") |
39 | |
void validateAction(final Action action, final Map<String, Object> params, |
40 | |
final Object form, final ValidationRules rules) { |
41 | 2 | for (ValidationRule rule : rules.getRules()) { |
42 | 4 | for (Validator v : rule.getValidators()) { |
43 | 4 | validate(action, params, form, v, rule); |
44 | 4 | } |
45 | 4 | } |
46 | 2 | } |
47 | |
|
48 | |
void validate(final Action action, final Map<String, Object> params, |
49 | |
final Object form, final Validator validator, |
50 | |
final ValidationRule rule) { |
51 | |
|
52 | 8 | PropertyValidationRule propRule = (PropertyValidationRule) rule; |
53 | 8 | Object value = getPropertyValue(params, propRule.getPropertyName()); |
54 | 8 | ValidationContext ctx = createValidContext(action, params, form, rule, |
55 | |
value); |
56 | 8 | String error = validator.validate(ctx); |
57 | 8 | if (error != null) { |
58 | 3 | action.getErrors().addFieldError(propRule.getPropertyName(), error); |
59 | |
} |
60 | 8 | } |
61 | |
|
62 | |
private ValidationContext createValidContext(final Action action, |
63 | |
final Map<String, Object> params, final Object form, |
64 | |
final ValidationRule rule, Object value) { |
65 | |
|
66 | 8 | PropertyValidationRule propRule = (PropertyValidationRule) rule; |
67 | 8 | String name = propRule.getPropertyNameKey(); |
68 | 8 | ValidationContext ctx = new ValidationContext(name, value, params, |
69 | |
cubbyConfiguration.getFormatPattern()); |
70 | 8 | return ctx; |
71 | |
} |
72 | |
|
73 | |
Object getPropertyValue(final Map<String, Object> params, |
74 | |
final String propertyName) { |
75 | 8 | String[] props = propertyName.split("\\."); |
76 | 8 | Object value = CubbyUtils.getParamsValue(params, props[0]); |
77 | 11 | for (int i = 1; i < props.length; i++) { |
78 | 3 | BeanDesc beanDesc = BeanDescFactory.getBeanDesc(value.getClass()); |
79 | 3 | PropertyDesc propertyDesc = beanDesc.getPropertyDesc(props[i]); |
80 | 3 | value = propertyDesc.getValue(value); |
81 | |
} |
82 | 8 | return value; |
83 | |
} |
84 | |
} |