1 /***
2 * The contents of this file are subject to the Mozilla Public
3 * License Version 1.1 (the "License"); you may not use this file
4 * except in compliance with the License. You may obtain a copy of
5 * the License at http://www.mozilla.org/MPL/
6 *
7 * Software distributed under the License is distributed on an "AS
8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9 * implied. See the License for the specific language governing
10 * rights and limitations under the License.
11 *
12 * The Original Code is pow2toolkit library.
13 *
14 * The Initial Owner of the Original Code is
15 * Claudio Fratarcangeli
16 *
17 * Portions created by Power Of Two S.R.L. are
18 * Copyright (C) Power Of Two S.R.L.
19 * All Rights Reserved.
20 *
21 * Contributor(s):
22 */
23
24 package com.pow2.dao.datalist;
25
26 import java.sql.Connection;
27 import java.sql.Statement;
28 import java.sql.ResultSet;
29 import java.sql.SQLException;
30
31 import com.pow2.dao.AbstractDAO;
32
33
34
35 /***
36 * ResultSetDataListImpl class
37 *
38 * @author Claudio Fratarcangeli
39 * @created 5 dicembre 2002
40 */
41 public class ResultSetDataListImpl extends AbstractDAO implements ResultSetDataList
42 {
43 private ResultSetRowMapper rowMapper = null;
44 private Connection conn = null;
45 private Statement stmt = null;
46 private ResultSet rs = null;
47
48
49 /***
50 * Constructor
51 *
52 * @param rowMapper the rowMapper instance
53 * @param conn the connection object
54 * @param stmt the statement object
55 * @param rs the resultSet object
56 */
57 public ResultSetDataListImpl(ResultSetRowMapper rowMapper,
58 Connection conn,
59 Statement stmt,
60 ResultSet rs)
61 {
62 this.rowMapper = rowMapper;
63 this.conn = conn;
64 this.stmt = stmt;
65 this.rs = rs;
66 }
67
68
69 /***
70 * Get the iterator object for this data list.
71 *
72 * @return the iterator object for this data list
73 * @exception Exception if any error occurs
74 */
75 public DataListIterator iterator() throws Exception
76 {
77 return new ResultSetDataListIterator(this);
78 }
79
80
81 /***
82 * Put object state in item and return it.
83 * <br>
84 * Use rowMapper to map fields of ResultSet to instance variables of item.
85 * ResultSet index ranges from 1, while ResultSetDataList index
86 * ranges from 0.
87 *
88 * @param index the index value, starting from 0
89 * @param item the item object to fill
90 * @return the item object filled with values retrieved from the resultSet
91 * @exception Exception if any error occurs
92 */
93 public Object get(int index, Object item) throws Exception
94 {
95 try
96 {
97
98 rs.absolute(index);
99
100
101
102 if (rs.getRow() != index)
103 {
104 cat.error("::get - the row value is not equal to index [row: " + rs.getRow() + "; index: " + index + "]");
105 throw new IndexOutOfBoundsException();
106 }
107
108
109 item = rowMapper.mapRow(rs, item);
110 }
111 catch (SQLException e)
112 {
113 cat.error("::get - a SQL exception occurs at recordSet index [" + index + "]", e);
114 throw e;
115 }
116
117 return item;
118 }
119
120
121 /***
122 * Check if the resultSet data list has got another record.
123 *
124 * @return true if the resultSet data list has got another record,
125 * false otherwise
126 * @exception Exception if any error occurs
127 */
128 public boolean hasNext() throws Exception
129 {
130
131
132 return (!rs.isLast() && ((rs.getRow() != 0) || rs.isBeforeFirst()));
133 }
134
135
136 /***
137 * Move the resultSetDataList cursor before the first row of the list.
138 *
139 * @exception Exception if any error occurs
140 */
141 public void beforeFirst() throws Exception
142 {
143 rs.beforeFirst();
144 }
145
146
147 /***
148 * Move the ResultSetDataList cursor to a specific row number.
149 * Rows are numbered starting from 0.
150 *
151 * @param index the cursor index value, starting from 0
152 * @exception Exception if any error occurs
153 */
154 public boolean absolute(int index) throws Exception
155 {
156 return rs.absolute(index + 1);
157 }
158
159
160 /***
161 * Check if the ResultSetDataList is empty.
162 *
163 * @return The empty value
164 * @exception Exception if any error occurs
165 */
166 public boolean isEmpty() throws Exception
167 {
168 boolean isEmpty = true;
169
170 try
171 {
172 isEmpty = (!rs.isBeforeFirst() &&
173 !rs.isAfterLast() &&
174 rs.getRow() == 0);
175 }
176 catch (SQLException e)
177 {
178 throw e;
179 }
180
181 return isEmpty;
182 }
183
184
185 /***
186 * Check if any element exist at the <code>index</code> poition.
187 * <br>
188 * Preserve current location of cursor, find out if element exists and then
189 * put cursor back where it was to begin with.
190 *
191 * @param index the index value, starting from 0
192 * @return true if any element exist at the <code>index</code> poition,
193 * false otherwise
194 * @exception Exception if any error occurs
195 */
196 public boolean elementExists(int index) throws Exception
197 {
198 boolean beforeFirst = rs.isBeforeFirst();
199 boolean afterLast = rs.isAfterLast();
200 int currIndex = rs.getRow();
201 boolean exists = rs.absolute(index + 1);
202
203
204 if (beforeFirst)
205 rs.beforeFirst();
206
207 else if (afterLast)
208 rs.afterLast();
209
210 else if (currIndex != 0)
211 rs.absolute(currIndex);
212
213 return exists;
214 }
215
216
217 /***
218 * Close the data list resultSet, statement, and connection
219 * resources.
220 *
221 * @exception Exception if any error occurs
222 */
223 public void close() throws Exception
224 {
225 closeResources(rs, stmt, conn, true);
226
227
228
229
230 }
231 }