KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > webdocwf > util > loader > generator > CsvTableDesignReader


1
2 /*
3 LoaderGenerator - tool for generated xml, sql and doml file needed for Octopus.
4
5
6     Copyright (C) 2003 Together
7
8     This library is free software; you can redistribute it and/or
9     modify it under the terms of the GNU Lesser General Public
10     License as published by the Free Software Foundation; either
11     version 2.1 of the License, or (at your option) any later version.
12
13     This library is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16     Lesser General Public License for more details.
17
18     You should have received a copy of the GNU Lesser General Public
19     License along with this library; if not, write to the Free Software
20     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */

22
23 package org.webdocwf.util.loader.generator;
24
25 import java.io.File JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.sql.Connection JavaDoc;
28 import java.sql.DriverManager JavaDoc;
29 import java.sql.ResultSet JavaDoc;
30 import java.sql.Statement JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32
33 import org.webdocwf.util.loader.LoaderException;
34 import org.webdocwf.util.loader.logging.Logger;
35 import org.webdocwf.util.loader.logging.StandardLogger;
36 import org.webdocwf.util.loader.wizard.AddClassPath;
37
38
39 /**
40  * CsvTableDesignReader class retrieves the input data from csv tables, and placed them in to
41  * ImportDefinition class.
42  * @author Radoslav Dutina
43  * @version 1.0
44  */

45 public class CsvTableDesignReader {
46
47   private ImportDefinitionAttributes importDefinitionAttributes= new ImportDefinitionAttributes();
48   private MappingTypeData mappingTypeData;
49   private Logger logger;
50   /**
51    * Construct object LoaderGenerator with associated parameters.
52    * @param tableName is name of the table form which we retrieve data.
53    * @param generatorParameters represents the references to InputParameter object.
54    * @throws LoaderException
55    */

56   public CsvTableDesignReader(String JavaDoc tableName, InputParameters generatorParameters)
57       throws LoaderException {
58         setLogger();
59         this.logger.write("full", "CsvTableDesignReader is started.");
60     importDefinitionAttributes.setName(tableName);
61     importDefinitionAttributes.setTableName(tableName);
62
63     try{
64       JdbcParameters sourceJdbc= new JdbcParameters("source", generatorParameters);
65       Connection JavaDoc conn = null;
66       URL JavaDoc url=null;
67       String JavaDoc app="";
68       String JavaDoc path=AddClassPath.getClassPathString();
69       if(path!=null){
70         StringTokenizer JavaDoc st=new StringTokenizer JavaDoc(path,";");
71         int count=0;
72         while(st.hasMoreTokens()) {
73           GeneratorClassLoader.addURL(
74               new File JavaDoc(st.nextElement().toString()).toURL());
75         }
76         Class.forName(sourceJdbc.getJdbcParameters("JdbcDriver"));
77       }else{
78         Class.forName(sourceJdbc.getJdbcParameters("JdbcDriver"));
79       }
80       conn=DriverManager.getConnection(sourceJdbc.getJdbcParameters("Connection.Url"),
81                                        sourceJdbc.getJdbcParameters("User"),sourceJdbc.getJdbcParameters("Password"));
82
83
84       Statement JavaDoc stmt=conn.createStatement();
85       ResultSet JavaDoc results=stmt.executeQuery("SELECT * FROM "+""+tableName+"");
86       int numberC=results.getMetaData().getColumnCount();
87       String JavaDoc[] columnNames=new String JavaDoc[numberC];
88       String JavaDoc[] targetTableNames= new String JavaDoc[numberC];
89       String JavaDoc[] targetTableID= new String JavaDoc[numberC];
90       String JavaDoc[] columnTargetTypes=new String JavaDoc[numberC];
91       String JavaDoc[] columnLength=new String JavaDoc[numberC];
92       String JavaDoc[] columnIsNullable=new String JavaDoc[numberC];
93
94       for(int x=1; x<numberC+1; x++){
95         columnNames[x-1] =results.getMetaData().getColumnName(x);
96         targetTableNames[x-1]=tableName;
97         targetTableID[x-1]="0";
98
99         if(generatorParameters.getSourceType().equalsIgnoreCase("access")){
100
101           String JavaDoc sourceTypeData=results.getMetaData().getColumnTypeName(x);
102           mappingTypeData=new MappingTypeData(sourceTypeData, generatorParameters);
103           String JavaDoc targetTypeData=mappingTypeData.getSQLType();
104           columnTargetTypes[x-1]=targetTypeData;
105          // if(sourceTypeData.equalsIgnoreCase("decimal")||targetTypeData.equalsIgnoreCase("decimal")){
106
if(generatorParameters.getIsDecimal(sourceTypeData).equalsIgnoreCase("true")||
107              generatorParameters.getIsDecimal(targetTypeData).equalsIgnoreCase("true")){
108             columnLength[x-1]=results.getMetaData().getPrecision(x)+","+
109                               results.getMetaData().getScale(x);
110 //ZK change this 6.5.2004
111
// }else if(sourceTypeData.equalsIgnoreCase("int")||sourceTypeData.equalsIgnoreCase("tinyint")||
112
// sourceTypeData.equalsIgnoreCase("smallint")||sourceTypeData.equalsIgnoreCase("tinyint")||
113
// sourceTypeData.equalsIgnoreCase("datetime")||sourceTypeData.equalsIgnoreCase("ntext")||
114
// sourceTypeData.equalsIgnoreCase("longchar")||sourceTypeData.equalsIgnoreCase("longbinary")||
115
// sourceTypeData.equalsIgnoreCase("binary")||sourceTypeData.equalsIgnoreCase("longinteger")){
116
}else if(generatorParameters.getHasSize(sourceTypeData).equalsIgnoreCase("false")){
117             
118               columnLength[x-1]=" ";
119             }else{
120               columnLength[x-1]=String.valueOf(results.getMetaData().getPrecision(x));
121             }
122             if(results.getMetaData().isNullable(x)==0){
123               columnIsNullable[x-1]="NOT NULL";
124             }else{
125               columnIsNullable[x-1]="";
126             }
127         }
128       }
129
130       importDefinitionAttributes.setTagSourceColumnName(columnNames);
131       importDefinitionAttributes.setTagTargetColumnName(columnNames);
132       importDefinitionAttributes.setTagTargetTableName(targetTableNames);
133       importDefinitionAttributes.setTagTargetTableID(targetTableID);
134       //only for access db
135
if(generatorParameters.getSourceType().equalsIgnoreCase("access")){
136
137         importDefinitionAttributes.setTagColumnType(columnTargetTypes);
138         importDefinitionAttributes.setTagColumnLenght(columnLength);
139         importDefinitionAttributes.setTagAllowNulls(columnIsNullable);
140       }
141
142       results.close();
143       stmt.close();
144       conn.close();
145     }catch(Exception JavaDoc e){
146       String JavaDoc msg="Exception in CsvTableDesignReader class:";
147       //e.printStackTrace();
148
LoaderException le=new LoaderException(msg+"\n"+e.getMessage(), (Throwable JavaDoc)e);
149         this.logger.write("full", "Error:Exception in CsvTableDesignReader class."+"\n"+le.getStackTraceAsString());
150       throw le;
151     }
152     this.logger.write("full", "CsvTableDesignReader is finished.");
153   }
154
155   /**
156    * This method read value of importDefinitionAttributes parameters.
157    * @return references to ImportDefinitionAttributes objects.
158    */

159   public ImportDefinitionAttributes getImportDefinition(){
160     return importDefinitionAttributes;
161   }
162
163   /**
164       * This method will set logger object
165       * @param logger
166       */

167      private void setLogger() {
168          this.logger = StandardLogger.getCentralLogger();
169      }
170 }
Popular Tags