1 23 24 package org.dbforms.config; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import org.dbforms.util.IEscaper; 30 import org.dbforms.util.Util; 31 32 import java.sql.ResultSet ; 33 import java.sql.ResultSetMetaData ; 34 import java.sql.SQLException ; 35 36 import java.util.HashMap ; 37 import java.util.Hashtable ; 38 import java.util.Map ; 39 import java.util.Vector ; 40 41 64 public class ResultSetVector implements java.io.Serializable { 65 private static Log logCat = LogFactory.getLog(ResultSetVector.class 66 .getName()); 67 68 private Hashtable selectFieldsHashtable; 69 70 private Map attributes = new HashMap (); 71 72 private Table table = null; 73 74 private Vector objectVector = new Vector (); 75 76 private Vector selectFields = new Vector (); 77 78 private boolean firstPage = false; 79 80 private boolean lastPage = false; 81 82 private int pointer = 0; 84 85 88 public ResultSetVector() { 89 } 90 91 97 public ResultSetVector(Table table) { 98 this.table = table; 99 100 if (table != null) { 101 setupSelectFieldsHashtable(table.getFields()); 102 setupSelectFieldsHashtable(table.getCalcFields()); 103 } 104 } 105 106 114 public ResultSetVector(Table table, Vector selectedFields) { 115 this.table = table; 116 setupSelectFieldsHashtable(selectedFields); 117 } 118 119 127 public static final boolean isNull(ResultSetVector rsv) { 128 return ((rsv == null) || (rsv.size() == 0)); 129 } 130 131 139 public void setAttribute(String key, Object value) { 140 attributes.put(key, value); 141 } 142 143 151 public Object getAttribute(String key) { 152 return attributes.get(key); 153 } 154 155 160 public Map getAttributes() { 161 return attributes; 162 } 163 164 public String getTableName() { 165 String result = null; 166 if (table != null) 167 result = table.getName(); 168 return result; 169 } 170 171 176 public String [] getCurrentRow() { 177 Object [] obj = getCurrentRowAsObjects(); 178 String [] res = null; 179 180 if (obj != null) { 181 res = new String [obj.length]; 182 183 for (int i = 0; i < obj.length; i++) { 184 res[i] = getField(i); 185 } 186 } 187 188 return res; 189 } 190 191 199 public FieldValues getCurrentRowAsFieldValues() { 200 if (selectFields == null) { 201 throw new IllegalArgumentException ( 202 "no field vector was provided to this result"); 203 } 204 205 String [] rowData = getCurrentRow(); 206 207 if (rowData == null) { 208 return null; 209 } 210 211 FieldValues fvHT = new FieldValues(); 212 213 for (int i = 0; i < selectFields.size(); i++) { 214 Field f = (Field) selectFields.elementAt(i); 215 FieldValue fv = new FieldValue(f, rowData[i]); 216 fvHT.put(fv); 217 } 218 219 return fvHT; 220 } 221 222 230 public Map getCurrentRowAsMap() { 231 if (selectFields == null) { 232 throw new IllegalArgumentException ( 233 "no field vector was provided to this result"); 234 } 235 236 String [] rowData = getCurrentRow(); 237 238 if (rowData == null) { 239 return null; 240 } 241 242 Hashtable ht = new Hashtable (); 243 244 for (int i = 0; i < selectFields.size(); i++) { 245 Field f = (Field) selectFields.elementAt(i); 246 ht.put(f.getName(), rowData[i]); 247 } 248 249 return ht; 250 } 251 252 257 public Object [] getCurrentRowAsObjects() { 258 Object [] res = null; 259 if (isPointerLegal(pointer)) { 260 res = (Object []) objectVector.elementAt(pointer); 261 } 262 return res; 263 } 264 265 273 public String getField(int i) { 274 Object obj = getFieldAsObject(i); 275 276 return (obj != null) ? obj.toString() : ""; 277 } 278 279 287 public String getField(String fieldName) { 288 int fieldIndex = getFieldIndex(fieldName); 289 290 if (fieldIndex < 0) { 291 return null; 292 } 293 294 return getField(fieldIndex); 295 } 296 297 305 public Object getFieldAsObject(int i) { 306 return getFieldAsObject(pointer, i); 307 } 308 309 319 public Object getFieldAsObject(int row, int i) { 320 Object res = null; 321 if (isPointerLegal(row)) { 322 try { 323 res = ((Object []) objectVector.elementAt(row))[i]; 324 } catch (Exception e) { 325 logCat.error("getFieldAsObject", e); 326 } 327 } 328 return res; 329 } 330 331 341 public void setFieldAsObject(int row, int i, Object obj) { 342 if (isPointerLegal(row)) { 343 try { 344 Object [] objRow = (Object []) objectVector.elementAt(row); 345 objRow[i] = obj; 346 } catch (Exception e) { 347 } 348 } 349 } 350 351 359 public Object getFieldAsObject(String fieldName) { 360 return getFieldAsObject(pointer, fieldName); 361 } 362 363 371 public Object getFieldAsObject(int row, String fieldName) { 372 int fieldIndex = getFieldIndex(fieldName); 373 374 if (fieldIndex < 0) { 375 return null; 376 } 377 378 return getFieldAsObject(row, fieldIndex); 379 } 380 381 391 public void setFieldAsObject(int row, String fieldName, Object obj) { 392 int fieldIndex = getFieldIndex(fieldName); 393 if (fieldIndex >= 0) { 394 setFieldAsObject(row, fieldIndex, obj); 395 } 396 } 397 398 406 public Field getFieldDescription(String fieldName) { 407 return (Field) selectFieldsHashtable.get(fieldName); 408 } 409 410 418 public int getFieldIndex(String fieldName) { 419 int res = -1; 420 421 if (!Util.isNull(fieldName)) { 422 Field f = (Field) selectFieldsHashtable.get(fieldName); 423 424 if (f != null) { 425 res = selectFields.indexOf(f); 426 } 427 } 428 429 return res; 430 } 431 432 437 public boolean isFirst() { 438 return (pointer == 0); 439 } 440 441 447 public void setFirstPage(boolean b) { 448 firstPage = b; 449 } 450 451 456 public boolean isFirstPage() { 457 return firstPage; 458 } 459 460 465 public boolean isLast() { 466 return (pointer == (size() - 1)); 467 } 468 469 475 public void setLastPage(boolean b) { 476 lastPage = b; 477 } 478 479 484 public boolean isLastPage() { 485 return lastPage; 486 } 487 488 499 public void addResultSet(DbEventInterceptorData interceptorData, 500 ResultSet rs) throws SQLException { 501 ResultSetMetaData rsmd = rs.getMetaData(); 502 int columns = rsmd.getColumnCount(); 503 IEscaper escaper = null; 504 505 try { 507 while (rs.next()) { 508 Object [] objectRow = new Object [columns]; 509 510 for (int i = 0; i < columns; i++) { 511 if ((selectFields != null) && (i < selectFields.size())) { 512 Field curField = (Field) selectFields.elementAt(i); 513 514 if (curField != null) { 515 escaper = curField.getEscaper(); 516 } 517 } 518 519 if (table != null) { 520 escaper = (escaper == null) ? escaper : table 521 .getEscaper(); 522 } 523 524 if (escaper == null) { 525 try { 526 escaper = DbFormsConfigRegistry.instance().lookup() 527 .getEscaper(); 528 } catch (Exception e) { 529 logCat.error( 530 "cannot create the new default escaper", e); 531 } 532 } 533 534 objectRow[i] = JDBCDataHelper.getData(rs, escaper, i + 1); 535 } 536 537 addRow(interceptorData, objectRow); 538 } 539 } finally { 540 rs.close(); 541 } 542 } 543 544 552 public void addRow(DbEventInterceptorData interceptorData, 553 Object [] objectRow) { 554 if (objectRow != null) { 555 boolean doit = true; 556 int size = (objectRow.length > selectFields.size()) ? objectRow.length 557 : selectFields.size(); 558 559 Object [] newRow = new Object [size]; 560 561 for (int i = 0; i < objectRow.length; i++) { 562 newRow[i] = objectRow[i]; 563 } 564 565 if ((interceptorData != null) 566 && (interceptorData.getTable() != null)) { 567 interceptorData.setAttribute(DbEventInterceptorData.RESULTSET, 568 this); 569 interceptorData.setAttribute(DbEventInterceptorData.OBJECTROW, 570 newRow); 571 572 int res = DbEventInterceptor.GRANT_OPERATION; 573 574 try { 575 res = interceptorData.getTable().processInterceptors( 576 DbEventInterceptor.PRE_ADDROW, interceptorData); 577 } catch (SQLException e) { 578 ; 579 } 580 581 doit = (res == DbEventInterceptor.GRANT_OPERATION); 582 } 583 584 if (doit) { 585 objectVector.addElement(newRow); 586 587 if ((interceptorData != null) 588 && (interceptorData.getTable() != null)) { 589 try { 590 interceptorData.getTable() 591 .processInterceptors( 592 DbEventInterceptor.POST_ADDROW, 593 interceptorData); 594 } catch (SQLException e) { 595 ; 596 } 597 } 598 } 599 } 600 } 601 602 605 public void flip() { 606 int vSize = this.size(); 607 608 if (vSize > 1) { 609 logCat.info("flipping " + vSize + " elements!"); 610 611 for (int i = 1; i < vSize; i++) { 612 Object o = objectVector.elementAt(i); 613 objectVector.remove(i); 614 objectVector.insertElementAt(o, 0); 615 } 616 } 617 } 618 619 622 public void moveFirst() { 623 this.pointer = 0; 624 } 625 626 629 public void moveLast() { 630 this.pointer = size() - 1; 631 } 632 633 638 public boolean moveNext() { 639 pointer++; 640 641 return (pointer >= size()); 642 } 643 644 649 public boolean movePrevious() { 650 pointer--; 651 652 return (pointer < 0); 653 } 654 655 660 public int size() { 661 return objectVector.size(); 662 } 663 664 private boolean isPointerLegal(int p) { 665 return ((p >= 0) && (p < size())); 666 } 667 668 private void setupSelectFieldsHashtable(Vector paramSelectFields) { 669 this.selectFields.addAll(paramSelectFields); 670 selectFieldsHashtable = new Hashtable (); 671 672 for (int i = 0; i < selectFields.size(); i++) { 673 Field f = (Field) selectFields.elementAt(i); 674 selectFieldsHashtable.put(f.getName(), f); 675 } 676 } 677 } 678 | Popular Tags |