001: /*
002: LoaderGenerator - tool for generated xml, sql and doml file needed for Octopus.
003:
004:
005: Copyright (C) 2003 Together
006:
007: This library is free software; you can redistribute it and/or
008: modify it under the terms of the GNU Lesser General Public
009: License as published by the Free Software Foundation; either
010: version 2.1 of the License, or (at your option) any later version.
011:
012: This library is distributed in the hope that it will be useful,
013: but WITHOUT ANY WARRANTY; without even the implied warranty of
014: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: Lesser General Public License for more details.
016:
017: You should have received a copy of the GNU Lesser General Public
018: License along with this library; if not, write to the Free Software
019: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021:
022: package org.webdocwf.util.loader.generator;
023:
024: import java.sql.Connection;
025: import java.sql.ResultSet;
026: import java.util.ArrayList;
027: import java.util.Arrays;
028: import java.util.List;
029:
030: import org.webdocwf.util.loader.LoaderException;
031: import org.webdocwf.util.loader.logging.Logger;
032: import org.webdocwf.util.loader.logging.StandardLogger;
033:
034: /**
035: * TableDesignReader class read the data which describe database tables.
036: * @author Radoslav Dutina
037: * @version 1.0
038: */
039: public class TableDesignReader {
040:
041: private static List listColumnNames = null;
042: private static List listTargetTableNames = null;
043: private static List listTargetTableID = null;
044: private static List listColumnType = null;
045: private static List listColumnLenght = null;
046: private static List listAllowNulls = null;
047: private Logger logger;
048:
049: private ImportDefinitionAttributes importDefinitionAttributes = new ImportDefinitionAttributes();
050: private MappingTypeData mappingTypeData;
051:
052: /**
053: * Construct object TableDesignReader with associated parameters.
054: * @param generatorParameters represents the references to InputParameter object.
055: * @param tableName is name of the table form which we retrieve data.
056: * @param conn is the named connection.
057: * @param catalogName is the name of the current database.
058: * @throws LoaderException
059: */
060:
061: public TableDesignReader(String tableName, Connection conn,
062: String catalogName, InputParameters generatorParameters)
063: throws LoaderException {
064: setLogger();
065: this .logger.write("full", "TableDesignReader is started.");
066: listColumnNames = new ArrayList();
067: listTargetTableNames = new ArrayList();
068: listTargetTableID = new ArrayList();
069: listColumnType = new ArrayList();
070: listColumnLenght = new ArrayList();
071: listAllowNulls = new ArrayList();
072:
073: importDefinitionAttributes.setName(tableName);
074: importDefinitionAttributes.setTableName(tableName);
075:
076: try {
077: if (generatorParameters.getSourceType() != null
078: && (generatorParameters.getSourceType().equals(
079: "Hsqldb")
080: || generatorParameters.getSourceType()
081: .equals("HypersonicSQL") || generatorParameters
082: .getSourceType().equals("DB2"))) {
083: catalogName = null;
084: }
085: ResultSet rs = conn.getMetaData().getColumns(catalogName,
086: null, tableName, "%");
087:
088: while (rs.next()) {
089: listColumnNames.add(rs.getString(4));
090: listTargetTableNames.add(tableName);
091: listTargetTableID.add("0");
092: //if types of databases are same, do not perform data type transformation
093: if (generatorParameters.getSourceType()
094: .equalsIgnoreCase(
095: generatorParameters.getTargetType())) {
096: listColumnType.add(rs.getString(6));
097: } else {
098: mappingTypeData = new MappingTypeData(rs
099: .getString(6), generatorParameters);
100: listColumnType.add(mappingTypeData.getSQLType());
101: }
102: if (generatorParameters.getIsDecimal(rs.getString(6))
103: .equalsIgnoreCase("true")
104: && generatorParameters.getHasSize(
105: rs.getString(6)).equalsIgnoreCase(
106: "true")) {
107: //TODO ZK change this 8.7 2004 because of problem with decimal type on DB2
108: if ((generatorParameters.getTargetType())
109: .equalsIgnoreCase("DB2")) {
110: int lenght = new Integer(rs.getString(7))
111: .intValue();
112: int dec = new Integer(rs.getString(9))
113: .intValue();
114: int result = lenght + dec;
115: listColumnLenght.add(" (" + result + ","
116: + rs.getString(9) + ") ");
117: } else {
118: listColumnLenght.add(" (" + rs.getString(7)
119: + "," + rs.getString(9) + ") ");
120: }
121: } else if (generatorParameters.getHasSize(
122: rs.getString(6)).equalsIgnoreCase("false")) {
123:
124: listColumnLenght.add(" ");
125:
126: } else {
127: //ZK change this 5.5.2004
128: listColumnLenght.add("(" + rs.getString(7) + ") ");
129: // listColumnLenght.add(rs.getString(7));
130: }
131:
132: if (rs.getString(18).equalsIgnoreCase("YES")) {
133: listAllowNulls.add("");
134: } else {
135: listAllowNulls.add("NOT NULL");
136: }
137: }
138:
139: importDefinitionAttributes
140: .setTagSourceColumnName(getColumnNames());
141: importDefinitionAttributes
142: .setTagTargetColumnName(getColumnNames());
143: importDefinitionAttributes
144: .setTagTargetTableName(getTargetTableNames());
145: importDefinitionAttributes
146: .setTagTargetTableID(getTargetTableID());
147: importDefinitionAttributes
148: .setTagColumnType(getColumnType());
149: importDefinitionAttributes
150: .setTagAllowNulls(getAllowNulls());
151: importDefinitionAttributes
152: .setTagColumnLenght(getColumnLenght());
153:
154: rs.close();
155:
156: } catch (java.sql.SQLException e) {
157: String msg = "Can't get the connection, to " + tableName
158: + " table. ";
159: LoaderException le = new LoaderException(msg
160: + e.getMessage(), (Throwable) e);
161: this .logger.write("full", "Exception:" + msg + "\n"
162: + le.getStackTraceAsString());
163: throw le;
164: } catch (Exception e) {
165: String msg = "Exception in class TableDesignReader! ";
166: LoaderException le = new LoaderException(msg
167: + e.getMessage(), (Throwable) e);
168: this .logger.write("full", "Exception:" + msg + "\n"
169: + le.getStackTraceAsString());
170: throw le;
171: }
172: this .logger.write("full", "TableDesignReader is finished.");
173: }
174:
175: /**
176: * This method read value of importDefinitionAttributes parameters.
177: * @return references to ImportDefinitionAttributes objects.
178: */
179: public ImportDefinitionAttributes getImportDefinition() {
180: return importDefinitionAttributes;
181: }
182:
183: /**
184: * This method sets value of listColumnNames parameters.
185: * @param list_ColumnNames is value of parameters.
186: */
187: public static void setColumnNames(String[] list_ColumnNames) {
188: listColumnNames = Arrays.asList(list_ColumnNames);
189: }
190:
191: /**
192: * This method read value of listColumnNames parameters.
193: * @return value of parameters.
194: */
195: public static String[] getColumnNames() {
196: String[] ret = new String[listColumnNames.size()];
197: listColumnNames.toArray(ret);
198: return ret;
199: }
200:
201: /**
202: * This method sets value of listTargetTableNames parameters.
203: * @param list_TargetTableNames is value of parameters.
204: */
205: public static void setTargetTableNames(
206: String[] list_TargetTableNames) {
207: listTargetTableNames = Arrays.asList(list_TargetTableNames);
208: }
209:
210: /**
211: * This method read value of listTargetTableNames parameters.
212: * @return value of parameters.
213: */
214: public static String[] getTargetTableNames() {
215: String[] ret = new String[listTargetTableNames.size()];
216: listTargetTableNames.toArray(ret);
217: return ret;
218: }
219:
220: /**
221: * This method sets value of listTargetTableID parameters.
222: * @param list_TargetTableID is value of parameter.
223: */
224: public static void setTargetTableID(String[] list_TargetTableID) {
225: listTargetTableID = Arrays.asList(list_TargetTableID);
226: }
227:
228: /**
229: * This method read value of listTargetTableID parameters.
230: * @return value of parameters.
231: */
232: public static String[] getTargetTableID() {
233: String[] ret = new String[listTargetTableID.size()];
234: listTargetTableID.toArray(ret);
235: return ret;
236: }
237:
238: /**
239: * This method sets value of listColumnType parameters.
240: * @param list_ColumnType is value of parameters.
241: */
242: public static void setColumnType(String[] list_ColumnType) {
243: listColumnType = Arrays.asList(list_ColumnType);
244: }
245:
246: /**
247: * This method read value of listColumnType parameters.
248: * @return value of parameters.
249: */
250: public static String[] getColumnType() {
251: String[] ret = new String[listColumnType.size()];
252: listColumnType.toArray(ret);
253: return ret;
254: }
255:
256: /**
257: * This method sets value of listColumnLenght parameters.
258: * @param list_ColumnLenght is value of parameter.
259: */
260: public static void setColumnLenght(String[] list_ColumnLenght) {
261: listColumnLenght = Arrays.asList(list_ColumnLenght);
262: }
263:
264: /**
265: * This method read value of listColumnLenght parameters.
266: * @return value of parameters.
267: */
268: public static String[] getColumnLenght() {
269: String[] ret = new String[listColumnLenght.size()];
270: listColumnLenght.toArray(ret);
271: return ret;
272: }
273:
274: /**
275: * This method sets value of listAllowNulls parameters.
276: * @param list_AllowNulls is value of parameters.
277: */
278: public static void setAllowNulls(String[] list_AllowNulls) {
279: listAllowNulls = Arrays.asList(list_AllowNulls);
280: }
281:
282: /**
283: * This method read value of listAllowNulls parameters.
284: * @return value of parameters.
285: */
286: public static String[] getAllowNulls() {
287: String[] ret = new String[listAllowNulls.size()];
288: listAllowNulls.toArray(ret);
289: return ret;
290: }
291:
292: /**
293: * This method will set logger object
294: * @param logger
295: */
296: private void setLogger() {
297: this.logger = StandardLogger.getCentralLogger();
298: }
299: }
|