1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.finalist.util.ejb.validation;
19
20 import java.io.Serializable;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.Iterator;
24 import java.util.HashMap;
25
26
27 /***
28 * <p>A class that encapsulates the error messages being reported by
29 * the <code>validate()</code> method of an <code>ActionForm</code>.
30 * Validation errors are either global to the entire <code>ActionForm</code>
31 * bean they are associated with, or they are specific to a particular
32 * bean property (and, therefore, a particular input field on the corresponding
33 * form).</p>
34 *
35 * <p>Each individual error is described by an <code>ModelError</code>
36 * object, which contains a message key (to be looked up in an appropriate
37 * message resources database), and up to four placeholder arguments used for
38 * parametric substitution in the resulting message.</p>
39 *
40 * <p><strong>IMPLEMENTATION NOTE</strong> - It is assumed that these objects
41 * are created and manipulated only within the context of a single thread.
42 * Therefore, no synchronization is required for access to internal
43 * collections.</p>
44 *
45 * @author David Geary (copied)
46 * @author Craig R. McClanahan (copied)
47 * @version $Revision: 1.1 $ $Date: 2004/03/01 19:29:19 $
48 */
49
50 public class ModelErrors implements Serializable {
51 /*** "Property" name to store global error under... */
52 public final static String GLOBAL_ERROR = "global_error";
53
54
55
56
57 /***
58 * The accumulated set of <code>ModelError</code> objects (represented
59 * as an ArrayList) for each property, keyed by property name.
60 */
61 protected HashMap errors = new HashMap();
62
63
64
65
66
67 /***
68 * Add an error message to the set of errors for the specified property.
69 *
70 * @param property Property name (or 4.GLOBAL_ERROR)
71 * @param error The error message to be added
72 */
73 public void add(String property, ModelError error) {
74
75 ArrayList list = (ArrayList) errors.get(property);
76 if (list == null) {
77 list = new ArrayList();
78 errors.put(property, list);
79 }
80 list.add(error);
81
82 }
83
84
85 /***
86 * Add an error message to the set of errors for the specified property.
87 *
88 * @param property Property name (or ModelErrors.GLOBAL_ERROR)
89 * @param error The error message to be added
90 */
91 public void addGenericError(ModelError error) {
92 add(GLOBAL_ERROR, error);
93 }
94
95
96 /***
97 * Clear all error messages recorded by this object.
98 */
99 public void clear() {
100
101 errors.clear();
102
103 }
104
105
106 /***
107 * Return <code>true</code> if there are no error messages recorded
108 * in this collection, or <code>false</code> otherwise.
109 */
110 public boolean empty() {
111
112 return (errors.size() == 0);
113
114 }
115
116
117 /***
118 * Return the set of all recorded error messages, without distinction
119 * by which property the messages are associated with. If there are
120 * no error messages recorded, an empty enumeration is returned.
121 */
122 public Iterator get() {
123
124 if (errors.size() == 0)
125 return (Collections.EMPTY_LIST.iterator());
126 ArrayList results = new ArrayList();
127 Iterator props = errors.keySet().iterator();
128 while (props.hasNext()) {
129 String prop = (String) props.next();
130 Iterator errors = ((ArrayList) this.errors.get(prop)).iterator();
131 while (errors.hasNext())
132 results.add(errors.next());
133 }
134 return (results.iterator());
135
136 }
137
138
139 /***
140 * Return the set of error messages related to a specific property.
141 * If there are no such errors, an empty enumeration is returned.
142 *
143 * @param property Property name (or ModelErrors.GLOBAL_ERROR)
144 */
145 public Iterator get(String property) {
146
147 ArrayList list = (ArrayList) errors.get(property);
148 if (list == null)
149 return (Collections.EMPTY_LIST.iterator());
150 else
151 return (list.iterator());
152
153 }
154
155
156 /***
157 * Return the set of property names for which at least one error has
158 * been recorded. If there are no errors, an empty Iterator is returned.
159 * If you have recorded global errors, the String value of
160 * <code>ModelErrors.GLOBAL_ERROR</code> will be one of the returned
161 * property names.
162 */
163 public Iterator properties() {
164
165 return (errors.keySet().iterator());
166
167 }
168
169
170 /***
171 * Return the number of errors recorded for all properties (including
172 * global errors). <strong>NOTE</strong> - it is more efficient to call
173 * <code>empty()</code> if all you care about is whether or not there are
174 * any error messages at all.
175 */
176 public int size() {
177
178 int total = 0;
179 Iterator keys = errors.keySet().iterator();
180 while (keys.hasNext()) {
181 String key = (String) keys.next();
182 ArrayList list = (ArrayList) errors.get(key);
183 total += list.size();
184 }
185 return (total);
186
187 }
188
189
190 /***
191 * Return the number of errors associated with the specified property.
192 *
193 * @param property Property name (or ModelErrors.GLOBAL_ERROR)
194 */
195 public int size(String property) {
196
197 ArrayList list = (ArrayList) errors.get(property);
198 if (list == null)
199 return (0);
200 else
201 return (list.size());
202
203 }
204
205
206 }