View Javadoc
1 /* 2 Loader - tool for transfering data from one JDBC source to another and 3 doing transformations during copy. 4 Copyright (C) 2002-2003 Together 5 This library is free software; you can redistribute it and/or 6 modify it under the terms of the GNU Lesser General Public 7 License as published by the Free Software Foundation; either 8 version 2.1 of the License, or (at your option) any later version. 9 This library is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 You should have received a copy of the GNU Lesser General Public 14 License along with this library; if not, write to the Free Software 15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 16 Loader.java 17 Date: 03.03.2003. 18 @version 2.1 alpha 19 @authors: 20 Radoslav Dutina rale@prozone.co.yu 21 */ 22 23 24 25 package org.webdocwf.util.loader; 26 27 import java.sql.Connection; 28 import java.sql.ResultSet; 29 import java.sql.SQLException; 30 import java.sql.Statement; 31 import java.text.SimpleDateFormat; 32 import java.util.Date; 33 import java.util.Hashtable; 34 import java.util.Vector; 35 36 import org.webdocwf.util.loader.logging.Logger; 37 38 /*** 39 * 40 * LoadAllSourceData class is used for loading source data which are not maped in 41 * importDefinition jobs. 42 * @author Radoslav Dutina 43 * @version 1.0 44 */ 45 public class LoadAllSourceData { 46 47 private Vector columnNameNoMap=new Vector(); 48 private Vector columnValueNoMap=new Vector(); 49 private Vector columnTypeNoMap=new Vector(); 50 51 52 53 54 /*** 55 * Public constructor of LoadAllSourceData class. Construct object LoadAllSourceData with an 56 * associated parameters. 57 * @param connSource defines the connection to source database 58 * @param connTarget defines the connection to target database 59 * @param sourceTableName defines soruce table name 60 * @param vecSourceColumn defines all source clolumn names, for the named source table 61 * @param targetTableName defines target table name 62 * @param logger defines object of Lo9gger class 63 * @param vecRelationColumns defines relation columns for target table 64 * @param vecVariableColumns defines variable columns for target table 65 * @param vecConstantColumns defines constant columns for target table 66 * @param vecUseIDColumns defines ID columns for target table 67 * @param vecTimesColumns defines time columns for target table 68 * @throws LoaderException 69 */ 70 public LoadAllSourceData(Connection connSource, String sourceTableName, Vector vecSourceColumn, 71 String targetTableName,Connection connTarget, 72 Logger logger, Vector vecRelationColumns, 73 Vector vecVariableColumns, Vector vecConstantColumns, 74 Vector vecUseIDColumns,Vector vecTimesColumns,boolean columnsSuportedTarget, ConfigReader configReaderTarget) throws LoaderException { 75 76 77 boolean noMap=true; 78 Vector columnNames=new Vector(); 79 Hashtable columnTypes=new Hashtable(); 80 try{ 81 82 Statement stmtSource=connSource.createStatement(); 83 Statement stmtTarget=connTarget.createStatement(); 84 //add columns that are apears in relations, variables, constant.... 85 for (int i = 0; i < vecRelationColumns.size(); i++) { 86 vecSourceColumn.add(vecRelationColumns.get(i).toString()); 87 } 88 for (int i = 0; i < vecVariableColumns.size(); i++) { 89 vecSourceColumn.add(vecVariableColumns.get(i).toString()); 90 } 91 for (int i = 0; i < vecConstantColumns.size(); i++) { 92 vecSourceColumn.add(vecConstantColumns.get(i).toString()); 93 } 94 for (int i = 0; i < vecUseIDColumns.size(); i++) { 95 vecSourceColumn.add(vecUseIDColumns.get(i).toString()); 96 } 97 for (int i = 0; i < vecTimesColumns.size(); i++) { 98 vecSourceColumn.add(vecTimesColumns.get(i).toString()); 99 } 100 101 ResultSet rsSource=stmtSource.executeQuery("select * from "+sourceTableName); 102 //ZK change this. Because of problems with getColumnTypeName()method. Some drivers doesn't support it. 103 //start 104 int countSourceColumns=rsSource.getMetaData().getColumnCount(); 105 106 ResultSet rsTarget=null; 107 if (columnsSuportedTarget){ 108 109 rsTarget = connTarget.getMetaData().getColumns( connTarget.getCatalog(), null, targetTableName, "%" ); 110 //int countTargetColumns=rsTarget.getMetaData().getColumnCount(); 111 String columnName = ""; 112 String columnType = ""; 113 while(rsTarget.next()){ 114 columnName = rsTarget.getString(4); 115 columnType = rsTarget.getString(6); 116 for (int j = 1; j < countSourceColumns+1; j++) { 117 if(columnName.equalsIgnoreCase(rsSource.getMetaData().getColumnName(j))){ 118 columnNames.add(columnName); 119 columnTypes.put(columnName,columnType); 120 break; 121 } 122 } 123 } 124 //end 125 }else{ 126 //TODO ZK ADDED stmtTarget.setMaxRows(1). Place this as parameter in conf file, like maxRowsSuported 127 if (configReaderTarget.getMaxRowsSupported()){ 128 stmtTarget.setMaxRows(1); 129 } 130 rsTarget=stmtTarget.executeQuery("select * from "+targetTableName); 131 int countTargetColumns=rsTarget.getMetaData().getColumnCount(); 132 133 logger.write("full", "\tAuto maping columns is started."); 134 for (int i = 1; i < countTargetColumns+1; i++) { 135 for (int j = 1; j < countSourceColumns+1; j++) { 136 if(rsTarget.getMetaData().getColumnName(i).equalsIgnoreCase(rsSource.getMetaData().getColumnName(j))){ 137 columnNames.add(rsTarget.getMetaData().getColumnName(i)); 138 columnTypes.put(rsTarget.getMetaData().getColumnName(i),rsTarget.getMetaData().getColumnTypeName(i)); 139 break; 140 } 141 } 142 } 143 } 144 int count=columnNames.size(); 145 for (int j = 0; j < count; j++) { 146 noMap=true; 147 //dont put column names which are appears in importDefinitin as value columns 148 for (int i = 0; i < vecSourceColumn.size(); i++) { 149 if(columnNames.get(j).toString().equalsIgnoreCase(vecSourceColumn.get(i).toString())){ 150 noMap=false; 151 break; 152 } 153 } 154 if(noMap){ 155 String name=columnNames.get(j).toString(); 156 String type=(String)columnTypes.get(name); 157 columnNameNoMap.add(name); 158 columnTypeNoMap.add(type); 159 logger.write("full", "\tColumn "+name+",from source table "+sourceTableName+", is automaped. "); 160 } 161 } 162 163 164 while(rsSource.next()){ 165 for (int i = 0; i <columnNameNoMap.size(); i++) { 166 String columnName=columnNameNoMap.get(i).toString(); 167 String type=columnTypeNoMap.get(i).toString(); 168 String columnValue=new String(); 169 // if(CheckType.isBinaryObject(type)){ 170 // columnValue=null; 171 // }else{ 172 if(rsSource.getObject(columnName)==null){ 173 columnValue=null; 174 }else{ 175 columnValue=rsSource.getObject(columnName).toString(); 176 } 177 if(columnValue!=null){ 178 if(columnValue.equalsIgnoreCase("")){ 179 columnValue=null; 180 } 181 } 182 // } 183 columnValueNoMap.add(columnValue); 184 } 185 } 186 logger.write("full", "\tAuto maping columns is finished."); 187 188 rsSource.close(); 189 stmtSource.close(); 190 191 rsTarget.close(); 192 stmtTarget.close(); 193 194 }catch(SQLException ex){ 195 String msg="Sorry, but you can't connect to source table: "+sourceTableName; 196 LoaderException le =new LoaderException(msg+"\n"+ex,(Throwable)ex); 197 throw le; 198 } 199 } 200 201 202 /*** 203 * This method read value of columnNameNoMap parameter 204 * @return value of parameter 205 */ 206 public Vector getNoMapSourceColumnName(){ 207 return columnNameNoMap; 208 } 209 210 /*** 211 * This method read value of columnValueNoMap parameter 212 * @return value of parameter 213 */ 214 public Vector getNoMapSourceColumnValue(){ 215 return columnValueNoMap; 216 } 217 218 /*** 219 * This method read value of columnTypeNoMap parameter 220 * @return value of parameter 221 */ 222 public Vector getNoMapSourceColumnType(){ 223 return columnTypeNoMap; 224 } 225 }

This page was automatically generated by Maven