001: /*
002: * Copyright (c) 2001 - 2005 ivata limited.
003: * All rights reserved.
004: * -----------------------------------------------------------------------------
005: * ivata masks may be redistributed under the GNU General Public
006: * License as published by the Free Software Foundation;
007: * version 2 of the License.
008: *
009: * These programs are free software; you can redistribute them and/or
010: * modify them under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; version 2 of the License.
012: *
013: * These programs are distributed in the hope that they will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: *
017: * See the GNU General Public License in the file LICENSE.txt for more
018: * details.
019: *
020: * If you would like a copy of the GNU General Public License write to
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place - Suite 330
024: * Boston, MA 02111-1307, USA.
025: *
026: *
027: * To arrange commercial support and licensing, contact ivata at
028: * http://www.ivata.com/contact.jsp
029: * -----------------------------------------------------------------------------
030: * $Log: ValidationError.java,v $
031: * Revision 1.8 2005/10/11 18:55:29 colinmacleod
032: * Fixed some checkstyle and javadoc issues.
033: *
034: * Revision 1.7 2005/10/03 10:17:25 colinmacleod
035: * Fixed some style and javadoc issues.
036: *
037: * Revision 1.6 2005/10/02 14:06:32 colinmacleod
038: * Added/improved log4j logging.
039: *
040: * Revision 1.5 2005/09/14 12:51:52 colinmacleod
041: * Added serialVersionUID.
042: *
043: * Revision 1.4 2005/04/09 18:04:16 colinmacleod
044: * Changed copyright text to GPL v2 explicitly.
045: *
046: * Revision 1.3 2005/03/10 10:23:50 colinmacleod
047: * Added bundle, errorKey and param.
048: *
049: * Revision 1.2 2005/01/06 22:13:22 colinmacleod
050: * Moved up a version number.
051: * Changed copyright notices to 2005.
052: * Updated the documentation:
053: * - started working on multiproject:site docu.
054: * - changed the logo.
055: * Added checkstyle and fixed LOADS of style issues.
056: * Added separate thirdparty subproject.
057: * Added struts (in web), util and webgui (in webtheme) from ivata op.
058: *
059: * Revision 1.1 2004/12/29 20:07:06 colinmacleod
060: * Renamed subproject masks to mask.
061: *
062: * Revision 1.1.1.1 2004/05/16 20:40:32 colinmacleod
063: * Ready for 0.1 release
064: *
065: * Revision 1.3 2004/03/21 21:16:23 colinmacleod
066: * Shortened name to ivata op.
067: *
068: * Revision 1.2 2004/02/01 22:07:29 colinmacleod
069: * Added full names to author tags
070: *
071: * Revision 1.1 2004/01/29 13:48:41 janboros
072: * Moved ivata op to SourceForge
073: *
074: * Revision 1.1 2003/10/16 07:27:45 colin
075: * caming to repository
076: *
077: * Revision 1.4 2003/02/24 19:08:17 colin
078: * *** empty log message ***
079: *
080: * Revision 1.3 2003/02/20 20:24:11 colin
081: * improved validation by adding ValidationField and ValidationException
082: *
083: * Revision 1.2 2003/02/04 17:36:47 colin
084: * copyright notice
085: *
086: * Revision 1.1 2002/11/12 10:40:47 colin
087: * first version in CVS. encapsulates Struts class ActionError
088: * and passes resource keys from server-side to client-side
089: * -----------------------------------------------------------------------------
090: */
091: package com.ivata.mask.validation;
092:
093: import org.apache.log4j.Logger;
094:
095: import java.io.Serializable;
096: import java.util.List;
097:
098: import com.ivata.mask.field.Field;
099:
100: /**
101: * <p>
102: * This class represents an error which occurred when data is validated for
103: * submission.
104: * </p>
105: *
106: * <p>
107: * Before new data is posted to <i>ivata masks </i>, it is possible to validate
108: * it. If the data is for any reason invalid, then an instance of this class is
109: * created to indicate what is wrong or missing ind the data (invalid date
110: * format? missing parameter?). Multiple errors which occur can be grouped
111: * together in a <code>ValidationErrors</code> instance.
112: * </p>
113: *
114: * <p>
115: * This class originally appeared as part of <a
116: * href="http://www.ivata.org">ivata op </a> and <a
117: * href="http://www.ivata.com/portal">ivata team portal </a>.
118: * </p>.
119: *
120: * @since ivata masks 0.4 (2002-11-11)
121: * @author Colin MacLeod
122: * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
123: * @version $Revision: 1.8 $
124: * @see ValidationErrors
125: */
126: public class ValidationError implements Serializable {
127: /**
128: * Logger for this class.
129: */
130: private static final Logger logger = Logger
131: .getLogger(ValidationError.class);
132:
133: /**
134: * Serialization version (for <code>Serializable</code> interface).
135: */
136: private static final long serialVersionUID = 1L;
137: /**
138: * <copyDoc>Refer to {@link #getBundle}.</copyDoc>
139: */
140: private String bundle;
141: /**
142: * <p>
143: * Label key of the error which occurred.
144: * </p>
145: */
146: private String errorKey;
147: /**
148: * <p>
149: * Field which caused this error.
150: * </p>
151: */
152: private Field field;
153: /**
154: * <p>
155: * A list of error string parameters. Depending on the value of
156: * <code>key</code>, there may be many parameters describing the error in
157: * detail. This attribute may be <code>null</code> if there are no
158: * parameters required.
159: * </p>
160: */
161: private List parameters = null;
162: /**
163: * <copyDoc>Refer to {@link #getResourceFieldPath}.</copyDoc>
164: */
165: private String resourceFieldPath;
166:
167: /**
168: * <p>
169: * Create a new instance with the given field, and error key.
170: * Attributes of this class are <i>immutable </i> so the values
171: * specified here cannot be later altered.
172: * </p>
173: *
174: * @param resourceFieldPathParam
175: * <copyDoc>Refer to {@link #getResourceFieldPathParam}.</copyDoc>
176: * @param fieldParam
177: * field which contained an invalid value and caused the error.
178: * @param errorKeyParam
179: * key of the error which occurred.
180: */
181: public ValidationError(final String resourceFieldPathParam,
182: final Field fieldParam, final String errorKeyParam) {
183: this (resourceFieldPathParam, null, fieldParam, errorKeyParam,
184: null);
185: }
186:
187: /**
188: * <p>
189: * Create a new instance with the given field and parameters. Attributes of
190: * this class are <i>immutable </i> so the values specified here cannot be
191: * later altered.
192: * </p>
193: *
194: * @param resourceFieldPathParam
195: * <copyDoc>Refer to {@link #getResourceFieldPathParam}.</copyDoc>
196: * @param fieldParam
197: * field which contained an invalid value and caused the error.
198: * @param errorKeyParam
199: * label key of the error which occurred.
200: * @param parametersParam
201: * list of error string parameters, used to completed the error
202: * message. This attribute may be
203: * <code>null</code> if there are no parameters required..
204: */
205: public ValidationError(final String resourceFieldPathParam,
206: final Field fieldParam, final String errorKeyParam,
207: final List parametersParam) {
208: this (resourceFieldPathParam, null, fieldParam, errorKeyParam,
209: parametersParam);
210: }
211:
212: /**
213: * <p>
214: * Create a new instance with the given error key and parameters.
215: * Attributes of
216: * this class are <i>immutable </i> so the values specified here cannot be
217: * later altered.
218: * </p>
219: *
220: * @param errorKeyParam
221: * label key of the error which occurred.
222: * @param parametersParam
223: * list of error string parameters, used to completed the error
224: * message. This attribute may be
225: * <code>null</code> if there are no parameters required..
226: */
227: public ValidationError(final String errorKeyParam,
228: final List parametersParam) {
229: this (null, null, null, errorKeyParam, parametersParam);
230: }
231:
232: /**
233: * <p>
234: * Create a new instance with the given field, error key and
235: * bundle. Attributes of this class are <i>immutable </i> so the values
236: * specified here cannot be later altered.
237: * </p>
238: *
239: * @param bundleParam
240: * <copyDoc>Refer to {@link #getBundle}.</copyDoc>
241: * @param resourceFieldPathParam
242: * <copyDoc>Refer to {@link #getResourceFieldPathParam}.</copyDoc>
243: * @param fieldParam
244: * field which contained an invalid value and caused the error.
245: * @param errorKeyParam
246: * key of the error which occurred.
247: */
248: public ValidationError(final String resourceFieldPathParam,
249: final String bundleParam, final Field fieldParam,
250: final String errorKeyParam) {
251: this (resourceFieldPathParam, bundleParam, fieldParam,
252: errorKeyParam, null);
253: }
254:
255: /**
256: * <p>
257: * Create a new instance with the given bundle, field, label and parameters.
258: * Attributes of this class are <i>immutable </i> so the values specified
259: * here cannot be later altered.
260: * </p>
261: *
262: * @param bundleParam
263: * <copyDoc>Refer to {@link #getBundle}.</copyDoc>
264: * @param resourceFieldPathParam
265: * <copyDoc>Refer to {@link #getResourceFieldPathParam}.</copyDoc>
266: * @param fieldParam
267: * field which contained an invalid value and caused the error.
268: * @param errorKeyParam
269: * label key of the error which occurred.
270: * @param parametersParam
271: * list of error string parameters. This attribute may be
272: * <code>null</code> if there are no parameters required..
273: */
274: public ValidationError(final String resourceFieldPathParam,
275: final String bundleParam, final Field fieldParam,
276: final String errorKeyParam, final List parametersParam) {
277: this .resourceFieldPath = resourceFieldPathParam;
278: this .bundle = bundleParam;
279: this .field = fieldParam;
280: this .errorKey = errorKeyParam;
281: this .parameters = parametersParam;
282: }
283:
284: /**
285: * Get the message resource bundle associated with this error. May be
286: * <code>null</code>.
287: *
288: * @return message resource used to localize the field name for this error.
289: */
290: public String getBundle() {
291: if (logger.isDebugEnabled()) {
292: logger.debug("getBundle() - start");
293: }
294:
295: if (logger.isDebugEnabled()) {
296: logger
297: .debug("getBundle() - end - return value = "
298: + bundle);
299: }
300: return bundle;
301: }
302:
303: /**
304: * <p>
305: * Message key of the error which occurred, from the error message
306: * resources.
307: * </p>
308: *
309: * @return message key describing the error.
310: */
311: public final String getErrorKey() {
312: if (logger.isDebugEnabled()) {
313: logger.debug("getErrorKey() - start");
314: }
315:
316: if (logger.isDebugEnabled()) {
317: logger.debug("getErrorKey() - end - return value = "
318: + errorKey);
319: }
320: return errorKey;
321: }
322:
323: /**
324: * <p>
325: * Get the field which contained an invalid value and caused the error.
326: * </p>
327: *
328: * @return the current value of field.
329: */
330: public final Field getField() {
331: if (logger.isDebugEnabled()) {
332: logger.debug("getField() - start");
333: }
334:
335: if (logger.isDebugEnabled()) {
336: logger.debug("getField() - end - return value = " + field);
337: }
338: return field;
339: }
340:
341: /**
342: * <p>
343: * A list of error string parameters. Depending on the value of
344: * <code>key</code>, there may be many parameters describing the error in
345: * detail. This attribute may be <code>null</code> if there are no
346: * parameters required.
347: * </p>
348: *
349: * @return the current value of parameters.
350: */
351: public final List getParameters() {
352: if (logger.isDebugEnabled()) {
353: logger.debug("getParameters() - start");
354: }
355:
356: if (logger.isDebugEnabled()) {
357: logger.debug("getParameters() - end - return value = "
358: + parameters);
359: }
360: return parameters;
361: }
362:
363: /**
364: * <p>
365: * Get the message resources field path associated with this field. This is
366: * the first part of the path used to search for string in the localization
367: * bundle.
368: * </p>
369: *
370: * @return message resource field path.
371: */
372: public String getResourceFieldPath() {
373: if (logger.isDebugEnabled()) {
374: logger.debug("getResourceFieldPath() - start");
375: }
376:
377: if (logger.isDebugEnabled()) {
378: logger
379: .debug("getResourceFieldPath() - end - return value = "
380: + resourceFieldPath);
381: }
382: return resourceFieldPath;
383: }
384:
385: /**
386: * Overridden from <code>Object</code> for convenience when debugging.
387: *
388: * @return the class name followed by key, field and parameters.
389: */
390: public final String toString() {
391: if (logger.isDebugEnabled()) {
392: logger.debug("toString() - start");
393: }
394:
395: StringBuffer errorString = new StringBuffer();
396: errorString.append(getClass().getName());
397: errorString.append("[field[");
398: errorString.append(field);
399: errorString.append("]:key[");
400: errorString.append(errorKey);
401: errorString.append("]:parameters");
402: errorString.append(parameters);
403: errorString.append("]");
404: String returnString = errorString.toString();
405: if (logger.isDebugEnabled()) {
406: logger.debug("toString() - end - return value = "
407: + returnString);
408: }
409: return returnString;
410: }
411: }
|