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