1 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 35 public class DatabaseBrowserIterator implements BrowserIterator 36 { 37 38 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 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 82 public List getResultSet() 83 { 84 return rsList; 85 } 86 87 91 public int getResultSetSize() 92 { 93 return rsListSize; 94 } 95 96 100 public List getResultSetTitleList() 101 { 102 return rsTitleList; 103 } 104 109 public List getResultSetTypesList() 110 { 111 return rsTypeList; 112 } 113 114 118 public int getTop() 119 { 120 return top; 121 } 122 123 128 public void setTop(int start) 129 { 130 top = start; 131 index = top; 132 setBottom(); 133 } 134 135 139 public int getBottom() 140 { 141 return bottom; 142 } 143 144 148 public int getWindowSize() 149 { 150 return windowSize; 151 } 152 153 158 private void setBottom() 159 { 160 bottom = top + windowSize; 161 if( bottom > rsListSize ) 162 { 163 bottom = rsListSize; 164 } 165 } 166 167 170 public boolean hasNext() 171 { 172 if(index <= rsListSize && index < bottom) 173 { 174 return true; 175 } 176 return false; 177 } 178 179 182 public Object next() 183 { 184 index = index + 1; 185 return rsList.get(index - 1); 186 } 187 188 192 public void remove() 193 { 194 logger.info("The remove operation is not supported."); 195 } 196 197 204 public void sort(String columnName) 205 { 206 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 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 if(!ascendingOrder) 328 { 329 order = 0-order; 330 } 331 return order; 332 } 333 334 335 } 336 | Popular Tags |