1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package nl.openedge.baritus.validation;
32
33 import java.util.Locale;
34
35 import nl.openedge.baritus.FormBeanContext;
36 import nl.openedge.baritus.FormBeanCtrlBase;
37 import nl.openedge.baritus.util.MessageUtils;
38
39 /***
40 * convenience class
41 * @author Eelco Hillenius
42 */
43 public abstract class AbstractValidator implements ValidationRuleDependend
44 {
45
46 private ValidationActivationRule validationActivationRule = null;
47
48 /***
49 * construct
50 */
51 public AbstractValidator()
52 {
53
54 }
55
56 /***
57 * construct with validator activation rule
58 * @param rule validator activation rule
59 */
60 public AbstractValidator(ValidationActivationRule rule)
61 {
62 setValidationRule(rule);
63 }
64
65 /***
66 * get localized message for given key
67 * @param key key of message
68 * @return String localized message
69 */
70 public String getLocalizedMessage(String key)
71 {
72 return getLocalizedMessage(key, Locale.getDefault());
73 }
74
75 /***
76 * get localized message for given key and locale.
77 * If locale is null, the default locale will be used
78 * @param key key of message
79 * @param locale locale for message
80 * @return String localized message
81 */
82 public String getLocalizedMessage(String key, Locale locale)
83 {
84 return MessageUtils.getLocalizedMessage(key, locale);
85 }
86
87 /***
88 * Get localized message for given key and format it with the given parameters.
89 * If locale is null, the default locale will be used
90 * @param key key of message
91 * @param parameters parameters for the message
92 * @return String localized message
93 */
94 public String getLocalizedMessage(
95 String key, Object[] parameters)
96 {
97 return getLocalizedMessage(key, null, parameters);
98 }
99
100 /***
101 * Get localized message for given key and locale
102 * and format it with the given parameters.
103 * If locale is null, the default locale will be used
104 * @param key key of message
105 * @param locale locale for message
106 * @param parameters parameters for the message
107 * @return String localized message
108 */
109 public String getLocalizedMessage(
110 String key, Locale locale, Object[] parameters)
111 {
112 return MessageUtils.getLocalizedMessage(key, locale, parameters);
113 }
114
115 /***
116 * @see nl.openedge.baritus.validation.ValidationRuleDependend#getValidationActivationRule()
117 */
118 public ValidationActivationRule getValidationActivationRule()
119 {
120 return this.validationActivationRule;
121 }
122
123 /***
124 * @see nl.openedge.baritus.validation.ValidationRuleDependend#removeValidationActivationRule()
125 */
126 public void removeValidationActivationRule()
127 {
128 this.validationActivationRule = null;
129 }
130
131 /***
132 * @see nl.openedge.baritus.validation.ValidationRuleDependend#setValidationRule(nl.openedge.baritus.validation.ValidationActivationRule)
133 */
134 public void setValidationRule(ValidationActivationRule rule)
135 {
136 this.validationActivationRule = rule;
137 }
138
139 /***
140 * Get the - possibly translated - name of the field. If lookup in the
141 * resources failed, the provided name will be returned as is.
142 * @param formBeanContext form bean context
143 * @param name original name of field
144 * @return String the - possibly translated - name of the field.
145 * If lookup in the resources failed, the provided name will be returned as is.
146 */
147 protected String getFieldName(FormBeanContext formBeanContext, String name)
148 {
149 FormBeanCtrlBase ctrl = formBeanContext.getController();
150 String fieldName = MessageUtils.getLocalizedMessage(
151 ctrl.getPropertyNameKey(name));
152 return (fieldName != null) ? fieldName : name;
153 }
154
155 /***
156 * set error message in formBeanContext using the provided name,
157 * the current locale that is stored in the form bean context, the provided
158 * errorMessageKey and the provided arguments for parsing the localized message;
159 * the message will be stored in the form bean context with key that was
160 * provided as argument name. If no
161 * message was found, the provided errorMessageKey will be used.
162 *
163 * @param formBeanContext form bean context
164 * @param name untransalated name of the field
165 * @param errorMessageKey message key for error
166 * @param messageArguments arguments for parsing the message
167 */
168 protected void setErrorMessage(
169 FormBeanContext formBeanContext,
170 String name,
171 String errorMessageKey,
172 Object[] messageArguments)
173 {
174 Locale locale = formBeanContext.getCurrentLocale();
175 String msg = getLocalizedMessage(errorMessageKey, locale, messageArguments);
176 if(msg == null) msg = errorMessageKey;
177 formBeanContext.setError(name, msg);
178 }
179
180 }