1 2 22 23 package org.webdocwf.util.loader.generator; 24 25 import java.io.File ; 26 import java.net.URL ; 27 import java.sql.Connection ; 28 import java.sql.DriverManager ; 29 import java.sql.ResultSet ; 30 import java.sql.Statement ; 31 import java.util.StringTokenizer ; 32 33 import org.webdocwf.util.loader.LoaderException; 34 import org.webdocwf.util.loader.logging.Logger; 35 import org.webdocwf.util.loader.logging.StandardLogger; 36 import org.webdocwf.util.loader.wizard.AddClassPath; 37 38 39 45 public class CsvTableDesignReader { 46 47 private ImportDefinitionAttributes importDefinitionAttributes= new ImportDefinitionAttributes(); 48 private MappingTypeData mappingTypeData; 49 private Logger logger; 50 56 public CsvTableDesignReader(String tableName, InputParameters generatorParameters) 57 throws LoaderException { 58 setLogger(); 59 this.logger.write("full", "CsvTableDesignReader is started."); 60 importDefinitionAttributes.setName(tableName); 61 importDefinitionAttributes.setTableName(tableName); 62 63 try{ 64 JdbcParameters sourceJdbc= new JdbcParameters("source", generatorParameters); 65 Connection conn = null; 66 URL url=null; 67 String app=""; 68 String path=AddClassPath.getClassPathString(); 69 if(path!=null){ 70 StringTokenizer st=new StringTokenizer (path,";"); 71 int count=0; 72 while(st.hasMoreTokens()) { 73 GeneratorClassLoader.addURL( 74 new File (st.nextElement().toString()).toURL()); 75 } 76 Class.forName(sourceJdbc.getJdbcParameters("JdbcDriver")); 77 }else{ 78 Class.forName(sourceJdbc.getJdbcParameters("JdbcDriver")); 79 } 80 conn=DriverManager.getConnection(sourceJdbc.getJdbcParameters("Connection.Url"), 81 sourceJdbc.getJdbcParameters("User"),sourceJdbc.getJdbcParameters("Password")); 82 83 84 Statement stmt=conn.createStatement(); 85 ResultSet results=stmt.executeQuery("SELECT * FROM "+""+tableName+""); 86 int numberC=results.getMetaData().getColumnCount(); 87 String [] columnNames=new String [numberC]; 88 String [] targetTableNames= new String [numberC]; 89 String [] targetTableID= new String [numberC]; 90 String [] columnTargetTypes=new String [numberC]; 91 String [] columnLength=new String [numberC]; 92 String [] columnIsNullable=new String [numberC]; 93 94 for(int x=1; x<numberC+1; x++){ 95 columnNames[x-1] =results.getMetaData().getColumnName(x); 96 targetTableNames[x-1]=tableName; 97 targetTableID[x-1]="0"; 98 99 if(generatorParameters.getSourceType().equalsIgnoreCase("access")){ 100 101 String sourceTypeData=results.getMetaData().getColumnTypeName(x); 102 mappingTypeData=new MappingTypeData(sourceTypeData, generatorParameters); 103 String targetTypeData=mappingTypeData.getSQLType(); 104 columnTargetTypes[x-1]=targetTypeData; 105 if(generatorParameters.getIsDecimal(sourceTypeData).equalsIgnoreCase("true")|| 107 generatorParameters.getIsDecimal(targetTypeData).equalsIgnoreCase("true")){ 108 columnLength[x-1]=results.getMetaData().getPrecision(x)+","+ 109 results.getMetaData().getScale(x); 110 }else if(generatorParameters.getHasSize(sourceTypeData).equalsIgnoreCase("false")){ 117 118 columnLength[x-1]=" "; 119 }else{ 120 columnLength[x-1]=String.valueOf(results.getMetaData().getPrecision(x)); 121 } 122 if(results.getMetaData().isNullable(x)==0){ 123 columnIsNullable[x-1]="NOT NULL"; 124 }else{ 125 columnIsNullable[x-1]=""; 126 } 127 } 128 } 129 130 importDefinitionAttributes.setTagSourceColumnName(columnNames); 131 importDefinitionAttributes.setTagTargetColumnName(columnNames); 132 importDefinitionAttributes.setTagTargetTableName(targetTableNames); 133 importDefinitionAttributes.setTagTargetTableID(targetTableID); 134 if(generatorParameters.getSourceType().equalsIgnoreCase("access")){ 136 137 importDefinitionAttributes.setTagColumnType(columnTargetTypes); 138 importDefinitionAttributes.setTagColumnLenght(columnLength); 139 importDefinitionAttributes.setTagAllowNulls(columnIsNullable); 140 } 141 142 results.close(); 143 stmt.close(); 144 conn.close(); 145 }catch(Exception e){ 146 String msg="Exception in CsvTableDesignReader class:"; 147 LoaderException le=new LoaderException(msg+"\n"+e.getMessage(), (Throwable )e); 149 this.logger.write("full", "Error:Exception in CsvTableDesignReader class."+"\n"+le.getStackTraceAsString()); 150 throw le; 151 } 152 this.logger.write("full", "CsvTableDesignReader is finished."); 153 } 154 155 159 public ImportDefinitionAttributes getImportDefinition(){ 160 return importDefinitionAttributes; 161 } 162 163 167 private void setLogger() { 168 this.logger = StandardLogger.getCentralLogger(); 169 } 170 } | Popular Tags |