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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 package com.ivata.mask.web.struts;
83 import java.util.Iterator;
84 import java.util.List;
85 import java.util.Locale;
86 import java.util.Vector;
87
88 import org.apache.log4j.Logger;
89 import org.apache.struts.action.ActionErrors;
90 import org.apache.struts.action.ActionMessage;
91
92 import com.ivata.mask.field.Field;
93 import com.ivata.mask.util.SystemException;
94 import com.ivata.mask.validation.ValidationError;
95 import com.ivata.mask.validation.ValidationErrors;
96 import com.ivata.mask.web.struts.util.MessageResourcesHandling;
97 /***
98 * <p>
99 * Converts ivata masks validation errors to struts action errors. This class
100 * has been split off from <code>ValidationErrors</code>, which was
101 * originally a part of <a href="http://www.ivata.org">ivata op </a> and <a
102 * href="http://www.ivata.com/portal">ivata team portal </a>.
103 * </p>.
104 *
105 * @since ivata masks 0.4 (2002-11-11)
106 * @author Colin MacLeod
107 * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
108 * @version $Revision: 1.6 $
109 * @see ValidationError
110 * @see org.apache.struts.action.ActionErrors
111 */
112 public final class ValidationErrorsConvertor {
113 /***
114 * Refer to {@link Logger}.
115 */
116 private static Logger log =
117 Logger.getLogger(ValidationErrorsConvertor.class);
118 /***
119 * Default text to use when there is no field.
120 */
121 private static final String NONE_FIELD = "[None]";
122 /***
123 * Private helper to avoid repetition. Get the label for the field specified
124 * and default it if the label is not available.
125 *
126 * @param locale Refer to {@link #toActionErrors}.
127 * @param bundle name of the resource bundle used to translate the field
128 * label.
129 * @param resourceFieldPath path within the bundle which prefixes the field
130 * name.
131 * @param field field whose label should be retrieved.
132 * @return localized text for this label.
133 * @throws SystemException Refer to
134 * @link MessageResourcesHandling#getDefaultLabel}.
135 */
136 private static String getFieldLabel(Locale locale, String bundle,
137 String resourceFieldPath, Field field)
138 throws SystemException {
139 String fieldLabel;
140 try {
141 fieldLabel = MessageResourcesHandling.getDefaultLabel(
142 locale, bundle, field.getName(),
143 null, null, resourceFieldPath, null,
144 false,
145 false);
146 } catch (Exception e) {
147
148
149 log.error(e);
150 fieldLabel = null;
151 }
152
153
154 if (fieldLabel == null) {
155 fieldLabel = bundle
156 + "-"
157 + resourceFieldPath
158 + "-"
159 + field.getName();
160 log.error("No label found for "
161 + "field '"
162 + fieldLabel
163 + "'");
164 }
165 return fieldLabel;
166 }
167 /***
168 * <p>
169 * Convert the errors to instances of <code>ActionError</code> for use in
170 * a <strong>Struts </strong> project.
171 * </p>
172 *
173 * @param messages This instance is used to localize the error strings.
174 * @param validationErrors contains error references which should be
175 * converted to plain text errors.
176 * @return {@link org.apache.struts.action.ActionErrors ActionErrors}
177 * collection containing <code>ActionError</code> instances whose
178 * contents mirror that of the <code>ValidationError</code>
179 * instances this instance contains.
180 * @throws SystemException if the message resources are undefined.
181 */
182 public static ActionErrors toActionErrors(
183 final ValidationErrors validationErrors,
184 final Locale locale)
185 throws SystemException {
186 assert (validationErrors != null);
187
188 List errors = validationErrors.getErrors();
189 ActionErrors actionErrors = new ActionErrors();
190 for (Iterator i = errors.iterator(); i.hasNext();) {
191 ValidationError error = (ValidationError) i.next();
192 Field field = error.getField();
193 String key = error.getErrorKey();
194 List originalParameters = error.getParameters();
195
196 Vector parameters = new Vector();
197
198 String fieldName;
199 if (field == null) {
200 fieldName= NONE_FIELD;
201 parameters.add(NONE_FIELD);
202 } else {
203 fieldName = field.getName();
204 parameters.add(getFieldLabel(locale, error.getBundle(),
205 error.getResourceFieldPath(), field));
206 }
207
208 if (originalParameters != null) {
209 for (Iterator j = originalParameters.iterator(); j.hasNext();) {
210 Object parameter = j.next();
211 if (parameter instanceof Field) {
212 String label = getFieldLabel(locale, error.getBundle(),
213 error.getResourceFieldPath(),
214 (Field) parameter);
215 parameters.add(label);
216 } else {
217 parameters.add(parameter);
218 }
219 }
220 }
221
222 int count;
223 count = parameters.size();
224 int parameterCount = 0;
225 if ((parameters == null) || (count == parameterCount++)) {
226 actionErrors.add(fieldName, new ActionMessage(key));
227
228 } else if (count == parameterCount++) {
229 actionErrors.add(fieldName,
230 new ActionMessage(key, parameters.get(0)));
231
232 } else if (count == parameterCount++) {
233 parameterCount = 0;
234 actionErrors.add(fieldName,
235 new ActionMessage(key,
236 parameters.get(parameterCount++),
237 parameters.get(parameterCount++)));
238
239 } else if (count == parameterCount++) {
240 parameterCount = 0;
241 actionErrors.add(fieldName,
242 new ActionMessage(key,
243 parameters.get(parameterCount++),
244 parameters.get(parameterCount++),
245 parameters.get(parameterCount++)));
246
247 } else if (count >= parameterCount){
248
249
250 if (count > parameterCount) {
251 log.warn(parameterCount
252 + " parameters "
253 + " encountered for key '"
254 + key
255 + "'. All parameters are: "
256 + parameters
257 + ". ActionMessage constructor only lets us have "
258 + "the first 4 - the rest will be ignored.");
259 }
260 parameterCount = 0;
261 actionErrors.add(fieldName,
262 new ActionMessage(key,
263 parameters.get(parameterCount++),
264 parameters.get(parameterCount++),
265 parameters.get(parameterCount++),
266 parameters.get(parameterCount++)));
267
268 }
269 }
270 return actionErrors;
271 }
272
273 /***
274 * Private constructor enforces utility class behavior.
275 */
276 private ValidationErrorsConvertor () {
277 }
278 }
279