1 2 22 23 package org.webdocwf.util.loader.generator; 24 25 import java.sql.Connection ; 26 import java.sql.ResultSet ; 27 import java.util.ArrayList ; 28 import java.util.Arrays ; 29 import java.util.List ; 30 31 import org.webdocwf.util.loader.LoaderException; 32 import org.webdocwf.util.loader.logging.Logger; 33 import org.webdocwf.util.loader.logging.StandardLogger; 34 35 40 public class TableDesignReader { 41 42 private static List listColumnNames = null; 43 private static List listTargetTableNames = null; 44 private static List listTargetTableID = null; 45 private static List listColumnType = null; 46 private static List listColumnLenght = null; 47 private static List listAllowNulls = null; 48 private Logger logger; 49 50 private ImportDefinitionAttributes importDefinitionAttributes = new ImportDefinitionAttributes(); 51 private MappingTypeData mappingTypeData; 52 60 61 public TableDesignReader(String tableName, Connection conn, String catalogName, InputParameters generatorParameters) throws LoaderException { 62 setLogger(); 63 this.logger.write("full", "TableDesignReader is started."); 64 listColumnNames = new ArrayList (); 65 listTargetTableNames = new ArrayList (); 66 listTargetTableID = new ArrayList (); 67 listColumnType = new ArrayList (); 68 listColumnLenght = new ArrayList (); 69 listAllowNulls = new ArrayList (); 70 71 importDefinitionAttributes.setName(tableName); 72 importDefinitionAttributes.setTableName(tableName); 73 74 try { 75 if(generatorParameters.getSourceType() != null && 76 (generatorParameters.getSourceType().equals("Hsqldb") || 77 generatorParameters.getSourceType().equals("HypersonicSQL") || 78 generatorParameters.getSourceType().equals("DB2")) 79 ) { 80 catalogName = null; 81 } 82 ResultSet rs = conn.getMetaData().getColumns(catalogName, null, tableName, "%"); 83 84 while (rs.next()) { 85 listColumnNames.add(rs.getString(4)); 86 listTargetTableNames.add(tableName); 87 listTargetTableID.add("0"); 88 if (generatorParameters.getSourceType().equalsIgnoreCase(generatorParameters.getTargetType())) { 90 listColumnType.add(rs.getString(6)); 91 } else { 92 mappingTypeData = new MappingTypeData(rs.getString(6), generatorParameters); 93 listColumnType.add(mappingTypeData.getSQLType()); 94 } 95 if (generatorParameters.getIsDecimal(rs.getString(6)).equalsIgnoreCase("true") && 96 generatorParameters.getHasSize(rs.getString(6)).equalsIgnoreCase("true")) { 97 if ((generatorParameters.getTargetType()).equalsIgnoreCase("DB2")) { 99 int lenght = new Integer (rs.getString(7)).intValue(); 100 int dec = new Integer (rs.getString(9)).intValue(); 101 int result = lenght + dec; 102 listColumnLenght.add(" (" + result + "," + rs.getString(9) + ") "); 103 } else { 104 listColumnLenght.add(" (" + rs.getString(7) + "," + rs.getString(9) + ") "); 105 } 106 } else if (generatorParameters.getHasSize(rs.getString(6)).equalsIgnoreCase("false")) { 107 108 listColumnLenght.add(" "); 109 110 } else { 111 listColumnLenght.add("(" + rs.getString(7) + ") "); 113 } 115 116 if (rs.getString(18).equalsIgnoreCase("YES")) { 117 listAllowNulls.add(""); 118 } else { 119 listAllowNulls.add("NOT NULL"); 120 } 121 } 122 123 importDefinitionAttributes.setTagSourceColumnName(getColumnNames()); 124 importDefinitionAttributes.setTagTargetColumnName(getColumnNames()); 125 importDefinitionAttributes.setTagTargetTableName(getTargetTableNames()); 126 importDefinitionAttributes.setTagTargetTableID(getTargetTableID()); 127 importDefinitionAttributes.setTagColumnType(getColumnType()); 128 importDefinitionAttributes.setTagAllowNulls(getAllowNulls()); 129 importDefinitionAttributes.setTagColumnLenght(getColumnLenght()); 130 131 rs.close(); 132 133 } catch (java.sql.SQLException e) { 134 String msg = "Can't get the connection, to " + tableName + " table. "; 135 LoaderException le = new LoaderException(msg + e.getMessage(), (Throwable ) e); 136 this.logger.write("full", "Exception:" + msg + "\n" + le.getStackTraceAsString()); 137 throw le; 138 } catch (Exception e) { 139 String msg = "Exception in class TableDesignReader! "; 140 LoaderException le = new LoaderException(msg + e.getMessage(), (Throwable ) e); 141 this.logger.write("full", "Exception:" + msg + "\n" + le.getStackTraceAsString()); 142 throw le; 143 } 144 this.logger.write("full", "TableDesignReader is finished."); 145 } 146 147 151 public ImportDefinitionAttributes getImportDefinition() { 152 return importDefinitionAttributes; 153 } 154 155 159 public static void setColumnNames(String [] list_ColumnNames) { 160 listColumnNames = Arrays.asList(list_ColumnNames); 161 } 162 163 167 public static String [] getColumnNames() { 168 String [] ret = new String [listColumnNames.size()]; 169 listColumnNames.toArray(ret); 170 return ret; 171 } 172 173 177 public static void setTargetTableNames(String [] list_TargetTableNames) { 178 listTargetTableNames = Arrays.asList(list_TargetTableNames); 179 } 180 181 185 public static String [] getTargetTableNames() { 186 String [] ret = new String [listTargetTableNames.size()]; 187 listTargetTableNames.toArray(ret); 188 return ret; 189 } 190 191 195 public static void setTargetTableID(String [] list_TargetTableID) { 196 listTargetTableID = Arrays.asList(list_TargetTableID); 197 } 198 199 203 public static String [] getTargetTableID() { 204 String [] ret = new String [listTargetTableID.size()]; 205 listTargetTableID.toArray(ret); 206 return ret; 207 } 208 209 213 public static void setColumnType(String [] list_ColumnType) { 214 listColumnType = Arrays.asList(list_ColumnType); 215 } 216 217 221 public static String [] getColumnType() { 222 String [] ret = new String [listColumnType.size()]; 223 listColumnType.toArray(ret); 224 return ret; 225 } 226 227 231 public static void setColumnLenght(String [] list_ColumnLenght) { 232 listColumnLenght = Arrays.asList(list_ColumnLenght); 233 } 234 235 239 public static String [] getColumnLenght() { 240 String [] ret = new String [listColumnLenght.size()]; 241 listColumnLenght.toArray(ret); 242 return ret; 243 } 244 245 249 public static void setAllowNulls(String [] list_AllowNulls) { 250 listAllowNulls = Arrays.asList(list_AllowNulls); 251 } 252 253 257 public static String [] getAllowNulls() { 258 String [] ret = new String [listAllowNulls.size()]; 259 listAllowNulls.toArray(ret); 260 return ret; 261 } 262 266 private void setLogger() { 267 this.logger = StandardLogger.getCentralLogger(); 268 } 269 } | Popular Tags |