001: /*
002: Loader - tool for transfering data from one JDBC source to another and
003: doing transformations during copy.
004: Copyright (C) 2002-2003 Together
005: This library is free software; you can redistribute it and/or
006: modify it under the terms of the GNU Lesser General Public
007: License as published by the Free Software Foundation; either
008: version 2.1 of the License, or (at your option) any later version.
009: This library is distributed in the hope that it will be useful,
010: but WITHOUT ANY WARRANTY; without even the implied warranty of
011: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: Lesser General Public License for more details.
013: You should have received a copy of the GNU Lesser General Public
014: License along with this library; if not, write to the Free Software
015: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
016: Loader.java
017: Date: 03.03.2003.
018: @version 2.1 alpha
019: @author:
020: Radoslav Dutina rale@prozone.co.yu
021: */
022:
023: package org.webdocwf.util.loader;
024:
025: import java.util.*;
026: import java.sql.*;
027: import org.webdocwf.util.loader.logging.*;
028:
029: /**
030: *
031: * CheckRowCache class is used to cache queries which will be sent to target
032: * database to check if some row exists
033: * @author Radoslav Dutina
034: * @version 1.0
035: */
036: public class CheckRowCache {
037: private Hashtable rowExist = new Hashtable();
038: private Hashtable rowVersionValue = new Hashtable();
039:
040: private String currentKey = "";
041: private Logger logger;
042: private int currentVersion = 0;
043:
044: /**
045: * This method is use to mark row if he is cahced
046: */
047: // public void setCheckRowValue() {
048: // new Throwable().printStackTrace();
049: // if (!this.currentKey.equalsIgnoreCase("")) {
050: //
051: // rowExist.put(this.currentKey, "true");
052: // }
053: // }
054: /**
055: * This method set value of cached row
056: * @param val is value of cached row
057: */
058: // public void setCheckRowVersionValue(String val) {
059: // if (!this.currentKey.equalsIgnoreCase("")) {
060: // rowVersionValue.put(this.currentKey, val);
061: // }
062: // }
063: /**
064: * This method is use to retrive value of cached row
065: * @return value of cache row
066: */
067: public int getCheckRowVersionValue() {
068: String ret = (String) rowVersionValue.get(this .currentKey);
069: return Integer.parseInt(ret);
070: }
071:
072: /**
073: * This method is use to retrive value of some sql query object
074: * @param key defines string representation of query object
075: * @param conn defines connection to target database
076: * @param iTargetFirstColumnResult is configuration parameter
077: * @return value of selected row
078: * @throws java.lang.Exception
079: */
080: private int numOfChecks = 0;
081: private int numCacheHits = 0;
082:
083: public boolean getCheckRowValue(String key, Connection conn,
084: int iTargetFirstColumnResult, String versionColumnName)
085: throws Exception {
086:
087: boolean retVal = false;
088: this .currentKey = key;
089: Object obj = rowExist.get(key);
090:
091: numOfChecks++;
092:
093: if (obj == null) {
094: this .logger.write("full", "\tQuery '" + key
095: + "' will be executed");
096: try {
097: Statement stmtCheckTarget = conn.createStatement();
098: ResultSet rsetCheckTarget = stmtCheckTarget
099: .executeQuery(key);
100: if (rsetCheckTarget.next()) { //update row mode
101: // this.setCheckRowValue();
102: rowExist.put(this .currentKey, "true");
103: if (versionColumnName != null) {
104: currentVersion = rsetCheckTarget
105: .getInt(versionColumnName);
106: rowVersionValue.put(this .currentKey, String
107: .valueOf(currentVersion));
108: // this.setCheckRowVersionValue(String.valueOf(this.currentVersion));
109: }
110: rsetCheckTarget.close();
111: stmtCheckTarget.close();
112: retVal = true;
113: } else {//insert row mode
114: // this.setCheckRowVersionValue(String.valueOf(0));
115: rowVersionValue.put(currentKey, "0");
116: rsetCheckTarget.close();
117: stmtCheckTarget.close();
118: retVal = false;
119: }
120: } catch (Exception ex) {
121: this .logger.write("full", ex.getMessage());
122: throw ex;
123: }
124: } else { //update row mode
125: numCacheHits++;
126: retVal = true;
127: }
128:
129: //this.logger.write("normal", "\t numOfCheck = '" + numOfChecks);
130: //this.logger.write("normal", "\t numCacheHits = '" + numCacheHits);
131: //this.logger.write("normal", "\t this.rowExist size = '" + this.rowExist.size());
132:
133: return retVal;
134: }
135:
136: /**
137: * This method is use to reset all parameters which are used in this class
138: */
139: public void resetCheckRowCache() {
140: rowExist.clear();
141: rowVersionValue.clear();
142: currentKey = "";
143: }
144:
145: /**
146: * This method is use to set logger object
147: * @param logger is current logger
148: */
149: public void setLogger(Logger logger) {
150: this .logger = logger;
151: }
152:
153: /**
154: * This method is use to retrive value of currentVersion parameter
155: * @return value of parameter
156: */
157: public int getCurrentVersion() {
158: return this.currentVersion;
159: }
160:
161: }
|