KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > webdocwf > util > loader > DataTransmition


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.sql.*;
26 import java.math.BigDecimal JavaDoc;
27 import org.webdocwf.util.loader.logging.*;
28
29 /**
30  *
31  * <p>DataTransmition class is used for handling the operation with ‘restore counter table’. </p>
32  * @author Radoslav Dutina
33  * @version 1.0
34  */

35 public class DataTransmition {
36   private ImportDefinitionElement importDefinitionElement;
37   private Logger logger;
38
39   public DataTransmition(ImportDefinitionElement importDefinitionElement) {
40     this.importDefinitionElement = importDefinitionElement;
41   }
42
43   /**
44    * This method create restore counter table
45    * @param conn defines the connection object of target database
46    * @throws LoaderException
47    */

48   public void createRestartCounterTable(Connection conn) throws
49       LoaderException {
50
51     this.logger.write("full", "\tcreateRestartCounterTable method is started.");
52     try {
53       Statement stmt;
54       stmt = conn.createStatement();
55
56       String JavaDoc sqlStmt = "create table " +
57                        this.importDefinitionElement.strRestartCounterTableName
58                        + " (" +
59                        this.importDefinitionElement.strRestartCounterImportDefinitionName +
60                        " VARCHAR(50) NOT NULL, "
61                        + this.importDefinitionElement.strRestartCounterValue + " DECIMAL(19,0));";
62
63       this.logger.write("full","\tQuery '" + sqlStmt + "' will be executed");
64       stmt.execute(sqlStmt);
65       this.importDefinitionElement.bRestartAutoCreate = false;
66       conn.commit();
67       stmt.close();
68     }
69     catch (SQLException e) {
70       LoaderException le = new LoaderException("SQLException: ", (Throwable JavaDoc)e);
71       this.logger.write("normal", le.getCause().toString());
72       throw le;
73     }
74         this.logger.write("full", "\tcreateRestartCounterTable method is finished.");
75   }
76
77   /**
78    * This method is used for checking the restart counter table
79    * @param c defines the connection object to target database
80    * @param rset defines the ResultSet object of source database
81    * @param jobName defines the current import job name
82    * @param iTargetFirstColumnResult is parameter from conf file
83    * @return value of counter field from restart counter table
84    * @throws SQLException
85    */

86   public BigDecimal JavaDoc checkDataTransmition(Connection c,
87       ResultSet rset, String JavaDoc jobName, int iTargetFirstColumnResult) throws SQLException {
88
89     String JavaDoc strQuery = "";
90     BigDecimal JavaDoc bdecRestartCounter = null;
91     String JavaDoc valueOfRestartColumn = "";
92
93     this.logger.write("full", "\tcheckDataTransmition method is started.");
94
95     if (jobName.equalsIgnoreCase("importDefinition")) {
96       valueOfRestartColumn = this.importDefinitionElement.strImportDefinitionName;
97     } else if (jobName.equalsIgnoreCase("copyTable")) {
98       valueOfRestartColumn = this.importDefinitionElement.strCopyTableName;
99     }
100
101     try {
102       strQuery = "SELECT " + this.importDefinitionElement.strRestartCounterValue +
103                  " FROM " +
104                  this.importDefinitionElement.strRestartCounterTableName + " WHERE " +
105                  this.importDefinitionElement.strRestartCounterImportDefinitionName
106                  + " = '" + valueOfRestartColumn + "'";
107
108       this.logger.write("full", "\tQuery '" + strQuery + "' will be executed");
109       Statement stmtCountT = c.createStatement();
110       ResultSet rsetCountT = stmtCountT.executeQuery(strQuery);
111
112       //check if row (current import definition) exists in restart counter table
113
if (!rsetCountT.next()) {
114         strQuery = "INSERT into " +
115                    this.importDefinitionElement.strRestartCounterTableName
116                    + " (" +
117                    this.importDefinitionElement.strRestartCounterImportDefinitionName
118                    + ", " + this.importDefinitionElement.strRestartCounterValue + ") " +
119                    " VALUES ('"
120                    + valueOfRestartColumn + "', null)";
121         this.logger.write("full", "\tQuery '" + strQuery + "' will be executed");
122         stmtCountT.executeUpdate(strQuery);
123         c.commit();
124         bdecRestartCounter = null;
125
126       } else { //row exist
127
if (iTargetFirstColumnResult == 1) {
128           bdecRestartCounter = new BigDecimal JavaDoc(Integer.parseInt(rsetCountT.getString(1)));
129         } else { //iTargetFirstColumnResult == 0
130
bdecRestartCounter = new BigDecimal JavaDoc(Integer.parseInt(rsetCountT.getString(0)));
131         }
132         if (false) {
133           rset.relative(bdecRestartCounter.intValue());
134         } else {
135           BigDecimal JavaDoc kl = new BigDecimal JavaDoc(0);
136           while (kl.compareTo(bdecRestartCounter) == -1) {
137             rset.next();
138             kl = kl.add(new BigDecimal JavaDoc(1));
139           }
140         }
141       }
142       rsetCountT.close();
143       stmtCountT.close();
144     }
145     catch (SQLException ex) {
146       ex.printStackTrace();
147     }
148     return bdecRestartCounter;
149   }
150
151   /**
152    * This method is used for inserting the appropriate values into restart counter
153    * table
154    * @param jobName defines the current import job name
155    * @param bdecCount defines the number of rows which are commited
156    * @param conn defines the connection object to target database
157    * @throws SQLException
158    */

159   public void insertCounter(String JavaDoc jobName, BigDecimal JavaDoc bdecCount,
160       Connection conn) throws SQLException {
161
162     String JavaDoc valueOfRestartColumn = "";
163
164     this.logger.write("full", "\tinsertCounter method is started.");
165
166     if (jobName.equalsIgnoreCase("importDefinition")) {
167       valueOfRestartColumn = this.importDefinitionElement.strImportDefinitionName;
168     } else if (jobName.equalsIgnoreCase("copyTable")) {
169       valueOfRestartColumn = this.importDefinitionElement.strCopyTableName;
170     }
171
172     String JavaDoc strQueryUpdate = "update " +
173                             this.importDefinitionElement.strRestartCounterTableName
174                             + " set " + this.importDefinitionElement.strRestartCounterValue + " = " +
175                             bdecCount
176                             + " where " +
177                             this.importDefinitionElement.strRestartCounterImportDefinitionName +
178                             " = '" + valueOfRestartColumn + "'";
179
180     try {
181       this.logger.write("full", "\tQuery '" + strQueryUpdate + "' will be executed");
182       Statement stmtChange = conn.createStatement();
183       int num = stmtChange.executeUpdate(strQueryUpdate);
184       stmtChange.close();
185       conn.commit();
186     }
187     catch (SQLException ex) {
188       throw ex;
189     }
190     this.logger.write("full", "\tinsertCounter method is finished.");
191   }
192
193   /**
194    * This method is used to set '0' into counter column from counter table, if the
195    * current import job was finished successfuly
196    * @param conn defines the connection object of target database
197    * @param jobName defines the current import job name
198    * @throws SQLException
199    */

200   public void resetRestartCounter(Connection conn, String JavaDoc jobName) throws SQLException {
201
202     String JavaDoc valueOfRestartColumn = "";
203
204     this.logger.write("full", "\tresetRestartCounter method is started.");
205
206     if (jobName.equalsIgnoreCase("importDefinition")) {
207       valueOfRestartColumn = this.importDefinitionElement.strImportDefinitionName;
208     } else if (jobName.equalsIgnoreCase("copyTable")) {
209       valueOfRestartColumn = this.importDefinitionElement.strCopyTableName;
210     }
211
212     String JavaDoc strQueryReset = "update " +
213                            this.importDefinitionElement.strRestartCounterTableName +
214                            " set " + this.importDefinitionElement.strRestartCounterValue +
215                            " = 0 where " +
216                            this.importDefinitionElement.strRestartCounterImportDefinitionName
217                            + " = '" + valueOfRestartColumn + "'";
218     try {
219       Statement stmtReset = conn.createStatement();
220       this.logger.write("full", "\tQuery '" + strQueryReset + "' will be executed");
221       int num = stmtReset.executeUpdate(strQueryReset);
222       stmtReset.close();
223       conn.commit();
224     }
225     catch (SQLException ex) {
226       throw ex;
227     }
228     this.logger.write("full", "\tresetRestartCounter method is finished.");
229   }
230
231   /**
232    * This method is used to set current Looger
233    * @param logger is the currently used logger
234    */

235   public void setLogger(Logger logger) {
236     this.logger = logger;
237   }
238
239 }
Popular Tags