View Javadoc
1 /*** 2 CounterColumns - insert auto increment values in target counter columns. 3 Copyright (C) 2002-2003 Together 4 This library is free software; you can redistribute it and/or 5 modify it under the terms of the GNU Lesser General Public 6 License as published by the Free Software Foundation; either 7 version 2.1 of the License, or (at your option) any later version. 8 This library is distributed in the hope that it will be useful, 9 but WITHOUT ANY WARRANTY; without even the implied warranty of 10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 Lesser General Public License for more details. 12 You should have received a copy of the GNU Lesser General Public 13 License along with this library; if not, write to the Free Software 14 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 15 CounterColumns.java 16 Date: 11.03.2003. 17 @version 1.1.0 18 @author: Milosevic Sinisa sinisa@prozone.co.yu 19 @author: Radoslav Dutina rale@prozone.co.yu 20 */ 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 /*** 40 * 41 * CounterColumns - insert auto increment values in target counter columns. 42 */ 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 //subCounter columns 58 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 //caching the subcounter value 90 private Hashtable subCounterCache = new Hashtable(); 91 private Logger logger; 92 93 /*** 94 * Empty constructor of CounterColumns class 95 */ 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 /*** 141 * This method set logger object 142 */ 143 public void setLogger(Logger logger) { 144 this.logger = logger; 145 } 146 147 /*** 148 * This method read value of counterTableName parameter 149 * @return string name of counter table 150 */ 151 public String getCounterTableName() { 152 return this.counterTableName; 153 } 154 155 /*** 156 * This method read value of counterValueColumn parameter 157 * @return String name of column in counter table with current values of each counter 158 */ 159 public String getCounterValueColumn() { 160 return this.counterValueColumn; 161 } 162 163 /*** 164 * This method read value of counterNameColumn parameter 165 * @return String name of column in counter table with counter names. 166 */ 167 public String getCounterNameColumn() { 168 return this.counterNameColumn; 169 } 170 171 /*** 172 * This method sets the value of counterNameColumn parameter 173 * @param nameColumn is name of the column 174 */ 175 public void setCounterNameColumn(String nameColumn) { 176 this.counterNameColumn = nameColumn; 177 } 178 179 /*** 180 * This method sets the value of counterValueColumn parameter 181 * @param valueColumn is value of the column 182 */ 183 public void setCounterValueColumn(String valueColumn) { 184 this.counterValueColumn = valueColumn; 185 } 186 187 /*** 188 * This method sets the value of counterTableName parameter 189 * @param tableName is table name 190 */ 191 public void setCounterTableName(String tableName) { 192 this.counterTableName = tableName; 193 } 194 195 /*** 196 * This method sets the value of: 197 * counterTableName, counterNameColumn and counterValueColumn parameters 198 * @param doc represents Object document 199 * @param importJob represents current import job 200 */ 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 /*** 211 * This method sets the value of next parameters: 212 * vecCounterName, vecCounterIncrement, vecCounterStartValue, vecTargetColumnName, 213 * vecValueMode, vecCounterStartValueReset, vecTargetTableName, vecTargetTableID, 214 * vecSubCounterName, vecSubCounterIncrement, vecSubCounterStartValue, 215 * vecSubTargetTableName, vecSubTargetTableID, vecSubTargetColumnName, 216 * vecSubValueMode and vecSubKeyColumns 217 * @param doc represents Object document 218 * @param importJob represents current import job 219 */ 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 /*** 251 * This method sets the value of Constant Columns parameters 252 * @param tableName is table name 253 * @param tableID is table ID 254 */ 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 //sub counter columns 306 } 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 // subCounter columns 346 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 /*** 363 * This method read value of counterNames parameter 364 * @param tableName is table name 365 * @param tableID is table ID 366 * @return vector 367 */ 368 public Vector getCounterName(String tableName, String tableID) { 369 return (Vector)this.counterName.get(tableName + "_" + tableID); 370 } 371 372 /*** 373 * This method read value of counterIncrement parameter 374 * @param tableName is table name 375 * @param tableID is table ID 376 * @return Vector of increment values for all counter columns in table tableName. 377 */ 378 public Vector getCounterIncrement(String tableName, String tableID) { 379 return (Vector)this.counterIncrement.get(tableName + "_" + tableID); 380 } 381 382 /*** 383 * This method read value of counterStartValue parameter 384 * @param tableName is table name 385 * @param tableID is table ID 386 * @return Vector of start values for all counter columns in table tableName. 387 */ 388 public Vector getCounterStartValue(String tableName, String tableID) { 389 return (Vector)this.counterStartValue.get(tableName + "_" + tableID); 390 } 391 392 /*** 393 * This method read value of targetColumnName parameter 394 * @param tableName is table name 395 * @param tableID is table ID 396 * @return Vector of counter column names for all counter columns in table tableName. 397 */ 398 public Vector getTargetColumnName(String tableName, String tableID) { 399 return (Vector)this.targetColumnName.get(tableName + "_" + tableID); 400 401 } 402 403 /*** 404 * This method read value of targetColumnTyp parameter 405 * @param tableName is table name 406 * @param tableID is table ID 407 * @return Vector of counter column types for all counter columns in table tableName. 408 */ 409 public Vector getTargetColumnTyp(String tableName, String tableID) { 410 return (Vector)this.targetColumnTyp.get(tableName + "_" + tableID); 411 412 } 413 414 /*** 415 * This method read value of valueMode parameter 416 * @param tableName is table name 417 * @param tableID is table ID 418 * @return vector 419 */ 420 public Vector getValueMode(String tableName, String tableID) { 421 return (Vector)this.valueMode.get(tableName + "_" + tableID); 422 } 423 424 /*** 425 * This method read value of counterStartValueReset parameter 426 * @param tableName is table name 427 * @param tableID is table ID 428 * @return vector 429 */ 430 public Vector getCounterStartValueReset(String tableName, String tableID) { 431 return (Vector)this.counterStartValueReset.get(tableName + "_" + tableID); 432 } 433 434 //subCounter columns 435 /*** 436 * This method read value of subCounterName parameter 437 * @param tableName is table name 438 * @param tableID is table ID 439 * @return vector 440 */ 441 public Vector getSubCounterName(String tableName, String tableID) { 442 return (Vector)this.subCounterName.get(tableName + "_" + tableID); 443 } 444 445 /*** 446 * This method read value of getSubCounterIncrement parameter 447 * @param tableName is table name 448 * @param tableID is table ID 449 * @return vector 450 */ 451 public Vector getSubCounterIncrement(String tableName, String tableID) { 452 return (Vector)this.subCounterIncrement.get(tableName + "_" + tableID); 453 } 454 455 /*** 456 * This method read value of getSubCounterStartValue parameter 457 * @param tableName is table name 458 * @param tableID is table ID 459 * @return vector 460 */ 461 public Vector getSubCounterStartValue(String tableName, String tableID) { 462 return (Vector)this.subCounterStartValue.get(tableName + "_" + tableID); 463 } 464 465 /*** 466 * This method read value of subTargetColumnName parameter 467 * @param tableName is table name 468 * @param tableID is table ID 469 * @return vector 470 */ 471 public Vector getSubTargetColumnName(String tableName, String tableID) { 472 return (Vector)this.subTargetColumnName.get(tableName + "_" + tableID); 473 474 } 475 476 /*** 477 * This method read value of subTargetColumnTyp parameter 478 * @param tableName is table name 479 * @param tableID is table ID 480 * @return vector 481 */ 482 public Vector getSubTargetColumnTyp(String tableName, String tableID) { 483 return (Vector)this.subTargetColumnTyp.get(tableName + "_" + tableID); 484 485 } 486 487 /*** 488 * This method read value of subValueMode parameter 489 * @param tableName is table name 490 * @param tableID is table ID 491 * @return vector 492 */ 493 public Vector getSubValueMode(String tableName, String tableID) { 494 return (Vector)this.subValueMode.get(tableName + "_" + tableID); 495 } 496 497 /*** 498 * This method read value of subCounterKeyColumns parameter 499 * @param tableName is table name 500 * @param tableID is table ID 501 * @return vector 502 */ 503 public Vector getSubCounterKeyColumns(String tableName, String tableID) { 504 return (Vector)this.subCounterKeyColumns.get(tableName + "_" + tableID); 505 } 506 507 /*** 508 * This method read value of subCounterKeyColumnsTyp parameter 509 * @param tableName is table name 510 * @param tableID is table ID 511 * @return vector 512 */ 513 public Vector getSubCounterKeyColumnsTyp(String tableName, String tableID) { 514 return (Vector)this.subCounterKeyColumnsTyp.get(tableName + "_" + tableID); 515 } 516 517 /*** 518 * This method read value of subCounterKeyValues parameter 519 * @param tableName is table name 520 * @param tableID is table ID 521 * @return vector 522 */ 523 public Vector getSubCounterKeyValues(String tableName, String tableID) { 524 return (Vector)this.subCounterKeyValues.get(tableName + "_" + tableID); 525 } 526 527 /*** 528 * This method set value of subCounterKeyValues parameter 529 * @param tableName defines table name 530 * @param tableID defines table ID 531 * @param values is hashtable new key values 532 */ 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 /*** 552 * This method set values for Counter columns 553 * @param tableName is table name 554 * @param tableID is table ID 555 */ 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 /*** 569 * This method set values for currentCounterValue parameter 570 * @param tableName is table name 571 * @param tableID is table ID 572 * @return Vector of current counter values for table - tableName 573 */ 574 public Vector getCounterValue(String tableName, String tableID) { 575 return (Vector)this.currentCounterValue.get(tableName + "_" + tableID); 576 } 577 578 /*** 579 * This method set values for currentCounterValue parameter 580 * @param tableName is table name 581 * @param tableID is table ID 582 * @param conn is connection to target database 583 * @param firstColumn is first column 584 * @throws SQLException 585 */ 586 public void setTargetColumnStartValues(String tableName, String tableID, Connection conn, 587 int firstColumn) throws SQLException { 588 Vector values = new Vector(); 589 //TODO ZORAN 590 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 /*** 613 * Method importValue reads values from desired XML tag and puts them into Vector. 614 * @param doc Parsed import XML file. 615 * @param tagName The name of XML tag. 616 * @param strAttrName The name of tag attribute which reads input strValue. 617 * @param iImportJobItem Number of ImportDefinition tag which is processed. 618 * @param defaultValue The default value of strattrname attribute. 619 * @return Vector of importing values. 620 */ 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 /*** 651 * Method importSubCounterKeyValue reads values from strAttrName attribute 652 * XML tag and puts them into Vector. 653 * @param doc Parsed import XML file. 654 * @param strAttrName The name of tag attribute which reads input strValue. 655 * @param iImportJobItem Number of ImportDefinition tag which is processed. 656 * @param iSubCounterItem Number of subCounterColumn tag which is processed. 657 * @return Vector of importing values. 658 */ 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 // /*** 695 // * This method check the update operation 696 // * @param tableName is table name 697 // * @param tableID is table ID 698 // * @param conn is connection 699 // * @return true if update operation OK, false otherwise 700 // * @throws SQLException 701 // */ 702 // public boolean updateCounter(String tableName, String tableID, 703 // Connection conn) throws SQLException { 704 // 705 // String strQuery = null; 706 // 707 // Vector counterNames = this.getCounterName(tableName, tableID); 708 // Vector counterValues = this.getCounterValue(tableName, tableID); 709 // Vector counterIncrements = this.getCounterIncrement(tableName, tableID); 710 // for (int i = 0; i < counterNames.size(); i++) { 711 // Statement stmt = conn.createStatement(); 712 // BigDecimal newValue = new BigDecimal(counterValues.get(i).toString()); 713 // strQuery = queryUpdateCounter(counterNames.get(i).toString(), newValue); 714 // stmt.executeUpdate(strQuery); 715 // stmt.close(); 716 // } 717 // return true; 718 // } 719 720 /*** 721 * This method check the update operation 722 * @param conn is connection 723 * @throws SQLException 724 */ 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 /*** 744 * This method sets the value of counter parameter 745 * @param tableName is table name 746 * @param tableID is table ID 747 * @param conn is connection 748 * @return true 749 * @throws SQLException 750 */ 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 /*** 769 * This method read value of counter parameter 770 * @param tableName is table name 771 * @param tableID is table ID 772 * @param conn is connection 773 * @param firstColumn is first column 774 * @return true 775 * @throws SQLException 776 */ 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 /*** 811 * 812 * @param counterColumnName is name of the column 813 * @return query 814 */ 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 /*** 824 * 825 * @param counterColumnName is column name 826 * @param counterColumnValue is column value 827 * @return query 828 */ 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 /*** 839 * 840 * @param counterColumnName is colulmn name 841 * @param newValue is column value 842 * @return query 843 */ 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 /*** 854 * This method read value of sub counter parameter 855 * @param tableName is table name 856 * @param tableID is table ID 857 * @param conn is connection 858 * @param firstColumn is first column 859 * @return vector 860 * @throws SQLException 861 */ 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 //ZK change this from CheckType to targetConfigReader 886 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 //caching the subcounter value 906 Object obj = subCounterCache.get(strQuery); 907 if (obj == null) { //query isn't cached before 908 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 { //firstColumnResult==1 921 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 { //query is cached before 936 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 {//tableMode=query 942 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 { //firstColumnResult==1 955 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 /*** 977 * This method reset cach for subcounter 978 */ 979 public void resetSubCounterCache(){ 980 if(this.subCounterCache.size()>0) 981 this.subCounterCache.clear(); 982 } 983 984 985 /*** 986 * Method importAttributeValue reads value for strAttrName attribute in strTagName tag. 987 * This method return this value. 988 * @param doc Parsed import XML file. 989 * @param strTagName The name of tag where attribute is situated. 990 * @param strAttrName The name of tag attribute which reads input value. 991 * @param iImportJobItem Number of ImportDefinition tag which is processed. 992 * @return String - importing value. 993 */ 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 // NodeList tag = docFragment.getElementsByTagName(tagName); 1001 // for (int i = 0; i < tag.getLength(); i++) { 1002 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 /*** 1014 * Method counterColumnTypes is used to put types of constant columns into 1015 * global vector sorted in target tables. If there is an error, Exception 1016 * "SQLException" or "NullPointerException" is thrown. 1017 * @param c Connection to target database. 1018 * @param tableName is table name 1019 * @param tableID is table ID 1020 * @param firstColumn is first column 1021 * @throws SQLException Constructs an SQLException object with a reason. 1022 * @throws NullPointerException Constructs a NullPointerException with the specified detail message. 1023 */ 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 //ZK change this. Because of problems with getColumnTypeName()method. Some drivers doesn't support it. 1044 //start 1045 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{//TODO ZK ADDED stmtConstant.setMaxRows(1). Place this as parameter in conf file, like maxRowsSuported 1061 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 /*** 1089 * This method reset all variables 1090 */ 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 }

This page was automatically generated by Maven