001: /*
002: Loader - tool for transfering data from one JDBC source to another and
003: doing transformations during copy.
004: Copyright (C) 2002-2003 Together
005: This library is free software; you can redistribute it and/or
006: modify it under the terms of the GNU Lesser General Public
007: License as published by the Free Software Foundation; either
008: version 2.1 of the License, or (at your option) any later version.
009: This library is distributed in the hope that it will be useful,
010: but WITHOUT ANY WARRANTY; without even the implied warranty of
011: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: Lesser General Public License for more details.
013: You should have received a copy of the GNU Lesser General Public
014: License along with this library; if not, write to the Free Software
015: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
016: Loader.java
017: Date: 03.03.2003.
018: @version 2.1 alpha
019: @authors:
020: Radoslav Dutina rale@prozone.co.yu
021: */
022:
023: package org.webdocwf.util.loader;
024:
025: import java.sql.Connection;
026: import java.sql.ResultSet;
027: import java.sql.SQLException;
028: import java.sql.Statement;
029: import java.text.SimpleDateFormat;
030: import java.util.Date;
031: import java.util.Hashtable;
032: import java.util.Vector;
033:
034: import org.webdocwf.util.loader.logging.Logger;
035:
036: /**
037: *
038: * LoadAllSourceData class is used for loading source data which are not maped in
039: * importDefinition jobs.
040: * @author Radoslav Dutina
041: * @version 1.0
042: */
043: public class LoadAllSourceData {
044:
045: private Vector columnNameNoMap = new Vector();
046: private Vector columnValueNoMap = new Vector();
047: private Vector columnTypeNoMap = new Vector();
048:
049: /**
050: * Public constructor of LoadAllSourceData class. Construct object LoadAllSourceData with an
051: * associated parameters.
052: * @param connSource defines the connection to source database
053: * @param connTarget defines the connection to target database
054: * @param sourceTableName defines soruce table name
055: * @param vecSourceColumn defines all source clolumn names, for the named source table
056: * @param targetTableName defines target table name
057: * @param logger defines object of Lo9gger class
058: * @param vecRelationColumns defines relation columns for target table
059: * @param vecVariableColumns defines variable columns for target table
060: * @param vecConstantColumns defines constant columns for target table
061: * @param vecUseIDColumns defines ID columns for target table
062: * @param vecTimesColumns defines time columns for target table
063: * @throws LoaderException
064: */
065: public LoadAllSourceData(Connection connSource,
066: String sourceTableName, Vector vecSourceColumn,
067: String targetTableName, Connection connTarget,
068: Logger logger, Vector vecRelationColumns,
069: Vector vecVariableColumns, Vector vecConstantColumns,
070: Vector vecUseIDColumns, Vector vecTimesColumns,
071: boolean columnsSuportedTarget,
072: ConfigReader configReaderTarget) throws LoaderException {
073:
074: boolean noMap = true;
075: Vector columnNames = new Vector();
076: Hashtable columnTypes = new Hashtable();
077: try {
078:
079: Statement stmtSource = connSource.createStatement();
080: Statement stmtTarget = connTarget.createStatement();
081: //add columns that are apears in relations, variables, constant....
082: for (int i = 0; i < vecRelationColumns.size(); i++) {
083: vecSourceColumn.add(vecRelationColumns.get(i)
084: .toString());
085: }
086: for (int i = 0; i < vecVariableColumns.size(); i++) {
087: vecSourceColumn.add(vecVariableColumns.get(i)
088: .toString());
089: }
090: for (int i = 0; i < vecConstantColumns.size(); i++) {
091: vecSourceColumn.add(vecConstantColumns.get(i)
092: .toString());
093: }
094: for (int i = 0; i < vecUseIDColumns.size(); i++) {
095: vecSourceColumn.add(vecUseIDColumns.get(i).toString());
096: }
097: for (int i = 0; i < vecTimesColumns.size(); i++) {
098: vecSourceColumn.add(vecTimesColumns.get(i).toString());
099: }
100:
101: ResultSet rsSource = stmtSource
102: .executeQuery("select * from " + sourceTableName);
103: //ZK change this. Because of problems with getColumnTypeName()method. Some drivers doesn't support it.
104: //start
105: int countSourceColumns = rsSource.getMetaData()
106: .getColumnCount();
107:
108: ResultSet rsTarget = null;
109: if (columnsSuportedTarget) {
110:
111: rsTarget = connTarget.getMetaData().getColumns(
112: connTarget.getCatalog(), null, targetTableName,
113: "%");
114: //int countTargetColumns=rsTarget.getMetaData().getColumnCount();
115: String columnName = "";
116: String columnType = "";
117: while (rsTarget.next()) {
118: columnName = rsTarget.getString(4);
119: columnType = rsTarget.getString(6);
120: for (int j = 1; j < countSourceColumns + 1; j++) {
121: if (columnName.equalsIgnoreCase(rsSource
122: .getMetaData().getColumnName(j))) {
123: columnNames.add(columnName);
124: columnTypes.put(columnName, columnType);
125: break;
126: }
127: }
128: }
129: //end
130: } else {
131: //TODO ZK ADDED stmtTarget.setMaxRows(1). Place this as parameter in conf file, like maxRowsSuported
132: if (configReaderTarget.getMaxRowsSupported()) {
133: stmtTarget.setMaxRows(1);
134: }
135: rsTarget = stmtTarget.executeQuery("select * from "
136: + targetTableName);
137: int countTargetColumns = rsTarget.getMetaData()
138: .getColumnCount();
139:
140: logger.write("full",
141: "\tAuto maping columns is started.");
142: for (int i = 1; i < countTargetColumns + 1; i++) {
143: for (int j = 1; j < countSourceColumns + 1; j++) {
144: if (rsTarget.getMetaData().getColumnName(i)
145: .equalsIgnoreCase(
146: rsSource.getMetaData()
147: .getColumnName(j))) {
148: columnNames.add(rsTarget.getMetaData()
149: .getColumnName(i));
150: columnTypes
151: .put(rsTarget.getMetaData()
152: .getColumnName(i), rsTarget
153: .getMetaData()
154: .getColumnTypeName(i));
155: break;
156: }
157: }
158: }
159: }
160: int count = columnNames.size();
161: for (int j = 0; j < count; j++) {
162: noMap = true;
163: //dont put column names which are appears in importDefinitin as value columns
164: for (int i = 0; i < vecSourceColumn.size(); i++) {
165: if (columnNames.get(j).toString().equalsIgnoreCase(
166: vecSourceColumn.get(i).toString())) {
167: noMap = false;
168: break;
169: }
170: }
171: if (noMap) {
172: String name = columnNames.get(j).toString();
173: String type = (String) columnTypes.get(name);
174: columnNameNoMap.add(name);
175: columnTypeNoMap.add(type);
176: logger.write("full", "\tColumn " + name
177: + ",from source table " + sourceTableName
178: + ", is automaped. ");
179: }
180: }
181:
182: while (rsSource.next()) {
183: for (int i = 0; i < columnNameNoMap.size(); i++) {
184: String columnName = columnNameNoMap.get(i)
185: .toString();
186: String type = columnTypeNoMap.get(i).toString();
187: String columnValue = new String();
188: // if(CheckType.isBinaryObject(type)){
189: // columnValue=null;
190: // }else{
191: if (rsSource.getObject(columnName) == null) {
192: columnValue = null;
193: } else {
194: columnValue = rsSource.getObject(columnName)
195: .toString();
196: }
197: if (columnValue != null) {
198: if (columnValue.equalsIgnoreCase("")) {
199: columnValue = null;
200: }
201: }
202: // }
203: columnValueNoMap.add(columnValue);
204: }
205: }
206: logger.write("full", "\tAuto maping columns is finished.");
207:
208: rsSource.close();
209: stmtSource.close();
210:
211: rsTarget.close();
212: stmtTarget.close();
213:
214: } catch (SQLException ex) {
215: String msg = "Sorry, but you can't connect to source table: "
216: + sourceTableName;
217: LoaderException le = new LoaderException(msg + "\n" + ex,
218: (Throwable) ex);
219: throw le;
220: }
221: }
222:
223: /**
224: * This method read value of columnNameNoMap parameter
225: * @return value of parameter
226: */
227: public Vector getNoMapSourceColumnName() {
228: return columnNameNoMap;
229: }
230:
231: /**
232: * This method read value of columnValueNoMap parameter
233: * @return value of parameter
234: */
235: public Vector getNoMapSourceColumnValue() {
236: return columnValueNoMap;
237: }
238:
239: /**
240: * This method read value of columnTypeNoMap parameter
241: * @return value of parameter
242: */
243: public Vector getNoMapSourceColumnType() {
244: return columnTypeNoMap;
245: }
246: }
|