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.Hashtable ; 30 import java.util.List ; 31 32 import org.webdocwf.util.loader.LoaderException; 33 import org.webdocwf.util.loader.logging.Logger; 34 import org.webdocwf.util.loader.logging.StandardLogger; 35 36 43 public class TableRelationshipsReader { 44 45 private static List listPrimaryKeys = null; 46 private static List listIndexVariables = null; 47 private static List listForeignVariables = null; 48 private Hashtable htIndexAndReplacement = new Hashtable (); 49 private RelationshipsAttributes relationshipsAttributes = new RelationshipsAttributes(); 50 private Logger logger; 51 57 public TableRelationshipsReader(Connection conn, String tableName, String catalogName, InputParameters generatorParameters, int constraintCount) throws LoaderException{ 58 setLogger(); 59 this.logger.write("full", "TableRelationshipsReader is started."); 60 listPrimaryKeys = new ArrayList (); 61 listIndexVariables = new ArrayList (); 62 listForeignVariables = new ArrayList (); 63 String maxConstraintLenght = generatorParameters.getMaxConstraintLength(); 64 int maxConstraintInt = 0; 65 if (!maxConstraintLenght.equalsIgnoreCase("") && !maxConstraintLenght.equalsIgnoreCase("-1")) { 66 maxConstraintInt = Integer.parseInt(maxConstraintLenght); 67 } 68 70 try { 71 boolean unique = false; 72 boolean app = false; 73 if(generatorParameters.getSourceType() != null && 74 (generatorParameters.getSourceType().equals("Hsqldb") || 75 generatorParameters.getSourceType().equals("HypersonicSQL") || 76 generatorParameters.getSourceType().equals("DB2") )) { 77 catalogName = null; 78 } 79 ResultSet rs2 = conn.getMetaData().getIndexInfo(catalogName, null, tableName, false, true); 81 ResultSet rs3 = conn.getMetaData().getPrimaryKeys(catalogName, null, tableName); 82 ResultSet rs5 = conn.getMetaData().getImportedKeys(catalogName, null, tableName); 83 84 while (rs3.next()) { 86 String primaryKeyName = rs3.getString(6); 88 if (maxConstraintInt > 0 && primaryKeyName.length() > maxConstraintInt) { 89 String newName = primaryKeyName.substring(0, maxConstraintInt - String.valueOf(constraintCount).length()); 90 primaryKeyName = newName + String.valueOf(constraintCount); 91 constraintCount++; 92 } 93 listPrimaryKeys.add(primaryKeyName); 94 listPrimaryKeys.add(rs3.getString(4)); 96 } 97 rs3.close(); 98 99 while (rs2.next()) { 101 if (rs2.getString(6) != null) { 102 listIndexVariables.add(rs2.getString(4)); 107 String indexName = rs2.getString(6); 109 String keyIndexName = indexName; 111 if (maxConstraintInt > 0 && indexName.length() > maxConstraintInt) { 112 String newName = indexName.substring(0, maxConstraintInt - String.valueOf(constraintCount).length()); 113 indexName = newName + String.valueOf(constraintCount); 114 if (!htIndexAndReplacement.containsKey(keyIndexName)) { 117 htIndexAndReplacement.put(keyIndexName, indexName); 118 constraintCount++; 119 } else { 120 indexName = htIndexAndReplacement.get(keyIndexName).toString(); 121 } 122 } 124 listIndexVariables.add(indexName); 126 listIndexVariables.add(rs2.getString(9)); 128 } 130 } 131 rs2.close(); 132 133 while (rs5.next()) { 135 listForeignVariables.add(rs5.getString(7)); 137 String foreignKeyName = rs5.getString(12); 139 if (maxConstraintInt > 0 && foreignKeyName.length() > maxConstraintInt) { 140 String newName = foreignKeyName.substring(0, maxConstraintInt - String.valueOf(constraintCount).length()); 141 foreignKeyName = newName + String.valueOf(constraintCount); 142 constraintCount++; 143 } 144 listForeignVariables.add(foreignKeyName); 146 listForeignVariables.add(rs5.getString(8)); 148 listForeignVariables.add(rs5.getString(3)); 150 listForeignVariables.add(rs5.getString(4)); 152 } 153 rs5.close(); 154 155 RelationshipsAttributes.setTableName(tableName); 156 RelationshipsAttributes.setPrimaryKeys(getPrimaryKeys()); 157 RelationshipsAttributes.setIndexVariables(getIndexVariables()); 158 RelationshipsAttributes.setForeignVariables(getForeignVariables()); 159 160 } catch (Exception e) { 161 LoaderException le = new LoaderException("Exception:",e); 162 this.logger.write("full", "Exception in class TableRelationshipsReader."+"\n"+le.getStackTraceAsString()); 163 throw le; 164 } 165 this.logger.write("full", "TableRelationshipsReader is finished."); 166 } 167 168 172 public static void setPrimaryKeys(String [] primary_Keys) { 173 listPrimaryKeys = Arrays.asList(primary_Keys); 174 } 175 176 180 public static String [] getPrimaryKeys() { 181 String [] ret = new String [listPrimaryKeys.size()]; 182 listPrimaryKeys.toArray(ret); 183 return ret; 184 } 185 186 190 public static void setIndexVariables(String [] index_Variables) { 191 listIndexVariables = Arrays.asList(index_Variables); 192 } 193 194 198 public static String [] getIndexVariables() { 199 String [] ret = new String [listIndexVariables.size()]; 200 listIndexVariables.toArray(ret); 201 return ret; 202 } 203 204 208 public static void setForeignVariables(String [] foreign_Variables) { 209 listForeignVariables = Arrays.asList(foreign_Variables); 210 } 211 212 216 public static String [] getForeignVariables() { 217 String [] ret = new String [listForeignVariables.size()]; 218 listForeignVariables.toArray(ret); 219 return ret; 220 } 221 222 226 public RelationshipsAttributes getRelationshipsAttributes() { 227 228 return relationshipsAttributes; 229 } 230 234 private void setLogger() { 235 this.logger = StandardLogger.getCentralLogger(); 236 } 237 } 238 | Popular Tags |