1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 package net.mlw.vlh.adapter.jdbc.dynabean.fix;
64
65
66 import java.sql.SQLException;
67 import java.util.Iterator;
68 import java.util.NoSuchElementException;
69
70 import org.apache.commons.beanutils.DynaBean;
71 import org.apache.commons.beanutils.DynaClass;
72
73
74 /***
75 * <p>Implementation of <code>java.util.Iterator</code> returned by the
76 * <code>iterator()</code> method of {@link ResultSetDynaClass}. Each
77 * object returned by this iterator will be a {@link DynaBean} that
78 * represents a single row from the result set being wrapped.</p>
79 *
80 * @author Craig R. McClanahan
81 * @version $Revision: 1.1 $ $Date: 2004/06/03 14:27:13 $
82 */
83
84 public class ResultSetIterator implements DynaBean, Iterator {
85
86
87
88
89
90 /***
91 * <p>Construct an <code>Iterator</code> for the result set being wrapped
92 * by the specified {@link ResultSetDynaClass}.</p>
93 *
94 * @param dynaClass The {@link ResultSetDynaClass} wrapping the
95 * result set we will iterate over
96 */
97 ResultSetIterator(ResultSetDynaClass dynaClass) {
98
99 this.dynaClass = dynaClass;
100
101 }
102
103
104
105
106
107
108 /***
109 * <p>Flag indicating whether the result set is currently positioned at a
110 * row for which we have not yet returned an element in the iteration.</p>
111 */
112 protected boolean current = false;
113
114
115 /***
116 * <p>The {@link ResultSetDynaClass} we are associated with.</p>
117 */
118 protected ResultSetDynaClass dynaClass = null;
119
120
121 /***
122 * <p>Flag indicating whether the result set has indicated that there are
123 * no further rows.</p>
124 */
125 protected boolean eof = false;
126
127
128
129
130
131 /***
132 * Does the specified mapped property contain a value for the specified
133 * key value?
134 *
135 * @param name Name of the property to check
136 * @param key Name of the key to check
137 *
138 * @exception IllegalArgumentException if there is no property
139 * of the specified name
140 */
141 public boolean contains(String name, String key) {
142
143 throw new UnsupportedOperationException
144 ("FIXME - mapped properties not currently supported");
145
146 }
147
148
149 /***
150 * Return the value of a simple property with the specified name.
151 *
152 * @param name Name of the property whose value is to be retrieved
153 *
154 * @exception IllegalArgumentException if there is no property
155 * of the specified name
156 */
157 public Object get(String name) {
158
159 if (dynaClass.getDynaProperty(name) == null) {
160 throw new IllegalArgumentException(name);
161 }
162 try {
163 return (dynaClass.getResultSet().getObject(name));
164 } catch (SQLException e) {
165 return null;
166 }
167
168 }
169
170
171 /***
172 * Return the value of an indexed property with the specified name.
173 *
174 * @param name Name of the property whose value is to be retrieved
175 * @param index Index of the value to be retrieved
176 *
177 * @exception IllegalArgumentException if there is no property
178 * of the specified name
179 * @exception IllegalArgumentException if the specified property
180 * exists, but is not indexed
181 * @exception IndexOutOfBoundsException if the specified index
182 * is outside the range of the underlying property
183 * @exception NullPointerException if no array or List has been
184 * initialized for this property
185 */
186 public Object get(String name, int index) {
187
188 throw new UnsupportedOperationException
189 ("FIXME - indexed properties not currently supported");
190
191 }
192
193
194 /***
195 * Return the value of a mapped property with the specified name,
196 * or <code>null</code> if there is no value for the specified key.
197 *
198 * @param name Name of the property whose value is to be retrieved
199 * @param key Key of the value to be retrieved
200 *
201 * @exception IllegalArgumentException if there is no property
202 * of the specified name
203 * @exception IllegalArgumentException if the specified property
204 * exists, but is not mapped
205 */
206 public Object get(String name, String key) {
207
208 throw new UnsupportedOperationException
209 ("FIXME - mapped properties not currently supported");
210
211 }
212
213
214 /***
215 * Return the <code>DynaClass</code> instance that describes the set of
216 * properties available for this DynaBean.
217 */
218 public DynaClass getDynaClass() {
219
220 return (this.dynaClass);
221
222 }
223
224
225 /***
226 * Remove any existing value for the specified key on the
227 * specified mapped property.
228 *
229 * @param name Name of the property for which a value is to
230 * be removed
231 * @param key Key of the value to be removed
232 *
233 * @exception IllegalArgumentException if there is no property
234 * of the specified name
235 */
236 public void remove(String name, String key) {
237
238 throw new UnsupportedOperationException
239 ("FIXME - mapped operations not currently supported");
240
241 }
242
243
244 /***
245 * Set the value of a simple property with the specified name.
246 *
247 * @param name Name of the property whose value is to be set
248 * @param value Value to which this property is to be set
249 *
250 * @exception ConversionException if the specified value cannot be
251 * converted to the type required for this property
252 * @exception IllegalArgumentException if there is no property
253 * of the specified name
254 * @exception NullPointerException if an attempt is made to set a
255 * primitive property to null
256 */
257 public void set(String name, Object value) {
258
259 if (dynaClass.getDynaProperty(name) == null) {
260 throw new IllegalArgumentException(name);
261 }
262 try {
263 dynaClass.getResultSet().updateObject(name, value);
264 } catch (SQLException e) {
265 throw new RuntimeException
266 ("set(" + name + "): SQLException: " + e);
267 }
268
269 }
270
271
272 /***
273 * Set the value of an indexed property with the specified name.
274 *
275 * @param name Name of the property whose value is to be set
276 * @param index Index of the property to be set
277 * @param value Value to which this property is to be set
278 *
279 * @exception ConversionException if the specified value cannot be
280 * converted to the type required for this property
281 * @exception IllegalArgumentException if there is no property
282 * of the specified name
283 * @exception IllegalArgumentException if the specified property
284 * exists, but is not indexed
285 * @exception IndexOutOfBoundsException if the specified index
286 * is outside the range of the underlying property
287 */
288 public void set(String name, int index, Object value) {
289
290 throw new UnsupportedOperationException
291 ("FIXME - indexed properties not currently supported");
292
293 }
294
295
296 /***
297 * Set the value of a mapped property with the specified name.
298 *
299 * @param name Name of the property whose value is to be set
300 * @param key Key of the property to be set
301 * @param value Value to which this property is to be set
302 *
303 * @exception ConversionException if the specified value cannot be
304 * converted to the type required for this property
305 * @exception IllegalArgumentException if there is no property
306 * of the specified name
307 * @exception IllegalArgumentException if the specified property
308 * exists, but is not mapped
309 */
310 public void set(String name, String key, Object value) {
311
312 throw new UnsupportedOperationException
313 ("FIXME - mapped properties not currently supported");
314
315 }
316
317
318
319
320
321 /***
322 * <p>Return <code>true</code> if the iteration has more elements.</p>
323 */
324 public boolean hasNext() {
325
326 try {
327 advance();
328 return (!eof);
329 } catch (SQLException e) {
330 throw new RuntimeException("hasNext(): SQLException: " + e);
331 }
332
333 }
334
335
336 /***
337 * <p>Return the next element in the iteration.</p>
338 */
339 public Object next() {
340
341 try {
342 advance();
343 if (eof) {
344 throw new NoSuchElementException();
345 }
346 current = false;
347 return (this);
348 } catch (SQLException e) {
349 throw new RuntimeException("next(): SQLException: " + e);
350 }
351
352 }
353
354
355 /***
356 * <p>Remove the current element from the iteration. This method is
357 * not supported.</p>
358 */
359 public void remove() {
360
361 throw new UnsupportedOperationException("remove()");
362
363 }
364
365
366
367
368
369 /***
370 * <p>Advance the result set to the next row, if there is not a current
371 * row (and if we are not already at eof).</p>
372 *
373 * @exception SQLException if the result set throws an exception
374 */
375 protected void advance() throws SQLException {
376
377 if (!current && !eof) {
378 if (dynaClass.getResultSet().next()) {
379 current = true;
380 eof = false;
381 } else {
382 current = false;
383 eof = true;
384 }
385 }
386
387 }
388
389
390 }