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.sql.*;
026: import java.math.BigDecimal;
027: import org.webdocwf.util.loader.logging.*;
028:
029: /**
030: *
031: * <p>DataTransmition class is used for handling the operation with ‘restore counter table’. </p>
032: * @author Radoslav Dutina
033: * @version 1.0
034: */
035: public class DataTransmition {
036: private ImportDefinitionElement importDefinitionElement;
037: private Logger logger;
038:
039: public DataTransmition(
040: ImportDefinitionElement importDefinitionElement) {
041: this .importDefinitionElement = importDefinitionElement;
042: }
043:
044: /**
045: * This method create restore counter table
046: * @param conn defines the connection object of target database
047: * @throws LoaderException
048: */
049: public void createRestartCounterTable(Connection conn)
050: throws LoaderException {
051:
052: this .logger.write("full",
053: "\tcreateRestartCounterTable method is started.");
054: try {
055: Statement stmt;
056: stmt = conn.createStatement();
057:
058: String sqlStmt = "create table "
059: + this .importDefinitionElement.strRestartCounterTableName
060: + " ("
061: + this .importDefinitionElement.strRestartCounterImportDefinitionName
062: + " VARCHAR(50) NOT NULL, "
063: + this .importDefinitionElement.strRestartCounterValue
064: + " DECIMAL(19,0));";
065:
066: this .logger.write("full", "\tQuery '" + sqlStmt
067: + "' will be executed");
068: stmt.execute(sqlStmt);
069: this .importDefinitionElement.bRestartAutoCreate = false;
070: conn.commit();
071: stmt.close();
072: } catch (SQLException e) {
073: LoaderException le = new LoaderException("SQLException: ",
074: (Throwable) e);
075: this .logger.write("normal", le.getCause().toString());
076: throw le;
077: }
078: this .logger.write("full",
079: "\tcreateRestartCounterTable method is finished.");
080: }
081:
082: /**
083: * This method is used for checking the restart counter table
084: * @param c defines the connection object to target database
085: * @param rset defines the ResultSet object of source database
086: * @param jobName defines the current import job name
087: * @param iTargetFirstColumnResult is parameter from conf file
088: * @return value of counter field from restart counter table
089: * @throws SQLException
090: */
091: public BigDecimal checkDataTransmition(Connection c,
092: ResultSet rset, String jobName, int iTargetFirstColumnResult)
093: throws SQLException {
094:
095: String strQuery = "";
096: BigDecimal bdecRestartCounter = null;
097: String valueOfRestartColumn = "";
098:
099: this .logger.write("full",
100: "\tcheckDataTransmition method is started.");
101:
102: if (jobName.equalsIgnoreCase("importDefinition")) {
103: valueOfRestartColumn = this .importDefinitionElement.strImportDefinitionName;
104: } else if (jobName.equalsIgnoreCase("copyTable")) {
105: valueOfRestartColumn = this .importDefinitionElement.strCopyTableName;
106: }
107:
108: try {
109: strQuery = "SELECT "
110: + this .importDefinitionElement.strRestartCounterValue
111: + " FROM "
112: + this .importDefinitionElement.strRestartCounterTableName
113: + " WHERE "
114: + this .importDefinitionElement.strRestartCounterImportDefinitionName
115: + " = '" + valueOfRestartColumn + "'";
116:
117: this .logger.write("full", "\tQuery '" + strQuery
118: + "' will be executed");
119: Statement stmtCountT = c.createStatement();
120: ResultSet rsetCountT = stmtCountT.executeQuery(strQuery);
121:
122: //check if row (current import definition) exists in restart counter table
123: if (!rsetCountT.next()) {
124: strQuery = "INSERT into "
125: + this .importDefinitionElement.strRestartCounterTableName
126: + " ("
127: + this .importDefinitionElement.strRestartCounterImportDefinitionName
128: + ", "
129: + this .importDefinitionElement.strRestartCounterValue
130: + ") " + " VALUES ('" + valueOfRestartColumn
131: + "', null)";
132: this .logger.write("full", "\tQuery '" + strQuery
133: + "' will be executed");
134: stmtCountT.executeUpdate(strQuery);
135: c.commit();
136: bdecRestartCounter = null;
137:
138: } else { //row exist
139: if (iTargetFirstColumnResult == 1) {
140: bdecRestartCounter = new BigDecimal(Integer
141: .parseInt(rsetCountT.getString(1)));
142: } else { //iTargetFirstColumnResult == 0
143: bdecRestartCounter = new BigDecimal(Integer
144: .parseInt(rsetCountT.getString(0)));
145: }
146: if (false) {
147: rset.relative(bdecRestartCounter.intValue());
148: } else {
149: BigDecimal kl = new BigDecimal(0);
150: while (kl.compareTo(bdecRestartCounter) == -1) {
151: rset.next();
152: kl = kl.add(new BigDecimal(1));
153: }
154: }
155: }
156: rsetCountT.close();
157: stmtCountT.close();
158: } catch (SQLException ex) {
159: ex.printStackTrace();
160: }
161: return bdecRestartCounter;
162: }
163:
164: /**
165: * This method is used for inserting the appropriate values into restart counter
166: * table
167: * @param jobName defines the current import job name
168: * @param bdecCount defines the number of rows which are commited
169: * @param conn defines the connection object to target database
170: * @throws SQLException
171: */
172: public void insertCounter(String jobName, BigDecimal bdecCount,
173: Connection conn) throws SQLException {
174:
175: String valueOfRestartColumn = "";
176:
177: this .logger.write("full", "\tinsertCounter method is started.");
178:
179: if (jobName.equalsIgnoreCase("importDefinition")) {
180: valueOfRestartColumn = this .importDefinitionElement.strImportDefinitionName;
181: } else if (jobName.equalsIgnoreCase("copyTable")) {
182: valueOfRestartColumn = this .importDefinitionElement.strCopyTableName;
183: }
184:
185: String strQueryUpdate = "update "
186: + this .importDefinitionElement.strRestartCounterTableName
187: + " set "
188: + this .importDefinitionElement.strRestartCounterValue
189: + " = "
190: + bdecCount
191: + " where "
192: + this .importDefinitionElement.strRestartCounterImportDefinitionName
193: + " = '" + valueOfRestartColumn + "'";
194:
195: try {
196: this .logger.write("full", "\tQuery '" + strQueryUpdate
197: + "' will be executed");
198: Statement stmtChange = conn.createStatement();
199: int num = stmtChange.executeUpdate(strQueryUpdate);
200: stmtChange.close();
201: conn.commit();
202: } catch (SQLException ex) {
203: throw ex;
204: }
205: this .logger
206: .write("full", "\tinsertCounter method is finished.");
207: }
208:
209: /**
210: * This method is used to set '0' into counter column from counter table, if the
211: * current import job was finished successfuly
212: * @param conn defines the connection object of target database
213: * @param jobName defines the current import job name
214: * @throws SQLException
215: */
216: public void resetRestartCounter(Connection conn, String jobName)
217: throws SQLException {
218:
219: String valueOfRestartColumn = "";
220:
221: this .logger.write("full",
222: "\tresetRestartCounter method is started.");
223:
224: if (jobName.equalsIgnoreCase("importDefinition")) {
225: valueOfRestartColumn = this .importDefinitionElement.strImportDefinitionName;
226: } else if (jobName.equalsIgnoreCase("copyTable")) {
227: valueOfRestartColumn = this .importDefinitionElement.strCopyTableName;
228: }
229:
230: String strQueryReset = "update "
231: + this .importDefinitionElement.strRestartCounterTableName
232: + " set "
233: + this .importDefinitionElement.strRestartCounterValue
234: + " = 0 where "
235: + this .importDefinitionElement.strRestartCounterImportDefinitionName
236: + " = '" + valueOfRestartColumn + "'";
237: try {
238: Statement stmtReset = conn.createStatement();
239: this .logger.write("full", "\tQuery '" + strQueryReset
240: + "' will be executed");
241: int num = stmtReset.executeUpdate(strQueryReset);
242: stmtReset.close();
243: conn.commit();
244: } catch (SQLException ex) {
245: throw ex;
246: }
247: this .logger.write("full",
248: "\tresetRestartCounter method is finished.");
249: }
250:
251: /**
252: * This method is used to set current Looger
253: * @param logger is the currently used logger
254: */
255: public void setLogger(Logger logger) {
256: this.logger = logger;
257: }
258:
259: }
|