1/*2 * Copyright 2000-2001,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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.portal.portlets.browser;
1819import org.apache.jetspeed.portal.portlets.browser.BrowserIterator;
20import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
21import org.apache.jetspeed.services.logging.JetspeedLogger;
2223import java.util.List;
24import java.util.Collections;
25import java.sql.Types;
2627/***28 * A class for iterating over the window. The window constitutes the selection29 * of rows being displayed to the user from the List storing all the ResultSet.30 *31 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>32 * @version $Id: DatabaseBrowserIterator.java,v 1.11 2004/02/23 03:26:43 jford Exp $33 *34*/35publicclassDatabaseBrowserIterator implements BrowserIterator36 {
3738/***39 * Static initialization of the logger for this class40 */41privatestaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(DatabaseBrowserIterator.class.getName());
4243privatestaticfinal String VELOCITY_NULL_ENTRY = "-";
4445int top = 0;
46int index = 0;
4748int bottom = -1;
49int windowSize = -1;
50int rsListSize = -1;
5152boolean ascendingOrder = true;
5354 String sortColumnName = null;
5556 List rsList;
57 List rsTitleList;
58 List rsTypeList;
5960/***61 * Constructor for the database browser iterator62 *63 * @param result The List containg all the rows from the resultSet.64 * @param columnTitles The List containg all the columnLabels from a resultSet.65 * @param pageSize The number of rows to be displayed in a window configured by the user.66 */67publicDatabaseBrowserIterator(List result, List columnTitles,
68 List columnTypes, int pageSize)
69 {
70this.rsList = result;
71this.rsTitleList= columnTitles;
72this.rsTypeList = columnTypes;
73this.windowSize = pageSize;
74this.rsListSize = result.size();
75 setBottom();
76 }
7778/***79 * This method returns the result set.80 *81 */82public List getResultSet()
83 {
84return rsList;
85 }
8687/***88 * This method returns the number of rows in the result set.89 *90 */91publicint getResultSetSize()
92 {
93return rsListSize;
94 }
9596/***97 * This method returns the List containg the column labels of the result set.98 *99 */100public List getResultSetTitleList()
101 {
102return rsTitleList;
103 }
104/***105 * This method returns the List containg the column type names of the result set.106 *107 *@see java.sql.Types108 */109public List getResultSetTypesList()
110 {
111return rsTypeList;
112 }
113114/***115 * This method returns the index of the row to which the cursor is pointing at.116 *117 */118publicint getTop()
119 {
120return top;
121 }
122123/***124 * This method points the cursor to the index provided.125 *126 * @param start Index to which cursor should point to127 */128publicvoid setTop(int start)
129 {
130 top = start;
131 index = top;
132 setBottom();
133 }
134135/***136 * This method returns the last index of the row in the window displayed.137 *138 */139publicint getBottom()
140 {
141return bottom;
142 }
143144/***145 * This method returns the window size.146 *147 */148publicint getWindowSize()
149 {
150return windowSize;
151 }
152153/***154 * This method sets the bottom based on which index the cursor points to and155 * the size of the result set.156 *157 */158privatevoid setBottom()
159 {
160 bottom = top + windowSize;
161if( bottom > rsListSize )
162 {
163 bottom = rsListSize;
164 }
165 }
166167/***168 * Returns true if the iteration has more elements169 */170publicboolean hasNext()
171 {
172if(index <= rsListSize && index < bottom)
173 {
174returntrue;
175 }
176return false;
177 }
178179/***180 * Returns the next element in the iteration181 */182public Object next()
183 {
184 index = index + 1;
185return rsList.get(index - 1);
186 }
187188/***189 * Logs as info - since remove operation is not supported190 * by this Iterator.191 */192publicvoid remove()
193 {
194 logger.info("The remove operation is not supported.");
195 }
196197/***198 * This method sorts the result set according to the value of the column as199 * specified by the parameter column name.200 * Changes the order of the result set vector.201 *202 *@param String sortColumnName203 */204publicvoid sort(String columnName)
205 {
206//System.out.println("current columnName="+columnName);207//System.out.println("old columnName="+sortColumnName);208if( columnName != null )
209 {
210if( sortColumnName != null && sortColumnName.equals(columnName) )
211 {
212 ascendingOrder = !ascendingOrder;
213 }
214else215 {
216 ascendingOrder= true;
217 sortColumnName = columnName;
218 }
219 Collections.sort( rsList, this);
220 }
221 }
222223/*224 * Compares its two arguments for order.225 *226 */227publicint compare(Object obj1, Object obj2)
228 {
229int idx = rsTitleList.indexOf(sortColumnName);
230int order = 0;
231232if( idx != -1 )
233 {
234 Object col1 = ((List)obj1).get(idx);
235 Object col2 = ((List)obj2).get(idx);
236237if( (col1).equals(VELOCITY_NULL_ENTRY) )
238 {
239if( (col2).equals(VELOCITY_NULL_ENTRY))
240 {
241 order = 0;
242 }
243else244 {
245 order = -1;
246 }
247 }
248elseif( (col2).equals(VELOCITY_NULL_ENTRY) )
249 {
250 order = 1;
251 }
252else253 {
254int type = Integer.parseInt((String)rsTypeList.get(idx));
255switch (type)
256 {
257258case Types.NUMERIC:
259 order = ( ((java.math.BigDecimal)col1).compareTo((java.math.BigDecimal)col2) );
260break;
261262case Types.DECIMAL:
263 order = ( ((java.math.BigDecimal)col1).compareTo((java.math.BigDecimal)col2) );
264break;
265266case Types.TINYINT:
267 order = ( ((Byte)col1).compareTo((Byte)col2) );
268break;
269270case Types.SMALLINT:
271 order = ( ((Short)col1).compareTo((Short)col2) );
272break;
273274case Types.INTEGER:
275 order = ( ((Integer)col1).compareTo((Integer)col2) );
276break;
277278case Types.BIGINT:
279 order = ( ((Long)col1).compareTo((Long)col2) );
280break;
281282case Types.REAL:
283 order = ( ((Float)col1).compareTo((Float)col2) );
284break;
285286case Types.FLOAT:
287 order = ( ((Double)col1).compareTo((Double)col2) );
288break;
289290case Types.DOUBLE:
291 order = ( ((Double)col1).compareTo((Double)col2) );
292break;
293294case Types.DATE:
295 order = ( ((java.sql.Date)col1).compareTo((java.sql.Date)col2) );
296break;
297298case Types.TIME:
299 order = ( ((java.sql.Time)col1).compareTo((java.sql.Time)col2) );
300break;
301302case Types.TIMESTAMP:
303 order = ( ((java.sql.Timestamp)col1).compareTo((java.sql.Timestamp)col2) );
304break;
305306case Types.CHAR:
307 order = ( ((String)col1).compareTo((String)col2) );
308break;
309310case Types.VARCHAR:
311 order = ( ((String)col1).compareTo((String)col2) );
312break;
313314case Types.LONGVARCHAR:
315 order = ( ((String)col1).compareTo((String)col2) );
316break;
317318default:
319 logger.info("DatabaseBrowserIterator.compare DataType mapping not found"+
320" in DatabaseBrowserIterator. "+
321"Hence cannot sort based on provided column.");
322break;
323 }
324 }
325 }
326//System.out.println("index of type= "+idx +", order= "+order+", ascending= "+ascendingOrder);327if(!ascendingOrder)
328 {
329 order = 0-order;
330 }
331return order;
332 }
333334335 }