1 package org.apache.ojb.tools.mapping.reversedb; 2 3 17 18 import javax.swing.tree.TreeNode ; 19 import java.sql.SQLException ; 20 import java.io.File ; 21 22 27 public class DBTable implements MetadataNodeInterface, TreeNode , org.apache.ojb.tools.mapping.reversedb.gui.PropertySheetModel 28 { 29 private java.sql.DatabaseMetaData dbMeta; 30 private DBSchema aSchema; 31 private java.util.HashMap hmReferences = new java.util.HashMap (0); 32 private java.util.HashMap hmCollections = new java.util.HashMap (0); 33 private java.util.TreeMap tmColumns = new java.util.TreeMap (); 34 private java.util.Vector vSubTreeNodes = new java.util.Vector (); 35 private String strTableName; 36 private String strClassName; 37 private String strPackageName = ""; private String strConversionStrategyClass = ""; 39 40 private boolean dynamicProxy = false; 41 42 private boolean enabled = true; 43 44 45 46 public DBTable (java.sql.DatabaseMetaData pdbMeta, DBSchema paSchema, 47 String pstrTableName) 48 { 49 strTableName = pstrTableName; 50 this.strClassName = Namer.nameClass(this.strTableName); 52 aSchema = paSchema; 53 dbMeta = pdbMeta; 54 } 55 56 public boolean hasDynamicProxy() 57 { 58 return dynamicProxy; 59 } 60 61 public void setDynamicProxy(boolean b) 62 { 63 dynamicProxy = b; 64 } 65 66 public String getConversionStrategyClass() 67 { 68 return this.strConversionStrategyClass; 69 } 70 71 public void setConversionStrategyClass(String s) 72 { 73 this.strConversionStrategyClass = s; 74 } 75 76 public boolean isEnabled() 77 { 78 return this.enabled; 79 } 80 81 public void setEnabled(boolean b) 82 { 83 this.enabled = b; 84 } 85 86 public boolean isTreeEnabled() 87 { 88 return this.aSchema.isTreeEnabled() && this.isEnabled(); 89 } 90 91 92 public DBColumn getColumn(String colName) 93 { 94 return (DBColumn)tmColumns.get(colName); 95 } 96 97 public String getTableName() 98 { 99 return strTableName; 100 } 101 102 public String getFQTableName() 103 { 104 String strReturn = null; 105 if (aSchema.getDBCatalog().getDBMeta().getSupportsCatalogsInTableDefinitions ()) 107 { 108 if (aSchema.getDBCatalog().getDBMeta().getIsCatalogAtStart ()) 111 { 112 strReturn = aSchema.getDBCatalog().getCatalogName() 114 + aSchema.getDBCatalog().getDBMeta().getCatalogSeparator () 115 + aSchema.getSchemaName() + "." + this.getTableName(); 116 } 117 else 118 { 119 strReturn = aSchema.getSchemaName() + "." + this.getTableName() 121 + aSchema.getDBCatalog().getDBMeta().getCatalogSeparator () 122 + aSchema.getDBCatalog().getCatalogName(); 123 } 124 } 125 else 126 { 127 strReturn = aSchema.getSchemaName() + "." + this.getTableName(); 128 } 129 return strReturn; 130 } 131 132 public String getClassName() 133 { 134 return strClassName; 135 } 136 137 public void setClassName(String s) 138 { 139 this.strClassName = s; 140 } 141 142 public String getPackageName() 143 { 144 return strPackageName; 145 } 146 147 public void setPackageName(String s) 148 { 149 this.strPackageName = s; 150 } 151 152 public String getFQClassName() 153 { 154 if (this.getPackageName() != null && this.getPackageName().trim().length() > 0) 155 return this.getPackageName() + "." + this.getClassName(); 156 else 157 return this.getClassName(); 158 } 159 160 public DBSchema getDBSchema() 161 { 162 return this.aSchema; 163 } 164 165 166 public void read() throws SQLException 167 { 168 169 } 170 171 public void addColumn(String strColumnName, 172 int iDataType, String strTypeName, int iColumnSize, int iNullable) 173 { 174 DBColumn aDBColumn = new DBColumn(this.dbMeta, this, strColumnName, iDataType, strTypeName); 175 this.tmColumns.put(strColumnName, aDBColumn); 176 } 177 178 public void addPrimaryKeyColumn(String strColumnName) 179 { 180 DBColumn aDBColumn = this.getColumn(strColumnName); 181 if (aDBColumn != null) 182 { 183 aDBColumn.setPrimaryKeyPart(true); 184 } 185 } 186 187 public void generateReferences() throws SQLException 188 { 189 java.util.Iterator it = this.tmColumns.values().iterator(); 193 boolean hasNoPrimaryKey = true; 194 while (hasNoPrimaryKey && it.hasNext()) if ( ((DBColumn)it.next()).isPrimaryKeyPart()) hasNoPrimaryKey = false; 195 if (hasNoPrimaryKey) readPrimaryKeys(); 196 197 generateFKReferences(); 199 generateFKCollections(); 200 vSubTreeNodes.addAll(this.tmColumns.values()); 201 vSubTreeNodes.addAll(this.hmCollections.values()); 202 vSubTreeNodes.addAll(this.hmReferences.values()); 203 } 204 205 public java.util.Enumeration children () 206 { 207 return vSubTreeNodes.elements(); 208 } 209 210 public boolean getAllowsChildren () 211 { 212 return true; 213 } 214 215 public TreeNode getChildAt(int param) 216 { 217 TreeNode tn = 218 (TreeNode )vSubTreeNodes.elementAt(param); 219 return tn; 220 } 221 222 public int getChildCount () 223 { 224 return this.vSubTreeNodes.size(); 225 } 226 227 public int getIndex(TreeNode treeNode) 228 { 229 return this.vSubTreeNodes.indexOf(treeNode); 230 } 231 232 public TreeNode getParent() 233 { 234 return this.aSchema; 235 } 236 237 public boolean isLeaf () 238 { 239 if (this.vSubTreeNodes.size() == 0) return true; 240 else return false; 241 } 242 243 public String toString() 244 { 245 return this.strTableName; 246 } 247 248 249 private void readPrimaryKeys() throws SQLException 250 { 251 java.sql.ResultSet rs = null; 254 try 255 { 256 rs = dbMeta.getPrimaryKeys(null, 257 this.getDBSchema().getSchemaName(), this.strTableName); 258 while (rs.next()) 259 { 260 String strCatalogName = rs.getString("TABLE_CAT"); 261 String strSchemaName = rs.getString("TABLE_SCHEM"); 262 if ( (strSchemaName == null && this.aSchema.getSchemaName() == null || strSchemaName.equals(this.aSchema.getSchemaName())) 263 && 264 (strCatalogName == null && this.aSchema.getDBCatalog().getCatalogName() == null 265 || strCatalogName.equals(this.aSchema.getDBCatalog().getCatalogName()))) 266 { 267 String strColumnName = rs.getString("COLUMN_NAME"); 268 String pkName = rs.getString("PK_NAME"); 269 DBColumn dbcol = (DBColumn)tmColumns.get(strColumnName); 270 if (dbcol != null) dbcol.setPrimaryKeyPart(true); 271 } 272 } 273 } 274 catch (SQLException sqlEx) 275 { 276 } 278 finally 279 { 280 try 281 { 282 rs.close(); 283 } 284 catch (Throwable t) 285 {} } 287 } 288 289 private void generateFKReferences() throws SQLException 290 { 291 java.sql.ResultSet rs = null; 295 try 296 { 297 rs = dbMeta.getImportedKeys(this.getDBSchema().getDBCatalog().getCatalogName(), 298 this.getDBSchema().getSchemaName(), strTableName); 299 while (rs.next()) 300 { 301 String strFKSchemaName = rs.getString("FKTABLE_SCHEM"); 302 String strFKCatalogName = rs.getString("FKTABLE_CAT"); 303 304 if ( (strFKCatalogName == null && this.aSchema.getDBCatalog().getCatalogName() == null || strFKCatalogName.equals(this.aSchema.getDBCatalog().getCatalogName())) 305 && (strFKSchemaName == null && this.aSchema.getSchemaName() == null || strFKSchemaName.equals(this.aSchema.getSchemaName()))) 306 { 307 String strPKCatalogName = rs.getString("PKTABLE_CAT"); 308 String strPKSchemaName = rs.getString("PKTABLE_SCHEM"); 309 String strPKTableName = rs.getString("PKTABLE_NAME"); 310 String strPKColumnName = rs.getString("PKCOLUMN_NAME"); 311 String strFKTableName = rs.getString("FKTABLE_NAME"); 312 String strFKColumnName = rs.getString("FKCOLUMN_NAME"); 313 314 DBCatalog dbPKCatalog = this.aSchema.getDBCatalog().getDBMeta().getCatalog(strPKCatalogName); 316 if (dbPKCatalog != null) 317 { 318 DBSchema dbPKSchem = dbPKCatalog.getSchema(strPKSchemaName); 319 if (dbPKSchem != null) 320 { 321 DBTable dbPKTable = dbPKSchem.getTable(strPKTableName); 322 if (dbPKTable != null) 323 { 324 DBColumn dbPKColumn = dbPKTable.getColumn(strPKColumnName); 325 DBColumn dbFKColumn = getColumn(strFKColumnName); 327 328 DBFKRelation aFKRef = 330 (DBFKRelation)this.hmReferences.get(dbPKSchem.getSchemaName() 331 + "." + dbPKTable.getTableName()); 332 if (aFKRef == null) 333 { 334 aFKRef = new DBFKRelation(dbPKTable, this, false); 335 this.hmReferences.put(dbPKSchem.getSchemaName() 336 + "." + dbPKTable.getTableName(), aFKRef); 337 } 338 aFKRef.addColumnPair(dbPKColumn, dbFKColumn); 339 } 340 } 341 } 342 } 343 } 344 } 345 catch (SQLException sqlEx) 346 { 347 } 349 try 350 { 351 rs.close(); 352 } 353 catch (Throwable t) {} 354 } 355 356 private void generateFKCollections() throws SQLException 357 { 358 java.sql.ResultSet rs = null; 362 try 363 { 364 rs = dbMeta.getExportedKeys(this.getDBSchema().getDBCatalog().getCatalogName(), 365 this.getDBSchema().getSchemaName(), strTableName); 366 while (rs.next()) 367 { 368 String strPKSchemaName = rs.getString("PKTABLE_SCHEM"); 369 String strPKCatalogName = rs.getString("PKTABLE_CAT"); 370 371 if ( (strPKSchemaName == null && this.aSchema.getSchemaName()==null || strPKSchemaName.equals(this.aSchema.getSchemaName())) 372 && (strPKCatalogName == null && this.aSchema.getDBCatalog().getCatalogName() == null 373 || strPKCatalogName.equals(this.aSchema.getDBCatalog().getCatalogName()))) 374 { 375 String strPKTableName = rs.getString("PKTABLE_NAME"); 376 String strPKColumnName = rs.getString("PKCOLUMN_NAME"); 377 String strFKCatalogName= rs.getString("FKTABLE_CAT"); 378 String strFKTableName = rs.getString("FKTABLE_NAME"); 379 String strFKColumnName = rs.getString("FKCOLUMN_NAME"); 380 String strFKSchemaName = rs.getString("FKTABLE_SCHEM"); 381 382 DBCatalog dbFKCatalog = null; 387 if (this.aSchema.getDBCatalog().getDBMeta().getSupportsCatalogsInIndexDefinitions()) 388 { 389 dbFKCatalog = this.aSchema.getDBCatalog().getDBMeta().getCatalog(strFKCatalogName); 390 } 391 else 392 { 393 dbFKCatalog = this.aSchema.getDBCatalog(); 394 } 395 if (dbFKCatalog != null) 396 { 397 DBSchema dbFKSchema = dbFKCatalog.getSchema(strFKSchemaName); 398 if (dbFKSchema != null) 399 { 400 DBTable dbFKTable = dbFKSchema.getTable(strFKTableName); 401 if (dbFKTable != null) 402 { 403 DBColumn dbFKColumn = dbFKTable.getColumn(strFKColumnName); 404 DBColumn dbPKColumn = getColumn(strPKColumnName); 406 DBFKRelation aFKRef = 408 (DBFKRelation)this.hmCollections.get(dbFKSchema.getSchemaName() 409 + "." + dbFKTable.getTableName()); 410 if (aFKRef == null) 411 { 412 aFKRef = new DBFKRelation(this, dbFKTable, true); 413 this.hmCollections.put(dbFKSchema.getSchemaName() 414 + "." + dbFKTable.getTableName(), aFKRef); 415 } 416 aFKRef.addColumnPair(dbPKColumn, dbFKColumn); 417 } 418 } 419 } 420 } 421 } 422 } 423 catch (SQLException sqlEx) 424 { 425 } 427 try 428 { 429 rs.close(); 430 } 431 catch (Throwable t) 432 { 433 } 434 } 435 436 437 public Class getPropertySheetClass () 438 { 439 return org.apache.ojb.tools.mapping.reversedb.gui.DBTablePropertySheet.class; 440 } 441 442 public String getXML() 443 { 444 java.io.StringWriter sw = new java.io.StringWriter (); 445 writeXML(new java.io.PrintWriter (sw)); 446 return sw.getBuffer().toString(); 447 } 448 449 public void writeXML(java.io.PrintWriter pw) 450 { 451 if (this.isTreeEnabled()) 453 { 454 java.util.Iterator it = tmColumns.values().iterator(); 455 if (it.hasNext()) 456 { 457 pw.println("<class-descriptor "); 459 pw.println(" class=\"" + this.getFQClassName() + "\""); 460 pw.println(" table=\"" + this.getFQTableName() + "\">"); 461 if (this.hasDynamicProxy()) 462 pw.println(" <class.proxy>dynamic</class.proxy>"); 463 if (this.getConversionStrategyClass () != null 464 && this.getConversionStrategyClass ().trim().length() > 0) 465 pw.println(" <conversionStrategy>" + this.getConversionStrategyClass () + "</conversionStrategy>"); 466 467 it = this.tmColumns.values().iterator(); 468 469 while (it.hasNext()) 470 { 471 ((DBColumn)it.next()).writeXML(pw); 472 } 473 474 it = this.hmReferences.values().iterator(); 476 while (it.hasNext()) 477 { 478 ((DBFKRelation)it.next()).writeXML(pw); 479 } 480 it = this.hmCollections.values().iterator(); 482 while (it.hasNext()) 483 { 484 ((DBFKRelation)it.next()).writeXML(pw); 485 } 486 pw.println("</class-descriptor>"); 487 } 488 } 489 } 490 491 public void generateJava(File aFile, String strHeader, String strFooter) throws java.io.IOException , java.io.FileNotFoundException 492 { 493 if (this.isTreeEnabled()) 494 { 495 String dirName = this.getPackageName().replace('.', File.separatorChar); 497 File packageDir; 498 if (this.getPackageName() != null && this.getPackageName().trim().length() > 0) 499 { 500 packageDir = new File (aFile, dirName); 501 } 502 else 503 { 504 packageDir = aFile; 505 } 506 507 if (!packageDir.exists()) packageDir.mkdirs(); 508 File javaFile = new File (packageDir, this.getClassName()+ ".java"); 509 if (!javaFile.exists()) javaFile.createNewFile(); 510 java.io.PrintWriter pw = new java.io.PrintWriter (new java.io.FileOutputStream (javaFile)); 511 pw.println(strHeader); 512 pw.println("// Generated by OJB SchemeGenerator"); 513 pw.println(); 514 if (this.getPackageName().trim().length() > 0) 515 { 516 pw.println("package " + this.getPackageName() + ";"); 517 pw.println(); 518 } 519 pw.println("public class " + this.getClassName()); 520 pw.println("{"); 521 522 java.util.Iterator it = this.tmColumns.values().iterator(); 524 while (it.hasNext()) 525 { 526 pw.println(((DBColumn)it.next()).getJavaFieldDefinition()); 527 pw.println(); 528 } 529 530 it = this.hmReferences.values().iterator(); 531 while (it.hasNext()) 532 { 533 pw.println(((DBFKRelation)it.next()).getJavaFieldDefinition()); 534 pw.println(); 535 } 536 537 it = this.hmCollections.values().iterator(); 538 while (it.hasNext()) 539 { 540 pw.println(((DBFKRelation)it.next()).getJavaFieldDefinition()); 541 pw.println(); 542 } 543 544 it = this.tmColumns.values().iterator(); 546 while (it.hasNext()) 547 { 548 pw.println(((DBColumn)it.next()).getJavaGetterSetterDefinition()); 549 pw.println(); 550 } 551 552 it = this.hmReferences.values().iterator(); 553 while (it.hasNext()) 554 { 555 pw.println(((DBFKRelation)it.next()).getJavaGetterSetterDefinition()); 556 pw.println(); 557 } 558 559 it = this.hmCollections.values().iterator(); 560 while (it.hasNext()) 561 { 562 pw.println(((DBFKRelation)it.next()).getJavaGetterSetterDefinition()); 563 pw.println(); 564 } 565 566 pw.println("}"); 567 pw.println(strFooter); 568 pw.close(); 569 } 570 } 571 572 public void setPackage (String packageName) 573 { 574 this.setPackageName(packageName); 575 } 576 577 public void disableClassesWithRegex(org.apache.regexp.RE aRegexp) 578 { 579 if (aRegexp.match(this.getClassName())) this.setEnabled(false); 580 } 581 582 } 583 584 585 661 | Popular Tags |