View Javadoc

1   /*
2    * Copyright (c) 2001 - 2005 ivata limited.
3    * All rights reserved.
4    * -----------------------------------------------------------------------------
5    * ivata masks may be redistributed under the GNU General Public
6    * License as published by the Free Software Foundation;
7    * version 2 of the License.
8    *
9    * These programs are free software; you can redistribute them and/or
10   * modify them under the terms of the GNU General Public License
11   * as published by the Free Software Foundation; version 2 of the License.
12   *
13   * These programs are distributed in the hope that they will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16   *
17   * See the GNU General Public License in the file LICENSE.txt for more
18   * details.
19   *
20   * If you would like a copy of the GNU General Public License write to
21   *
22   * Free Software Foundation, Inc.
23   * 59 Temple Place - Suite 330
24   * Boston, MA 02111-1307, USA.
25   *
26   *
27   * To arrange commercial support and licensing, contact ivata at
28   *                  http://www.ivata.com/contact.jsp
29   * -----------------------------------------------------------------------------
30   * $Log: ValidationError.java,v $
31   * Revision 1.4  2005/04/09 18:04:16  colinmacleod
32   * Changed copyright text to GPL v2 explicitly.
33   *
34   * Revision 1.3  2005/03/10 10:23:50  colinmacleod
35   * Added bundle, errorKey and param.
36   *
37   * Revision 1.2  2005/01/06 22:13:22  colinmacleod
38   * Moved up a version number.
39   * Changed copyright notices to 2005.
40   * Updated the documentation:
41   *   - started working on multiproject:site docu.
42   *   - changed the logo.
43   * Added checkstyle and fixed LOADS of style issues.
44   * Added separate thirdparty subproject.
45   * Added struts (in web), util and webgui (in webtheme) from ivata op.
46   *
47   * Revision 1.1  2004/12/29 20:07:06  colinmacleod
48   * Renamed subproject masks to mask.
49   *
50   * Revision 1.1.1.1  2004/05/16 20:40:32  colinmacleod
51   * Ready for 0.1 release
52   *
53   * Revision 1.3  2004/03/21 21:16:23  colinmacleod
54   * Shortened name to ivata op.
55   *
56   * Revision 1.2  2004/02/01 22:07:29  colinmacleod
57   * Added full names to author tags
58   *
59   * Revision 1.1  2004/01/29 13:48:41  janboros
60   * Moved ivata op to SourceForge
61   *
62   * Revision 1.1  2003/10/16 07:27:45  colin
63   * caming to repository
64   *
65   * Revision 1.4  2003/02/24 19:08:17  colin
66   * *** empty log message ***
67   *
68   * Revision 1.3  2003/02/20 20:24:11  colin
69   * improved validation by adding ValidationField and ValidationException
70   *
71   * Revision 1.2  2003/02/04 17:36:47  colin
72   * copyright notice
73   *
74   * Revision 1.1  2002/11/12 10:40:47  colin
75   * first version in CVS. encapsulates Struts class ActionError
76   * and passes resource keys from server-side to client-side
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 }