1:
31: package ;
32:
33: import ;
34: import ;
35: import ;
36:
37: import ;
38: import ;
39:
40:
45: public class SQLReportData implements ReportData
46: {
47: private ResultSet resultSet;
48: private int rowCount;
49: private int columnCount;
50: private int cursor;
51: private String[] columnNames;
52: private boolean labelMapping;
53:
54: public SQLReportData(final ResultSet resultSet,
55: final boolean labelMapping)
56: throws SQLException, DataSourceException
57: {
58: if (resultSet == null)
59: {
60: throw new NullPointerException();
61: }
62: if (resultSet.getType() == ResultSet.TYPE_FORWARD_ONLY)
63: {
64: throw new IllegalArgumentException();
65: }
66: this.resultSet = resultSet;
67: this.labelMapping = labelMapping;
68:
69: if (resultSet.last())
70: {
71: rowCount = resultSet.getRow();
72: }
73: else
74: {
75: rowCount = 0;
76: }
77:
78: final ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
79: columnCount = resultSetMetaData.getColumnCount();
80: columnNames = new String[columnCount];
81: for (int i = 1; i <= columnCount; i++)
82: {
83: if (labelMapping)
84: {
85: columnNames[i - 1] = resultSetMetaData.getColumnLabel(i);
86: }
87: else
88: {
89: columnNames[i - 1] = resultSetMetaData.getColumnName(i);
90: }
91: }
92:
93: if (resultSet.first() == false)
94: {
95: throw new DataSourceException("Unable to reset the dataset.");
96: }
97: cursor = 0;
98: }
99:
100: public boolean isLabelMapping()
101: {
102: return labelMapping;
103: }
104:
105: public int getRowCount() throws DataSourceException
106: {
107: return rowCount;
108: }
109:
110:
118: public boolean isAdvanceable() throws DataSourceException
119: {
120: return cursor < rowCount;
121: }
122:
123: public int getColumnCount() throws DataSourceException
124: {
125: return columnCount;
126: }
127:
128: public boolean setCursorPosition(final int row) throws DataSourceException
129: {
130: if (row < 0)
131: {
132: throw new DataSourceException("Negative row number is not valid");
133: }
134: if (row > rowCount)
135: {
136: return false;
137:
138: }
139:
140: try
141: {
142: if (cursor == 0)
143: {
144: resultSet.beforeFirst();
145: return true;
146: }
147:
148: if (resultSet.absolute(row))
149: {
150: cursor = row;
151: return true;
152: }
153: return false;
154:
155: }
156: catch (SQLException e)
157: {
158: throw new DataSourceException("Failed to move the cursor: ", e);
159: }
160: }
161:
162: public boolean next() throws DataSourceException
163: {
164: try
165: {
166: if (resultSet.next())
167: {
168: cursor += 1;
169: return true;
170: }
171: else
172: {
173: return false;
174: }
175: }
176: catch (SQLException e)
177: {
178: throw new DataSourceException("Failed to move the cursor: ", e);
179: }
180: }
181:
182: public void close() throws DataSourceException
183: {
184: try
185: {
186: resultSet.close();
187: }
188: catch (SQLException e)
189: {
190: throw new DataSourceException("Failed to close the resultset: ", e);
191: }
192: }
193:
194: public String getColumnName(final int column) throws DataSourceException
195: {
196: return columnNames[column];
197: }
198:
199: public Object get(final int column) throws DataSourceException
200: {
201: if (isReadable() == false)
202: {
203: throw new DataSourceException("Cannot read from this datasource");
204: }
205:
206: try
207: {
208: final Object retval = resultSet.getObject(column + 1);
209: if (resultSet.wasNull())
210: {
211: return null;
212: }
213: return retval;
214: }
215: catch (SQLException e)
216: {
217: throw new DataSourceException("Failed to query data", e);
218: }
219: }
220:
221: public int getCursorPosition() throws DataSourceException
222: {
223: return cursor;
224: }
225:
226: public boolean isReadable() throws DataSourceException
227: {
228: return cursor > 0 && rowCount > 0;
229: }
230: }