001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: ValidationError.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.site;
009:
010: import java.util.logging.Logger;
011:
012: import com.uwyn.rife.tools.ExceptionUtils;
013:
014: /**
015: * Instances of this class detail subjects that were found invalid during
016: * validation.
017: * <p>Each <code>ValidationError</code> is tied to a specific subject and
018: * provides more information through an explicative textual identifier.
019: * <p>A collection of commonly used identifiers and implementations are
020: * provided as static member variables and static inner classes.
021: *
022: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
023: * @version $Revision: 3634 $
024: * @see Validated
025: * @since 1.0
026: */
027: public abstract class ValidationError implements Cloneable {
028: public final static String IDENTIFIER_MANDATORY = "MANDATORY";
029: public final static String IDENTIFIER_UNICITY = "UNICITY";
030: public final static String IDENTIFIER_WRONGLENGTH = "WRONGLENGTH";
031: public final static String IDENTIFIER_WRONGFORMAT = "WRONGFORMAT";
032: public final static String IDENTIFIER_NOTNUMERIC = "NOTNUMERIC";
033: public final static String IDENTIFIER_UNEXPECTED = "UNEXPECTED";
034: public final static String IDENTIFIER_INCOMPLETE = "INCOMPLETE";
035: public final static String IDENTIFIER_INVALID = "INVALID";
036: public final static String IDENTIFIER_NOTSAME = "NOTSAME";
037:
038: private String mIdentifier = null;
039: private String mSubject = null;
040: private Object mErroneousValue = null;
041: private boolean mOverridable = false;
042:
043: /**
044: * Creates a new <code>ValidationError</code> instance for the specified
045: * identifier and subject.
046: * <p>The error will not be automatic overridable.
047: *
048: * @param identifier a non-<code>null</code> <code>String</code> with the
049: * textual error identifier
050: * @param subject a non-<code>null</code> <code>String</code> with the
051: * name of the erroneous subject
052: * @since 1.0
053: */
054: public ValidationError(String identifier, String subject) {
055: if (null == identifier)
056: throw new IllegalArgumentException(
057: "identifier can't be null");
058: if (null == subject)
059: throw new IllegalArgumentException("subject can't be null");
060:
061: mIdentifier = identifier;
062: mSubject = subject;
063: }
064:
065: /**
066: * Creates a new <code>ValidationError</code> instance for the specified
067: * identifier and subject.
068: *
069: * @param identifier a non-<code>null</code> <code>String</code> with the
070: * textual error identifier
071: * @param subject a non-<code>null</code> <code>String</code> with the
072: * name of the erroneous subject
073: * @param overridable <code>true</code> to make any other error for the same
074: * subject override this error, <code>false</code> if this error should
075: * always be shown
076: * @since 1.5
077: */
078: public ValidationError(String identifier, String subject,
079: boolean overridable) {
080: this (identifier, subject);
081:
082: mOverridable = overridable;
083: }
084:
085: /**
086: * Returns the textual identifier that categorizes this validation error.
087: *
088: * @since 1.0
089: */
090: public final String getIdentifier() {
091: return mIdentifier;
092: }
093:
094: /**
095: * Returns the erroneous subject name of this validation error.
096: *
097: * @since 1.0
098: */
099: public final String getSubject() {
100: return mSubject;
101: }
102:
103: /**
104: * Returns wether this error is overridable for the same subject.
105: *
106: * @since 1.5
107: */
108: public final boolean isOverridable() {
109: return mOverridable;
110: }
111:
112: /**
113: * Stores the erroneous value that caused the validation error.
114: * This is optional and should only be done when the erroneous value
115: * gives more information from the context in which the validation
116: * error occurred.
117: *
118: * @since 1.0
119: */
120: public void setErroneousValue(Object erroneousValue) {
121: mErroneousValue = erroneousValue;
122: }
123:
124: /**
125: * Chainable setter to make validation error construction easier
126: *
127: * @see #setErroneousValue
128: * @since 1.0
129: */
130: public ValidationError erroneousValue(Object erroneousValue) {
131: setErroneousValue(erroneousValue);
132:
133: return this ;
134: }
135:
136: /**
137: * Returns the erroneous value that caused the validation error, if it's present.
138: *
139: * @since 1.0
140: */
141: public Object getErroneousValue() {
142: return mErroneousValue;
143: }
144:
145: public Object clone() {
146: try {
147: return super .clone();
148: } catch (CloneNotSupportedException e) {
149: ///CLOVER:OFF
150: // this should never happen
151: Logger.getLogger("com.uwyn.rife.site").severe(
152: ExceptionUtils.getExceptionStackTrace(e));
153: return null;
154: ///CLOVER:ON
155: }
156: }
157:
158: public int hashCode() {
159: return mIdentifier.hashCode() * mSubject.hashCode();
160: }
161:
162: public boolean equals(Object object) {
163: if (null == object) {
164: return false;
165: }
166:
167: if (object instanceof ValidationError) {
168: ValidationError other_error = (ValidationError) object;
169: return other_error.mIdentifier.equals(mIdentifier)
170: && other_error.mSubject.equals(mSubject);
171: }
172:
173: return false;
174: }
175:
176: public static class MANDATORY extends ValidationError {
177: public MANDATORY(String subject) {
178: super (IDENTIFIER_MANDATORY, subject, true);
179: }
180: }
181:
182: public static class UNICITY extends ValidationError {
183: public UNICITY(String subject) {
184: super (IDENTIFIER_UNICITY, subject);
185: }
186: }
187:
188: public static class WRONGLENGTH extends ValidationError {
189: public WRONGLENGTH(String subject) {
190: super (IDENTIFIER_WRONGLENGTH, subject);
191: }
192: }
193:
194: public static class WRONGFORMAT extends ValidationError {
195: public WRONGFORMAT(String subject) {
196: super (IDENTIFIER_WRONGFORMAT, subject);
197: }
198: }
199:
200: public static class NOTNUMERIC extends ValidationError {
201: public NOTNUMERIC(String subject) {
202: super (IDENTIFIER_NOTNUMERIC, subject);
203: }
204: }
205:
206: public static class UNEXPECTED extends ValidationError {
207: public UNEXPECTED(String subject) {
208: super (IDENTIFIER_UNEXPECTED, subject);
209: }
210: }
211:
212: public static class INCOMPLETE extends ValidationError {
213: public INCOMPLETE(String subject) {
214: super (IDENTIFIER_INCOMPLETE, subject);
215: }
216: }
217:
218: public static class INVALID extends ValidationError {
219: public INVALID(String subject) {
220: super (IDENTIFIER_INVALID, subject);
221: }
222: }
223:
224: public static class NOTSAMEAS extends ValidationError {
225: public NOTSAMEAS(String subject) {
226: super(IDENTIFIER_NOTSAME, subject);
227: }
228: }
229: }
|