KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > controller > ErrorCollection


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.core.controller;
66
67 import com.jcorporate.expresso.kernel.exception.ChainedException;
68 import org.apache.struts.Globals;
69 import org.apache.struts.action.ActionError;
70 import org.apache.struts.action.ActionErrors;
71
72 import java.io.Serializable JavaDoc;
73 import java.util.Iterator JavaDoc;
74
75 /**
76  * An ErrorCollection is the Controller-specific ActionErrors collection for Expresso
77  * Creation date: (8/29/00 2:10:58 PM)
78  *
79  * @author Adam Rossi, PlatinumSolutions
80  */

81 public class ErrorCollection
82         extends ActionErrors
83         implements Serializable JavaDoc,
84         Cloneable JavaDoc {
85     public static final String JavaDoc ERRORCOLLECTIONKEY = Globals.ERROR_KEY;
86
87     /**
88      * ErrorCollection constructor comment.
89      */

90     public ErrorCollection() {
91         super();
92     } /* ErrorCollection() */
93
94     /**
95      * Adds an error to the default collection.
96      * Creation date: (8/29/00 2:14:33 PM)
97      * author Adam Rossi, PlatinumSolutions
98      *
99      * @param errorMessage com.jcorporate.expresso.core.controller.Output
100      */

101     public void addError(String JavaDoc errorMessage) {
102         if (!hasErrorMessage(errorMessage)) {
103             add(ActionErrors.GLOBAL_ERROR, new ActionError(errorMessage));
104         }
105     } /* addError(String) */
106
107     /**
108      * Adds an error with one param to the default collection.
109      * author Kris Thompson frameworks-boulder.org
110      *
111      * @param errorMessage java.lang.String
112      * @param errorMessageParam1 java.lang.String
113      */

114     public void addError(String JavaDoc errorMessage, Object JavaDoc errorMessageParam1) {
115         if (!hasErrorMessage(errorMessage)) {
116             add(ActionErrors.GLOBAL_ERROR, new ActionError(errorMessage, errorMessageParam1));
117         }
118     } /* addError(String) */
119
120
121     /**
122      * Adds an error with one param to the default collection.
123      * author Kris Thompson frameworks-boulder.org
124      *
125      * @param errorMessage java.lang.String
126      * @param errorMessageParam1 java.lang.String
127      * @param errorMessageParam2 java.lang.String
128      */

129     public void addError(String JavaDoc errorMessage, Object JavaDoc errorMessageParam1, Object JavaDoc errorMessageParam2) {
130         if (!hasErrorMessage(errorMessage)) {
131             add(ActionErrors.GLOBAL_ERROR, new ActionError(errorMessage, errorMessageParam1, errorMessageParam2));
132         }
133     } /* addError(String) */
134
135
136     /**
137      * Adds an error with one param to the default collection.
138      * author Kris Thompson frameworks-boulder.org
139      *
140      * @param errorMessage java.lang.String
141      * @param errorMessageParam1 java.lang.String
142      * @param errorMessageParam2 java.lang.String
143      * @param errorMessageParam3 java.lang.String
144      */

145     public void addError(String JavaDoc errorMessage,
146                          Object JavaDoc errorMessageParam1,
147                          Object JavaDoc errorMessageParam2,
148                          Object JavaDoc errorMessageParam3) {
149         if (!hasErrorMessage(errorMessage)) {
150             add(ActionErrors.GLOBAL_ERROR,
151                     new ActionError(errorMessage, errorMessageParam1, errorMessageParam2, errorMessageParam3));
152         }
153     } /* addError(String) */
154
155     /**
156      * Adds an error with an array of arguments
157      *
158      * @param errorMessage java.lang.String
159      */

160     public void addError(String JavaDoc errorMessage, Object JavaDoc[] args) {
161         if (!hasErrorMessage(errorMessage)) {
162             add(ActionErrors.GLOBAL_ERROR, new ActionError(errorMessage, args));
163         }
164     } /* addError(String) */
165
166
167     /**
168      * Checks to see if a particular error message exists in the current collection
169      *
170      * @param errorMessage
171      * @return
172      */

173     private boolean hasErrorMessage(String JavaDoc errorMessage) {
174         String JavaDoc oneMessage = null;
175
176         /* Reject identical errors */
177         ActionError oneError = null;
178
179         for (Iterator JavaDoc allErrors = get(); allErrors.hasNext();) {
180             oneError = (ActionError) allErrors.next();
181             oneMessage = oneError.getKey();
182
183             if (oneMessage.equals(errorMessage)) {
184                 return true;
185             }
186         }
187         return false;
188     }
189
190
191     /**
192      * This method adds an error for each message in the ChainedException link.
193      *
194      * @param ex the ChainedException (DBException is an example)
195      */

196     public void addError(ChainedException ex) {
197         boolean done = false;
198
199         while (!done) {
200             String JavaDoc msg = ex.getMessage();
201             if (msg != null) {
202                 addError(ex.getMessage());
203             }
204
205             Throwable JavaDoc t = ex.getNested();
206
207             if (t == null) {
208                 done = true;
209             } else if (t instanceof ChainedException) {
210                 ex = (ChainedException) t;
211                 // another loop
212
} else {
213                 // final item is not chained--it is original exception;
214
// message and exception type are captured in last getMessage
215
done = true;
216             }
217         }
218
219     }
220
221     /**
222      * @param propName java.lang.String
223      * @param errorMessage java.lang.String
224      * @deprecated 7/04 v5.5 since expresso uses only one property name, use addError(String errmsgkey) instead; if you want args/params for template replacement, use addError(key, (Object)replacementParam)
225      * Creation date: (8/29/00 2:43:13 PM)
226      * author Adam Rossi, PlatinumSolutions
227      */

228     public void addError(String JavaDoc propName, String JavaDoc errorMessage) {
229         add(propName, new ActionError(errorMessage));
230     } /* addError(String, String) */
231
232
233     /**
234      * Creation date: (8/29/00 2:38:57 PM)
235      * author Adam Rossi, PlatinumSolutions
236      *
237      * @return int
238      */

239     public int getErrorCount() {
240         return size();
241     } /* getErrorCount() */
242
243     /**
244      * Creation date: (8/29/00 2:17:36 PM)
245      * author Adam Rossi, PlatinumSolutions
246      *
247      * @return java.util.Iterator
248      */

249     public Iterator JavaDoc getErrors() {
250         return get();
251     } /* getErrors() */
252
253     /**
254      * Retrieve a list of error strings corresponding to the ActionError
255      *
256      * @return array of Strings or null if there are no errors
257      */

258     public String JavaDoc[] getErrorStrings() {
259         if (size() == 0) {
260             return null;
261         }
262
263         String JavaDoc returnValue[] = new String JavaDoc[size()];
264         int index = 0;
265         for (Iterator JavaDoc i = this.getErrors(); i.hasNext();) {
266             returnValue[index] = ((ActionError) i.next()).getKey();
267             index++;
268         }
269
270         return returnValue;
271     }
272
273
274     /**
275      *
276      */

277     public Iterator JavaDoc getErrors(String JavaDoc label) {
278         return get(label);
279     }
280
281     /**
282      * Allows us to retract an error if at one point in the code
283      * we decide we don't actually want that error, but some previous parts
284      * were too isolated to make this decision.
285      */

286     public void removeError(String JavaDoc label) {
287         for (Iterator JavaDoc i = get(label); i.hasNext();) {
288             i.next();
289             i.remove();
290         }
291     }
292
293
294     /**
295      * clone method
296      */

297     public Object JavaDoc clone()
298             throws CloneNotSupportedException JavaDoc {
299         ErrorCollection e;
300
301         synchronized (this) {
302             e = (ErrorCollection) super.clone();
303         }
304
305         return e;
306     }
307 } /* ErrorCollection */
308
Popular Tags