1 22 23 24 25 package org.webdocwf.util.loader; 26 27 import java.util.*; 28 import java.sql.*; 29 import org.webdocwf.util.loader.logging.*; 30 31 38 public class LoadAllSourceData { 39 40 private Vector columnNameNoMap=new Vector(); 41 private Vector columnValueNoMap=new Vector(); 42 private Vector columnTypeNoMap=new Vector(); 43 44 45 46 47 63 public LoadAllSourceData(Connection connSource, String sourceTableName, Vector vecSourceColumn, 64 String targetTableName,Connection connTarget, 65 Logger logger, Vector vecRelationColumns, 66 Vector vecVariableColumns, Vector vecConstantColumns, 67 Vector vecUseIDColumns,Vector vecTimesColumns,boolean columnsSuportedTarget, ConfigReader configReaderTarget) throws LoaderException { 68 69 70 boolean noMap=true; 71 Vector columnNames=new Vector(); 72 Hashtable columnTypes=new Hashtable(); 73 try{ 74 75 Statement stmtSource=connSource.createStatement(); 76 Statement stmtTarget=connTarget.createStatement(); 77 for (int i = 0; i < vecRelationColumns.size(); i++) { 79 vecSourceColumn.add(vecRelationColumns.get(i).toString()); 80 } 81 for (int i = 0; i < vecVariableColumns.size(); i++) { 82 vecSourceColumn.add(vecVariableColumns.get(i).toString()); 83 } 84 for (int i = 0; i < vecConstantColumns.size(); i++) { 85 vecSourceColumn.add(vecConstantColumns.get(i).toString()); 86 } 87 for (int i = 0; i < vecUseIDColumns.size(); i++) { 88 vecSourceColumn.add(vecUseIDColumns.get(i).toString()); 89 } 90 for (int i = 0; i < vecTimesColumns.size(); i++) { 91 vecSourceColumn.add(vecTimesColumns.get(i).toString()); 92 } 93 94 ResultSet rsSource=stmtSource.executeQuery("select * from "+sourceTableName); 95 int countSourceColumns=rsSource.getMetaData().getColumnCount(); 98 99 ResultSet rsTarget=null; 100 if (columnsSuportedTarget){ 101 102 rsTarget = connTarget.getMetaData().getColumns( connTarget.getCatalog(), null, targetTableName, "%" ); 103 String columnName = ""; 105 String columnType = ""; 106 while(rsTarget.next()){ 107 columnName = rsTarget.getString(4); 108 columnType = rsTarget.getString(6); 109 for (int j = 1; j < countSourceColumns+1; j++) { 110 if(columnName.equalsIgnoreCase(rsSource.getMetaData().getColumnName(j))){ 111 columnNames.add(columnName); 112 columnTypes.put(columnName,columnType); 113 break; 114 } 115 } 116 } 117 }else{ 119 if (configReaderTarget.getMaxRowsSupported()){ 121 stmtTarget.setMaxRows(1); 122 } 123 rsTarget=stmtTarget.executeQuery("select * from "+targetTableName); 124 int countTargetColumns=rsTarget.getMetaData().getColumnCount(); 125 126 logger.write("full", "\tAuto maping columns is started."); 127 for (int i = 1; i < countTargetColumns+1; i++) { 128 for (int j = 1; j < countSourceColumns+1; j++) { 129 if(rsTarget.getMetaData().getColumnName(i).equalsIgnoreCase(rsSource.getMetaData().getColumnName(j))){ 130 columnNames.add(rsTarget.getMetaData().getColumnName(i)); 131 columnTypes.put(rsTarget.getMetaData().getColumnName(i),rsTarget.getMetaData().getColumnTypeName(i)); 132 break; 133 } 134 } 135 } 136 } 137 int count=columnNames.size(); 138 for (int j = 0; j < count; j++) { 139 noMap=true; 140 for (int i = 0; i < vecSourceColumn.size(); i++) { 142 if(columnNames.get(j).toString().equalsIgnoreCase(vecSourceColumn.get(i).toString())){ 143 noMap=false; 144 break; 145 } 146 } 147 if(noMap){ 148 String name=columnNames.get(j).toString(); 149 String type=(String )columnTypes.get(name); 150 columnNameNoMap.add(name); 151 columnTypeNoMap.add(type); 152 logger.write("full", "\tColumn "+name+",from source table "+sourceTableName+", is automaped. "); 153 } 154 } 155 156 157 while(rsSource.next()){ 158 for (int i = 0; i <columnNameNoMap.size(); i++) { 159 String columnName=columnNameNoMap.get(i).toString(); 160 String type=columnTypeNoMap.get(i).toString(); 161 String columnValue=new String (); 162 if(rsSource.getObject(columnName)==null){ 166 columnValue=null; 167 }else{ 168 columnValue=rsSource.getObject(columnName).toString(); 169 } 170 if(columnValue!=null){ 171 if(columnValue.equalsIgnoreCase("")){ 172 columnValue=null; 173 } 174 } 175 columnValueNoMap.add(columnValue); 177 } 178 } 179 logger.write("full", "\tAuto maping columns is finished."); 180 181 rsSource.close(); 182 stmtSource.close(); 183 184 rsTarget.close(); 185 stmtTarget.close(); 186 187 }catch(SQLException ex){ 188 String msg="Sorry, but you can't connect to source table: "+sourceTableName; 189 LoaderException le =new LoaderException(msg+"\n"+ex,(Throwable )ex); 190 throw le; 191 } 192 } 193 194 195 199 public Vector getNoMapSourceColumnName(){ 200 return columnNameNoMap; 201 } 202 203 207 public Vector getNoMapSourceColumnValue(){ 208 return columnValueNoMap; 209 } 210 211 215 public Vector getNoMapSourceColumnType(){ 216 return columnTypeNoMap; 217 } 218 } | Popular Tags |