001: /**
002: CounterColumns - insert auto increment values in target counter columns.
003:
004: Copyright (C) 2002-2003 Together
005:
006: This library is free software; you can redistribute it and/or
007: modify it under the terms of the GNU Lesser General Public
008: License as published by the Free Software Foundation; either
009: version 2.1 of the License, or (at your option) any later version.
010:
011: This library is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public
017: License along with this library; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019:
020: CounterColumns.java
021: Date: 11.03.2003.
022: @version 1.1.0
023: @author: Milosevic Sinisa sinisa@prozone.co.yu
024: */package org.webdocwf.util.loader;
025:
026: import java.sql.Connection;
027: import java.sql.PreparedStatement;
028: import java.sql.SQLException;
029: import java.util.ArrayList;
030: import java.util.Iterator;
031: import java.util.Vector;
032:
033: import org.webdocwf.util.loader.transformation.Transformation;
034: import org.webdocwf.util.loader.transformation.Transformations;
035:
036: /**
037: *
038: * InsertRowQuerySet class creates insert in to statement for one import definition
039: * @author Radoslav Dutina
040: * @version 1.0
041: */
042: public class InsertRowQuerySet {
043: private String strQuery = null;
044: private String tableName = null;
045: private String tableID = null;
046: private Vector vecRelationColumnTargetTableName = null;
047: private Vector vecRelationColumnTargetTableID = null;
048: private Vector vecRelationColumnTargetColumnName = null;
049: private Vector indexDummyInsertNull = new Vector();
050: private Vector indexDummyInsertRelation = new Vector();
051: private Vector indexDummyIsNotRelationsColumn = new Vector();
052: private Vector columnNamesPstmt = new Vector();
053: // private Vector columnTypesPstmt=new Vector();
054: private Connection conn;
055: private String oidColumnName = "oid";
056: private String versionColumnName = "version";
057:
058: // private Hashtable sqlToJavaMap=new Hashtable();
059:
060: /**
061: * Construct object InsertRowQuerySet with associated parameters.
062: * @param c Connetion to source tables
063: * @param vecColumnNames is Vector of column names in a target table.
064: * @param vecColumnMode is Vector of column modes in a target table.
065: * @param tableName is current table name
066: * @param tableID is current table id
067: * @param iTableInt is the number of target table
068: * @param vecTempConstantColumns is Vector of column names for target table.
069: * @param vecVariableTimesTableName is Vector of variable times, for target table.
070: * @param vecVariableTimesTableID is Vector of variable times id, for target table.
071: * @param vecVariableTimesColumnName is Vector of variable (times) column names, for target table.
072: * @param vecVariableColumnTargetTableName is Vector of variable table names.
073: * @param vecVariableColumnTargetTableID is Vector of variable table id.
074: * @param vecVariableColumnTargetColumnName is Vector of variable column names, for target table.
075: * @param vecRelationColumnTargetColumnName is Vector of relations column names, for target table.
076: * @param vecRelationColumnTargetTableName is Vector of relations table names, for target table.
077: * @param vecRelationColumnTargetTableID is Vector of relations table id's, for target table.
078: * @param vecRelationColumnRelationMode is Vector of relations modes, for target table.
079: * @param counterColumns defines Vector of number of columns
080: * @param bOidLogicValue is boolean object, which decide is oid logic true or false
081: * @param isTOS is boolean object, which decide is oid logic true or false
082: * @throws SQLException
083: * @throws NullPointerException
084: * @throws Exception
085: */
086: public InsertRowQuerySet(Connection c, Vector vecColumnNames,
087: Vector vecColumnMode, String tableName, String tableID,
088: int iTableInt, Vector vecTempConstantColumns,
089: Vector vecVariableTimesTableName,
090: Vector vecVariableTimesTableID,
091: Vector vecVariableTimesColumnName,
092: Vector vecVariableColumnTargetTableName,
093: Vector vecVariableColumnTargetTableID,
094: Vector vecVariableColumnTargetColumnName,
095: Vector vecRelationColumnTargetColumnName,
096: Vector vecRelationColumnTargetTableName,
097: Vector vecRelationColumnTargetTableID,
098: Vector vecRelationColumnRelationMode,
099: CounterColumns counterColumns,
100: Transformations transformations, boolean bOidLogicValue,
101: boolean isTOS, String oidColumnName,
102: String versionColumnName) throws SQLException,
103: NullPointerException, Exception {
104:
105: this .conn = c;
106: // if(this.sqlToJavaMap.size()>0)
107: // this.sqlToJavaMap.clear();
108: // this.sqlToJavaMap=sqlToJavaMap;
109:
110: this .oidColumnName = oidColumnName;
111: this .versionColumnName = versionColumnName;
112: this .tableName = tableName;
113: this .tableID = tableID;
114: this .vecRelationColumnTargetTableName = vecRelationColumnTargetTableName;
115: this .vecRelationColumnTargetTableID = vecRelationColumnTargetTableID;
116: this .vecRelationColumnTargetColumnName = vecRelationColumnTargetColumnName;
117: strQuery = "insert into " + tableName;
118:
119: if (bOidLogicValue) {
120: if (isTOS) {
121: strQuery += "(" + this .oidColumnName + ", ";
122: columnNamesPstmt.add(this .oidColumnName);
123:
124: } else {
125: strQuery += "(" + this .oidColumnName + ","
126: + this .versionColumnName + ", ";
127: columnNamesPstmt.add(this .oidColumnName);
128: columnNamesPstmt.add(this .versionColumnName);
129:
130: }
131: } else
132: strQuery += "(";
133:
134: for (int j = 0; j < vecColumnNames.size(); j++) {
135: if (!this .isRelationColumn(
136: vecColumnNames.get(j).toString(), iTableInt)) {
137: strQuery += vecColumnNames.get(j).toString() + ", ";
138: columnNamesPstmt.add(vecColumnNames.get(j).toString());
139: indexDummyIsNotRelationsColumn.add(String.valueOf(j));
140: }
141: }
142:
143: for (int p = 0; p < vecTempConstantColumns.size(); p++) {
144: strQuery += vecTempConstantColumns.get(p).toString() + ", ";
145: columnNamesPstmt.add(vecTempConstantColumns.get(p)
146: .toString());
147: }
148:
149: for (int i = 0; i < vecVariableTimesTableName.size(); i++) {
150: if (vecVariableTimesTableName.get(i).toString()
151: .equalsIgnoreCase(tableName)
152: && vecVariableTimesTableID.get(i).toString()
153: .equalsIgnoreCase(tableID)) {
154: strQuery += vecVariableTimesColumnName.get(i)
155: .toString()
156: + ", ";
157: columnNamesPstmt.add(vecVariableTimesColumnName.get(i)
158: .toString());
159: }
160: }
161: for (int i = 0; i < vecVariableColumnTargetTableName.size(); i++) {
162: if (vecVariableColumnTargetTableName.get(i).toString()
163: .equalsIgnoreCase(tableName)
164: && vecVariableColumnTargetTableID.get(i).toString()
165: .equalsIgnoreCase(tableID)) {
166: strQuery += vecVariableColumnTargetColumnName.get(i)
167: .toString()
168: + ", ";
169: columnNamesPstmt.add(vecVariableColumnTargetColumnName
170: .get(i).toString());
171: }
172: }
173: for (int i = 0; i < vecRelationColumnTargetColumnName.size(); i++) {
174: if (vecRelationColumnTargetTableName.get(i).toString()
175: .equalsIgnoreCase(tableName)
176: && vecRelationColumnTargetTableID.get(i).toString()
177: .equalsIgnoreCase(tableID)) {
178: strQuery += vecRelationColumnTargetColumnName.get(i)
179: .toString()
180: + ", ";
181: columnNamesPstmt.add(vecRelationColumnTargetColumnName
182: .get(i).toString());
183: }
184: }
185:
186: Vector counterColNames = counterColumns.getTargetColumnName(
187: tableName, tableID);
188:
189: for (int i = 0; i < counterColNames.size(); i++) {
190: if (counterColNames.get(i) != null)
191: strQuery += counterColNames.get(i).toString() + ", ";
192: columnNamesPstmt.add(counterColNames.get(i).toString());
193: }
194: Vector subCounterColNames = counterColumns
195: .getSubTargetColumnName(tableName, tableID);
196: for (int i = 0; i < subCounterColNames.size(); i++) {
197: if (subCounterColNames.get(i) != null)
198: strQuery += subCounterColNames.get(i).toString() + ", ";
199: columnNamesPstmt.add(subCounterColNames.get(i).toString());
200: }
201: //sinisa add transformations
202: ArrayList trans = transformations.getTransformations();
203: Iterator iTrans = trans.iterator();
204:
205: while (iTrans.hasNext()) {
206: Transformation transformation = (Transformation) iTrans
207: .next();
208: // String transTableID = transformation.getTargetTableID();
209: // if( !transTableID.equals(tableID) )
210: // continue;
211: Vector transColNames = transformation
212: .getTargetColumnNames(Integer.parseInt(tableID));
213: for (int i = 0; i < transColNames.size(); i++) {
214: if (transColNames.get(i) != null)
215: strQuery += transColNames.get(i).toString() + ", ";
216: columnNamesPstmt.add(transColNames.get(i).toString());
217: }
218: }
219:
220: }
221:
222: /**
223: * This method read the value of strQuery parameter
224: * @return value of paramter
225: */
226: public String getStrQuery() {
227: strQuery = strQuery.substring(0, strQuery.length() - 2);
228: return strQuery;
229: }
230:
231: /**
232: * This method read the value of indexDummyIsNotRelationsColumn parameter
233: * @return value of paramter
234: */
235: public Vector getIndexDummyIsNotRelationsColumn() {
236: return indexDummyIsNotRelationsColumn;
237: }
238:
239: /**
240: * This method construct prepare statement
241: * @return string representation of prepareStatement
242: * @throws Exception
243: */
244: public PreparedStatement getPreparedStatementForInsert()
245: throws Exception {
246:
247: String strPstmt = "insert into " + this .tableName + " (";
248: int count = columnNamesPstmt.size();
249: for (int i = 0; i < count; i++) {
250: if (i != count - 1)
251: strPstmt += columnNamesPstmt.get(i).toString() + ",";
252: else
253: strPstmt += columnNamesPstmt.get(i).toString() + ")";
254: }
255: strPstmt += " VALUES (";
256: for (int i = 0; i < count; i++) {
257: if (i != count - 1)
258: strPstmt += "?,";
259: else
260: strPstmt += "?)";
261: }
262: PreparedStatement pstmt;
263: try {
264: pstmt = this .conn.prepareStatement(strPstmt);
265: } catch (Exception e) {
266: throw e;
267: }
268: return pstmt;
269: }
270:
271: // public Hashtable getPreparedStatemetTypes() throws LoaderException{
272: // Hashtable pstmtTypes=new Hashtable();
273: // try {
274: // Statement stmt=conn.createStatement();
275: // ResultSet rs=stmt.executeQuery("select * from "+this.tableName);
276: // int columnCount=rs.getMetaData().getColumnCount();
277: // for (int i = 1; i < columnCount+1; i++) {
278: // String columnName=rs.getMetaData().getColumnName(i).toUpperCase();
279: // String columnType=rs.getMetaData().getColumnTypeName(i).toUpperCase();
280: // String javaType=(String)sqlToJavaMap.get(columnType);
281: // pstmtTypes.put(columnName,javaType);
282: // }
283: //
284: // stmt.close();
285: // rs.close();
286: // }
287: // catch (Exception ex) {
288: // throw new LoaderException(ex.getMessage());
289: // }
290: // return pstmtTypes;
291: // }
292:
293: /**
294: * This method read value form columnNamesPstmt parameter
295: * @return value of parameter
296: */
297: public Vector getColumnNames() {
298: return this .columnNamesPstmt;
299: }
300:
301: private boolean isRelationColumn(String strColumnName, int iTable) {
302: boolean bFind = false;
303: endFor: for (int i = 0; i < this .vecRelationColumnTargetTableName
304: .size(); i++) {
305: if (this .vecRelationColumnTargetTableName.get(i).toString()
306: .equalsIgnoreCase(tableName)
307: && this .vecRelationColumnTargetTableID.get(i)
308: .toString().equalsIgnoreCase(tableID)
309: && this .vecRelationColumnTargetColumnName.get(i)
310: .toString().equalsIgnoreCase(strColumnName)) {
311: bFind = true;
312: break endFor;
313: }
314: }
315: return bFind;
316: }
317: }
|