1 package org.apache.ojb.tools.mapping.reversedb; 2 3 17 18 import java.util.Iterator ; 19 import javax.swing.tree.TreeNode ; 20 21 26 public class DBCatalog implements MetadataNodeInterface, TreeNode , org.apache.ojb.tools.mapping.reversedb.gui.PropertySheetModel 27 { 28 private DBMeta parsedMetaData; 29 private java.sql.DatabaseMetaData dbMeta; 30 31 private boolean enabled = true; 32 33 private String strCatalogName; 34 private java.util.TreeMap hmSchemas = new java.util.TreeMap (); 35 36 public DBCatalog(java.sql.DatabaseMetaData pdbMeta, DBMeta paMeta, 37 String pstrCatalogName) 38 { 39 this.dbMeta = pdbMeta; 40 this.parsedMetaData = paMeta; 41 this.strCatalogName = pstrCatalogName; 42 } 43 44 public boolean isEnabled() 45 { 46 return this.enabled; 47 } 48 49 public void setEnabled(boolean b) 50 { 51 this.enabled = b; 52 } 53 54 public DBSchema getSchema(String strSchemaName) 55 { 56 return (DBSchema)this.hmSchemas.get(strSchemaName); 57 } 58 59 public void putSchema(String key, DBSchema schema) 60 { 61 this.hmSchemas.put(key, schema); 62 } 63 64 public String getCatalogName() 65 { 66 return this.strCatalogName; 67 } 68 69 public DBMeta getDBMeta() 70 { 71 return parsedMetaData; 72 } 73 74 public boolean isTreeEnabled() 75 { 76 return getDBMeta().isEnabled() && this.isEnabled(); 77 } 78 79 public void generateReferences() 80 throws java.sql.SQLException 81 { 82 dbMeta.getConnection().setCatalog(this.getCatalogName()); 84 Iterator it = this.hmSchemas.values().iterator(); 85 while (it.hasNext()) 86 { 87 ((DBSchema)it.next()).generateReferences(); 88 } 89 } 90 91 public void read() 92 throws java.sql.SQLException 93 { 94 java.sql.ResultSet rs = dbMeta.getSchemas(); 96 int count = 0; 97 while (rs.next()) 98 { 99 count++; 100 String strSchemaName = rs.getString("TABLE_SCHEM"); 101 strSchemaName = strSchemaName.trim(); 103 try 104 { 105 if (new org.apache.regexp.RE(this.getDBMeta().getSchemaPattern()).match(strSchemaName)) 106 { 107 this.hmSchemas.put(strSchemaName, new DBSchema(dbMeta, this, strSchemaName)); 108 } 109 } 110 catch (org.apache.regexp.RESyntaxException ex) 111 { 112 ex.printStackTrace(); 114 } 115 116 } 117 if (count == 0) 119 { 120 this.hmSchemas.put("", new DBSchema(dbMeta, this, "")); 121 } 122 123 rs.close(); 124 Iterator it = hmSchemas.values().iterator(); 125 while (it.hasNext()) ((DBSchema)it.next()).read(); 126 127 } 128 129 public void addTable(String strSchemaName, String strTableName, String strTableType) 130 throws java.sql.SQLException 131 { 132 DBSchema aDBSchema= this.getSchema(strSchemaName); 133 if (aDBSchema == null) 134 { 135 aDBSchema = new DBSchema(dbMeta, this, strSchemaName); 136 this.putSchema(strSchemaName, aDBSchema); 137 aDBSchema.read(); 138 } 139 aDBSchema.addTable(strTableName, strTableType); 140 } 141 142 public void addColumn(String strSchemaName, String strTableName, String strColumnName, 143 int iDataType, String strTypeName, int iColumnSize, int iNullable) 144 { 145 DBSchema aDBSchema = this.getSchema(strSchemaName); 146 if (aDBSchema != null) 147 { 148 aDBSchema.addColumn(strTableName, strColumnName, 149 iDataType, strTypeName, iColumnSize, iNullable); 150 } 151 } 152 153 public void addPrimaryKeyColumn(String strSchemaName, String strTableName, 154 String strColumnName) 155 { 156 DBSchema aDBSchema = this.getSchema(strSchemaName); 157 if (aDBSchema != null) 158 { 159 aDBSchema.addPrimaryKeyColumn(strTableName, strColumnName); 160 } 161 } 162 163 164 public java.util.Enumeration children() 165 { 166 return java.util.Collections.enumeration(this.hmSchemas.values()); 167 } 168 169 public boolean getAllowsChildren() 170 { 171 return true; 172 } 173 174 public TreeNode getChildAt(int param) 175 { 176 TreeNode tn = (TreeNode )this.hmSchemas.values().toArray()[param]; 177 return tn; 178 } 179 180 public int getChildCount() 181 { 182 return this.hmSchemas.size(); 183 } 184 185 public int getIndex(TreeNode treeNode) 186 { 187 int indexOf = new java.util.Vector (this.hmSchemas.values()).indexOf(treeNode); 188 return indexOf; 189 } 190 191 public TreeNode getParent() 192 { 193 return this.parsedMetaData; 194 } 195 196 public boolean isLeaf() 197 { 198 return false; 199 } 200 201 public String toString() 202 { 203 if (this.strCatalogName == null || this.strCatalogName.trim().length() == 0) 204 return "<empty catalog>"; 205 else return this.strCatalogName; 206 } 207 208 public Class getPropertySheetClass() 209 { 210 return org.apache.ojb.tools.mapping.reversedb.gui.DBCatalogPropertySheet.class; 211 } 212 213 public String getXML() 214 { 215 java.io.StringWriter swr = new java.io.StringWriter (); 216 writeXML(new java.io.PrintWriter (swr)); 217 return swr.getBuffer().toString(); 218 } 219 220 public void writeXML(java.io.PrintWriter pw) 221 { 222 Iterator i = this.hmSchemas.values().iterator(); 223 while (i.hasNext()) 224 { 225 ((DBSchema)i.next()).writeXML(pw); 226 } 227 } 228 229 230 public void generateJava(java.io.File aFile, String strHeader, String strFooter) throws java.io.IOException , java.io.FileNotFoundException 231 { 232 Iterator i = this.hmSchemas.values().iterator(); 233 while (i.hasNext()) ((DBSchema)i.next()).generateJava(aFile, strHeader, strFooter); 234 } 235 236 public void setPackage(String packageName) 237 { 238 Iterator i = this.hmSchemas.values().iterator(); 239 while (i.hasNext()) ((DBSchema)i.next()).setPackage(packageName); 240 } 241 242 public void disableClassesWithRegex(org.apache.regexp.RE aRegexp) 243 { 244 Iterator it = this.hmSchemas.values().iterator(); 245 while (it.hasNext()) ((DBSchema)it.next()).disableClassesWithRegex(aRegexp); 246 } 247 248 249 } 250 251 252 318 | Popular Tags |