1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.portals.gems.browser;
18
19 import java.util.List;
20 import java.util.Collections;
21 import java.sql.Types;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 /***
27 * A class for iterating over the window. The window constitutes the selection
28 * of rows being displayed to the user from the List storing all the ResultSet.
29 *
30 * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
31 * @version $Id: DatabaseBrowserIterator.java 517121 2007-03-12 07:45:49Z ate $
32 */
33 public class DatabaseBrowserIterator implements BrowserIterator
34 {
35 private static final long serialVersionUID = 1;
36
37 /***
38 * Static initialization of the logger for this class
39 */
40 transient protected Log log = LogFactory.getLog(DatabaseBrowserIterator.class);
41
42 private static final String VELOCITY_NULL_ENTRY = "-";
43
44 int top = 0;
45
46 int index = 0;
47
48 int bottom = -1;
49
50 int windowSize = -1;
51
52 int rsListSize = -1;
53
54 boolean ascendingOrder = true;
55
56 String sortColumnName = null;
57
58 List rsList;
59
60 List rsTitleList;
61
62 List rsTypeList;
63
64 /***
65 * Constructor for the database browser iterator
66 *
67 * @param result
68 * The List containg all the rows from the resultSet.
69 * @param columnTitles
70 * The List containg all the columnLabels from a resultSet.
71 * @param pageSize
72 * The number of rows to be displayed in a window configured by
73 * the user.
74 */
75 public DatabaseBrowserIterator(List result, List columnTitles,
76 List columnTypes, int pageSize)
77 {
78 this.rsList = result;
79 this.rsTitleList = columnTitles;
80 this.rsTypeList = columnTypes;
81 this.windowSize = pageSize;
82 this.rsListSize = result.size();
83 setBottom();
84 }
85
86 /***
87 * This method returns the result set.
88 *
89 */
90 public List getResultSet()
91 {
92 return rsList;
93 }
94
95 /***
96 * This method returns the number of rows in the result set.
97 *
98 */
99 public int getResultSetSize()
100 {
101 return rsListSize;
102 }
103
104 /***
105 * This method returns the List containg the column labels of the result
106 * set.
107 *
108 */
109 public List getResultSetTitleList()
110 {
111 return rsTitleList;
112 }
113
114 /***
115 * This method returns the List containg the column type names of the result
116 * set.
117 *
118 * @see java.sql.Types
119 */
120 public List getResultSetTypesList()
121 {
122 return rsTypeList;
123 }
124
125 /***
126 * This method returns the index of the row to which the cursor is pointing
127 * at.
128 *
129 */
130 public int getTop()
131 {
132 return top;
133 }
134
135 /***
136 * This method points the cursor to the index provided.
137 *
138 * @param start
139 * Index to which cursor should point to
140 */
141 public void setTop(int start)
142 {
143 top = start;
144 index = top;
145 setBottom();
146 }
147
148 /***
149 * This method returns the last index of the row in the window displayed.
150 *
151 */
152 public int getBottom()
153 {
154 return bottom;
155 }
156
157 /***
158 * This method returns the window size.
159 *
160 */
161 public int getWindowSize()
162 {
163 return windowSize;
164 }
165
166 /***
167 * This method sets the bottom based on which index the cursor points to and
168 * the size of the result set.
169 *
170 */
171 private void setBottom()
172 {
173 bottom = top + windowSize;
174 if (bottom > rsListSize)
175 {
176 bottom = rsListSize;
177 }
178 }
179
180 /***
181 * Returns true if the iteration has more elements
182 */
183 public boolean hasNext()
184 {
185 if (index <= rsListSize && index < bottom) { return true; }
186 return false;
187 }
188
189 /***
190 * Returns the next element in the iteration
191 */
192 public Object next()
193 {
194 index = index + 1;
195 return rsList.get(index - 1);
196 }
197
198 /***
199 * Logs as info - since remove operation is not supported by this Iterator.
200 */
201 public void remove()
202 {
203 log.info("The remove operation is not supported.");
204 }
205
206 /***
207 * This method sorts the result set according to the value of the column as
208 * specified by the parameter column name. Changes the order of the result
209 * set vector.
210 *
211 * @param String
212 * sortColumnName
213 */
214 public void sort(String columnName)
215 {
216
217
218 if (columnName != null)
219 {
220 if (sortColumnName != null && sortColumnName.equals(columnName))
221 {
222 ascendingOrder = !ascendingOrder;
223 } else
224 {
225 ascendingOrder = true;
226 sortColumnName = columnName;
227 }
228 Collections.sort(rsList, this);
229 }
230 }
231
232
233
234
235
236 public int compare(Object obj1, Object obj2)
237 {
238 int idx = rsTitleList.indexOf(sortColumnName);
239 int order = 0;
240
241 if (idx != -1)
242 {
243 Object col1 = null;
244 Object col2 = null;
245
246 if (obj1 instanceof String)
247 {
248 col1 = obj1;
249 col2 = obj2;
250 }
251 else if (obj1 instanceof List)
252 {
253 col1 = ((List) obj1).get(idx);
254 col2 = ((List) obj2).get(idx);
255 }
256
257 if ((col1).equals(VELOCITY_NULL_ENTRY))
258 {
259 if ((col2).equals(VELOCITY_NULL_ENTRY))
260 {
261 order = 0;
262 } else
263 {
264 order = -1;
265 }
266 } else if ((col2).equals(VELOCITY_NULL_ENTRY))
267 {
268 order = 1;
269 } else
270 {
271 int type = Integer.parseInt((String) rsTypeList.get(idx));
272 switch (type)
273 {
274
275 case Types.NUMERIC:
276 order = (((java.math.BigDecimal) col1)
277 .compareTo((java.math.BigDecimal) col2));
278 break;
279
280 case Types.DECIMAL:
281 order = (((java.math.BigDecimal) col1)
282 .compareTo((java.math.BigDecimal) col2));
283 break;
284
285 case Types.TINYINT:
286 order = (((Byte) col1).compareTo((Byte) col2));
287 break;
288
289 case Types.SMALLINT:
290 order = (((Short) col1).compareTo((Short) col2));
291 break;
292
293 case Types.INTEGER:
294 order = (((Integer) col1).compareTo((Integer) col2));
295 break;
296
297 case Types.BIGINT:
298 order = (((Long) col1).compareTo((Long) col2));
299 break;
300
301 case Types.REAL:
302 order = (((Float) col1).compareTo((Float) col2));
303 break;
304
305 case Types.FLOAT:
306 order = (((Double) col1).compareTo((Double) col2));
307 break;
308
309 case Types.DOUBLE:
310 order = (((Double) col1).compareTo((Double) col2));
311 break;
312
313 case Types.DATE:
314 order = (((java.sql.Date) col1)
315 .compareTo((java.sql.Date) col2));
316 break;
317
318 case Types.TIME:
319 order = (((java.sql.Time) col1)
320 .compareTo((java.sql.Time) col2));
321 break;
322
323 case Types.TIMESTAMP:
324 order = (((java.sql.Timestamp) col1)
325 .compareTo((java.sql.Timestamp) col2));
326 break;
327
328 case Types.CHAR:
329 order = (((String) col1).compareTo((String) col2));
330 break;
331
332 case Types.VARCHAR:
333 order = (((String) col1).compareTo((String) col2));
334 break;
335
336 case Types.LONGVARCHAR:
337 order = (((String) col1).compareTo((String) col2));
338 break;
339
340 default:
341 log
342 .info("DatabaseBrowserIterator.compare DataType mapping not found"
343 + " in DatabaseBrowserIterator. "
344 + "Hence cannot sort based on provided column.");
345 break;
346 }
347 }
348 }
349
350
351 if (!ascendingOrder)
352 {
353 order = 0 - order;
354 }
355 return order;
356 }
357
358 public boolean getAscendingOrder()
359 {
360 return ascendingOrder;
361 }
362
363 }