1 21 22 package org.webdocwf.util.loader; 23 24 import java.util.Hashtable ; 25 import java.util.Enumeration ; 26 import java.util.Vector ; 27 import org.w3c.dom.Document ; 28 import org.w3c.dom.NodeList ; 29 import org.w3c.dom.Node ; 30 import org.w3c.dom.Element ; 31 import org.w3c.dom.NamedNodeMap ; 32 import java.sql.Connection ; 33 import java.math.BigDecimal ; 34 import java.sql.Statement ; 35 import java.sql.SQLException ; 36 import java.sql.ResultSet ; 37 import org.webdocwf.util.loader.logging.Logger; 38 39 43 public class CounterColumns { 44 45 private String counterTableName = null; 46 private String counterNameColumn = null; 47 private String counterValueColumn = null; 48 49 private Hashtable counterName; 50 private Hashtable counterStartValue; 51 private Hashtable counterIncrement; 52 private Hashtable targetColumnName; 53 private Hashtable valueMode; 54 private Hashtable counterStartValueReset; 55 private Hashtable targetColumnTyp; 56 57 private Hashtable subCounterName; 59 private Hashtable subCounterStartValue; 60 private Hashtable subCounterIncrement; 61 private Hashtable subTargetColumnName; 62 private Hashtable subValueMode; 63 private Hashtable subCounterKeyColumns; 64 private Hashtable subCounterKeyValues; 65 private Hashtable subCounterKeyColumnsTyp; 66 private Hashtable subTargetColumnTyp; 67 68 private Vector vecCounterName = new Vector (); 69 private Vector vecCounterIncrement = new Vector (); 70 private Vector vecCounterStartValue = new Vector (); 71 private Vector vecTargetColumnName = new Vector (); 72 private Vector vecValueMode = new Vector (); 73 private Vector vecCounterStartValueReset = new Vector (); 74 private Vector vecTargetTableName = new Vector (); 75 private Vector vecTargetTableID = new Vector (); 76 77 private Vector vecSubCounterName = new Vector (); 78 private Vector vecSubCounterIncrement = new Vector (); 79 private Vector vecSubCounterStartValue = new Vector (); 80 private Vector vecSubTargetTableName = new Vector (); 81 private Vector vecSubTargetTableID = new Vector (); 82 private Vector vecSubTargetColumnName = new Vector (); 83 private Vector vecSubValueMode = new Vector (); 84 private Vector vecSubKeyColumns = new Vector (); 85 private Vector vecSubKeyColumnsTyp = new Vector (); 86 87 private Hashtable currentCounterValue; 88 89 private Hashtable subCounterCache = new Hashtable (); 91 private Logger logger; 92 93 96 public CounterColumns() { 97 98 this.counterTableName = null; 99 this.counterNameColumn = null; 100 this.counterValueColumn = null; 101 this.counterName = new Hashtable (); 102 this.counterStartValue = new Hashtable (); 103 this.counterIncrement = new Hashtable (); 104 this.targetColumnName = new Hashtable (); 105 this.targetColumnTyp = new Hashtable (); 106 this.valueMode = new Hashtable (); 107 this.counterStartValueReset = new Hashtable (); 108 109 this.subCounterName = new Hashtable (); 110 this.subCounterStartValue = new Hashtable (); 111 this.subCounterIncrement = new Hashtable (); 112 this.subTargetColumnName = new Hashtable (); 113 this.subValueMode = new Hashtable (); 114 this.subCounterKeyColumns = new Hashtable (); 115 this.subCounterKeyValues = new Hashtable (); 116 this.subCounterKeyColumnsTyp = new Hashtable (); 117 this.subTargetColumnTyp = new Hashtable (); 118 119 this.vecCounterName = new Vector (); 120 this.vecCounterIncrement = new Vector (); 121 this.vecCounterStartValue = new Vector (); 122 this.vecTargetColumnName = new Vector (); 123 this.vecValueMode = new Vector (); 124 this.vecCounterStartValueReset = new Vector (); 125 this.vecTargetTableName = new Vector (); 126 this.vecTargetTableID = new Vector (); 127 this.currentCounterValue = new Hashtable (); 128 129 this.vecSubCounterName = new Vector (); 130 this.vecSubCounterIncrement = new Vector (); 131 this.vecSubCounterStartValue = new Vector (); 132 this.vecSubTargetTableName = new Vector (); 133 this.vecSubTargetTableID = new Vector (); 134 this.vecSubTargetColumnName = new Vector (); 135 this.vecSubValueMode = new Vector (); 136 this.vecSubKeyColumns = new Vector (); 137 this.vecSubKeyColumnsTyp = new Vector (); 138 } 139 140 143 public void setLogger(Logger logger) { 144 this.logger = logger; 145 } 146 147 151 public String getCounterTableName() { 152 return this.counterTableName; 153 } 154 155 159 public String getCounterValueColumn() { 160 return this.counterValueColumn; 161 } 162 163 167 public String getCounterNameColumn() { 168 return this.counterNameColumn; 169 } 170 171 175 public void setCounterNameColumn(String nameColumn) { 176 this.counterNameColumn = nameColumn; 177 } 178 179 183 public void setCounterValueColumn(String valueColumn) { 184 this.counterValueColumn = valueColumn; 185 } 186 187 191 public void setCounterTableName(String tableName) { 192 this.counterTableName = tableName; 193 } 194 195 201 public void readConstantColumnAttributes(Document doc, int importJob) { 202 this.counterTableName = importAttributeValue(doc, "counterColumns", "counterTableName", 203 importJob); 204 this.counterNameColumn = importAttributeValue(doc, "counterColumns", "counterNameColumn", 205 importJob); 206 this.counterValueColumn = importAttributeValue(doc, "counterColumns", "counterValueColumn", 207 importJob); 208 } 209 210 220 public void readConstantColumnsParameters(Document doc, int importJob) { 221 this.vecCounterName = importValue(doc, "counterColumn", "counterName", importJob, null); 222 this.vecCounterIncrement = importValue(doc, "counterColumn", "counterIncrement", importJob, "1"); 223 this.vecCounterStartValue = importValue(doc, "counterColumn", "counterStartValue", importJob, 224 "0"); 225 this.vecTargetColumnName = importValue(doc, "counterColumn", "targetColumnName", importJob, null); 226 this.vecValueMode = importValue(doc, "counterColumn", "valueMode", importJob, "setIfCreated"); 227 this.vecCounterStartValueReset = importValue(doc, "counterColumn", "counterStartValueReset", 228 importJob, (new Boolean (false)).toString()); 229 this.vecTargetTableName = importValue(doc, "counterColumn", "targetTableName", importJob, null); 230 this.vecTargetTableID = importValue(doc, "counterColumn", "targetTableID", importJob, null); 231 232 this.vecSubCounterName = importValue(doc, "subCounterColumn", "counterName", importJob, null); 233 this.vecSubCounterIncrement = importValue(doc, "subCounterColumn", "counterIncrement", 234 importJob, "1"); 235 this.vecSubCounterStartValue = importValue(doc, "subCounterColumn", "counterStartValue", 236 importJob, "0"); 237 this.vecSubTargetTableName = importValue(doc, "subCounterColumn", "targetTableName", importJob, null); 238 this.vecSubTargetTableID = importValue(doc, "subCounterColumn", "targetTableID", importJob, null); 239 this.vecSubTargetColumnName = importValue(doc, "subCounterColumn", "targetColumnName", 240 importJob, null); 241 this.vecSubValueMode = importValue(doc, "subCounterColumn", "valueMode", importJob, 242 "setIfCreated"); 243 244 for (int i = 0; i < this.vecSubCounterName.size(); i++) { 245 this.vecSubKeyColumns.addElement(importSubCounterKeyValue(doc, "keyColumnName", importJob, i)); 246 } 247 248 } 249 250 255 public void setConstantColumnsParameters(String tableName, String tableID) { 256 257 Vector targetCounterName = new Vector (); 258 Vector targetCounterIncrement = new Vector (); 259 Vector targetCounterStartValue = new Vector (); 260 Vector targetTargetColumnName = new Vector (); 261 Vector targetValueMode = new Vector (); 262 Vector targetCounterStartValueReset = new Vector (); 263 264 Vector targetSubCounterName = new Vector (); 265 Vector targetSubCounterIncrement = new Vector (); 266 Vector targetSubCounterStartValue = new Vector (); 267 Vector targetSubTargetColumnName = new Vector (); 268 Vector targetSubValueMode = new Vector (); 269 Vector targetSubKeyColumns = new Vector (); 270 Vector subKeyColumnsKey = new Vector (); 271 272 for (int i = 0; i < this.vecTargetTableName.size(); i++) { 273 if (tableName.equalsIgnoreCase(this.vecTargetTableName.get(i).toString()) && 274 tableID.equalsIgnoreCase(this.vecTargetTableID.get(i).toString())) { 275 if (this.vecCounterName.get(i) != null) 276 targetCounterName.addElement(this.vecCounterName.get(i).toString()); 277 else 278 targetCounterName.addElement(null); 279 280 if (this.vecCounterStartValue.get(i) != null) 281 targetCounterStartValue.addElement(this.vecCounterStartValue.get(i).toString()); 282 else 283 targetCounterStartValue.addElement(null); 284 285 if (this.vecCounterIncrement.get(i) != null) 286 targetCounterIncrement.addElement(this.vecCounterIncrement.get(i).toString()); 287 else 288 targetCounterIncrement.addElement(null); 289 290 if (this.vecTargetColumnName.get(i) != null) 291 targetTargetColumnName.addElement(this.vecTargetColumnName.get(i).toString()); 292 else 293 targetTargetColumnName.addElement(null); 294 295 if (this.vecValueMode.get(i) != null) 296 targetValueMode.addElement(this.vecValueMode.get(i).toString()); 297 else 298 targetValueMode.addElement(null); 299 300 if (this.vecCounterStartValueReset.get(i) != null) 301 targetCounterStartValueReset.addElement(this.vecCounterStartValueReset.get(i).toString()); 302 else 303 targetCounterStartValueReset.addElement(null); 304 305 } 307 } 308 for (int i = 0; i < this.vecSubTargetTableName.size(); i++) { 309 if (tableName.equalsIgnoreCase(this.vecSubTargetTableName.get(i).toString()) && 310 tableID.equalsIgnoreCase(this.vecSubTargetTableID.get(i).toString())) { 311 if (this.vecSubCounterName.get(i) != null) { 312 targetSubCounterName.addElement(this.vecSubCounterName.get(i).toString()); 313 } else 314 targetSubCounterName.addElement(null); 315 if (this.vecSubCounterStartValue.get(i) != null) 316 targetSubCounterStartValue.addElement(this.vecSubCounterStartValue.get(i).toString()); 317 else 318 targetSubCounterStartValue.addElement(null); 319 if (this.vecSubCounterIncrement.get(i) != null) 320 targetSubCounterIncrement.addElement(this.vecSubCounterIncrement.get(i).toString()); 321 else 322 targetSubCounterIncrement.addElement(null); 323 if (this.vecSubTargetColumnName.get(i) != null) 324 targetSubTargetColumnName.addElement(this.vecSubTargetColumnName.get(i).toString()); 325 else 326 targetSubTargetColumnName.addElement(null); 327 if (this.vecSubValueMode.get(i) != null) 328 targetSubValueMode.addElement(this.vecSubValueMode.get(i).toString()); 329 else 330 targetSubValueMode.addElement(null); 331 if (this.vecSubKeyColumns.get(i) != null) 332 targetSubKeyColumns.addElement(this.vecSubKeyColumns.get(i)); 333 else 334 targetSubKeyColumns.addElement(null); 335 336 } 337 } 338 this.counterName.put(tableName + "_" + tableID, targetCounterName); 339 this.counterIncrement.put(tableName + "_" + tableID, targetCounterIncrement); 340 this.counterStartValue.put(tableName + "_" + tableID, targetCounterStartValue); 341 this.targetColumnName.put(tableName + "_" + tableID, targetTargetColumnName); 342 this.valueMode.put(tableName + "_" + tableID, targetValueMode); 343 this.counterStartValueReset.put(tableName + "_" + tableID, targetCounterStartValueReset); 344 345 this.subCounterName.put(tableName + "_" + tableID, targetSubCounterName); 347 this.subCounterIncrement.put(tableName + "_" + tableID, targetSubCounterIncrement); 348 this.subCounterStartValue.put(tableName + "_" + tableID, targetSubCounterStartValue); 349 this.subTargetColumnName.put(tableName + "_" + tableID, targetSubTargetColumnName); 350 this.subValueMode.put(tableName + "_" + tableID, targetSubValueMode); 351 this.subCounterKeyColumns.put(tableName + "_" + tableID, targetSubKeyColumns); 352 353 } 354 355 private int getVectorsSize() { 356 if (this.vecCounterName != null) 357 return this.vecCounterName.size(); 358 else 359 return 0; 360 } 361 362 368 public Vector getCounterName(String tableName, String tableID) { 369 return (Vector )this.counterName.get(tableName + "_" + tableID); 370 } 371 372 378 public Vector getCounterIncrement(String tableName, String tableID) { 379 return (Vector )this.counterIncrement.get(tableName + "_" + tableID); 380 } 381 382 388 public Vector getCounterStartValue(String tableName, String tableID) { 389 return (Vector )this.counterStartValue.get(tableName + "_" + tableID); 390 } 391 392 398 public Vector getTargetColumnName(String tableName, String tableID) { 399 return (Vector )this.targetColumnName.get(tableName + "_" + tableID); 400 401 } 402 403 409 public Vector getTargetColumnTyp(String tableName, String tableID) { 410 return (Vector )this.targetColumnTyp.get(tableName + "_" + tableID); 411 412 } 413 414 420 public Vector getValueMode(String tableName, String tableID) { 421 return (Vector )this.valueMode.get(tableName + "_" + tableID); 422 } 423 424 430 public Vector getCounterStartValueReset(String tableName, String tableID) { 431 return (Vector )this.counterStartValueReset.get(tableName + "_" + tableID); 432 } 433 434 441 public Vector getSubCounterName(String tableName, String tableID) { 442 return (Vector )this.subCounterName.get(tableName + "_" + tableID); 443 } 444 445 451 public Vector getSubCounterIncrement(String tableName, String tableID) { 452 return (Vector )this.subCounterIncrement.get(tableName + "_" + tableID); 453 } 454 455 461 public Vector getSubCounterStartValue(String tableName, String tableID) { 462 return (Vector )this.subCounterStartValue.get(tableName + "_" + tableID); 463 } 464 465 471 public Vector getSubTargetColumnName(String tableName, String tableID) { 472 return (Vector )this.subTargetColumnName.get(tableName + "_" + tableID); 473 474 } 475 476 482 public Vector getSubTargetColumnTyp(String tableName, String tableID) { 483 return (Vector )this.subTargetColumnTyp.get(tableName + "_" + tableID); 484 485 } 486 487 493 public Vector getSubValueMode(String tableName, String tableID) { 494 return (Vector )this.subValueMode.get(tableName + "_" + tableID); 495 } 496 497 503 public Vector getSubCounterKeyColumns(String tableName, String tableID) { 504 return (Vector )this.subCounterKeyColumns.get(tableName + "_" + tableID); 505 } 506 507 513 public Vector getSubCounterKeyColumnsTyp(String tableName, String tableID) { 514 return (Vector )this.subCounterKeyColumnsTyp.get(tableName + "_" + tableID); 515 } 516 517 523 public Vector getSubCounterKeyValues(String tableName, String tableID) { 524 return (Vector )this.subCounterKeyValues.get(tableName + "_" + tableID); 525 } 526 527 533 public void setSubCounterKeyValues(String tableName, String tableID, Hashtable values) { 534 535 Enumeration keys = values.keys(); 536 Vector vecColumns = (Vector )this.subCounterKeyColumns.get(tableName + "_" + tableID); 537 Vector vecValues = new Vector (vecColumns.size()); 538 539 String column; 540 for (int i = 0; i < vecColumns.size(); i++) { 541 Vector subValues = new Vector (); 542 for (int j = 0; j < ( (Vector )vecColumns.get(i)).size(); j++) { 543 column = ( (Vector )vecColumns.get(i)).get(j).toString(); 544 subValues.add(values.get(column)); 545 } 546 vecValues.add(subValues); 547 } 548 this.subCounterKeyValues.put(tableName + "_" + tableID, vecValues); 549 } 550 551 556 public void setCounterValue(String tableName, String tableID) { 557 Vector targetCounterValue = new Vector (); 558 Vector oldValues = getCounterValue(tableName, tableID); 559 Vector counterIncrement = getCounterIncrement(tableName, tableID); 560 for (int i = 0; i < counterIncrement.size(); i++) { 561 BigDecimal newValue = new BigDecimal (oldValues.get(i).toString()); 562 newValue = newValue.add(new BigDecimal (counterIncrement.get(i).toString())); 563 targetCounterValue.addElement(newValue); 564 } 565 this.currentCounterValue.put(tableName + "_" + tableID, targetCounterValue); 566 } 567 568 574 public Vector getCounterValue(String tableName, String tableID) { 575 return (Vector )this.currentCounterValue.get(tableName + "_" + tableID); 576 } 577 578 586 public void setTargetColumnStartValues(String tableName, String tableID, Connection conn, 587 int firstColumn) throws SQLException { 588 Vector values = new Vector (); 589 Vector valuesFromTable = readCounter(tableName, tableID, conn, firstColumn); 591 for (int i = 0; i < this.getCounterName(tableName, tableID).size(); i++) { 592 if (this.getCounterStartValueReset(tableName, 593 tableID).get(i).toString().equalsIgnoreCase("true")) { 594 if (this.getCounterStartValue(tableName, tableID).get(i) != null) { 595 BigDecimal newValue = new BigDecimal ( (this.getCounterStartValue(tableName, 596 tableID)).get(i).toString()); 597 values.addElement(newValue); 598 } else 599 values.addElement(null); 600 } else if (this.getCounterStartValueReset(tableName, 601 tableID).get(i).toString().equalsIgnoreCase("false")) { 602 if (this.getCounterStartValue(tableName, tableID).get(i) != null) { 603 BigDecimal newValue = new BigDecimal (valuesFromTable.get(i).toString()); 604 values.addElement(newValue); 605 } else 606 values.addElement(null); 607 } 608 } 609 this.currentCounterValue.put(tableName + "_" + tableID, values); 610 } 611 612 621 private Vector importValue(Document doc, String tagName, String strAttrName, 622 int iImportJobItem, String defaultValue) { 623 Vector strValue = new Vector (); 624 NodeList tagBasic = doc.getElementsByTagName("importDefinition"); 625 if (tagBasic.getLength() != 0) { 626 Element docFragment = (Element )tagBasic.item(iImportJobItem); 627 NodeList tag = docFragment.getElementsByTagName(tagName); 628 for (int i = 0; i < tag.getLength(); i++) { 629 String nodeValue = ""; 630 if (strAttrName != null) { 631 NamedNodeMap attrs = tag.item(i).getAttributes(); 632 Node nodeResult = attrs.getNamedItem(strAttrName); 633 if (nodeResult != null) 634 nodeValue = nodeResult.getNodeValue(); 635 else 636 nodeValue = defaultValue; 637 strValue.addElement(nodeValue); 638 } else { 639 NodeList nodeText = tag.item(i).getChildNodes(); 640 if (nodeText.item(0) != null) { 641 nodeValue = nodeText.item(0).getNodeValue(); 642 strValue.addElement(nodeValue); 643 } 644 } 645 } 646 } 647 return strValue; 648 } 649 650 659 private Vector importSubCounterKeyValue(Document doc, String strAttrName, 660 int iImportJobItem, int iSubCounterItem) { 661 Vector strValue = null; 662 NodeList tagBasic = doc.getElementsByTagName("importDefinition"); 663 if (tagBasic.getLength() != 0) { 664 Element docFragment = (Element )tagBasic.item(iImportJobItem); 665 NodeList tag = docFragment.getElementsByTagName("subCounterColumn"); 666 if (tag.getLength() != 0) { 667 Element docCounterColumn = (Element )tag.item(iSubCounterItem); 668 NodeList tagSubCounterColumn = docCounterColumn.getElementsByTagName("subCounterKeyColumn"); 669 strValue = new Vector (); 670 for (int i = 0; i < tagSubCounterColumn.getLength(); i++) { 671 String nodeValue = ""; 672 if (strAttrName != null) { 673 NamedNodeMap attrs = tagSubCounterColumn.item(i).getAttributes(); 674 Node nodeResult = attrs.getNamedItem(strAttrName); 675 if (nodeResult != null) { 676 nodeValue = nodeResult.getNodeValue(); 677 } else 678 nodeValue = null; 679 strValue.addElement(nodeValue); 680 681 } else { 682 NodeList nodeText = tag.item(i).getChildNodes(); 683 if (nodeText.item(0) != null) { 684 nodeValue = nodeText.item(0).getNodeValue(); 685 strValue.addElement(nodeValue); 686 } 687 } 688 } 689 } 690 } 691 return strValue; 692 } 693 694 720 725 public void updateCounter(Connection conn) throws SQLException { 726 String strQuery = ""; 727 for (int i=0;i<this.vecTargetTableName.size();i++){ 728 String tableName = this.vecTargetTableName.get(i).toString(); 729 String tableID = this.vecTargetTableID.get(i).toString(); 730 Vector counterNames = this.getCounterName(tableName, tableID); 731 Vector counterValues = this.getCounterValue(tableName, tableID); 732 Vector counterIncrements = this.getCounterIncrement(tableName, tableID); 733 for (int j = 0; j < counterNames.size(); j++) { 734 Statement stmt = conn.createStatement(); 735 BigDecimal newValue = new BigDecimal (counterValues.get(i).toString()); 736 strQuery = queryUpdateCounter(counterNames.get(i).toString(), newValue); 737 stmt.executeUpdate(strQuery); 738 stmt.close(); 739 } 740 } 741 } 742 743 751 private boolean insertCounter(String tableName, String tableID, 752 Connection conn) throws SQLException { 753 754 String strQuery = null; 755 756 Vector counterNames = this.getCounterName(tableName, tableID); 757 Vector startValues = this.getCounterIncrement(tableName, tableID); 758 for (int i = 0; i < counterNames.size(); i++) { 759 Statement stmt = conn.createStatement(); 760 BigDecimal newValue = new BigDecimal (startValues.get(i).toString()); 761 strQuery = queryInsertCounter(counterNames.get(i).toString(), newValue); 762 stmt.execute(strQuery); 763 stmt.close(); 764 } 765 return true; 766 } 767 768 777 private Vector readCounter(String tableName, String tableID, Connection conn, 778 int firstColumn) throws SQLException { 779 780 String strQuery = null; 781 BigDecimal value = new BigDecimal (0); 782 Vector startValues = new Vector (); 783 Vector counterNames = this.getCounterName(tableName, tableID); 784 Vector counterValues = this.getCounterValue(tableName, tableID); 785 Vector counterIncrements = this.getCounterIncrement(tableName, tableID); 786 Vector defaultStartValues = this.getCounterIncrement(tableName, tableID); 787 788 for (int i = 0; i < counterNames.size(); i++) { 789 Statement stmt = conn.createStatement(); 790 strQuery = querySelectCounter(counterNames.get(i).toString()); 791 this.logger.write("full", "\tQuery '" + strQuery + "' will be executed"); 792 ResultSet rset = stmt.executeQuery(strQuery); 793 if (rset.next()) { 794 if (firstColumn == 0) 795 value = new BigDecimal (rset.getString(0)); 796 else 797 value = new BigDecimal (rset.getString(1)); 798 } else { 799 insertCounter(tableName, tableID, conn); 800 value = new BigDecimal (defaultStartValues.get(i).toString()); 801 } 802 stmt.close(); 803 rset.close(); 804 startValues.addElement(value); 805 806 } 807 return startValues; 808 } 809 810 815 private String querySelectCounter(String counterColumnName) { 816 String query = "select "; 817 818 query += this.counterValueColumn + " from " + this.counterTableName + " where " + 819 this.counterNameColumn + " = '" + counterColumnName + "'"; 820 return query; 821 } 822 823 829 private String queryInsertCounter(String counterColumnName, BigDecimal counterColumnValue) { 830 String query = "INSERT into "; 831 832 query += this.counterTableName + " (" + this.counterNameColumn + ", " + this.counterValueColumn 833 + ") VALUES('" + 834 counterColumnName + "', " + counterColumnValue.intValue() + ")"; 835 return query; 836 } 837 838 844 845 private String queryUpdateCounter(String counterColumnName, BigDecimal newValue) { 846 String query = "update "; 847 query += this.counterTableName + " set " + 848 this.counterValueColumn + " = " + newValue.toString() + " where " + 849 this.counterNameColumn + " = '" + counterColumnName + "'"; 850 return query; 851 } 852 853 862 public Vector readSubCounterValue(String tableName, String tableID, Connection conn, 863 int firstColumn, String tableMode, ConfigReader targetConfigReader) throws SQLException , LoaderException { 864 865 BigDecimal value = new BigDecimal (0); 866 Vector startValues = new Vector (); 867 868 this.logger.write("full", "\treadSubCounterValue method is started"); 869 870 Vector columns = this.getSubCounterKeyColumns(tableName, tableID); 871 Vector typs = this.getSubCounterKeyColumnsTyp(tableName, tableID); 872 Vector subColumnNames = this.getSubTargetColumnName(tableName, tableID); 873 Vector subIncrement = this.getSubCounterIncrement(tableName, tableID); 874 Vector subCounterStartValues = this.getSubCounterStartValue(tableName, tableID); 875 Vector subCounterKeyColumnValues = this.getSubCounterKeyValues(tableName, tableID); 876 877 for (int i = 0; i < subColumnNames.size(); i++) { 878 879 String strQuery = "select "; 880 Statement stmt = conn.createStatement(); 881 strQuery += subColumnNames.get(i).toString() + " from " + tableName + " where "; 882 for (int j = 0; j < ( (Vector )columns.get(i)).size(); j++) { 883 if ( ( (Vector )subCounterKeyColumnValues.get(i)).get(j) != null && 884 !( (Vector )subCounterKeyColumnValues.get(i)).get(j).toString().equalsIgnoreCase("")) { 885 try { 887 if (!targetConfigReader.isNumber( ( (Vector )typs.get(i)).get(j).toString())) 888 strQuery += ( (Vector )columns.get(i)).get(j).toString() + " = '" 889 + ( (Vector )subCounterKeyColumnValues.get(i)).get(j).toString() + "' and "; 890 else 891 strQuery += ( (Vector )columns.get(i)).get(j).toString() + " = " 892 + ( (Vector )subCounterKeyColumnValues.get(i)).get(j).toString() + " and "; 893 } catch (LoaderException e) { 894 LoaderException le= new LoaderException("Exception:This sql type isn't present in conf file for target database. Yuo must add it into conf file.",(Throwable )e); 895 throw le; 896 } 897 } else 898 strQuery += ( (Vector )columns.get(i)).get(j).toString() + " is null and "; 899 } 900 901 if (strQuery.endsWith(" and ")) 902 strQuery = strQuery.substring(0, strQuery.length() - 5); 903 904 if (tableMode.equalsIgnoreCase("cache")) { 905 Object obj = subCounterCache.get(strQuery); 907 if (obj == null) { this.logger.write("full", "\tQuery '" + strQuery + "' will be executed"); 909 ResultSet rset = stmt.executeQuery(strQuery); 910 BigDecimal currentValue = new BigDecimal (0); 911 if (rset.next()) { 912 if (firstColumn == 0) { 913 value = new BigDecimal (rset.getString(0)); 914 currentValue = value; 915 while (rset.next()) { 916 value = new BigDecimal (rset.getString(0)); 917 if (currentValue.intValue() < value.intValue()) 918 currentValue = value; 919 } 920 } else { value = new BigDecimal (rset.getString(1)); 922 currentValue = value; 923 while (rset.next()) { 924 value = new BigDecimal (rset.getString(1)); 925 if (currentValue.intValue() < value.intValue()) 926 currentValue = value; 927 } 928 } 929 value = value.add(new BigDecimal (subIncrement.get(i).toString())); 930 } else { 931 value = new BigDecimal (subCounterStartValues.get(i).toString()); 932 } 933 subCounterCache.put(strQuery, value); 934 stmt.close(); 935 } else { BigDecimal oldValue = (BigDecimal )obj; 937 value = oldValue.add(new BigDecimal (subIncrement.get(i).toString())); 938 subCounterCache.remove(strQuery); 939 subCounterCache.put(strQuery, value); 940 } 941 } else { this.logger.write("full", "\tQuery '" + strQuery + "' will be executed"); 943 ResultSet rset = stmt.executeQuery(strQuery); 944 BigDecimal currentValue = new BigDecimal (0); 945 if (rset.next()) { 946 if (firstColumn == 0) { 947 value = new BigDecimal (rset.getString(0)); 948 currentValue = value; 949 while (rset.next()) { 950 value = new BigDecimal (rset.getString(0)); 951 if (currentValue.intValue() < value.intValue()) 952 currentValue = value; 953 } 954 } else { value = new BigDecimal (rset.getString(1)); 956 currentValue = value; 957 while (rset.next()) { 958 value = new BigDecimal (rset.getString(1)); 959 if (currentValue.intValue() < value.intValue()) 960 currentValue = value; 961 } 962 } 963 value = value.add(new BigDecimal (subIncrement.get(i).toString())); 964 } else { 965 value = new BigDecimal (subCounterStartValues.get(i).toString()); 966 } 967 stmt.close(); 968 969 } 970 startValues.addElement(value); 971 } 972 this.logger.write("full", "\treadSubCounterValue method is finished"); 973 return startValues; 974 } 975 976 979 public void resetSubCounterCache(){ 980 if(this.subCounterCache.size()>0) 981 this.subCounterCache.clear(); 982 } 983 984 985 994 private String importAttributeValue(Document doc, String strTagName, String strAttrName, 995 int iImportJobItem) { 996 String strValue = ""; 997 NodeList tagBasic = doc.getElementsByTagName("importDefinition"); 998 if (tagBasic.getLength() != 0) { 999 Element docFragment = (Element )tagBasic.item(iImportJobItem); 1000 1003 tagBasic = docFragment.getElementsByTagName(strTagName); 1004 if (tagBasic.getLength() != 0) { 1005 docFragment = (Element )tagBasic.item(0); 1006 if (docFragment != null) 1007 strValue = docFragment.getAttribute(strAttrName); 1008 } 1009 } 1010 return strValue; 1011 } 1012 1013 1024 public void counterColumnTypes(String tableName, String tableID, Connection c, 1025 int firstColumn, boolean columnsSuportedTarget, ConfigReader configReaderTarget) throws SQLException , NullPointerException { 1026 int iCnt = 0; 1027 try { 1028 Vector columnNames = this.getSubCounterKeyColumns(tableName, tableID); 1029 Statement stmtConstant = c.createStatement(); 1030 Vector typs = new Vector (); 1031 Vector subTyps = new Vector (); 1032 String strQuery = "select "; 1033 ResultSet rsetConstant=null; 1034 1035 if (columnNames.size() != 0) { 1036 for (int i = 0; i < columnNames.size(); i++) { 1037 for (int j = 0; j < ( (Vector )columnNames.get(i)).size(); j++) { 1038 strQuery += ( (Vector )columnNames.get(i)).get(j).toString() + 1039 ", "; 1040 } 1041 strQuery = strQuery.substring(0, strQuery.length() - 2); 1042 strQuery += " from " + tableName; 1043 if (columnsSuportedTarget){ 1046 1047 rsetConstant = c.getMetaData().getColumns( c.getCatalog(), null, tableName, "%" ); 1048 String columnName = ""; 1049 String columnType = ""; 1050 while(rsetConstant.next()){ 1051 columnName = rsetConstant.getString(3+firstColumn); 1052 columnType = rsetConstant.getString(5+firstColumn); 1053 Vector temp = (Vector )columnNames.get(i); 1054 for (int j = 0; j < temp.size(); j++) { 1055 if( temp.get(j).toString().equalsIgnoreCase( columnName ) ){ 1056 typs.add(columnType); 1057 } 1058 } 1059 } 1060 }else{ if (configReaderTarget.getMaxRowsSupported()){ 1062 stmtConstant.setMaxRows(1); 1063 } 1064 rsetConstant = stmtConstant.executeQuery(strQuery); 1065 1066 for (int j = 0; j < ( (Vector )columnNames.get(i)).size(); j++) { 1067 1068 typs.add(rsetConstant.getMetaData().getColumnTypeName(j + firstColumn)); 1069 } 1070 1071 } 1072 rsetConstant.close(); 1073 subTyps.addElement(typs); 1074 } 1075 } 1076 this.subCounterKeyColumnsTyp.put(tableName + "_" + tableID, subTyps); 1077 stmtConstant.close(); 1078 1079 } 1080 catch (SQLException ex) { 1081 throw ex; 1082 } 1083 catch (NullPointerException ex) { 1084 throw ex; 1085 } 1086 } 1087 1088 1091 public void reset() { 1092 this.counterTableName = null; 1093 this.counterNameColumn = null; 1094 this.counterValueColumn = null; 1095 this.counterName = new Hashtable (); 1096 this.counterStartValue = new Hashtable (); 1097 this.counterIncrement = new Hashtable (); 1098 this.targetColumnName = new Hashtable (); 1099 this.targetColumnTyp = new Hashtable (); 1100 this.valueMode = new Hashtable (); 1101 this.counterStartValueReset = new Hashtable (); 1102 1103 this.subCounterName = new Hashtable (); 1104 this.subCounterStartValue = new Hashtable (); 1105 this.subCounterIncrement = new Hashtable (); 1106 this.subTargetColumnName = new Hashtable (); 1107 this.subValueMode = new Hashtable (); 1108 this.subCounterKeyColumns = new Hashtable (); 1109 this.subCounterKeyValues = new Hashtable (); 1110 this.subCounterKeyColumnsTyp = new Hashtable (); 1111 this.subTargetColumnTyp = new Hashtable (); 1112 1113 this.vecCounterName = new Vector (); 1114 this.vecCounterIncrement = new Vector (); 1115 this.vecCounterStartValue = new Vector (); 1116 this.vecTargetColumnName = new Vector (); 1117 this.vecValueMode = new Vector (); 1118 this.vecCounterStartValueReset = new Vector (); 1119 this.vecTargetTableName = new Vector (); 1120 this.vecTargetTableID = new Vector (); 1121 this.currentCounterValue = new Hashtable (); 1122 1123 this.vecSubCounterName = new Vector (); 1124 this.vecSubCounterIncrement = new Vector (); 1125 this.vecSubCounterStartValue = new Vector (); 1126 this.vecSubTargetTableName = new Vector (); 1127 this.vecSubTargetTableID = new Vector (); 1128 this.vecSubTargetColumnName = new Vector (); 1129 this.vecSubValueMode = new Vector (); 1130 this.vecSubKeyColumns = new Vector (); 1131 this.vecSubKeyColumnsTyp = new Vector (); 1132 this.subCounterCache.clear(); 1133 } 1134} 1135 | Popular Tags |