KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.Connection JavaDoc;
26 import java.sql.ResultSet JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.List JavaDoc;
30
31 import org.webdocwf.util.loader.LoaderException;
32 import org.webdocwf.util.loader.logging.Logger;
33 import org.webdocwf.util.loader.logging.StandardLogger;
34
35 /**
36  * TableDesignReader class read the data which describe database tables.
37  * @author Radoslav Dutina
38  * @version 1.0
39  */

40 public class TableDesignReader {
41
42     private static List JavaDoc listColumnNames = null;
43     private static List JavaDoc listTargetTableNames = null;
44     private static List JavaDoc listTargetTableID = null;
45     private static List JavaDoc listColumnType = null;
46     private static List JavaDoc listColumnLenght = null;
47     private static List JavaDoc listAllowNulls = null;
48     private Logger logger;
49
50     private ImportDefinitionAttributes importDefinitionAttributes = new ImportDefinitionAttributes();
51     private MappingTypeData mappingTypeData;
52     /**
53      * Construct object TableDesignReader with associated parameters.
54      * @param generatorParameters represents the references to InputParameter object.
55      * @param tableName is name of the table form which we retrieve data.
56      * @param conn is the named connection.
57      * @param catalogName is the name of the current database.
58      * @throws LoaderException
59      */

60
61     public TableDesignReader(String JavaDoc tableName, Connection JavaDoc conn, String JavaDoc catalogName, InputParameters generatorParameters) throws LoaderException {
62         setLogger();
63         this.logger.write("full", "TableDesignReader is started.");
64         listColumnNames = new ArrayList JavaDoc();
65         listTargetTableNames = new ArrayList JavaDoc();
66         listTargetTableID = new ArrayList JavaDoc();
67         listColumnType = new ArrayList JavaDoc();
68         listColumnLenght = new ArrayList JavaDoc();
69         listAllowNulls = new ArrayList JavaDoc();
70
71         importDefinitionAttributes.setName(tableName);
72         importDefinitionAttributes.setTableName(tableName);
73
74         try {
75             if(generatorParameters.getSourceType() != null &&
76                     (generatorParameters.getSourceType().equals("Hsqldb") ||
77                      generatorParameters.getSourceType().equals("HypersonicSQL") ||
78                      generatorParameters.getSourceType().equals("DB2"))
79                      ) {
80                     catalogName = null;
81             }
82             ResultSet JavaDoc rs = conn.getMetaData().getColumns(catalogName, null, tableName, "%");
83
84             while (rs.next()) {
85                 listColumnNames.add(rs.getString(4));
86                 listTargetTableNames.add(tableName);
87                 listTargetTableID.add("0");
88                 //if types of databases are same, do not perform data type transformation
89
if (generatorParameters.getSourceType().equalsIgnoreCase(generatorParameters.getTargetType())) {
90                     listColumnType.add(rs.getString(6));
91                 } else {
92                     mappingTypeData = new MappingTypeData(rs.getString(6), generatorParameters);
93                     listColumnType.add(mappingTypeData.getSQLType());
94                 }
95                 if (generatorParameters.getIsDecimal(rs.getString(6)).equalsIgnoreCase("true") &&
96                                         generatorParameters.getHasSize(rs.getString(6)).equalsIgnoreCase("true")) {
97                     //TODO ZK change this 8.7 2004 because of problem with decimal type on DB2
98
if ((generatorParameters.getTargetType()).equalsIgnoreCase("DB2")) {
99                         int lenght = new Integer JavaDoc(rs.getString(7)).intValue();
100                         int dec = new Integer JavaDoc(rs.getString(9)).intValue();
101                         int result = lenght + dec;
102                         listColumnLenght.add(" (" + result + "," + rs.getString(9) + ") ");
103                     } else {
104                         listColumnLenght.add(" (" + rs.getString(7) + "," + rs.getString(9) + ") ");
105                     }
106                 } else if (generatorParameters.getHasSize(rs.getString(6)).equalsIgnoreCase("false")) {
107
108                     listColumnLenght.add(" ");
109
110                 } else {
111                     //ZK change this 5.5.2004
112
listColumnLenght.add("(" + rs.getString(7) + ") ");
113                     // listColumnLenght.add(rs.getString(7));
114
}
115
116                 if (rs.getString(18).equalsIgnoreCase("YES")) {
117                     listAllowNulls.add("");
118                 } else {
119                     listAllowNulls.add("NOT NULL");
120                 }
121             }
122
123             importDefinitionAttributes.setTagSourceColumnName(getColumnNames());
124             importDefinitionAttributes.setTagTargetColumnName(getColumnNames());
125             importDefinitionAttributes.setTagTargetTableName(getTargetTableNames());
126             importDefinitionAttributes.setTagTargetTableID(getTargetTableID());
127             importDefinitionAttributes.setTagColumnType(getColumnType());
128             importDefinitionAttributes.setTagAllowNulls(getAllowNulls());
129             importDefinitionAttributes.setTagColumnLenght(getColumnLenght());
130
131             rs.close();
132
133         } catch (java.sql.SQLException JavaDoc e) {
134             String JavaDoc msg = "Can't get the connection, to " + tableName + " table. ";
135                         LoaderException le = new LoaderException(msg + e.getMessage(), (Throwable JavaDoc) e);
136             this.logger.write("full", "Exception:" + msg + "\n" + le.getStackTraceAsString());
137             throw le;
138         } catch (Exception JavaDoc e) {
139             String JavaDoc msg = "Exception in class TableDesignReader! ";
140                         LoaderException le = new LoaderException(msg + e.getMessage(), (Throwable JavaDoc) e);
141             this.logger.write("full", "Exception:" + msg + "\n" + le.getStackTraceAsString());
142             throw le;
143         }
144         this.logger.write("full", "TableDesignReader is finished.");
145     }
146
147     /**
148      * This method read value of importDefinitionAttributes parameters.
149      * @return references to ImportDefinitionAttributes objects.
150      */

151     public ImportDefinitionAttributes getImportDefinition() {
152         return importDefinitionAttributes;
153     }
154
155     /**
156      * This method sets value of listColumnNames parameters.
157      * @param list_ColumnNames is value of parameters.
158      */

159     public static void setColumnNames(String JavaDoc[] list_ColumnNames) {
160         listColumnNames = Arrays.asList(list_ColumnNames);
161     }
162
163     /**
164      * This method read value of listColumnNames parameters.
165      * @return value of parameters.
166      */

167     public static String JavaDoc[] getColumnNames() {
168         String JavaDoc[] ret = new String JavaDoc[listColumnNames.size()];
169         listColumnNames.toArray(ret);
170         return ret;
171     }
172
173     /**
174      * This method sets value of listTargetTableNames parameters.
175      * @param list_TargetTableNames is value of parameters.
176      */

177     public static void setTargetTableNames(String JavaDoc[] list_TargetTableNames) {
178         listTargetTableNames = Arrays.asList(list_TargetTableNames);
179     }
180
181     /**
182      * This method read value of listTargetTableNames parameters.
183      * @return value of parameters.
184      */

185     public static String JavaDoc[] getTargetTableNames() {
186         String JavaDoc[] ret = new String JavaDoc[listTargetTableNames.size()];
187         listTargetTableNames.toArray(ret);
188         return ret;
189     }
190
191     /**
192      * This method sets value of listTargetTableID parameters.
193      * @param list_TargetTableID is value of parameter.
194      */

195     public static void setTargetTableID(String JavaDoc[] list_TargetTableID) {
196         listTargetTableID = Arrays.asList(list_TargetTableID);
197     }
198
199     /**
200      * This method read value of listTargetTableID parameters.
201      * @return value of parameters.
202      */

203     public static String JavaDoc[] getTargetTableID() {
204         String JavaDoc[] ret = new String JavaDoc[listTargetTableID.size()];
205         listTargetTableID.toArray(ret);
206         return ret;
207     }
208
209     /**
210      * This method sets value of listColumnType parameters.
211      * @param list_ColumnType is value of parameters.
212      */

213     public static void setColumnType(String JavaDoc[] list_ColumnType) {
214         listColumnType = Arrays.asList(list_ColumnType);
215     }
216
217     /**
218      * This method read value of listColumnType parameters.
219      * @return value of parameters.
220      */

221     public static String JavaDoc[] getColumnType() {
222         String JavaDoc[] ret = new String JavaDoc[listColumnType.size()];
223         listColumnType.toArray(ret);
224         return ret;
225     }
226
227     /**
228      * This method sets value of listColumnLenght parameters.
229      * @param list_ColumnLenght is value of parameter.
230      */

231     public static void setColumnLenght(String JavaDoc[] list_ColumnLenght) {
232         listColumnLenght = Arrays.asList(list_ColumnLenght);
233     }
234
235     /**
236      * This method read value of listColumnLenght parameters.
237      * @return value of parameters.
238      */

239     public static String JavaDoc[] getColumnLenght() {
240         String JavaDoc[] ret = new String JavaDoc[listColumnLenght.size()];
241         listColumnLenght.toArray(ret);
242         return ret;
243     }
244
245     /**
246      * This method sets value of listAllowNulls parameters.
247      * @param list_AllowNulls is value of parameters.
248      */

249     public static void setAllowNulls(String JavaDoc[] list_AllowNulls) {
250         listAllowNulls = Arrays.asList(list_AllowNulls);
251     }
252
253     /**
254      * This method read value of listAllowNulls parameters.
255      * @return value of parameters.
256      */

257     public static String JavaDoc[] getAllowNulls() {
258         String JavaDoc[] ret = new String JavaDoc[listAllowNulls.size()];
259         listAllowNulls.toArray(ret);
260         return ret;
261     }
262     /**
263      * This method will set logger object
264      * @param logger
265        */

266     private void setLogger() {
267         this.logger = StandardLogger.getCentralLogger();
268     }
269 }
Popular Tags