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 package com.ivata.mask.validation;
80 import java.io.Serializable;
81 import java.util.List;
82 import com.ivata.mask.field.Field;
83 /***
84 * <p>
85 * This class represents an error which occurred when data is validated for
86 * submission.
87 * </p>
88 *
89 * <p>
90 * Before new data is posted to <i>ivata masks </i>, it is possible to validate
91 * it. If the data is for any reason invalid, then an instance of this class is
92 * created to indicate what is wrong or missing ind the data (invalid date
93 * format? missing parameter?). Multiple errors which occur can be grouped
94 * together in a <code>ValidationErrors</code> instance.
95 * </p>
96 *
97 * <p>
98 * This class originally appeared as part of <a
99 * href="http://www.ivata.org">ivata op </a> and <a
100 * href="http://www.ivata.com/portal">ivata team portal </a>.
101 * </p>.
102 *
103 * @since ivata masks 0.4 (2002-11-11)
104 * @author Colin MacLeod
105 * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
106 * @version $Revision: 1.4 $
107 * @see ValidationErrors
108 * @see org.apache.struts.action.ActionError
109 */
110 public class ValidationError implements Serializable {
111 /***
112 * Refer to {@link #getBundle}.
113 */
114 private String bundle;
115 /***
116 * <p>
117 * Label key of the error which occurred.
118 * </p>
119 */
120 private String errorKey;
121 /***
122 * <p>
123 * Field which caused this error.
124 * </p>
125 */
126 private Field field;
127 /***
128 * <p>
129 * A list of error string parameters. Depending on the value of
130 * <code>key</code>, there may be many parameters describing the error in
131 * detail. This attribute may be <code>null</code> if there are no
132 * parameters required.
133 * </p>
134 */
135 private List parameters = null;
136 /***
137 * Refer to {@link #getResourceFieldPath}.
138 */
139 private String resourceFieldPath;
140 /***
141 * <p>
142 * Create a new instance with the given error key and parameters.
143 * Attributes of
144 * this class are <i>immutable </i> so the values specified here cannot be
145 * later altered.
146 * </p>
147 *
148 * @param errorKeyParam
149 * label key of the error which occurred.
150 * @param parametersParam
151 * list of error string parameters, used to completed the error
152 * message. This attribute may be
153 * <code>null</code> if there are no parameters required..
154 */
155 public ValidationError(
156 final String errorKeyParam,
157 final List parametersParam) {
158 this(null, null, null, errorKeyParam,
159 parametersParam);
160 }
161 /***
162 * <p>
163 * Create a new instance with the given field, and error key.
164 * Attributes of this class are <i>immutable </i> so the values
165 * specified here cannot be later altered.
166 * </p>
167 *
168 * @param fieldParam
169 * field which contained an invalid value and caused the error.
170 * @param errorKeyParam
171 * key of the error which occurred.
172 */
173 public ValidationError(
174 final String resourceFieldPathParam,
175 final Field fieldParam, final String errorKeyParam) {
176 this(resourceFieldPathParam, null, fieldParam, errorKeyParam, null);
177 }
178 /***
179 * <p>
180 * Create a new instance with the given field and parameters. Attributes of
181 * this class are <i>immutable </i> so the values specified here cannot be
182 * later altered.
183 * </p>
184 *
185 * @param resourceFieldPathParam
186 * Refer to {@link #getResourceFieldPathParam}.
187 * @param fieldParam
188 * field which contained an invalid value and caused the error.
189 * @param errorKeyParam
190 * label key of the error which occurred.
191 * @param parametersParam
192 * list of error string parameters, used to completed the error
193 * message. This attribute may be
194 * <code>null</code> if there are no parameters required..
195 */
196 public ValidationError(
197 final String resourceFieldPathParam,
198 final Field fieldParam,
199 final String errorKeyParam,
200 final List parametersParam) {
201 this(resourceFieldPathParam, null, fieldParam, errorKeyParam,
202 parametersParam);
203 }
204 /***
205 * <p>
206 * Create a new instance with the given field, error key and
207 * bundle. Attributes of this class are <i>immutable </i> so the values
208 * specified here cannot be later altered.
209 * </p>
210 *
211 * @param bundle
212 * Refer to {@link #getBundle}.
213 * @param resourceFieldPathParam
214 * Refer to {@link #getResourceFieldPathParam}.
215 * @param fieldParam
216 * field which contained an invalid value and caused the error.
217 * @param errorKeyParam
218 * key of the error which occurred.
219 */
220 public ValidationError(
221 final String resourceFieldPathParam,
222 final String bundle,
223 final Field fieldParam,
224 final String errorKeyParam
225 ) {
226 this(resourceFieldPathParam, bundle, fieldParam, errorKeyParam, null);
227 }
228 /***
229 * <p>
230 * Create a new instance with the given bundle, field, label and parameters.
231 * Attributes of this class are <i>immutable </i> so the values specified
232 * here cannot be later altered.
233 * </p>
234 *
235 * @param bundle
236 * Refer to {@link #getBundle}.
237 * @param resourceFieldPathParam
238 * Refer to {@link #getResourceFieldPathParam}.
239 * @param fieldParam
240 * field which contained an invalid value and caused the error.
241 * @param errorKeyParam
242 * label key of the error which occurred.
243 * @param parametersParam
244 * list of error string parameters. This attribute may be
245 * <code>null</code> if there are no parameters required..
246 */
247 public ValidationError(
248 final String resourceFieldPathParam,
249 final String bundle,
250 final Field fieldParam,
251 final String errorKeyParam,
252 final List parametersParam
253 ) {
254 this.resourceFieldPath = resourceFieldPathParam;
255 this.bundle = bundle;
256 this.field = fieldParam;
257 this.errorKey = errorKeyParam;
258 this.parameters = parametersParam;
259 }
260
261 /***
262 * Get the message resource bundle associated with this error. May be
263 * <code>null</code>.
264 *
265 * @return message resource used to localize the field name for this error.
266 */
267 public String getBundle() {
268 return bundle;
269 }
270 /***
271 * <p>
272 * Message key of the error which occurred, from the error message
273 * resources.
274 * </p>
275 *
276 * @return message key describing the error.
277 */
278 public final String getErrorKey() {
279 return errorKey;
280 }
281 /***
282 * <p>
283 * Get the field which contained an invalid value and caused the error.
284 * </p>
285 *
286 * @return the current value of field.
287 */
288 public final Field getField() {
289 return field;
290 }
291 /***
292 * <p>
293 * A list of error string parameters. Depending on the value of
294 * <code>key</code>, there may be many parameters describing the error in
295 * detail. This attribute may be <code>null</code> if there are no
296 * parameters required.
297 * </p>
298 *
299 * @return the current value of parameters.
300 */
301 public final List getParameters() {
302 return parameters;
303 }
304 /***
305 * <p>
306 * Get the message resources field path associated with this field. This is
307 * the first part of the path used to search for string in the localization
308 * bundle.
309 * </p>
310 *
311 * @return message resource field path.
312 */
313 public String getResourceFieldPath() {
314 return resourceFieldPath;
315 }
316 /***
317 * Overridden from <code>Object</code> for convenience when debugging.
318 *
319 * @return the class name followed by key, field and parameters.
320 */
321 public final String toString() {
322 StringBuffer errorString = new StringBuffer();
323 errorString.append(getClass().getName());
324 errorString.append("[field[");
325 errorString.append(field);
326 errorString.append("]:key[");
327 errorString.append(errorKey);
328 errorString.append("]:parameters");
329 errorString.append(parameters);
330 errorString.append("]");
331 return errorString.toString();
332 }
333 }