KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > util > iterator > ResultSetIterator


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.util.iterator;
19
20 import java.sql.ResultSet JavaDoc;
21 import java.sql.ResultSetMetaData JavaDoc;
22 import java.sql.SQLException JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.NoSuchElementException JavaDoc;
25 import java.util.SortedMap JavaDoc;
26 import java.util.TreeMap JavaDoc;
27
28 import org.apache.beehive.netui.util.Bundle;
29 import org.apache.beehive.netui.util.logging.Logger;
30
31 /**
32  * <p>
33  * {@link Iterator} implementation for {@link ResultSet}.
34  * </p>
35  */

36 public class ResultSetIterator
37     implements Iterator JavaDoc {
38
39     private static final Logger LOGGER = Logger.getInstance(ResultSetIterator.class);
40
41     private boolean _primed = false;
42     private String JavaDoc[] _columnNames = null;
43     private ResultSet JavaDoc _rs = null;
44
45     /**
46      * Create a ResultSetIterator for the <code>resultSet</code>.
47      *
48      * @param resultSet the ResultSet to iterate over
49      * @throws IllegalStateException when a {@link SQLException} occurs manipulating the ResultSet
50      */

51     public ResultSetIterator(ResultSet JavaDoc resultSet) {
52         if(resultSet == null)
53             return;
54
55         _rs = resultSet;
56
57         try {
58             // handle RSMD here to build a template map that can contain the data for each row
59
ResultSetMetaData JavaDoc rsmd = _rs.getMetaData();
60             assert rsmd != null;
61
62             int cols = rsmd.getColumnCount();
63             _columnNames = new String JavaDoc[cols];
64             for(int i = 1; i <= cols; i++) {
65                 _columnNames[i - 1] = rsmd.getColumnName(i);
66                 LOGGER.trace("column[" + i + "]: " + _columnNames[i-1]);
67             }
68         } catch(SQLException JavaDoc sql) {
69             String JavaDoc msg = "An exception occurred reading ResultSetMetaData from a ResultSet. Cause: " + sql;
70             LOGGER.error(msg, sql);
71             throw new IllegalStateException JavaDoc(msg, sql);
72         }
73     }
74
75     /**
76      * Check for a subsequent item in the ResultSet.
77      *
78      * @return <code>true</code> if there is another element; <code>false</code> otherwise
79      * @throws IllegalStateException when a {@link SQLException} occurs advancing the ResultSet
80      */

81     public boolean hasNext() {
82         if(_rs == null)
83             return false;
84
85         if(_primed)
86             return true;
87
88         try {
89             _primed = _rs.next();
90             return _primed;
91         }
92         catch(SQLException JavaDoc sql) {
93             String JavaDoc msg = "An exception occurred reading from the Iterator. Cause: " + sql;
94             LOGGER.error(msg, sql);
95             throw new IllegalStateException JavaDoc(msg, sql);
96         }
97     }
98
99     /**
100      * Advance to the next row in the ResultSet.
101      *
102      * @return a {@link java.util.Map} containing the data in the next row. The keys in the map
103      * correspond to the ResultSet's column names and are case insensitive when checking a key.
104      * @throws NoSuchElementException if the ResultSet is null or the end of the ResultSet has been reached
105      */

106     public Object JavaDoc next() {
107         if(_rs == null)
108             throw new NoSuchElementException JavaDoc(Bundle.getErrorString("IteratorFactory_Iterator_noSuchElement"));
109
110         try {
111             if(!_primed) {
112                 _primed = _rs.next();
113                 if(!_primed) {
114                     throw new NoSuchElementException JavaDoc(Bundle.getErrorString("IteratorFactory_Iterator_noSuchElement"));
115                 }
116             }
117
118             _primed = false;
119             return convertRow(_rs, _columnNames);
120         }
121         catch(SQLException JavaDoc sql) {
122             String JavaDoc msg = "An exception occurred reading from the Iterator. Cause: " + sql;
123             LOGGER.error(msg, sql);
124             throw new IllegalStateException JavaDoc(msg, sql);
125         }
126     }
127
128     /**
129      * The remove operation is unsupported on the ResultSetIterator.
130      *
131      * @throws UnsupportedOperationException always
132      */

133     public void remove() {
134         throw new UnsupportedOperationException JavaDoc(Bundle.getErrorString("IteratorFactory_Iterator_removeUnsupported",
135                                                 new Object JavaDoc[]{this.getClass().getName()}));
136     }
137
138     private static final Object JavaDoc convertRow(final ResultSet JavaDoc resultSet, final String JavaDoc[] columnNames)
139         throws SQLException JavaDoc {
140         SortedMap JavaDoc map = new TreeMap JavaDoc(String.CASE_INSENSITIVE_ORDER);
141         for(int i = 0; i < columnNames.length; i++) {
142             Object JavaDoc value = resultSet.getObject(i+1);
143             if(resultSet.wasNull())
144                 value = null;
145             map.put(columnNames[i], value);
146         }
147         return map;
148     }
149 }
150
Popular Tags