KickJava   Java API By Example, From Geeks To Geeks.

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


1
2 /**
3   CounterColumns - insert auto increment values in target counter columns.
4
5     Copyright (C) 2002-2003 Together
6
7     This library is free software; you can redistribute it and/or
8     modify it under the terms of the GNU Lesser General Public
9     License as published by the Free Software Foundation; either
10     version 2.1 of the License, or (at your option) any later version.
11
12     This library is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15     Lesser General Public License for more details.
16
17     You should have received a copy of the GNU Lesser General Public
18     License along with this library; if not, write to the Free Software
19     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
21  CounterColumns.java
22  Date: 11.03.2003.
23  @version 1.1.0
24  @author: Milosevic Sinisa sinisa@prozone.co.yu
25  */

26
27 package org.webdocwf.util.loader;
28
29 import java.sql.Connection JavaDoc;
30 import java.sql.PreparedStatement JavaDoc;
31 import java.sql.SQLException JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Vector JavaDoc;
35
36 import org.webdocwf.util.loader.transformation.Transformation;
37 import org.webdocwf.util.loader.transformation.Transformations;
38
39 /**
40  *
41  * InsertRowQuerySet class creates insert in to statement for one import definition
42  * @author Radoslav Dutina
43  * @version 1.0
44  */

45 public class InsertRowQuerySet {
46   private String JavaDoc strQuery=null;
47   private String JavaDoc tableName=null;
48   private String JavaDoc tableID=null;
49   private Vector JavaDoc vecRelationColumnTargetTableName=null;
50   private Vector JavaDoc vecRelationColumnTargetTableID=null;
51   private Vector JavaDoc vecRelationColumnTargetColumnName=null;
52   private Vector JavaDoc indexDummyInsertNull=new Vector JavaDoc();
53   private Vector JavaDoc indexDummyInsertRelation=new Vector JavaDoc();
54   private Vector JavaDoc indexDummyIsNotRelationsColumn=new Vector JavaDoc();
55   private Vector JavaDoc columnNamesPstmt=new Vector JavaDoc();
56 // private Vector columnTypesPstmt=new Vector();
57
private Connection JavaDoc conn;
58   private String JavaDoc oidColumnName="oid";
59   private String JavaDoc versionColumnName="version";
60 // private Hashtable sqlToJavaMap=new Hashtable();
61

62
63   /**
64    * Construct object InsertRowQuerySet with associated parameters.
65    * @param c Connetion to source tables
66    * @param vecColumnNames is Vector of column names in a target table.
67    * @param vecColumnMode is Vector of column modes in a target table.
68    * @param tableName is current table name
69    * @param tableID is current table id
70    * @param iTableInt is the number of target table
71    * @param vecTempConstantColumns is Vector of column names for target table.
72    * @param vecVariableTimesTableName is Vector of variable times, for target table.
73    * @param vecVariableTimesTableID is Vector of variable times id, for target table.
74    * @param vecVariableTimesColumnName is Vector of variable (times) column names, for target table.
75    * @param vecVariableColumnTargetTableName is Vector of variable table names.
76    * @param vecVariableColumnTargetTableID is Vector of variable table id.
77    * @param vecVariableColumnTargetColumnName is Vector of variable column names, for target table.
78    * @param vecRelationColumnTargetColumnName is Vector of relations column names, for target table.
79    * @param vecRelationColumnTargetTableName is Vector of relations table names, for target table.
80    * @param vecRelationColumnTargetTableID is Vector of relations table id's, for target table.
81    * @param vecRelationColumnRelationMode is Vector of relations modes, for target table.
82    * @param counterColumns defines Vector of number of columns
83    * @param bOidLogicValue is boolean object, which decide is oid logic true or false
84    * @param isTOS is boolean object, which decide is oid logic true or false
85    * @throws SQLException
86    * @throws NullPointerException
87    * @throws Exception
88    */

89   public InsertRowQuerySet(Connection JavaDoc c,
90                            Vector JavaDoc vecColumnNames, Vector JavaDoc vecColumnMode, String JavaDoc tableName,
91                            String JavaDoc tableID, int iTableInt,Vector JavaDoc vecTempConstantColumns,
92                            Vector JavaDoc vecVariableTimesTableName, Vector JavaDoc vecVariableTimesTableID,
93                            Vector JavaDoc vecVariableTimesColumnName, Vector JavaDoc vecVariableColumnTargetTableName,
94                            Vector JavaDoc vecVariableColumnTargetTableID,Vector JavaDoc vecVariableColumnTargetColumnName,
95                            Vector JavaDoc vecRelationColumnTargetColumnName, Vector JavaDoc vecRelationColumnTargetTableName,
96                            Vector JavaDoc vecRelationColumnTargetTableID, Vector JavaDoc vecRelationColumnRelationMode,
97                            CounterColumns counterColumns, Transformations transformations, boolean bOidLogicValue,
98                            boolean isTOS, String JavaDoc oidColumnName, String JavaDoc versionColumnName)
99       throws SQLException JavaDoc, NullPointerException JavaDoc, Exception JavaDoc {
100
101     this.conn=c;
102 // if(this.sqlToJavaMap.size()>0)
103
// this.sqlToJavaMap.clear();
104
// this.sqlToJavaMap=sqlToJavaMap;
105

106     this.oidColumnName=oidColumnName;
107     this.versionColumnName=versionColumnName;
108     this.tableName=tableName;
109     this.tableID=tableID;
110     this.vecRelationColumnTargetTableName=vecRelationColumnTargetTableName;
111     this.vecRelationColumnTargetTableID=vecRelationColumnTargetTableID;
112     this.vecRelationColumnTargetColumnName=vecRelationColumnTargetColumnName;
113     strQuery = "insert into " + tableName;
114
115     if(bOidLogicValue){
116       if(isTOS){
117         strQuery+="("+this.oidColumnName+", ";
118         columnNamesPstmt.add(this.oidColumnName);
119
120       }else{
121         strQuery +="("+this.oidColumnName+","+this.versionColumnName+", ";
122         columnNamesPstmt.add(this.oidColumnName);
123         columnNamesPstmt.add(this.versionColumnName);
124
125       }
126     }else
127       strQuery+="(";
128
129     for (int j = 0; j < vecColumnNames.size(); j++) {
130       if (!this.isRelationColumn(vecColumnNames.get(j).toString(),iTableInt)) {
131         strQuery += vecColumnNames.get(j).toString() + ", ";
132         columnNamesPstmt.add(vecColumnNames.get(j).toString());
133         indexDummyIsNotRelationsColumn.add(String.valueOf(j));
134       }
135     }
136
137     for (int p = 0; p < vecTempConstantColumns.size(); p++){
138       strQuery += vecTempConstantColumns.get(p).toString() + ", ";
139       columnNamesPstmt.add(vecTempConstantColumns.get(p).toString());
140     }
141
142     for (int i = 0; i < vecVariableTimesTableName.size(); i++) {
143       if (vecVariableTimesTableName.get(i).toString().equalsIgnoreCase(tableName)
144           && vecVariableTimesTableID.get(i).toString().equalsIgnoreCase(tableID)) {
145       strQuery += vecVariableTimesColumnName.get(i).toString()
146                + ", ";
147            columnNamesPstmt.add(vecVariableTimesColumnName.get(i).toString());
148     }
149     }
150     for (int i = 0; i < vecVariableColumnTargetTableName.size(); i++) {
151       if (vecVariableColumnTargetTableName.get(i).toString().equalsIgnoreCase(tableName)
152           && vecVariableColumnTargetTableID.get(i).toString().equalsIgnoreCase(tableID)) {
153       strQuery += vecVariableColumnTargetColumnName.get(i).toString()
154                + ", ";
155            columnNamesPstmt.add(vecVariableColumnTargetColumnName.get(i).toString());
156     }
157     }
158     for (int i = 0; i < vecRelationColumnTargetColumnName.size(); i++) {
159       if (vecRelationColumnTargetTableName.get(i).toString().equalsIgnoreCase(tableName)
160           && vecRelationColumnTargetTableID.get(i).toString().equalsIgnoreCase(tableID)) {
161         strQuery += vecRelationColumnTargetColumnName.get(i).toString()+ ", ";
162         columnNamesPstmt.add(vecRelationColumnTargetColumnName.get(i).toString());
163       }
164     }
165
166     Vector JavaDoc counterColNames = counterColumns.getTargetColumnName(tableName,tableID);
167
168     for (int i = 0; i < counterColNames.size(); i++) {
169       if(counterColNames.get(i)!=null)
170         strQuery += counterColNames.get(i).toString() + ", ";
171       columnNamesPstmt.add(counterColNames.get(i).toString());
172     }
173     Vector JavaDoc subCounterColNames = counterColumns.getSubTargetColumnName(tableName,tableID);
174     for (int i = 0; i < subCounterColNames.size(); i++) {
175       if(subCounterColNames.get(i)!=null)
176         strQuery += subCounterColNames.get(i).toString() + ", ";
177         columnNamesPstmt.add(subCounterColNames.get(i).toString());
178     }
179     //sinisa add transformations
180
ArrayList JavaDoc trans = transformations.getTransformations();
181     Iterator JavaDoc iTrans = trans.iterator();
182       
183     while(iTrans.hasNext()) {
184          Transformation transformation = (Transformation)iTrans.next();
185 // String transTableID = transformation.getTargetTableID();
186
// if( !transTableID.equals(tableID) )
187
// continue;
188
Vector JavaDoc transColNames = transformation.getTargetColumnNames(Integer.parseInt(tableID));
189          for (int i = 0; i < transColNames.size(); i++) {
190             if(transColNames.get(i)!=null)
191               strQuery += transColNames.get(i).toString() + ", ";
192               columnNamesPstmt.add(transColNames.get(i).toString());
193          }
194    }
195  
196   }
197
198   /**
199    * This method read the value of strQuery parameter
200    * @return value of paramter
201    */

202   public String JavaDoc getStrQuery(){
203     strQuery = strQuery.substring(0, strQuery.length() - 2);
204     return strQuery;
205   }
206
207   /**
208    * This method read the value of indexDummyIsNotRelationsColumn parameter
209    * @return value of paramter
210    */

211   public Vector JavaDoc getIndexDummyIsNotRelationsColumn(){
212     return indexDummyIsNotRelationsColumn;
213   }
214
215   /**
216    * This method construct prepare statement
217    * @return string representation of prepareStatement
218    * @throws Exception
219    */

220   public PreparedStatement JavaDoc getPreparedStatementForInsert() throws Exception JavaDoc{
221
222     String JavaDoc strPstmt="insert into "+this.tableName+" (";
223     int count=columnNamesPstmt.size();
224     for (int i = 0; i < count; i++) {
225       if(i!=count-1)
226         strPstmt+=columnNamesPstmt.get(i).toString()+",";
227       else
228         strPstmt+=columnNamesPstmt.get(i).toString()+")";
229     }
230     strPstmt+=" VALUES (";
231     for (int i = 0; i < count; i++) {
232       if(i!=count-1)
233         strPstmt+="?,";
234       else
235         strPstmt+="?)";
236     }
237     PreparedStatement JavaDoc pstmt;
238     try{
239       pstmt = this.conn.prepareStatement(strPstmt);
240     }catch(Exception JavaDoc e){
241       throw e;
242     }
243     return pstmt;
244   }
245
246 // public Hashtable getPreparedStatemetTypes() throws LoaderException{
247
// Hashtable pstmtTypes=new Hashtable();
248
// try {
249
// Statement stmt=conn.createStatement();
250
// ResultSet rs=stmt.executeQuery("select * from "+this.tableName);
251
// int columnCount=rs.getMetaData().getColumnCount();
252
// for (int i = 1; i < columnCount+1; i++) {
253
// String columnName=rs.getMetaData().getColumnName(i).toUpperCase();
254
// String columnType=rs.getMetaData().getColumnTypeName(i).toUpperCase();
255
// String javaType=(String)sqlToJavaMap.get(columnType);
256
// pstmtTypes.put(columnName,javaType);
257
// }
258
//
259
// stmt.close();
260
// rs.close();
261
// }
262
// catch (Exception ex) {
263
// throw new LoaderException(ex.getMessage());
264
// }
265
// return pstmtTypes;
266
// }
267

268   /**
269    * This method read value form columnNamesPstmt parameter
270    * @return value of parameter
271    */

272   public Vector JavaDoc getColumnNames(){
273     return this.columnNamesPstmt;
274   }
275
276
277   private boolean isRelationColumn (String JavaDoc strColumnName, int iTable) {
278     boolean bFind = false;
279     endFor: for (int i = 0; i < this.vecRelationColumnTargetTableName.size(); i++) {
280       if (this.vecRelationColumnTargetTableName.get(i).toString().equalsIgnoreCase(tableName)
281           && this.vecRelationColumnTargetTableID.get(i).toString().equalsIgnoreCase(tableID)
282           && this.vecRelationColumnTargetColumnName.get(i).toString().equalsIgnoreCase(strColumnName)) {
283       bFind = true;
284       break endFor;
285     }
286     }
287     return bFind;
288   }
289 }
Popular Tags