-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path111.java
More file actions
660 lines (591 loc) · 21 KB
/
Copy path111.java
File metadata and controls
660 lines (591 loc) · 21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
package org.apache.ojb.tools.mapping.reversedb;
/* Copyright 2002-2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import javax.swing.tree.TreeNode;
import java.sql.SQLException;
import java.io.File;
/**
*
* @author <a href="mailto:bfl@florianbruckner.com">Florian Bruckner</a>
* @version $Id: DBTable.java,v 1.1 2007-08-24 22:17:28 ewestfal Exp $
*/
public class DBTable implements MetadataNodeInterface, TreeNode, org.apache.ojb.tools.mapping.reversedb.gui.PropertySheetModel
{
private java.sql.DatabaseMetaData dbMeta;
private DBSchema aSchema;
private java.util.HashMap hmReferences = new java.util.HashMap(0);
private java.util.HashMap hmCollections = new java.util.HashMap(0);
private java.util.TreeMap tmColumns = new java.util.TreeMap();
private java.util.Vector vSubTreeNodes = new java.util.Vector();
private String strTableName;
private String strClassName;
private String strPackageName = ""; // In default package by default ;-)
private String strConversionStrategyClass = "";
private boolean dynamicProxy = false;
private boolean enabled = true;
/** Creates a new instance of DBTable */
public DBTable (java.sql.DatabaseMetaData pdbMeta, DBSchema paSchema,
String pstrTableName)
{
strTableName = pstrTableName;
// this.strClassName = Character.toUpperCase (strTableName.charAt(0)) + strTableName.substring(1).toLowerCase();
this.strClassName = Namer.nameClass(this.strTableName);
aSchema = paSchema;
dbMeta = pdbMeta;
}
public boolean hasDynamicProxy()
{
return dynamicProxy;
}
public void setDynamicProxy(boolean b)
{
dynamicProxy = b;
}
public String getConversionStrategyClass()
{
return this.strConversionStrategyClass;
}
public void setConversionStrategyClass(String s)
{
this.strConversionStrategyClass = s;
}
public boolean isEnabled()
{
return this.enabled;
}
public void setEnabled(boolean b)
{
this.enabled = b;
}
public boolean isTreeEnabled()
{
return this.aSchema.isTreeEnabled() && this.isEnabled();
}
public DBColumn getColumn(String colName)
{
return (DBColumn)tmColumns.get(colName);
}
public String getTableName()
{
return strTableName;
}
public String getFQTableName()
{
String strReturn = null;
// Are table names supported in table definitions?
if (aSchema.getDBCatalog().getDBMeta().getSupportsCatalogsInTableDefinitions ())
{
// Yes, include catalog name in fq table name
// Now check, where we have to specify the catalog
if (aSchema.getDBCatalog().getDBMeta().getIsCatalogAtStart ())
{
// At the beginning
strReturn = aSchema.getDBCatalog().getCatalogName()
+ aSchema.getDBCatalog().getDBMeta().getCatalogSeparator ()
+ aSchema.getSchemaName() + "." + this.getTableName();
}
else
{
// At the end
strReturn = aSchema.getSchemaName() + "." + this.getTableName()
+ aSchema.getDBCatalog().getDBMeta().getCatalogSeparator ()
+ aSchema.getDBCatalog().getCatalogName();
}
}
else
{
strReturn = aSchema.getSchemaName() + "." + this.getTableName();
}
return strReturn;
}
public String getClassName()
{
return strClassName;
}
public void setClassName(String s)
{
this.strClassName = s;
}
public String getPackageName()
{
return strPackageName;
}
public void setPackageName(String s)
{
this.strPackageName = s;
}
public String getFQClassName()
{
if (this.getPackageName() != null && this.getPackageName().trim().length() > 0)
return this.getPackageName() + "." + this.getClassName();
else
return this.getClassName();
}
public DBSchema getDBSchema()
{
return this.aSchema;
}
public void read() throws SQLException
{
}
public void addColumn(String strColumnName,
int iDataType, String strTypeName, int iColumnSize, int iNullable)
{
DBColumn aDBColumn = new DBColumn(this.dbMeta, this, strColumnName, iDataType, strTypeName);
this.tmColumns.put(strColumnName, aDBColumn);
}
public void addPrimaryKeyColumn(String strColumnName)
{
DBColumn aDBColumn = this.getColumn(strColumnName);
if (aDBColumn != null)
{
aDBColumn.setPrimaryKeyPart(true);
}
}
public void generateReferences() throws SQLException
{
// Check if primary keys for this table have already been set. If not, do it now...
// This is necessary because Oracle doesn't return the Primary Keys for all tables
// and for Informix it speeds up things significantly if we do it onve for all tables.
java.util.Iterator it = this.tmColumns.values().iterator();
boolean hasNoPrimaryKey = true;
while (hasNoPrimaryKey && it.hasNext()) if ( ((DBColumn)it.next()).isPrimaryKeyPart()) hasNoPrimaryKey = false;
if (hasNoPrimaryKey) readPrimaryKeys();
// Now generate References and Collections
generateFKReferences();
generateFKCollections();
vSubTreeNodes.addAll(this.tmColumns.values());
vSubTreeNodes.addAll(this.hmCollections.values());
vSubTreeNodes.addAll(this.hmReferences.values());
}
public java.util.Enumeration children ()
{
return vSubTreeNodes.elements();
}
public boolean getAllowsChildren ()
{
return true;
}
public TreeNode getChildAt(int param)
{
TreeNode tn =
(TreeNode)vSubTreeNodes.elementAt(param);
return tn;
}
public int getChildCount ()
{
return this.vSubTreeNodes.size();
}
public int getIndex(TreeNode treeNode)
{
return this.vSubTreeNodes.indexOf(treeNode);
}
public TreeNode getParent()
{
return this.aSchema;
}
public boolean isLeaf ()
{
if (this.vSubTreeNodes.size() == 0) return true;
else return false;
}
public String toString()
{
return this.strTableName;
}
private void readPrimaryKeys() throws SQLException
{
// Now get the primary keys for this table. Ignore any exceptions thrown here as
// primary keys are not absolutely necessary for reverse engineering
java.sql.ResultSet rs = null;
try
{
rs = dbMeta.getPrimaryKeys(null,
this.getDBSchema().getSchemaName(), this.strTableName);
while (rs.next())
{
String strCatalogName = rs.getString("TABLE_CAT");
String strSchemaName = rs.getString("TABLE_SCHEM");
if ( (strSchemaName == null && this.aSchema.getSchemaName() == null || strSchemaName.equals(this.aSchema.getSchemaName()))
&&
(strCatalogName == null && this.aSchema.getDBCatalog().getCatalogName() == null
|| strCatalogName.equals(this.aSchema.getDBCatalog().getCatalogName())))
{
String strColumnName = rs.getString("COLUMN_NAME");
String pkName = rs.getString("PK_NAME");
DBColumn dbcol = (DBColumn)tmColumns.get(strColumnName);
if (dbcol != null) dbcol.setPrimaryKeyPart(true);
}
}
}
catch (SQLException sqlEx)
{
// Ignore excpetions here.
}
finally
{
try
{
rs.close();
}
catch (Throwable t)
{} // Ignore this exception
}
}
private void generateFKReferences() throws SQLException
{
// References, points from this class to another class using attributes
// of this class
// Ignore any exceptions thrown here.
java.sql.ResultSet rs = null;
try
{
rs = dbMeta.getImportedKeys(this.getDBSchema().getDBCatalog().getCatalogName(),
this.getDBSchema().getSchemaName(), strTableName);
while (rs.next())
{
String strFKSchemaName = rs.getString("FKTABLE_SCHEM");
String strFKCatalogName = rs.getString("FKTABLE_CAT");
if ( (strFKCatalogName == null && this.aSchema.getDBCatalog().getCatalogName() == null || strFKCatalogName.equals(this.aSchema.getDBCatalog().getCatalogName()))
&& (strFKSchemaName == null && this.aSchema.getSchemaName() == null || strFKSchemaName.equals(this.aSchema.getSchemaName())))
{
String strPKCatalogName = rs.getString("PKTABLE_CAT");
String strPKSchemaName = rs.getString("PKTABLE_SCHEM");
String strPKTableName = rs.getString("PKTABLE_NAME");
String strPKColumnName = rs.getString("PKCOLUMN_NAME");
String strFKTableName = rs.getString("FKTABLE_NAME");
String strFKColumnName = rs.getString("FKCOLUMN_NAME");
// Resolove the primaryKey column
DBCatalog dbPKCatalog = this.aSchema.getDBCatalog().getDBMeta().getCatalog(strPKCatalogName);
if (dbPKCatalog != null)
{
DBSchema dbPKSchem = dbPKCatalog.getSchema(strPKSchemaName);
if (dbPKSchem != null)
{
DBTable dbPKTable = dbPKSchem.getTable(strPKTableName);
if (dbPKTable != null)
{
DBColumn dbPKColumn = dbPKTable.getColumn(strPKColumnName);
// The FK column is always from this table.
DBColumn dbFKColumn = getColumn(strFKColumnName);
// Now retrieve the FKReference to this table from the collection
DBFKRelation aFKRef =
(DBFKRelation)this.hmReferences.get(dbPKSchem.getSchemaName()
+ "." + dbPKTable.getTableName());
if (aFKRef == null)
{
aFKRef = new DBFKRelation(dbPKTable, this, false);
this.hmReferences.put(dbPKSchem.getSchemaName()
+ "." + dbPKTable.getTableName(), aFKRef);
}
aFKRef.addColumnPair(dbPKColumn, dbFKColumn);
}
}
}
}
}
}
catch (SQLException sqlEx)
{
// sqlEx.printStackTrace();
}
try
{
rs.close();
}
catch (Throwable t) {}
}
private void generateFKCollections() throws SQLException
{
// Collections, points from this class to a collection of objects using
// attributes from the referenced class
// Ignore any exceptions thrown here
java.sql.ResultSet rs = null;
try
{
rs = dbMeta.getExportedKeys(this.getDBSchema().getDBCatalog().getCatalogName(),
this.getDBSchema().getSchemaName(), strTableName);
while (rs.next())
{
String strPKSchemaName = rs.getString("PKTABLE_SCHEM");
String strPKCatalogName = rs.getString("PKTABLE_CAT");
if ( (strPKSchemaName == null && this.aSchema.getSchemaName()==null || strPKSchemaName.equals(this.aSchema.getSchemaName()))
&& (strPKCatalogName == null && this.aSchema.getDBCatalog().getCatalogName() == null
|| strPKCatalogName.equals(this.aSchema.getDBCatalog().getCatalogName())))
{
String strPKTableName = rs.getString("PKTABLE_NAME");
String strPKColumnName = rs.getString("PKCOLUMN_NAME");
String strFKCatalogName= rs.getString("FKTABLE_CAT");
String strFKTableName = rs.getString("FKTABLE_NAME");
String strFKColumnName = rs.getString("FKCOLUMN_NAME");
String strFKSchemaName = rs.getString("FKTABLE_SCHEM");
// Resolve the FK column. If catalog is supported in the index
// definition, resolve the catalog of the FK column, otherwise
// assume the current catalog (Note: This is needed for Informix,
// because the driver reports null for the catalog in this case.
DBCatalog dbFKCatalog = null;
if (this.aSchema.getDBCatalog().getDBMeta().getSupportsCatalogsInIndexDefinitions())
{
dbFKCatalog = this.aSchema.getDBCatalog().getDBMeta().getCatalog(strFKCatalogName);
}
else
{
dbFKCatalog = this.aSchema.getDBCatalog();
}
if (dbFKCatalog != null)
{
DBSchema dbFKSchema = dbFKCatalog.getSchema(strFKSchemaName);
if (dbFKSchema != null)
{
DBTable dbFKTable = dbFKSchema.getTable(strFKTableName);
if (dbFKTable != null)
{
DBColumn dbFKColumn = dbFKTable.getColumn(strFKColumnName);
// The PK column is always from this table
DBColumn dbPKColumn = getColumn(strPKColumnName);
//Retrieve the FK Reference for the FK Table
DBFKRelation aFKRef =
(DBFKRelation)this.hmCollections.get(dbFKSchema.getSchemaName()
+ "." + dbFKTable.getTableName());
if (aFKRef == null)
{
aFKRef = new DBFKRelation(this, dbFKTable, true);
this.hmCollections.put(dbFKSchema.getSchemaName()
+ "." + dbFKTable.getTableName(), aFKRef);
}
aFKRef.addColumnPair(dbPKColumn, dbFKColumn);
}
}
}
}
}
}
catch (SQLException sqlEx)
{
// sqlEx.printStackTrace();
}
try
{
rs.close();
}
catch (Throwable t)
{
}
}
public Class getPropertySheetClass ()
{
return org.apache.ojb.tools.mapping.reversedb.gui.DBTablePropertySheet.class;
}
public String getXML()
{
java.io.StringWriter sw = new java.io.StringWriter();
writeXML(new java.io.PrintWriter(sw));
return sw.getBuffer().toString();
}
public void writeXML(java.io.PrintWriter pw)
{
// Only generate a Classdescriptor if this table is enabled
if (this.isTreeEnabled())
{
java.util.Iterator it = tmColumns.values().iterator();
if (it.hasNext())
{
// Generate the class descriptor
pw.println("<class-descriptor ");
pw.println(" class=\"" + this.getFQClassName() + "\"");
pw.println(" table=\"" + this.getFQTableName() + "\">");
if (this.hasDynamicProxy())
pw.println(" <class.proxy>dynamic</class.proxy>");
if (this.getConversionStrategyClass () != null
&& this.getConversionStrategyClass ().trim().length() > 0)
pw.println(" <conversionStrategy>" + this.getConversionStrategyClass () + "</conversionStrategy>");
it = this.tmColumns.values().iterator();
while (it.hasNext())
{
((DBColumn)it.next()).writeXML(pw);
}
// Generate references
it = this.hmReferences.values().iterator();
while (it.hasNext())
{
((DBFKRelation)it.next()).writeXML(pw);
}
// Generate collections
it = this.hmCollections.values().iterator();
while (it.hasNext())
{
((DBFKRelation)it.next()).writeXML(pw);
}
pw.println("</class-descriptor>");
}
}
}
public void generateJava(File aFile, String strHeader, String strFooter) throws java.io.IOException, java.io.FileNotFoundException
{
if (this.isTreeEnabled())
{
// 1. Generate the package directories
String dirName = this.getPackageName().replace('.', File.separatorChar);
File packageDir;
if (this.getPackageName() != null && this.getPackageName().trim().length() > 0)
{
packageDir = new File(aFile, dirName);
}
else
{
packageDir = aFile;
}
if (!packageDir.exists()) packageDir.mkdirs();
File javaFile = new File(packageDir, this.getClassName()+ ".java");
if (!javaFile.exists()) javaFile.createNewFile();
java.io.PrintWriter pw = new java.io.PrintWriter(new java.io.FileOutputStream(javaFile));
pw.println(strHeader);
pw.println("// Generated by OJB SchemeGenerator");
pw.println();
if (this.getPackageName().trim().length() > 0)
{
pw.println("package " + this.getPackageName() + ";");
pw.println();
}
pw.println("public class " + this.getClassName());
pw.println("{");
// Generate Fields
java.util.Iterator it = this.tmColumns.values().iterator();
while (it.hasNext())
{
pw.println(((DBColumn)it.next()).getJavaFieldDefinition());
pw.println();
}
it = this.hmReferences.values().iterator();
while (it.hasNext())
{
pw.println(((DBFKRelation)it.next()).getJavaFieldDefinition());
pw.println();
}
it = this.hmCollections.values().iterator();
while (it.hasNext())
{
pw.println(((DBFKRelation)it.next()).getJavaFieldDefinition());
pw.println();
}
// Generate Getter/Setter
it = this.tmColumns.values().iterator();
while (it.hasNext())
{
pw.println(((DBColumn)it.next()).getJavaGetterSetterDefinition());
pw.println();
}
it = this.hmReferences.values().iterator();
while (it.hasNext())
{
pw.println(((DBFKRelation)it.next()).getJavaGetterSetterDefinition());
pw.println();
}
it = this.hmCollections.values().iterator();
while (it.hasNext())
{
pw.println(((DBFKRelation)it.next()).getJavaGetterSetterDefinition());
pw.println();
}
pw.println("}");
pw.println(strFooter);
pw.close();
}
}
public void setPackage (String packageName)
{
this.setPackageName(packageName);
}
public void disableClassesWithRegex(org.apache.regexp.RE aRegexp)
{
if (aRegexp.match(this.getClassName())) this.setEnabled(false);
}
}
/***************************** Changelog *****************************
// $Log: not supported by cvs2svn $
// Revision 1.1.2.1 2005/12/21 22:32:04 tomdz
// Updated license
//
// Revision 1.1 2004/05/05 16:39:05 arminw
// fix fault
// wrong package structure used:
// org.apache.ojb.tools.reversdb
// org.apache.ojb.tools.reversdb2
//
// instead of
// org.apache.ojb.tools.mapping.reversdb
// org.apache.ojb.tools.mapping.reversdb2
//
// Revision 1.1 2004/05/04 13:45:01 arminw
// move reverseDB stuff
//
// Revision 1.9 2004/04/04 23:53:42 brianm
// Fixed initial copyright dates to match cvs repository
//
// Revision 1.8 2004/03/11 18:16:22 brianm
// ASL 2.0
//
// Revision 1.7 2003/12/12 16:37:16 brj
// removed unnecessary casts, semicolons etc.
//
// Revision 1.6 2003/07/22 11:05:13 florianbruckner
// add a name beautifier (courtesy G.Wayne Kidd)
//
// Revision 1.5 2003/06/21 10:35:03 florianbruckner
// implement XML generation with PrintWriter; getXML() still works and uses writeXML(java.io.PrintWriter)
// does not generate an Id anymore.
//
// Revision 1.4 2003/01/28 21:42:53 florianbruckner
// update XML generation
//
// Revision 1.3 2003/01/28 19:59:14 florianbruckner
// some updates to schema reading to make it a bit more compatible
//
// Revision 1.2 2002/06/17 19:34:33 jvanzyl
// Correcting all the package references.
// PR:
// Obtained from:
// Submitted by:
// Reviewed by:
//
// Revision 1.1.1.1 2002/06/17 18:16:52 jvanzyl
// Initial OJB import
//
// Revision 1.3 2002/05/16 11:47:09 florianbruckner
// fix CR/LF issue, change license to ASL
//
// Revision 1.2 2002/05/16 10:43:59 florianbruckner
// use jakarta-regexp instead of gnu-regexp due to the move to jakarta.
//
// Revision 1.1 2002/04/18 11:44:16 mpoeschl
//
// move files to new location
//
// Revision 1.3 2002/04/07 09:05:16 thma
// *** empty log message ***
//
// Revision 1.2 2002/03/11 17:36:05 florianbruckner
// fix line break issue for these files that were previously checked in with -kb
//
// Revision 1.1 2002/03/04 17:19:32 thma
// initial checking for Florians Reverse engineering tool
//
// Revision 1.2 2002/02/20 13:55:11 Administrator
// add semicolon after package name
//
// Revision 1.1.1.1 2002/02/20 13:35:25 Administrator
// initial import
//
/***************************** Changelog *****************************/