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.io.File;
025: import java.net.URL;
026: import java.sql.Connection;
027: import java.sql.DriverManager;
028: import java.sql.ResultSet;
029: import java.sql.Statement;
030: import java.util.StringTokenizer;
031:
032: import org.webdocwf.util.loader.LoaderException;
033: import org.webdocwf.util.loader.logging.Logger;
034: import org.webdocwf.util.loader.logging.StandardLogger;
035: import org.webdocwf.util.loader.wizard.AddClassPath;
036:
037: /**
038: * CsvTableDesignReader class retrieves the input data from csv tables, and placed them in to
039: * ImportDefinition class.
040: * @author Radoslav Dutina
041: * @version 1.0
042: */
043: public class CsvTableDesignReader {
044:
045: private ImportDefinitionAttributes importDefinitionAttributes = new ImportDefinitionAttributes();
046: private MappingTypeData mappingTypeData;
047: private Logger logger;
048:
049: /**
050: * Construct object LoaderGenerator with associated parameters.
051: * @param tableName is name of the table form which we retrieve data.
052: * @param generatorParameters represents the references to InputParameter object.
053: * @throws LoaderException
054: */
055: public CsvTableDesignReader(String tableName,
056: InputParameters generatorParameters) throws LoaderException {
057: setLogger();
058: this .logger.write("full", "CsvTableDesignReader is started.");
059: importDefinitionAttributes.setName(tableName);
060: importDefinitionAttributes.setTableName(tableName);
061:
062: try {
063: JdbcParameters sourceJdbc = new JdbcParameters("source",
064: generatorParameters);
065: Connection conn = null;
066: URL url = null;
067: String app = "";
068: String path = AddClassPath.getClassPathString();
069: if (path != null) {
070: StringTokenizer st = new StringTokenizer(path, ";");
071: int count = 0;
072: while (st.hasMoreTokens()) {
073: GeneratorClassLoader.addURL(new File(st
074: .nextElement().toString()).toURL());
075: }
076: Class.forName(sourceJdbc
077: .getJdbcParameters("JdbcDriver"));
078: } else {
079: Class.forName(sourceJdbc
080: .getJdbcParameters("JdbcDriver"));
081: }
082: conn = DriverManager.getConnection(sourceJdbc
083: .getJdbcParameters("Connection.Url"), sourceJdbc
084: .getJdbcParameters("User"), sourceJdbc
085: .getJdbcParameters("Password"));
086:
087: Statement stmt = conn.createStatement();
088: ResultSet results = stmt.executeQuery("SELECT * FROM " + ""
089: + tableName + "");
090: int numberC = results.getMetaData().getColumnCount();
091: String[] columnNames = new String[numberC];
092: String[] targetTableNames = new String[numberC];
093: String[] targetTableID = new String[numberC];
094: String[] columnTargetTypes = new String[numberC];
095: String[] columnLength = new String[numberC];
096: String[] columnIsNullable = new String[numberC];
097:
098: for (int x = 1; x < numberC + 1; x++) {
099: columnNames[x - 1] = results.getMetaData()
100: .getColumnName(x);
101: targetTableNames[x - 1] = tableName;
102: targetTableID[x - 1] = "0";
103:
104: if (generatorParameters.getSourceType()
105: .equalsIgnoreCase("access")) {
106:
107: String sourceTypeData = results.getMetaData()
108: .getColumnTypeName(x);
109: mappingTypeData = new MappingTypeData(
110: sourceTypeData, generatorParameters);
111: String targetTypeData = mappingTypeData
112: .getSQLType();
113: columnTargetTypes[x - 1] = targetTypeData;
114: // if(sourceTypeData.equalsIgnoreCase("decimal")||targetTypeData.equalsIgnoreCase("decimal")){
115: if (generatorParameters
116: .getIsDecimal(sourceTypeData)
117: .equalsIgnoreCase("true")
118: || generatorParameters.getIsDecimal(
119: targetTypeData).equalsIgnoreCase(
120: "true")) {
121: columnLength[x - 1] = results.getMetaData()
122: .getPrecision(x)
123: + ","
124: + results.getMetaData().getScale(x);
125: //ZK change this 6.5.2004
126: // }else if(sourceTypeData.equalsIgnoreCase("int")||sourceTypeData.equalsIgnoreCase("tinyint")||
127: // sourceTypeData.equalsIgnoreCase("smallint")||sourceTypeData.equalsIgnoreCase("tinyint")||
128: // sourceTypeData.equalsIgnoreCase("datetime")||sourceTypeData.equalsIgnoreCase("ntext")||
129: // sourceTypeData.equalsIgnoreCase("longchar")||sourceTypeData.equalsIgnoreCase("longbinary")||
130: // sourceTypeData.equalsIgnoreCase("binary")||sourceTypeData.equalsIgnoreCase("longinteger")){
131: } else if (generatorParameters.getHasSize(
132: sourceTypeData).equalsIgnoreCase("false")) {
133:
134: columnLength[x - 1] = " ";
135: } else {
136: columnLength[x - 1] = String.valueOf(results
137: .getMetaData().getPrecision(x));
138: }
139: if (results.getMetaData().isNullable(x) == 0) {
140: columnIsNullable[x - 1] = "NOT NULL";
141: } else {
142: columnIsNullable[x - 1] = "";
143: }
144: }
145: }
146:
147: importDefinitionAttributes
148: .setTagSourceColumnName(columnNames);
149: importDefinitionAttributes
150: .setTagTargetColumnName(columnNames);
151: importDefinitionAttributes
152: .setTagTargetTableName(targetTableNames);
153: importDefinitionAttributes
154: .setTagTargetTableID(targetTableID);
155: //only for access db
156: if (generatorParameters.getSourceType().equalsIgnoreCase(
157: "access")) {
158:
159: importDefinitionAttributes
160: .setTagColumnType(columnTargetTypes);
161: importDefinitionAttributes
162: .setTagColumnLenght(columnLength);
163: importDefinitionAttributes
164: .setTagAllowNulls(columnIsNullable);
165: }
166:
167: results.close();
168: stmt.close();
169: conn.close();
170: } catch (Exception e) {
171: String msg = "Exception in CsvTableDesignReader class:";
172: //e.printStackTrace();
173: LoaderException le = new LoaderException(msg + "\n"
174: + e.getMessage(), (Throwable) e);
175: this .logger.write("full",
176: "Error:Exception in CsvTableDesignReader class."
177: + "\n" + le.getStackTraceAsString());
178: throw le;
179: }
180: this .logger.write("full", "CsvTableDesignReader is finished.");
181: }
182:
183: /**
184: * This method read value of importDefinitionAttributes parameters.
185: * @return references to ImportDefinitionAttributes objects.
186: */
187: public ImportDefinitionAttributes getImportDefinition() {
188: return importDefinitionAttributes;
189: }
190:
191: /**
192: * This method will set logger object
193: * @param logger
194: */
195: private void setLogger() {
196: this.logger = StandardLogger.getCentralLogger();
197: }
198: }
|