1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.cubby.validator; |
17 | |
|
18 | |
import static org.seasar.cubby.CubbyConstants.ATTR_CONVERSION_FAILURES; |
19 | |
|
20 | |
import java.util.List; |
21 | |
import java.util.Map; |
22 | |
|
23 | |
import javax.servlet.http.HttpServletRequest; |
24 | |
|
25 | |
import org.seasar.cubby.action.ActionErrors; |
26 | |
import org.seasar.cubby.action.FieldInfo; |
27 | |
import org.seasar.cubby.action.MessageInfo; |
28 | |
import org.seasar.cubby.internal.controller.ConversionFailure; |
29 | |
import org.seasar.cubby.internal.controller.ThreadContext; |
30 | |
import org.seasar.cubby.internal.util.RequestUtils; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public class ConversionValidationRule implements ValidationRule { |
38 | |
|
39 | |
|
40 | |
private final String resourceKeyPrefix; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public ConversionValidationRule() { |
46 | 0 | this(null); |
47 | 0 | } |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | 0 | public ConversionValidationRule(final String resourceKeyPrefix) { |
56 | 0 | this.resourceKeyPrefix = resourceKeyPrefix; |
57 | 0 | } |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
public void apply(final Map<String, Object[]> params, final Object form, |
63 | |
final ActionErrors errors) throws ValidationException { |
64 | 0 | final HttpServletRequest request = ThreadContext.getRequest(); |
65 | 0 | final List<ConversionFailure> conversionFailures = RequestUtils |
66 | |
.getAttribute(request, ATTR_CONVERSION_FAILURES); |
67 | 0 | if (conversionFailures != null && !conversionFailures.isEmpty()) { |
68 | 0 | for (final ConversionFailure conversionFailure : conversionFailures) { |
69 | 0 | final MessageInfo messageInfo = conversionFailure |
70 | |
.getMessageInfo(); |
71 | |
final String fieldNameKey; |
72 | 0 | if (resourceKeyPrefix == null) { |
73 | 0 | fieldNameKey = conversionFailure.getFieldName(); |
74 | |
} else { |
75 | 0 | fieldNameKey = resourceKeyPrefix |
76 | |
+ conversionFailure.getFieldName(); |
77 | |
} |
78 | 0 | final String message = messageInfo.toMessage(fieldNameKey); |
79 | 0 | final FieldInfo[] fieldInfos = conversionFailure |
80 | |
.getFieldInfos(); |
81 | 0 | errors.add(message, fieldInfos); |
82 | 0 | } |
83 | |
} |
84 | 0 | } |
85 | |
|
86 | |
} |