KickJava   Java API By Example, From Geeks To Geeks.

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


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 static String JavaDoc currentKey = "";
41   private Logger logger;
42   private int currentVersion = 0;
43
44   /**
45    * Empty constructor
46    */

47   public CheckRowCache() {
48   }
49
50   /**
51    * This method is use to mark row if he is cahced
52    */

53   public void setCheckRowValue() {
54     if (!this.currentKey.equalsIgnoreCase(""))
55       rowExist.put(this.currentKey, "true");
56   }
57
58   /**
59    * This method set value of cached row
60    * @param val is value of cached row
61    */

62   public void setCheckRowVersionValue(String JavaDoc val) {
63     if (!this.currentKey.equalsIgnoreCase("")) {
64       if (rowVersionValue.get(this.currentKey) != null)
65         rowVersionValue.remove(this.currentKey);
66       rowVersionValue.put(this.currentKey, val);
67     }
68   }
69
70   /**
71    * This method is use to retrive value of cached row
72    * @return value of cache row
73    */

74   public int getCheckRowVersionValue() {
75     String JavaDoc ret = (String JavaDoc)rowVersionValue.get(this.currentKey);
76     return Integer.parseInt(ret);
77   }
78
79   /**
80    * This method is use to retrive query string
81    * @return query string
82    */

83   public static String JavaDoc getKey() {
84     return currentKey;
85   }
86
87   /**
88    * This method is use to retrive value of some sql query object
89    * @param key defines string representation of query object
90    * @param conn defines connection to target database
91    * @param iTargetFirstColumnResult is configuration parameter
92    * @return value of selected row
93    * @throws java.lang.Exception
94    */

95   public boolean getCheckRowValue(String JavaDoc key, Connection conn,
96       int iTargetFirstColumnResult, String JavaDoc versionColumnName) throws Exception JavaDoc {
97
98     this.currentKey = key;
99     Object JavaDoc obj = rowExist.get(key);
100     if (obj == null) {
101       this.logger.write("full", "\tQuery '" + key + "' will be executed");
102       try {
103         Statement stmtCheckTarget = conn.createStatement();
104         ResultSet rsetCheckTarget = stmtCheckTarget.executeQuery(key);
105         if (rsetCheckTarget.next()) { //update row mode
106
this.setCheckRowValue();
107           if (iTargetFirstColumnResult != 100) {
108 // this.currentVersion = rsetCheckTarget.getInt("version");
109
this.currentVersion = rsetCheckTarget.getInt(versionColumnName);
110             this.setCheckRowVersionValue(String.valueOf(this.currentVersion));
111           }
112           rsetCheckTarget.close();
113           stmtCheckTarget.close();
114           return true;
115         } else {//insert row mode
116
this.setCheckRowVersionValue(String.valueOf(0));
117           rsetCheckTarget.close();
118           stmtCheckTarget.close();
119         }
120         return false;
121       }
122       catch (Exception JavaDoc ex) {
123         this.logger.write("full", ex.getMessage());
124         throw ex;
125       }
126     } else { //update row mode
127
return true;
128     }
129   }
130
131   /**
132    * This method is use to reset all parameters which are used in this class
133    */

134   public void resetCheckRowCache() {
135     rowExist.clear();
136     rowVersionValue.clear();
137     currentKey = "";
138   }
139
140   /**
141    * This method is use to set logger object
142    * @param logger is current logger
143    */

144   public void setLogger(Logger logger) {
145     this.logger = logger;
146   }
147
148   /**
149    * This method is use to retrive value of currentVersion parameter
150    * @return value of parameter
151    */

152   public int getCurrentVersion() {
153     return this.currentVersion;
154   }
155
156 }
Popular Tags