1 29 package org.webdocwf.util.loader; 30 31 import java.sql.Connection ; 32 import java.sql.ResultSet ; 33 import java.sql.SQLException ; 34 import java.sql.Statement ; 35 import java.util.Calendar ; 36 import java.util.Date ; 37 import java.util.Hashtable ; 38 39 import org.webdocwf.util.loader.logging.Logger; 40 41 47 public class DataCleaning { 48 49 private Logger logger; 50 private Statement stmt; 51 private ResultSet rsetTarget; 52 private ResultSet rs; 53 private Hashtable colNamesDataTypes = new Hashtable (); 54 private Hashtable colNamesDataLenght = new Hashtable (); 55 private String currentTableName = ""; 56 57 private Hashtable colNamesDataTypesUpdate = new Hashtable (); 58 private Hashtable colNamesDataLenghtUpdate = new Hashtable (); 59 private ResultSet rsUpdate; 60 61 private String logTableName = "LOGTABLENAME"; 63 private String logTable = "LOGTABLE"; 64 private String logColumnName = "LOGCOLUMNNAME"; 65 private String logRowNumber = "LOGROWNUMBER"; 66 private String logOriginalValue = "LOGORIGINALVALUE"; 67 private String logNewValue = "LOGNEWVALUE"; 68 private String logImportDefinitionName = "LOGIMPORTDEFINITIONNAME"; 69 private String logOperationName = "LOGOPERATIONNAME"; 70 private String logTypeName = "LOGTYPENAME"; 71 private String logTime = "LOGTIME"; 73 private String logFailedStatement = "LOGFAILEDSTATEMENT"; 74 private boolean logTableExists = false; 76 private boolean checkIsDone = false; 77 78 private ConfigReader configReader; 79 80 83 public DataCleaning(ConfigReader configReader) { 84 this.configReader = configReader; 85 } 86 87 91 public void setLogTableName(String logTableName) { 92 this.logTableName = logTableName; 93 } 94 95 99 public void setLogTable(String logTable) { 100 this.logTable = logTable; 101 } 102 103 107 public void setLogColumnName(String logColumnName) { 108 this.logColumnName = logColumnName; 109 } 110 111 115 public void setLogRowNumber(String logRowNumber) { 116 this.logRowNumber = logRowNumber; 117 } 118 119 123 public void setLogOriginalValue(String logOriginalValue) { 124 this.logOriginalValue = logOriginalValue; 125 } 126 127 131 public void setLogNewValue(String logNewValue) { 132 this.logNewValue = logNewValue; 133 } 134 135 139 public void setLogImportDefinitionName(String logImportDefinitionName) { 140 this.logImportDefinitionName = logImportDefinitionName; 141 } 142 143 147 public void setLogOperationName(String logOperationName) { 148 this.logOperationName = logOperationName; 149 } 150 151 155 public void setLogTypeName(String logTypeName) { 156 this.logTypeName = logTypeName; 157 } 158 159 163 public String getLogTableName() { 164 return this.logTableName; 165 } 166 167 171 public String getLogTable() { 172 return this.logTable; 173 } 174 175 179 public String getLogColumnName() { 180 return this.logColumnName; 181 } 182 183 187 public String getLogRowNumber() { 188 return this.logRowNumber; 189 } 190 191 195 public String getLogOriginalValue() { 196 return this.logOriginalValue; 197 } 198 199 203 public String getLogNewValue() { 204 return this.logNewValue; 205 } 206 207 211 public String getLogImportDefinitionName() { 212 return this.logImportDefinitionName; 213 } 214 215 219 public String getLogOperationName() { 220 return this.logOperationName; 221 } 222 223 227 public String getLogTypeName() { 228 return this.logTypeName; 229 } 230 231 240 public void cleaningInsert(String tableName, Connection conn, int rowNumber, String typeOfInsert, String msg, String importDefinitionName, String logFailedStatement) { 241 this.logger.write("full", "\tBecause DataCleaning features is turn on, some replacement were made:"); 242 this.logger.write("full", "\t " + typeOfInsert + ":FAILS: in table " + tableName + ", in row " + (rowNumber + 1) + " . " + msg); 243 String newLogFailedStatement =logFailedStatement.replaceAll("'",""); 244 String logTime = ""; 245 246 try { 247 logTime = getCurrentTime(); 248 String [] types = { "TABLE" }; 249 String catalogName = conn.getCatalog(); 250 stmt = conn.createStatement(); 251 msg = Utils.replaceAll(msg, "'", "''"); 252 ResultSet check = null; 253 try { 254 if (!checkIsDone) { 255 this.logTableExists = true; 256 Statement stmtt = conn.createStatement(); 257 try { 258 stmtt.executeQuery("SELECT * FROM "+getLogTableName()); 259 } catch (Exception e) { 260 this.logTableExists = false; 261 } 262 this.checkIsDone = true; 263 stmtt.close(); 264 270 } 271 } catch (UnsupportedOperationException ex) { 272 String message = "Error while trying to get meta data from target table." + "\n" + "\tMethod getMetaDatata().getTables() is not supported."; 273 throw new SQLException (message); 274 } 275 276 if (this.logTableExists) { 277 278 stmt.executeUpdate("INSERT INTO " + getLogTableName() + " (" 279 + getLogImportDefinitionName() + ", " + getLogOperationName() 280 + ", " + getLogTypeName() + ", " + getLogTable() + ", " 281 + getLogColumnName() + ", " + getLogRowNumber() + ", " 282 + getLogOriginalValue() + ", " + getLogNewValue() + ", " 283 + getLogFailedStatement() +", " +getLogTime()+ ")" 284 + " VALUES (" + "'" + importDefinitionName + "','" + typeOfInsert 285 + "','ERROR','" + tableName + "','','" + (rowNumber + 1) 286 + "','" + msg + "','" + typeOfInsert + " FAILS'" + ","+"'"+newLogFailedStatement+"'"+"," +"'" +logTime+"'" +")"); 287 288 } else { 289 this.logger.write("full", "\t " + getLogTableName() + " does not exists, or is invalid!"); 290 } 291 stmt.close(); 293 } catch (SQLException ex) { 294 295 this.logger.write("full", "\t Error: " + ex.getMessage()); 296 } 297 298 } 299 303 private String getCurrentTime() { 304 String logTime; 305 Calendar time = Calendar.getInstance(); 306 Date dateAndTime = time.getTime(); 307 logTime = dateAndTime.toString(); 308 return logTime; 309 } 310 311 321 public void cleaningColumnValues(String tableName, String columnName, String replacement, int rowNumber, Connection conn, String typeOfInsert, String importDefinitionName) { 322 this.logger.write("full", "\tBecause DataCleaning features is turn on, some replacement were made:"); 323 this.logger.write("full", "\t " + typeOfInsert + ":REPLACED VALUES: In table " + tableName + ", column " + columnName + ", value 'null' were replaced"); 324 this.logger.write("full", "\t with value '" + replacement + "' (row number " + (rowNumber + 1) + ")"); 325 String logTime = ""; 326 String newLogFailedStatement =logFailedStatement.replaceAll("'",""); 327 328 try { 329 330 logTime = getCurrentTime(); 331 String [] types = { "TABLE" }; 332 String catalogName = conn.getCatalog(); 333 stmt = conn.createStatement(); 334 ResultSet check = null; 335 try { 336 if (!checkIsDone) { 337 this.logTableExists = true; 338 Statement stmtt = conn.createStatement(); 339 try { 340 stmtt.executeQuery("SELECT * FROM "+getLogTableName()); 341 } catch (Exception e) { 342 this.logTableExists = false; 343 } 344 this.checkIsDone = true; 345 stmtt.close(); 346 352 } 353 } catch (UnsupportedOperationException ex) { 354 String message = "Error while trying to get meta data from target table."; 355 throw new SQLException (message); 356 } 357 358 if (this.logTableExists) { 359 stmt.executeUpdate("INSERT INTO " + getLogTableName() + " (" 360 + getLogImportDefinitionName() + ", " + getLogOperationName() 361 + ", " + getLogTypeName() + ", " + getLogTable() + ", " 362 + getLogColumnName() + ", " + getLogRowNumber() + ", " 363 + getLogOriginalValue() + ", " + getLogNewValue() + ", " 364 + getLogFailedStatement() +", " +getLogTime()+ ")" 365 + " VALUES (" + "'" + importDefinitionName + "','" + typeOfInsert 366 + "','REPLACE NULL VALUES','" + tableName + "','" 367 + columnName + "','" + (rowNumber + 1) + "','" + "null" 368 + "','" + replacement + "'" + ","+"'"+newLogFailedStatement+"'"+","+"'"+logTime+"'"+")"); 369 } else 370 this.logger.write("full", "\t " + getLogTableName() + " does not exists, or is invalid!"); 371 stmt.close(); 373 } catch (SQLException ex) { 374 this.logger.write("full", "\t Error:" + ex.getMessage()); 375 } 376 } 377 378 393 public String cleaningRelationValues(String tableName, String columnName, String replacement, String dataType, int rowNumber, Connection conn, String typeOfInsert, int currentVersion, boolean oid, String importDefinitionName, String versionColumnName, String logFailedStatement) throws LoaderException { 394 395 String [] types = { "TABLE" }; 396 String relValue = ""; 397 String addins = ""; 398 String prefix = ""; 399 String oidVersion = ""; 400 String newLogFailedStatement =logFailedStatement.replaceAll("'",""); 401 402 String logTime = ""; 403 404 if (oid) { 405 oidVersion = ", " + versionColumnName + "=" + currentVersion; 407 } 408 if (typeOfInsert.equalsIgnoreCase("update")) { 409 addins = oidVersion + " where "; 410 prefix = " = "; 411 } else { 412 addins = ""; 413 } 414 try { 415 logTime = getCurrentTime(); 416 String catalogName = conn.getCatalog(); 417 stmt = conn.createStatement(); 418 ResultSet relations = null; 419 ResultSet check = null; 420 421 try { 422 relations = conn.getMetaData().getColumns(catalogName, null, tableName, "%"); 423 424 if (!checkIsDone) { 425 this.logTableExists = true; 426 Statement stmtt = conn.createStatement(); 427 try { 428 stmtt.executeQuery("SELECT * FROM "+getLogTableName()); 429 } catch (Exception e) { 430 this.logTableExists = false; 431 } 432 this.checkIsDone = true; 433 stmtt.close(); 434 440 } 441 } catch (UnsupportedOperationException ex) { 442 String message = "Error while trying to get meta data from target table."; 443 throw new SQLException (message); 444 } 445 446 while (relations.next()) { 447 448 if (columnName.equalsIgnoreCase(relations.getString(4))) { 449 450 451 453 try { 454 if (configReader.isNumber(dataType)) { 455 relValue = prefix + replacement + addins; 456 } else { 457 relValue = prefix + "'" + replacement + "'" + addins; 458 } 459 } catch (LoaderException e) { 460 461 LoaderException le = new LoaderException("Exception:", e); 462 throw le; 463 } 464 465 if (this.logTableExists) 466 467 stmt.executeUpdate("INSERT INTO " + getLogTableName() + " (" 468 + getLogImportDefinitionName() + ", " + getLogOperationName() 469 + ", " + getLogTypeName() + ", " + getLogTable() + ", " 470 + getLogColumnName() + ", " + getLogRowNumber() + ", " 471 + getLogOriginalValue() + ", " + getLogNewValue() + ", " 472 + getLogFailedStatement() +", " +getLogTime()+ ")" 473 + " VALUES (" + "'" + importDefinitionName + "','" + typeOfInsert 474 + "','CLEANING RELATIONS','" + tableName + "','" + columnName 475 + "','" + (rowNumber + 1) + "'," + "null,'" + replacement + "'" + ","+"'"+newLogFailedStatement+"'"+","+"'"+logTime+"'"+")"); 476 477 else { 478 479 this.logger.write("full", "\t " + getLogTableName() + " does not exists, or is invalid!"); 480 } 481 this.logger.write("full", "\tBecause DataCleaning features is turn on, some replacement were made:"); 482 this.logger.write("full", "\t " + typeOfInsert + ":RELATIONS: In table " + tableName + ", column " + columnName + ", value null were replaced"); 483 this.logger.write("full", "\t with value " + replacement + " (row number " + (rowNumber + 1) + ")"); 484 485 } 486 } 487 relations.close(); 488 stmt.close(); 489 } catch (SQLException ex) { 490 this.logger.write("full", "\t Error:" + ex.getMessage()); 491 } 492 return relValue; 493 } 494 495 506 public void cutingDataLenghtUpdate(String tableName, Connection conn, String strQuery, int rowNumber, String update, String onErrorContinue, String importDefinitionName, String logFailedStatement) throws SQLException , LoaderException { 507 508 boolean end = false; 509 boolean endTemp = false; 510 SqlParser sqlParser = new SqlParser(); 511 String newLogFailedStatement =logFailedStatement.replaceAll("'",""); 512 String logTime = ""; 513 try { 514 logTime = getCurrentTime(); 515 try { 516 sqlParser.parse(update + strQuery); 517 } catch (Exception e) { 518 throw new SQLException (e.getMessage()); 519 } 520 String [] columnNames = sqlParser.getColumnNames(); 521 String [] columnValues = sqlParser.getColumnValues(); 522 523 String [] types = { "TABLE" }; 524 String catalogName = conn.getCatalog(); 525 stmt = conn.createStatement(); 526 527 if (!this.currentTableName.equalsIgnoreCase(tableName)) { 528 try { 529 rsUpdate = conn.getMetaData().getColumns(catalogName, null, tableName, "%"); 530 } catch (UnsupportedOperationException ex) { 531 String message = "Error while trying to get meta data from target table."; 532 throw new SQLException (message); 533 } 534 colNamesDataTypesUpdate.clear(); 535 colNamesDataLenghtUpdate.clear(); 536 this.currentTableName = tableName; 537 while (rsUpdate.next()) { 538 String columnName = rsUpdate.getString(4).toUpperCase(); 539 colNamesDataTypesUpdate.put(columnName, rsUpdate.getString(6)); 540 colNamesDataLenghtUpdate.put(columnName, rsUpdate.getString(7)); 541 } 542 rsUpdate.close(); 543 } 544 545 for (int i = 0; i < columnNames.length; i++) { 546 String dataNameInQyery = columnNames[i]; 547 String dataValueInQyery = columnValues[i]; 548 String dataTypeName = (String ) colNamesDataTypesUpdate.get(dataNameInQyery.toUpperCase()); 549 String dataTypeLenght = (String ) colNamesDataLenghtUpdate.get(dataNameInQyery.toUpperCase()); 550 if (!dataValueInQyery.equalsIgnoreCase("null")) { 551 try { 553 554 if (!configReader.isNumber(dataTypeName)) { 555 if ((dataValueInQyery.length()) > Integer.parseInt(dataTypeLenght)) { 556 String original = dataValueInQyery.substring(0, dataValueInQyery.length()); 557 String replacement = replaceQuote(original, Integer.parseInt(dataTypeLenght)); 558 update = replaceFirst(update, dataValueInQyery, replacement); 559 ResultSet check = null; 560 try { 561 if (!checkIsDone) { 562 this.logTableExists = true; 563 Statement stmtt = conn.createStatement(); 564 try { 565 stmtt.executeQuery("SELECT * FROM "+getLogTableName()); 566 } catch (Exception e) { 567 this.logTableExists = false; 568 } 569 this.checkIsDone = true; 570 stmtt.close(); 571 577 } 578 } catch (UnsupportedOperationException ex) { 579 String message = "Error while trying to get meta data from target table."; 580 throw new SQLException (message); 581 } 582 583 if (this.logTableExists) { 584 stmt.executeUpdate("INSERT INTO " + getLogTableName() 585 + " (" + getLogImportDefinitionName() + ", " 586 + getLogOperationName() + ", " + getLogTypeName() 587 + ", " + getLogTable() + ", " + getLogColumnName() 588 + ", " + getLogRowNumber() + ", " + getLogOriginalValue() 589 + ", " + getLogNewValue() + ", " + getLogFailedStatement() 590 +", " +getLogTime()+ ")" 591 + " VALUES (" + "'" 592 + importDefinitionName + "','UPDATE','CUT OFF DATA','" 593 + tableName + "','" + dataNameInQyery + "','" 594 + (rowNumber + 1) + "','" + original + "','" 595 + replacement + "'" + ","+"'"+newLogFailedStatement+"'"+","+"'"+logTime+"'"+")"); 596 597 } else { 598 this.logger.write("full", "\t " + getLogTableName() + " does not exists, or is invalid"); 599 } 600 this.logger.write("full", "\t Because dataCutOff attribute is true, some replacement were made:"); 601 this.logger.write("full", "\t UPDATE:CutOff: In table " + tableName + ", column " + dataNameInQyery + ", value " + dataValueInQyery + " were replaced"); 602 this.logger.write("full", "\t with value " + replacement + " (row number " + (rowNumber + 1) + ")"); 603 604 } 605 } 606 } catch (LoaderException e) { 607 LoaderException le = new LoaderException("Exception:", (Throwable ) e); 608 throw le; 609 610 } 611 } 612 } 613 stmt.executeUpdate(update + strQuery); 614 stmt.close(); 615 616 } catch (SQLException ex) { 617 if (onErrorContinue.equalsIgnoreCase("true")) 618 cleaningInsert(tableName, conn, rowNumber, "UPDATE", "Update fails. Message: " + ex.getMessage(), importDefinitionName,logFailedStatement); 619 else { 620 LoaderException le = new LoaderException("SQLException: ", (Throwable ) ex); 621 this.logger.write("full", "\tError in SQL statement: " + le.getCause()); 622 throw ex; 624 625 } 626 } 627 } 628 629 640 public void cutingDataLenght(String tableName, Connection conn, String strQuery, int rowNumber, String onErrorContinue, String msg, String importDefinitionName, String logFailedStatement) throws SQLException { 641 boolean end = false; 642 boolean endTemp = false; 643 SqlParser sqlParser = new SqlParser(); 644 String newLogFailedStatement =logFailedStatement.replaceAll("'",""); 645 String logTime = ""; 646 try { 647 logTime = getCurrentTime(); 648 try { 649 sqlParser.parse(strQuery); 650 } catch (Exception e) { 651 throw new SQLException (e.getMessage()); 652 } 653 String [] columnNames = sqlParser.getColumnNames(); 654 String [] columnValues = sqlParser.getColumnValues(); 655 656 String [] types = { "TABLE" }; 657 String catalogName = conn.getCatalog(); 658 stmt = conn.createStatement(); 659 660 if (!this.currentTableName.equalsIgnoreCase(tableName)) { 661 try { 662 rs = conn.getMetaData().getColumns(catalogName, null, tableName, "%"); 663 } catch (UnsupportedOperationException ex) { 664 String message = "Error while trying to get meta data from target table."; 665 throw new SQLException (message); 666 } 667 colNamesDataTypes.clear(); 668 colNamesDataLenght.clear(); 669 this.currentTableName = tableName; 670 while (rs.next()) { 671 String columnName = rs.getString(4).toUpperCase(); 672 colNamesDataTypes.put(columnName, rs.getString(6)); 673 colNamesDataLenght.put(columnName, rs.getString(7)); 674 } 675 } 676 677 for (int i = 0; i < columnNames.length; i++) { 678 String dataNameInQyery = columnNames[i]; 679 String dataValueInQyery = columnValues[i]; 680 String dataTypeName = (String ) colNamesDataTypes.get(dataNameInQyery.toUpperCase()); 681 String dataTypeLenght = (String ) colNamesDataLenght.get(dataNameInQyery.toUpperCase()); 682 if (!dataValueInQyery.equalsIgnoreCase("null")) { 683 685 try { 686 if (!configReader.isNumber(dataTypeName)) { 687 if ((dataValueInQyery.length()) > Integer.parseInt(dataTypeLenght)) { 688 String original = dataValueInQyery.substring(0, dataValueInQyery.length()); 689 String replacement = replaceQuote(original, Integer.parseInt(dataTypeLenght)); 690 strQuery = replaceFirst(strQuery, dataValueInQyery, replacement); 691 ResultSet check = null; 692 try { 693 if (!checkIsDone) { 694 this.logTableExists = true; 695 Statement stmtt = conn.createStatement(); 696 try { 697 stmtt.executeQuery("SELECT * FROM "+getLogTableName()); 698 } catch (Exception e) { 699 this.logTableExists = false; 700 } 701 this.checkIsDone = true; 702 stmtt.close(); 703 709 } 710 } catch (UnsupportedOperationException ex) { 711 String message = "Error while trying to get meta data from target table."; 712 throw new SQLException (message); 713 } 714 715 if (this.logTableExists) { 716 stmt.executeUpdate("INSERT INTO " + getLogTableName() 717 + " (" + getLogImportDefinitionName() + ", " 718 + getLogOperationName() + ", " + getLogTypeName() 719 + ", " + getLogTable() + ", " + getLogColumnName() 720 + ", " + getLogRowNumber() + ", " + getLogOriginalValue() 721 + ", " + getLogNewValue() + ", " + getLogFailedStatement() 722 +", " +getLogTime() 723 + ")" + " VALUES (" + "'" 724 + importDefinitionName + "','INSERT','CUT OFF DATA','" 725 + tableName + "','" + dataNameInQyery + "','" + (rowNumber + 1) 726 + "','" + original + "','" + replacement + "'" + ","+"'"+newLogFailedStatement+"'"+","+"'"+logTime+"'"+")"); 727 728 } else { 729 this.logger.write("full", "\t " + getLogTableName() + " does not exists, or is invalid"); 730 731 } 732 this.logger.write("full", "\t Because dataCutOff attribute is true, some replacement were made:"); 734 this.logger.write("full", "\t INSERT:CutOff: In table " + tableName + ", column " + dataNameInQyery + ", value " + dataValueInQyery + " were replaced"); 735 this.logger.write("full", "\t with value " + replacement + " (row number " + (rowNumber + 1) + ")"); 736 } 737 } 738 739 } catch (LoaderException e) { 740 LoaderException le = new LoaderException("Exception:", (Throwable ) e); 741 742 } 743 } 744 } 745 746 stmt.executeUpdate(strQuery); 747 stmt.close(); 748 } catch (SQLException ex) { 749 if (onErrorContinue.equalsIgnoreCase("true")) 750 cleaningInsert(tableName, conn, rowNumber, "INSERT", msg, importDefinitionName,logFailedStatement); 751 else { 752 LoaderException le = new LoaderException("SQLException: ", (Throwable ) ex); 753 this.logger.write("full", "\tError in SQL statement: " + le.getCause()); 754 throw ex; 756 757 } 758 } 759 } 760 761 private String replaceQuote(String replacement, int length) { 762 replacement = Utils.replaceAll(replacement, "''", "'"); 763 replacement = replacement.substring(0, length); 764 int index = replacement.indexOf("'"); 765 if (index != -1) { 766 replacement = Utils.replaceAll(replacement, "'", "''"); 767 } 768 return replacement; 769 } 770 771 private String replaceFirst(String input, String forReplace, String replaceWith) { 772 String retVal = input; 773 int start = input.indexOf(forReplace); 774 int end = start + forReplace.length(); 775 if (start != -1) { 776 retVal = input.substring(0, start) + replaceWith + input.substring(end); 777 } 778 return retVal; 779 } 780 781 785 public void setLogger(Logger logger) { 786 this.logger = logger; 787 } 788 792 public void setLogPrimaryKeyValue(String string) { 793 this.logFailedStatement = string; 794 } 795 796 800 public void setLogTime(String string) { 801 this.logTime = string; 802 } 803 804 808 public String getLogFailedStatement() { 809 return this.logFailedStatement; 810 } 811 812 816 public String getLogTime() { 817 return this.logTime; 818 } 819 820 } | Popular Tags |