KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > exception > ConstraintException


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.exception;
25
26
27 /**
28  * <p>Thrown to indicate that some business rule rule has been violated. Examples
29  * include trying to assign non-unique values, illegal syntax, missing values for required
30  * fields...</p>
31  * <p>As there is a possibility that more than one rule is validated, ConstraintException
32  * can be chained.</p>
33  * <p>Note! This is not an internal exception </p>
34  *
35  * @author <a HREF="mailto:meat_for_the_butcher@yahoo.com">Patrik Nyborg</a>
36  */

37 public class ConstraintException extends Exception JavaDoc {
38   // --- [Constants] -----------------------------------------------------------
39
// --- [Attributes] ----------------------------------------------------------
40

41   // Indicates the error type (basically a resource bundle key).
42
private String JavaDoc errorCode;
43   // The name of the (entity) field causing the exception.
44
private String JavaDoc fieldName;
45   // The next ConstraintException in the chain (may be null).
46
private ConstraintException chainedException;
47
48
49
50   // --- [Static] --------------------------------------------------------------
51
// --- [Constructors] --------------------------------------------------------
52

53   /**
54    * Construct a ConstraintException with the specified field name and error code.
55    *
56    * @param fieldName the name of the (entity) field causing the exception.
57    * @param errorCode indicates the error type.
58    */

59     public ConstraintException(String JavaDoc fieldName, String JavaDoc errorCode)
60     {
61         super();
62
63         // defensive, otherwise add null checks in equals()
64
this.fieldName = (fieldName == null) ? "" : fieldName;
65         this.errorCode = (errorCode == null) ? "" : errorCode;
66     }
67
68   /**
69    *
70    */

71   public ConstraintException(String JavaDoc fieldName, String JavaDoc errorCode, ConstraintException chainedException) {
72     this(fieldName, errorCode);
73     this.chainedException = chainedException;
74   }
75
76
77
78   // --- [Public] --------------------------------------------------------------
79

80   /**
81    *
82    */

83   public void setChainedException(ConstraintException chainedException) {
84     this.chainedException = chainedException;
85   }
86
87   /**
88    *
89    */

90   public ConstraintException getChainedException() {
91     return this.chainedException;
92   }
93
94   /**
95    *
96    */

97   public String JavaDoc getFieldName() {
98     return this.fieldName;
99   }
100
101   /**
102    *
103    */

104   public String JavaDoc getErrorCode() {
105     return this.errorCode;
106   }
107
108
109
110   // --- [X implementation] ----------------------------------------------------
111
// --- [java.lang.Exception Overrides] ---------------------------------------
112

113   /**
114    *
115    */

116   public String JavaDoc getMessage() {
117     return "Constrain violated on field [" + this.fieldName + "], code [" + this.errorCode + "]";
118   }
119   
120
121
122   // --- [java.lang.Object Overrides] ------------------------------------------
123

124   /**
125    *
126    */

127   public String JavaDoc toString() {
128     return "<ConstraintException>[" + getFieldName() + "," + getErrorCode() + "]";
129   }
130
131   /**
132    * <p>Returns true if the specified object is a constraint exception with the same field name and
133    * error code.</p>
134    * <p>Note! Does not include equals check for any chained exceptions. To check for the, use
135    * the ConstraintExceptionBuffer class.</p>
136    *
137    * @param o the reference object with which to compare.
138    * @return true if this object is the same as the specified object; false otherwise.
139    */

140   public boolean equals(Object JavaDoc o) {
141     if(o == null || !(o instanceof ConstraintException)) {
142       return false;
143     }
144     final ConstraintException e = (ConstraintException) o;
145     return getFieldName().equals(e.getFieldName()) && getErrorCode().equals(e.getErrorCode());
146   }
147
148   /**
149    * Note! It is generally necessary to override the hashCode method whenever the equals() method is overridden.
150    * Returns a hash code value for the object.
151    *
152    * @return a hash code value for this object.
153    */

154   public int hashCode() {
155     return (getFieldName() + getErrorCode()).hashCode();
156   }
157
158
159
160   // --- [Package protected] ---------------------------------------------------
161
// --- [Private] -------------------------------------------------------------
162
// --- [Protected] -----------------------------------------------------------
163
// --- [Inner classes] -------------------------------------------------------
164
}
Popular Tags