KickJava   Java API By Example, From Geeks To Geeks.

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


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  @authors:
20  Radoslav Dutina rale@prozone.co.yu
21  */

22
23
24
25 package org.webdocwf.util.loader;
26
27 import java.util.*;
28 import java.sql.*;
29 import org.webdocwf.util.loader.logging.*;
30
31 /**
32  *
33  * LoadAllSourceData class is used for loading source data which are not maped in
34  * importDefinition jobs.
35  * @author Radoslav Dutina
36  * @version 1.0
37  */

38 public class LoadAllSourceData {
39
40  private Vector columnNameNoMap=new Vector();
41  private Vector columnValueNoMap=new Vector();
42  private Vector columnTypeNoMap=new Vector();
43
44
45
46
47  /**
48   * Public constructor of LoadAllSourceData class. Construct object LoadAllSourceData with an
49   * associated parameters.
50   * @param connSource defines the connection to source database
51   * @param connTarget defines the connection to target database
52   * @param sourceTableName defines soruce table name
53   * @param vecSourceColumn defines all source clolumn names, for the named source table
54   * @param targetTableName defines target table name
55   * @param logger defines object of Lo9gger class
56   * @param vecRelationColumns defines relation columns for target table
57   * @param vecVariableColumns defines variable columns for target table
58   * @param vecConstantColumns defines constant columns for target table
59   * @param vecUseIDColumns defines ID columns for target table
60   * @param vecTimesColumns defines time columns for target table
61   * @throws LoaderException
62   */

63  public LoadAllSourceData(Connection connSource, String JavaDoc sourceTableName, Vector vecSourceColumn,
64                            String JavaDoc targetTableName,Connection connTarget,
65                            Logger logger, Vector vecRelationColumns,
66                            Vector vecVariableColumns, Vector vecConstantColumns,
67                            Vector vecUseIDColumns,Vector vecTimesColumns,boolean columnsSuportedTarget, ConfigReader configReaderTarget) throws LoaderException {
68
69
70     boolean noMap=true;
71     Vector columnNames=new Vector();
72     Hashtable columnTypes=new Hashtable();
73       try{
74
75         Statement stmtSource=connSource.createStatement();
76         Statement stmtTarget=connTarget.createStatement();
77         //add columns that are apears in relations, variables, constant....
78
for (int i = 0; i < vecRelationColumns.size(); i++) {
79           vecSourceColumn.add(vecRelationColumns.get(i).toString());
80         }
81         for (int i = 0; i < vecVariableColumns.size(); i++) {
82           vecSourceColumn.add(vecVariableColumns.get(i).toString());
83         }
84         for (int i = 0; i < vecConstantColumns.size(); i++) {
85           vecSourceColumn.add(vecConstantColumns.get(i).toString());
86         }
87         for (int i = 0; i < vecUseIDColumns.size(); i++) {
88           vecSourceColumn.add(vecUseIDColumns.get(i).toString());
89         }
90         for (int i = 0; i < vecTimesColumns.size(); i++) {
91           vecSourceColumn.add(vecTimesColumns.get(i).toString());
92         }
93
94         ResultSet rsSource=stmtSource.executeQuery("select * from "+sourceTableName);
95 //ZK change this. Because of problems with getColumnTypeName()method. Some drivers doesn't support it.
96
//start
97
int countSourceColumns=rsSource.getMetaData().getColumnCount();
98         
99         ResultSet rsTarget=null;
100         if (columnsSuportedTarget){
101             
102             rsTarget = connTarget.getMetaData().getColumns( connTarget.getCatalog(), null, targetTableName, "%" );
103             //int countTargetColumns=rsTarget.getMetaData().getColumnCount();
104
String JavaDoc columnName = "";
105             String JavaDoc columnType = "";
106               while(rsTarget.next()){
107                 columnName = rsTarget.getString(4);
108                 columnType = rsTarget.getString(6);
109                 for (int j = 1; j < countSourceColumns+1; j++) {
110                   if(columnName.equalsIgnoreCase(rsSource.getMetaData().getColumnName(j))){
111                      columnNames.add(columnName);
112                      columnTypes.put(columnName,columnType);
113                      break;
114                   }
115                 }
116               }
117 //end
118
}else{
119                         //TODO ZK ADDED stmtTarget.setMaxRows(1). Place this as parameter in conf file, like maxRowsSuported
120
if (configReaderTarget.getMaxRowsSupported()){
121                         stmtTarget.setMaxRows(1);
122             }
123             rsTarget=stmtTarget.executeQuery("select * from "+targetTableName);
124             int countTargetColumns=rsTarget.getMetaData().getColumnCount();
125
126         logger.write("full", "\tAuto maping columns is started.");
127           for (int i = 1; i < countTargetColumns+1; i++) {
128             for (int j = 1; j < countSourceColumns+1; j++) {
129               if(rsTarget.getMetaData().getColumnName(i).equalsIgnoreCase(rsSource.getMetaData().getColumnName(j))){
130             columnNames.add(rsTarget.getMetaData().getColumnName(i));
131             columnTypes.put(rsTarget.getMetaData().getColumnName(i),rsTarget.getMetaData().getColumnTypeName(i));
132             break;
133               }
134             }
135           }
136         }
137       int count=columnNames.size();
138       for (int j = 0; j < count; j++) {
139         noMap=true;
140         //dont put column names which are appears in importDefinitin as value columns
141
for (int i = 0; i < vecSourceColumn.size(); i++) {
142           if(columnNames.get(j).toString().equalsIgnoreCase(vecSourceColumn.get(i).toString())){
143         noMap=false;
144         break;
145           }
146         }
147         if(noMap){
148           String JavaDoc name=columnNames.get(j).toString();
149           String JavaDoc type=(String JavaDoc)columnTypes.get(name);
150           columnNameNoMap.add(name);
151           columnTypeNoMap.add(type);
152           logger.write("full", "\tColumn "+name+",from source table "+sourceTableName+", is automaped. ");
153         }
154       }
155
156
157           while(rsSource.next()){
158             for (int i = 0; i <columnNameNoMap.size(); i++) {
159               String JavaDoc columnName=columnNameNoMap.get(i).toString();
160               String JavaDoc type=columnTypeNoMap.get(i).toString();
161               String JavaDoc columnValue=new String JavaDoc();
162 // if(CheckType.isBinaryObject(type)){
163
// columnValue=null;
164
// }else{
165
if(rsSource.getObject(columnName)==null){
166                   columnValue=null;
167                 }else{
168                   columnValue=rsSource.getObject(columnName).toString();
169                 }
170                 if(columnValue!=null){
171                   if(columnValue.equalsIgnoreCase("")){
172                     columnValue=null;
173                   }
174                 }
175 // }
176
columnValueNoMap.add(columnValue);
177             }
178           }
179         logger.write("full", "\tAuto maping columns is finished.");
180
181         rsSource.close();
182         stmtSource.close();
183
184         rsTarget.close();
185         stmtTarget.close();
186
187     }catch(SQLException ex){
188       String JavaDoc msg="Sorry, but you can't connect to source table: "+sourceTableName;
189       LoaderException le =new LoaderException(msg+"\n"+ex,(Throwable JavaDoc)ex);
190       throw le;
191     }
192   }
193
194
195   /**
196    * This method read value of columnNameNoMap parameter
197    * @return value of parameter
198    */

199   public Vector getNoMapSourceColumnName(){
200     return columnNameNoMap;
201   }
202
203   /**
204    * This method read value of columnValueNoMap parameter
205    * @return value of parameter
206    */

207   public Vector getNoMapSourceColumnValue(){
208     return columnValueNoMap;
209   }
210
211   /**
212    * This method read value of columnTypeNoMap parameter
213    * @return value of parameter
214    */

215   public Vector getNoMapSourceColumnType(){
216     return columnTypeNoMap;
217   }
218 }
Popular Tags