KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > mlw > vlh > adapter > jdbc > dynabean > fix > ResultSetIterator


1 /*
2  * $Header: /cvsroot/valuelist/valuelist/src/java/net/mlw/vlh/adapter/jdbc/dynabean/fix/ResultSetIterator.java,v 1.1 2004/06/03 14:27:13 mlavwilson Exp $
3  * $Revision: 1.1 $
4  * $Date: 2004/06/03 14:27:13 $
5  *
6  * ====================================================================
7  *
8  * The Apache Software License, Version 1.1
9  *
10  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
11  * reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  * 1. Redistributions of source code must retain the above copyright
18  * notice, this list of conditions and the following disclaimer.
19  *
20  * 2. Redistributions in binary form must reproduce the above copyright
21  * notice, this list of conditions and the following disclaimer in
22  * the documentation and/or other materials provided with the
23  * distribution.
24  *
25  * 3. The end-user documentation included with the redistribution,
26  * if any, must include the following acknowledgement:
27  * "This product includes software developed by the
28  * Apache Software Foundation (http://www.apache.org/)."
29  * Alternately, this acknowledgement may appear in the software itself,
30  * if and wherever such third-party acknowledgements normally appear.
31  *
32  * 4. The names "Apache", "The Jakarta Project", "Commons", and "Apache Software
33  * Foundation" must not be used to endorse or promote products derived
34  * from this software without prior written permission. For written
35  * permission, please contact apache@apache.org.
36  *
37  * 5. Products derived from this software may not be called "Apache",
38  * "Apache" nor may "Apache" appear in their names without prior
39  * written permission of the Apache Software Foundation.
40  *
41  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  * ====================================================================
54  *
55  * This software consists of voluntary contributions made by many
56  * individuals on behalf of the Apache Software Foundation. For more
57  * information on the Apache Software Foundation, please see
58  * <http://www.apache.org/>.
59  *
60  */

61
62
63 package net.mlw.vlh.adapter.jdbc.dynabean.fix;
64
65
66 import java.sql.SQLException JavaDoc;
67 import java.util.Iterator JavaDoc;
68 import java.util.NoSuchElementException JavaDoc;
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 JavaDoc {
85
86
87     // ------------------------------------------------------------ Constructor
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     // ----------------------------------------------------- Instance Variables
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     // ------------------------------------------------------- DynaBean Methods
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 JavaDoc name, String JavaDoc key) {
142
143         throw new UnsupportedOperationException JavaDoc
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 JavaDoc get(String JavaDoc name) {
158
159         if (dynaClass.getDynaProperty(name) == null) {
160             throw new IllegalArgumentException JavaDoc(name);
161         }
162         try {
163             return (dynaClass.getResultSet().getObject(name));
164         } catch (SQLException JavaDoc 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 JavaDoc get(String JavaDoc name, int index) {
187
188         throw new UnsupportedOperationException JavaDoc
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 JavaDoc get(String JavaDoc name, String JavaDoc key) {
207
208         throw new UnsupportedOperationException JavaDoc
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 JavaDoc name, String JavaDoc key) {
237
238         throw new UnsupportedOperationException JavaDoc
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 JavaDoc name, Object JavaDoc value) {
258
259         if (dynaClass.getDynaProperty(name) == null) {
260             throw new IllegalArgumentException JavaDoc(name);
261         }
262         try {
263             dynaClass.getResultSet().updateObject(name, value);
264         } catch (SQLException JavaDoc e) {
265             throw new RuntimeException JavaDoc
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 JavaDoc name, int index, Object JavaDoc value) {
289
290         throw new UnsupportedOperationException JavaDoc
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 JavaDoc name, String JavaDoc key, Object JavaDoc value) {
311
312         throw new UnsupportedOperationException JavaDoc
313             ("FIXME - mapped properties not currently supported");
314
315     }
316
317
318     // ------------------------------------------------------- Iterator Methods
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 JavaDoc e) {
330             throw new RuntimeException JavaDoc("hasNext(): SQLException: " + e);
331         }
332
333     }
334
335
336     /**
337      * <p>Return the next element in the iteration.</p>
338      */

339     public Object JavaDoc next() {
340
341         try {
342             advance();
343             if (eof) {
344                 throw new NoSuchElementException JavaDoc();
345             }
346             current = false;
347             return (this);
348         } catch (SQLException JavaDoc e) {
349             throw new RuntimeException JavaDoc("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 JavaDoc("remove()");
362
363     }
364
365
366     // ------------------------------------------------------ Protected Methods
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 JavaDoc {
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 }
391
Popular Tags