1 18 19 package org.apache.jorphan.collections; 20 import java.io.Serializable ; 21 import java.sql.ResultSet ; 22 import java.sql.ResultSetMetaData ; 23 import java.sql.SQLException ; 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 30 import org.apache.jorphan.logging.LoggingManager; 31 import org.apache.jorphan.util.JOrphanUtils; 32 import org.apache.log.Logger; 33 34 43 public class Data implements Serializable 44 { 45 private static Logger log = LoggingManager.getLoggerForClass(); 46 Map data; 47 ArrayList header; 50 int currentPos, size; 52 53 56 public Data() 57 { 58 header = new ArrayList (); 59 data = new HashMap (); 60 currentPos = -1; 61 size = currentPos + 1; 62 } 63 64 69 public void replaceHeader(String oldHeader, String newHeader) 70 { 71 List tempList; 72 int index = header.indexOf(oldHeader); 73 header.set(index, newHeader); 74 tempList = (List ) data.remove(oldHeader); 75 data.put(newHeader, tempList); 76 } 77 78 82 public void append(Data d) 83 { 84 boolean valid = true; 85 String [] headers = getHeaders(); 86 String [] dHeaders = d.getHeaders(); 87 if (headers.length != dHeaders.length) 88 { 89 valid = false; 90 } 91 else 92 { 93 for (int count = 0; count < dHeaders.length; count++) 94 { 95 if (!header.contains(dHeaders[count])) 96 { 97 valid = false; 98 } 99 } 100 } 101 102 if (valid) 103 { 104 currentPos = size; 105 d.reset(); 106 while (d.next()) 107 { 108 for (int count = 0; count < headers.length; count++) 109 { 110 addColumnValue( 111 headers[count], 112 d.getColumnValue(headers[count])); 113 } 114 } 115 } 116 } 117 118 122 public int getCurrentPos() 123 { 124 return currentPos; 125 } 126 127 130 public void removeRow() 131 { 132 List tempList; 133 Iterator it = data.keySet().iterator(); 134 log.debug("removing row, size = " + size); 135 if (currentPos > -1 && currentPos < size) 136 { 137 log.debug("got to here"); 138 while (it.hasNext()) 139 { 140 tempList = (List ) data.get(it.next()); 141 tempList.remove(currentPos); 142 } 143 if (currentPos > 0) 144 { 145 currentPos--; 146 } 147 size--; 148 } 149 } 150 151 public void removeRow(int index) 152 { 153 log.debug("Removing row: " + index); 154 if (index < size) 155 { 156 setCurrentPos(index); 157 log.debug("Setting currentpos to " + index); 158 removeRow(); 159 } 160 } 161 162 public void addRow() 163 { 164 String [] headers = getHeaders(); 165 List tempList = new ArrayList (); 166 for (int i = 0; i < headers.length; i++) 167 { 168 if ((tempList = (ArrayList ) data.get(header.get(i))) == null) 169 { 170 tempList = new ArrayList (); 171 data.put(headers[i], tempList); 172 } 173 tempList.add(""); 174 } 175 size = tempList.size(); 176 setCurrentPos(size - 1); 177 } 178 179 184 public void setCurrentPos(int r) 185 { 186 currentPos = r; 187 } 188 189 198 public void sort(String column, boolean asc) 199 { 200 sortData(column, 0, size); 201 } 202 203 private void swapRows(int row1, int row2) 204 { 205 List temp; 206 Object o; 207 Iterator it = data.keySet().iterator(); 208 while (it.hasNext()) 209 { 210 temp = (List ) data.get(it.next()); 211 o = temp.get(row1); 212 temp.set(row1, temp.get(row2)); 213 temp.set(row2, o); 214 } 215 } 216 217 225 private void sortData(String column, int start, int end) 226 { 227 int x = start, y = end - 1; 228 String basis = 229 ((List ) data.get(column)).get((int) ((x + y) / 2)).toString(); 230 if (x == y) 231 { 232 return; 233 } 234 235 while (x <= y) 236 { 237 while (x < end 238 && ((List ) data.get(column)).get(x).toString().compareTo(basis) 239 < 0) 240 { 241 x++; 242 } 243 244 while (y >= (start - 1) 245 && ((List ) data.get(column)).get(y).toString().compareTo(basis) 246 > 0) 247 { 248 y--; 249 } 250 251 if (x <= y) 252 { 253 swapRows(x, y); 254 x++; 255 y--; 256 } 257 } 258 259 if (x == y) 260 { 261 x++; 262 } 263 264 y = end - x; 265 266 if (x > 0) 267 { 268 sortData(column, start, x); 269 } 270 271 if (y > 0) 272 { 273 sortData(column, x, end); 274 } 275 } 276 277 281 public int size() 282 { 283 return size; 284 } 286 292 public void addColumnValue(String column, Object value) 293 { 294 ArrayList tempList; 295 if ((tempList = (ArrayList ) data.get(column)) == null) 296 { 297 tempList = new ArrayList (); 298 data.put(column, tempList); 299 } 300 int s = tempList.size(); 301 if (currentPos == -1) 302 { 303 currentPos = size; 304 } 305 306 if (currentPos >= size) 307 { 308 size = currentPos + 1; 309 } 310 311 while (currentPos > s) 312 { 313 s++; 314 tempList.add(null); 315 } 316 317 if (currentPos == s) 318 { 319 tempList.add(value); 320 } 321 else 322 { 323 tempList.set(currentPos, value); 324 } 325 } 326 327 334 public int findValue(String column, Object value) 335 { 336 List list = (List ) data.get(column); 337 int ret = -1; 338 ret = list.indexOf(value); 339 return ret; 340 } 341 342 349 public void setColumnValue(String column, Object value) 350 { 351 List tempList; 352 if ((tempList = (List ) data.get(column)) == null) 353 { 354 tempList = new ArrayList (); 355 data.put(column, tempList); 356 } 357 358 if (currentPos == -1) 359 { 360 currentPos = 0; 361 } 362 363 if (currentPos >= size) 364 { 365 size++; 366 tempList.add(value); 367 } 368 else if (currentPos >= tempList.size()) 369 { 370 tempList.add(value); 371 } 372 else 373 { 374 tempList.set(currentPos, value); 375 } 376 } 377 378 383 public boolean hasHeader(String column) 384 { 385 return data.containsKey(column); 386 } 387 388 392 public boolean next() 393 { 394 return (++currentPos < size); 395 } 396 397 404 public static Data getDataFromResultSet(ResultSet rs) throws SQLException 405 { 406 ResultSetMetaData meta = rs.getMetaData(); 407 Data data = new Data(); 408 409 int numColumns = meta.getColumnCount(); 410 String [] dbCols = new String [numColumns]; 411 for (int i = 0; i < numColumns; i++) 412 { 413 dbCols[i] = meta.getColumnName(i + 1); 414 data.addHeader(dbCols[i]); 415 } 416 417 while (rs.next()) 418 { 419 data.next(); 420 for (int i = 0; i < numColumns; i++) 421 { 422 Object o = rs.getObject(i + 1); 423 if (o instanceof byte[]) 424 { 425 o = new String ((byte[]) o); 426 } 427 data.addColumnValue(dbCols[i], o); 428 } 429 } 430 return data; 431 } 432 433 437 public boolean previous() 438 { 439 return (--currentPos >= 0); 440 } 441 442 446 public void reset() 447 { 448 currentPos = -1; 449 } 450 451 456 public Object getColumnValue(String column) 457 { 458 try 459 { 460 if (currentPos < size) 461 { 462 return ((List ) data.get(column)).get(currentPos); 463 } 464 else 465 { 466 return null; 467 } 468 } 469 catch (Exception e) 470 { 471 return null; 472 } 473 } 474 475 480 public Object getColumnValue(int column) 481 { 482 String columnName = (String ) header.get(column); 483 try 484 { 485 if (currentPos < size) 486 { 487 return ((List ) data.get(columnName)).get(currentPos); 488 } 489 else 490 { 491 return null; 492 } 493 } 494 catch (Exception e) 495 { 496 return null; 497 } 498 } 499 500 public Object getColumnValue(int column, int row) 501 { 502 setCurrentPos(row); 503 return getColumnValue(column); 504 } 505 506 public void removeColumn(int col) 507 { 508 String columnName = (String ) header.get(col); 509 data.remove(columnName); 510 header.remove(columnName); 511 } 512 513 520 public void setHeaders(String [] h) 521 { 522 int x = 0; 523 header = new ArrayList (h.length); 524 for (x = 0; x < h.length; x++) 525 { 526 header.add(h[x]); 527 data.put(h[x], new ArrayList ()); 528 } 529 } 530 531 535 public String [] getHeaders() 536 { 537 String [] r = new String [header.size()]; 538 if (r.length > 0) 539 { 540 r = (String []) header.toArray(r); 541 } 542 return r; 543 } 544 545 552 public List getColumnAsObjectArray(String columnName) 553 { 554 return (List ) data.get(columnName); 555 } 556 557 565 public String [] getColumn(String columnName) 566 { 567 String [] returnValue; 568 Object o; 569 List temp = (List ) data.get(columnName); 570 if (temp != null) 571 { 572 returnValue = new String [temp.size()]; 573 Iterator it = temp.iterator(); 574 int index = 0; 575 while (it.hasNext()) 576 { 577 o = it.next(); 578 if (o != null) 579 { 580 if (o instanceof String ) 581 { 582 returnValue[index++] = (String ) o; 583 } 584 else 585 { 586 returnValue[index++] = o.toString(); 587 } 588 } 589 } 590 } 591 else 592 { 593 returnValue = new String [0]; 594 } 595 return returnValue; 596 } 597 598 610 public void setData(String [] contents, String delimiter) 611 { 612 setHeaders(JOrphanUtils.split(contents[0], delimiter)); 613 int x = 1; 614 while (x < contents.length) 615 { 616 setLine(JOrphanUtils.split(contents[x++], delimiter)); 617 } 618 } 619 620 629 630 633 public void setColumnData(String colName, Object value) 634 { 635 List list = this.getColumnAsObjectArray(colName); 636 while (list.size() < size()) 637 { 638 list.add(value); 639 } 640 } 641 642 public void setColumnData(int col, List data) 643 { 644 reset(); 645 Iterator iter = data.iterator(); 646 String columnName = (String ) header.get(col); 647 while (iter.hasNext()) 648 { 649 next(); 650 setColumnValue(columnName, iter.next()); 651 } 652 } 653 654 658 public void addHeader(String s) 659 { 660 header.add(s); 661 data.put(s, new ArrayList (size())); 662 } 663 664 671 public void setLine(String [] line) 672 { 673 List tempList; 674 String [] h = getHeaders(); 675 for (int count = 0; count < h.length; count++) 676 { 677 tempList = (List ) data.get(h[count]); 678 if (count < line.length && line[count].length() > 0) 679 { 680 tempList.add(line[count]); 681 } 682 else 683 { 684 tempList.add("N/A"); 685 } 686 } 687 size++; 688 } 689 690 699 public void setLine(String [] line, String deflt) 700 { 701 List tempList; 702 String [] h = getHeaders(); 703 for (int count = 0; count < h.length; count++) 704 { 705 tempList = (List ) data.get(h[count]); 706 if (count < line.length && line[count].length() > 0) 707 { 708 tempList.add(line[count]); 709 } 710 else 711 { 712 tempList.add(deflt); 713 } 714 } 715 size++; 716 } 717 718 724 public String [] getDataAsText() 725 { 726 StringBuffer temp = new StringBuffer (""); 727 String [] line = new String [size + 1]; 728 String [] elements = getHeaders(); 729 for (int count = 0; count < elements.length; count++) 730 { 731 temp.append(elements[count]); 732 if (count + 1 < elements.length) 733 { 734 temp.append("\t"); 735 } 736 } 737 line[0] = temp.toString(); 738 reset(); 739 int index = 1; 740 while (next()) 741 { 742 temp = new StringBuffer (""); 743 for (int count = 0; count < elements.length; count++) 744 { 745 temp.append(getColumnValue(count)); 746 if (count + 1 < elements.length) 747 { 748 temp.append("\t"); 749 } 750 } 751 line[index++] = temp.toString(); 752 } 753 return line; 754 } 755 756 public String toString() 757 { 758 String [] contents = getDataAsText(); 759 StringBuffer sb = new StringBuffer (); 760 boolean first = true; 761 for (int x = 0; x < contents.length; x++) 762 { 763 if (!first) 764 { 765 sb.append("\n"); 766 } 767 else 768 { 769 first = false; 770 } 771 sb.append(contents[x]); 772 } 773 return sb.toString(); 774 } 775 } 776 | Popular Tags |