View Javadoc
1 /* 2 Loader - tool for transfering data from one JDBC source to another and 3 doing transformations during copy. 4 Copyright (C) 2002-2003 Together 5 This library is free software; you can redistribute it and/or 6 modify it under the terms of the GNU Lesser General Public 7 License as published by the Free Software Foundation; either 8 version 2.1 of the License, or (at your option) any later version. 9 This library is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 You should have received a copy of the GNU Lesser General Public 14 License along with this library; if not, write to the Free Software 15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 16 Loader.java 17 Date: 03.03.2003. 18 @version 2.1 alpha 19 @author: 20 Radoslav Dutina rale@prozone.co.yu 21 */ 22 23 package org.webdocwf.util.loader; 24 25 import java.util.*; 26 import java.sql.*; 27 import org.webdocwf.util.loader.logging.*; 28 29 /*** 30 * 31 * CheckRowCache class is used to cache queries which will be sent to target 32 * database to check if some row exists 33 * @author Radoslav Dutina 34 * @version 1.0 35 */ 36 public class CheckRowCache { 37 private Hashtable rowExist = new Hashtable(); 38 private Hashtable rowVersionValue = new Hashtable(); 39 40 private String currentKey = ""; 41 private Logger logger; 42 private int currentVersion = 0; 43 44 45 /*** 46 * This method is use to mark row if he is cahced 47 */ 48 // public void setCheckRowValue() { 49 // new Throwable().printStackTrace(); 50 // if (!this.currentKey.equalsIgnoreCase("")) { 51 // 52 // rowExist.put(this.currentKey, "true"); 53 // } 54 // } 55 56 /*** 57 * This method set value of cached row 58 * @param val is value of cached row 59 */ 60 // public void setCheckRowVersionValue(String val) { 61 // if (!this.currentKey.equalsIgnoreCase("")) { 62 // rowVersionValue.put(this.currentKey, val); 63 // } 64 // } 65 66 /*** 67 * This method is use to retrive value of cached row 68 * @return value of cache row 69 */ 70 public int getCheckRowVersionValue() { 71 String ret = (String)rowVersionValue.get(this.currentKey); 72 return Integer.parseInt(ret); 73 } 74 75 /*** 76 * This method is use to retrive value of some sql query object 77 * @param key defines string representation of query object 78 * @param conn defines connection to target database 79 * @param iTargetFirstColumnResult is configuration parameter 80 * @return value of selected row 81 * @throws java.lang.Exception 82 */ 83 private int numOfChecks = 0; 84 private int numCacheHits = 0; 85 public boolean getCheckRowValue(String key, Connection conn, 86 int iTargetFirstColumnResult, String versionColumnName) throws Exception { 87 88 boolean retVal = false; 89 this.currentKey = key; 90 Object obj = rowExist.get(key); 91 92 numOfChecks++; 93 94 if (obj == null) { 95 this.logger.write("full", "\tQuery '" + key + "' will be executed"); 96 try { 97 Statement stmtCheckTarget = conn.createStatement(); 98 ResultSet rsetCheckTarget = stmtCheckTarget.executeQuery(key); 99 if (rsetCheckTarget.next()) { //update row mode 100 // this.setCheckRowValue(); 101 rowExist.put(this.currentKey, "true"); 102 if (versionColumnName != null) { 103 currentVersion = rsetCheckTarget.getInt(versionColumnName); 104 rowVersionValue.put(this.currentKey, String.valueOf(currentVersion)); 105 // this.setCheckRowVersionValue(String.valueOf(this.currentVersion)); 106 } 107 rsetCheckTarget.close(); 108 stmtCheckTarget.close(); 109 retVal = true; 110 } else {//insert row mode 111 // this.setCheckRowVersionValue(String.valueOf(0)); 112 rowVersionValue.put(currentKey, "0"); 113 rsetCheckTarget.close(); 114 stmtCheckTarget.close(); 115 retVal = false; 116 } 117 } 118 catch (Exception ex) { 119 this.logger.write("full", ex.getMessage()); 120 throw ex; 121 } 122 } else { //update row mode 123 numCacheHits++; 124 retVal = true; 125 } 126 127 //this.logger.write("normal", "\t numOfCheck = '" + numOfChecks); 128 //this.logger.write("normal", "\t numCacheHits = '" + numCacheHits); 129 //this.logger.write("normal", "\t this.rowExist size = '" + this.rowExist.size()); 130 131 return retVal; 132 } 133 134 /*** 135 * This method is use to reset all parameters which are used in this class 136 */ 137 public void resetCheckRowCache() { 138 rowExist.clear(); 139 rowVersionValue.clear(); 140 currentKey = ""; 141 } 142 143 /*** 144 * This method is use to set logger object 145 * @param logger is current logger 146 */ 147 public void setLogger(Logger logger) { 148 this.logger = logger; 149 } 150 151 /*** 152 * This method is use to retrive value of currentVersion parameter 153 * @return value of parameter 154 */ 155 public int getCurrentVersion() { 156 return this.currentVersion; 157 } 158 159 }

This page was automatically generated by Maven