View Javadoc

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