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: package org.apache.portals.gems.browser;
018:
019: import java.util.List;
020: import java.util.Collections;
021: import java.sql.Types;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025:
026: /**
027: * A class for iterating over the window. The window constitutes the selection
028: * of rows being displayed to the user from the List storing all the ResultSet.
029: *
030: * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
031: * @version $Id: DatabaseBrowserIterator.java 517121 2007-03-12 07:45:49Z ate $
032: */
033: public class DatabaseBrowserIterator implements BrowserIterator {
034: private static final long serialVersionUID = 1;
035:
036: /**
037: * Static initialization of the logger for this class
038: */
039: transient protected Log log = LogFactory
040: .getLog(DatabaseBrowserIterator.class);
041:
042: private static final String VELOCITY_NULL_ENTRY = "-";
043:
044: int top = 0;
045:
046: int index = 0;
047:
048: int bottom = -1;
049:
050: int windowSize = -1;
051:
052: int rsListSize = -1;
053:
054: boolean ascendingOrder = true;
055:
056: String sortColumnName = null;
057:
058: List rsList;
059:
060: List rsTitleList;
061:
062: List rsTypeList;
063:
064: /**
065: * Constructor for the database browser iterator
066: *
067: * @param result
068: * The List containg all the rows from the resultSet.
069: * @param columnTitles
070: * The List containg all the columnLabels from a resultSet.
071: * @param pageSize
072: * The number of rows to be displayed in a window configured by
073: * the user.
074: */
075: public DatabaseBrowserIterator(List result, List columnTitles,
076: List columnTypes, int pageSize) {
077: this .rsList = result;
078: this .rsTitleList = columnTitles;
079: this .rsTypeList = columnTypes;
080: this .windowSize = pageSize;
081: this .rsListSize = result.size();
082: setBottom();
083: }
084:
085: /**
086: * This method returns the result set.
087: *
088: */
089: public List getResultSet() {
090: return rsList;
091: }
092:
093: /**
094: * This method returns the number of rows in the result set.
095: *
096: */
097: public int getResultSetSize() {
098: return rsListSize;
099: }
100:
101: /**
102: * This method returns the List containg the column labels of the result
103: * set.
104: *
105: */
106: public List getResultSetTitleList() {
107: return rsTitleList;
108: }
109:
110: /**
111: * This method returns the List containg the column type names of the result
112: * set.
113: *
114: * @see java.sql.Types
115: */
116: public List getResultSetTypesList() {
117: return rsTypeList;
118: }
119:
120: /**
121: * This method returns the index of the row to which the cursor is pointing
122: * at.
123: *
124: */
125: public int getTop() {
126: return top;
127: }
128:
129: /**
130: * This method points the cursor to the index provided.
131: *
132: * @param start
133: * Index to which cursor should point to
134: */
135: public void setTop(int start) {
136: top = start;
137: index = top;
138: setBottom();
139: }
140:
141: /**
142: * This method returns the last index of the row in the window displayed.
143: *
144: */
145: public int getBottom() {
146: return bottom;
147: }
148:
149: /**
150: * This method returns the window size.
151: *
152: */
153: public int getWindowSize() {
154: return windowSize;
155: }
156:
157: /**
158: * This method sets the bottom based on which index the cursor points to and
159: * the size of the result set.
160: *
161: */
162: private void setBottom() {
163: bottom = top + windowSize;
164: if (bottom > rsListSize) {
165: bottom = rsListSize;
166: }
167: }
168:
169: /**
170: * Returns true if the iteration has more elements
171: */
172: public boolean hasNext() {
173: if (index <= rsListSize && index < bottom) {
174: return true;
175: }
176: return false;
177: }
178:
179: /**
180: * Returns the next element in the iteration
181: */
182: public Object next() {
183: index = index + 1;
184: return rsList.get(index - 1);
185: }
186:
187: /**
188: * Logs as info - since remove operation is not supported by this Iterator.
189: */
190: public void remove() {
191: log.info("The remove operation is not supported.");
192: }
193:
194: /**
195: * This method sorts the result set according to the value of the column as
196: * specified by the parameter column name. Changes the order of the result
197: * set vector.
198: *
199: * @param String
200: * sortColumnName
201: */
202: public void sort(String columnName) {
203: //System.out.println("current columnName="+columnName);
204: //System.out.println("old columnName="+sortColumnName);
205: if (columnName != null) {
206: if (sortColumnName != null
207: && sortColumnName.equals(columnName)) {
208: ascendingOrder = !ascendingOrder;
209: } else {
210: ascendingOrder = true;
211: sortColumnName = columnName;
212: }
213: Collections.sort(rsList, this );
214: }
215: }
216:
217: /*
218: * Compares its two arguments for order.
219: *
220: */
221: public int compare(Object obj1, Object obj2) {
222: int idx = rsTitleList.indexOf(sortColumnName);
223: int order = 0;
224:
225: if (idx != -1) {
226: Object col1 = null;
227: Object col2 = null;
228:
229: if (obj1 instanceof String) {
230: col1 = obj1;
231: col2 = obj2;
232: } else if (obj1 instanceof List) {
233: col1 = ((List) obj1).get(idx);
234: col2 = ((List) obj2).get(idx);
235: }
236:
237: if ((col1).equals(VELOCITY_NULL_ENTRY)) {
238: if ((col2).equals(VELOCITY_NULL_ENTRY)) {
239: order = 0;
240: } else {
241: order = -1;
242: }
243: } else if ((col2).equals(VELOCITY_NULL_ENTRY)) {
244: order = 1;
245: } else {
246: int type = Integer.parseInt((String) rsTypeList
247: .get(idx));
248: switch (type) {
249:
250: case Types.NUMERIC:
251: order = (((java.math.BigDecimal) col1)
252: .compareTo((java.math.BigDecimal) col2));
253: break;
254:
255: case Types.DECIMAL:
256: order = (((java.math.BigDecimal) col1)
257: .compareTo((java.math.BigDecimal) col2));
258: break;
259:
260: case Types.TINYINT:
261: order = (((Byte) col1).compareTo((Byte) col2));
262: break;
263:
264: case Types.SMALLINT:
265: order = (((Short) col1).compareTo((Short) col2));
266: break;
267:
268: case Types.INTEGER:
269: order = (((Integer) col1).compareTo((Integer) col2));
270: break;
271:
272: case Types.BIGINT:
273: order = (((Long) col1).compareTo((Long) col2));
274: break;
275:
276: case Types.REAL:
277: order = (((Float) col1).compareTo((Float) col2));
278: break;
279:
280: case Types.FLOAT:
281: order = (((Double) col1).compareTo((Double) col2));
282: break;
283:
284: case Types.DOUBLE:
285: order = (((Double) col1).compareTo((Double) col2));
286: break;
287:
288: case Types.DATE:
289: order = (((java.sql.Date) col1)
290: .compareTo((java.sql.Date) col2));
291: break;
292:
293: case Types.TIME:
294: order = (((java.sql.Time) col1)
295: .compareTo((java.sql.Time) col2));
296: break;
297:
298: case Types.TIMESTAMP:
299: order = (((java.sql.Timestamp) col1)
300: .compareTo((java.sql.Timestamp) col2));
301: break;
302:
303: case Types.CHAR:
304: order = (((String) col1).compareTo((String) col2));
305: break;
306:
307: case Types.VARCHAR:
308: order = (((String) col1).compareTo((String) col2));
309: break;
310:
311: case Types.LONGVARCHAR:
312: order = (((String) col1).compareTo((String) col2));
313: break;
314:
315: default:
316: log
317: .info("DatabaseBrowserIterator.compare DataType mapping not found"
318: + " in DatabaseBrowserIterator. "
319: + "Hence cannot sort based on provided column.");
320: break;
321: }
322: }
323: }
324: //System.out.println("index of type= "+idx +", order= "+order+",
325: // ascending= "+ascendingOrder);
326: if (!ascendingOrder) {
327: order = 0 - order;
328: }
329: return order;
330: }
331:
332: public boolean getAscendingOrder() {
333: return ascendingOrder;
334: }
335:
336: }
|