001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.beanutils;
019:
020: import java.sql.SQLException;
021: import java.util.Iterator;
022: import java.util.NoSuchElementException;
023:
024: /**
025: * <p>Implementation of <code>java.util.Iterator</code> returned by the
026: * <code>iterator()</code> method of {@link ResultSetDynaClass}. Each
027: * object returned by this iterator will be a {@link DynaBean} that
028: * represents a single row from the result set being wrapped.</p>
029: *
030: * @author Craig R. McClanahan
031: * @version $Revision: 556221 $ $Date: 2007-07-14 05:19:21 +0100 (Sat, 14 Jul 2007) $
032: */
033:
034: public class ResultSetIterator implements DynaBean, Iterator {
035:
036: // ------------------------------------------------------------ Constructor
037:
038: /**
039: * <p>Construct an <code>Iterator</code> for the result set being wrapped
040: * by the specified {@link ResultSetDynaClass}.</p>
041: *
042: * @param dynaClass The {@link ResultSetDynaClass} wrapping the
043: * result set we will iterate over
044: */
045: ResultSetIterator(ResultSetDynaClass dynaClass) {
046:
047: this .dynaClass = dynaClass;
048:
049: }
050:
051: // ----------------------------------------------------- Instance Variables
052:
053: /**
054: * <p>Flag indicating whether the result set is currently positioned at a
055: * row for which we have not yet returned an element in the iteration.</p>
056: */
057: protected boolean current = false;
058:
059: /**
060: * <p>The {@link ResultSetDynaClass} we are associated with.</p>
061: */
062: protected ResultSetDynaClass dynaClass = null;
063:
064: /**
065: * <p>Flag indicating whether the result set has indicated that there are
066: * no further rows.</p>
067: */
068: protected boolean eof = false;
069:
070: // ------------------------------------------------------- DynaBean Methods
071:
072: /**
073: * Does the specified mapped property contain a value for the specified
074: * key value?
075: *
076: * @param name Name of the property to check
077: * @param key Name of the key to check
078: * @return <code>true<code> if the mapped property contains a value for
079: * the specified key, otherwise <code>false</code>
080: *
081: * @exception IllegalArgumentException if there is no property
082: * of the specified name
083: */
084: public boolean contains(String name, String key) {
085:
086: throw new UnsupportedOperationException(
087: "FIXME - mapped properties not currently supported");
088:
089: }
090:
091: /**
092: * Return the value of a simple property with the specified name.
093: *
094: * @param name Name of the property whose value is to be retrieved
095: * @return The property's value
096: *
097: * @exception IllegalArgumentException if there is no property
098: * of the specified name
099: */
100: public Object get(String name) {
101:
102: if (dynaClass.getDynaProperty(name) == null) {
103: throw new IllegalArgumentException(name);
104: }
105: try {
106: return dynaClass.getObjectFromResultSet(name);
107: } catch (SQLException e) {
108: throw new RuntimeException("get(" + name
109: + "): SQLException: " + e);
110: }
111:
112: }
113:
114: /**
115: * Return the value of an indexed property with the specified name.
116: *
117: * @param name Name of the property whose value is to be retrieved
118: * @param index Index of the value to be retrieved
119: * @return The indexed property's value
120: *
121: * @exception IllegalArgumentException if there is no property
122: * of the specified name
123: * @exception IllegalArgumentException if the specified property
124: * exists, but is not indexed
125: * @exception IndexOutOfBoundsException if the specified index
126: * is outside the range of the underlying property
127: * @exception NullPointerException if no array or List has been
128: * initialized for this property
129: */
130: public Object get(String name, int index) {
131:
132: throw new UnsupportedOperationException(
133: "FIXME - indexed properties not currently supported");
134:
135: }
136:
137: /**
138: * Return the value of a mapped property with the specified name,
139: * or <code>null</code> if there is no value for the specified key.
140: *
141: * @param name Name of the property whose value is to be retrieved
142: * @param key Key of the value to be retrieved
143: * @return The mapped property's value
144: *
145: * @exception IllegalArgumentException if there is no property
146: * of the specified name
147: * @exception IllegalArgumentException if the specified property
148: * exists, but is not mapped
149: */
150: public Object get(String name, String key) {
151:
152: throw new UnsupportedOperationException(
153: "FIXME - mapped properties not currently supported");
154:
155: }
156:
157: /**
158: * Return the <code>DynaClass</code> instance that describes the set of
159: * properties available for this DynaBean.
160: *
161: * @return The associated DynaClass
162: */
163: public DynaClass getDynaClass() {
164:
165: return (this .dynaClass);
166:
167: }
168:
169: /**
170: * Remove any existing value for the specified key on the
171: * specified mapped property.
172: *
173: * @param name Name of the property for which a value is to
174: * be removed
175: * @param key Key of the value to be removed
176: *
177: * @exception IllegalArgumentException if there is no property
178: * of the specified name
179: */
180: public void remove(String name, String key) {
181:
182: throw new UnsupportedOperationException(
183: "FIXME - mapped operations not currently supported");
184:
185: }
186:
187: /**
188: * Set the value of a simple property with the specified name.
189: *
190: * @param name Name of the property whose value is to be set
191: * @param value Value to which this property is to be set
192: *
193: * @exception ConversionException if the specified value cannot be
194: * converted to the type required for this property
195: * @exception IllegalArgumentException if there is no property
196: * of the specified name
197: * @exception NullPointerException if an attempt is made to set a
198: * primitive property to null
199: */
200: public void set(String name, Object value) {
201:
202: if (dynaClass.getDynaProperty(name) == null) {
203: throw new IllegalArgumentException(name);
204: }
205: try {
206: dynaClass.getResultSet().updateObject(name, value);
207: } catch (SQLException e) {
208: throw new RuntimeException("set(" + name
209: + "): SQLException: " + e);
210: }
211:
212: }
213:
214: /**
215: * Set the value of an indexed property with the specified name.
216: *
217: * @param name Name of the property whose value is to be set
218: * @param index Index of the property to be set
219: * @param value Value to which this property is to be set
220: *
221: * @exception ConversionException if the specified value cannot be
222: * converted to the type required for this property
223: * @exception IllegalArgumentException if there is no property
224: * of the specified name
225: * @exception IllegalArgumentException if the specified property
226: * exists, but is not indexed
227: * @exception IndexOutOfBoundsException if the specified index
228: * is outside the range of the underlying property
229: */
230: public void set(String name, int index, Object value) {
231:
232: throw new UnsupportedOperationException(
233: "FIXME - indexed properties not currently supported");
234:
235: }
236:
237: /**
238: * Set the value of a mapped property with the specified name.
239: *
240: * @param name Name of the property whose value is to be set
241: * @param key Key of the property to be set
242: * @param value Value to which this property is to be set
243: *
244: * @exception ConversionException if the specified value cannot be
245: * converted to the type required for this property
246: * @exception IllegalArgumentException if there is no property
247: * of the specified name
248: * @exception IllegalArgumentException if the specified property
249: * exists, but is not mapped
250: */
251: public void set(String name, String key, Object value) {
252:
253: throw new UnsupportedOperationException(
254: "FIXME - mapped properties not currently supported");
255:
256: }
257:
258: // ------------------------------------------------------- Iterator Methods
259:
260: /**
261: * <p>Return <code>true</code> if the iteration has more elements.</p>
262: *
263: * @return <code>true</code> if the result set has another
264: * row, otherwise <code>false</code>
265: */
266: public boolean hasNext() {
267:
268: try {
269: advance();
270: return (!eof);
271: } catch (SQLException e) {
272: throw new RuntimeException("hasNext(): SQLException: "
273: + e);
274: }
275:
276: }
277:
278: /**
279: * <p>Return the next element in the iteration.</p>
280: *
281: * @return advance to the new row and return this
282: */
283: public Object next() {
284:
285: try {
286: advance();
287: if (eof) {
288: throw new NoSuchElementException();
289: }
290: current = false;
291: return (this );
292: } catch (SQLException e) {
293: throw new RuntimeException("next(): SQLException: " + e);
294: }
295:
296: }
297:
298: /**
299: * <p>Remove the current element from the iteration. This method is
300: * not supported.</p>
301: */
302: public void remove() {
303:
304: throw new UnsupportedOperationException("remove()");
305:
306: }
307:
308: // ------------------------------------------------------ Protected Methods
309:
310: /**
311: * <p>Advance the result set to the next row, if there is not a current
312: * row (and if we are not already at eof).</p>
313: *
314: * @exception SQLException if the result set throws an exception
315: */
316: protected void advance() throws SQLException {
317:
318: if (!current && !eof) {
319: if (dynaClass.getResultSet().next()) {
320: current = true;
321: eof = false;
322: } else {
323: current = false;
324: eof = true;
325: }
326: }
327:
328: }
329:
330: }
|