KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > dbschema > jdbcimpl > SchemaElementImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.dbschema.jdbcimpl;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.beans.PropertyChangeSupport JavaDoc;
24 import java.sql.*;
25 import java.util.*;
26
27 import org.netbeans.modules.dbschema.*;
28 import org.netbeans.modules.dbschema.util.*;
29
30 public class SchemaElementImpl extends DBElementImpl implements SchemaElement.Impl {
31
32     private DBElementsCollection tables;
33
34     private DBIdentifier _schema;
35     private DBIdentifier _catalog;
36     private String JavaDoc _url;
37     private String JavaDoc _username;
38     private String JavaDoc _driver;
39     private String JavaDoc _databaseProductName;
40     private String JavaDoc _databaseProductVersion;
41     private String JavaDoc _driverName;
42     private String JavaDoc _driverVersion;
43
44     private transient DatabaseMetaData dmd;
45     private transient String JavaDoc catalog;
46     
47     private transient volatile boolean stop;
48     
49     public transient PropertyChangeSupport JavaDoc propertySupport = new PropertyChangeSupport JavaDoc(this);
50     
51     private transient int progress;
52     
53     /** Creates new SchemaElementImpl */
54     public SchemaElementImpl() {
55         this(null);
56     }
57   
58     public SchemaElementImpl(ConnectionProvider cp) {
59         tables = new DBElementsCollection(this, new TableElement[0]);
60         
61         //workaround for bug #4396371
62
//http://andorra.eng:8080/cgi-bin/ws.exe/bugtraq/bug.hts?where=bugid_value%3D4396371
63
Object JavaDoc hc = String.valueOf(tables.hashCode());
64         while (DBElementsCollection.instances.contains(hc)) {
65             tables = new DBElementsCollection(this, new TableElement[0]);
66             hc = String.valueOf(tables.hashCode());
67         }
68         DBElementsCollection.instances.add(hc);
69         
70         if (cp != null) {
71             try {
72                 String JavaDoc schema;
73
74                 dmd = cp.getDatabaseMetaData();
75
76                 _url = dmd.getURL();
77                 _username = dmd.getUserName();
78 // schema = dmd.getUserName();
79
schema = cp.getSchema();
80                 _schema = schema == null ? DBIdentifier.create("") : DBIdentifier.create(schema); //NOI18N
81
catalog = cp.getConnection().getCatalog();
82                 _catalog = catalog == null ? DBIdentifier.create("") : DBIdentifier.create(catalog); //NOI18N
83
_driver = cp.getDriver();
84                 _databaseProductName = dmd.getDatabaseProductName().trim();
85                 _databaseProductVersion = dmd.getDatabaseProductVersion();
86                 _driverName = dmd.getDriverName();
87                 _driverVersion = dmd.getDriverVersion();
88             } catch (Exception JavaDoc exc) {
89                 exc.printStackTrace();
90             }
91         }
92         
93         stop = false;
94     }
95   
96     public void setName(DBIdentifier name) throws DBException {
97         int pos;
98         String JavaDoc fullName = name.getFullName();
99         
100         if (fullName == null) {
101             fullName = name.getName();
102             name.setFullName(fullName);
103         }
104         
105         pos = fullName.lastIndexOf("/");
106         if (pos != -1)
107             name.setName(fullName.substring(pos + 1));
108         else if (fullName.indexOf(".") != -1)
109             name.setName(fullName);
110         
111         _name = name;
112     }
113     
114     public DBIdentifier getName() {
115         return _name;
116     }
117
118     /** Get the parsing status of the element.
119     * This is a non-blocking operation.
120     * @return one of {@link #STATUS_NOT}, {@link #STATUS_ERROR},
121     * {@link #STATUS_PARTIAL}, or {@link #STATUS_OK}
122     */

123     public int getStatus() {
124 //NOT IMPLEMENTED YET !!!
125
return SchemaElement.STATUS_OK;
126     }
127   
128     /** Set the schema name of this schema snapshot.
129     * @param id the schema name, or <code>null</code>
130     * @exception DBException if the operation cannot proceed
131     */

132     public void setSchema(DBIdentifier schema) throws DBException {
133         _schema = schema;
134     }
135   
136     /** Get the schema name of this schema snapshot.
137     * @return the schema name, or <code>null</code> if this snapshot does
138     * not have a schema name
139     */

140     public DBIdentifier getSchema() {
141         return _schema;
142     }
143   
144     /** Set the catalog name of this schema snapshot.
145     * @param id the catalog name, or <code>null</code>
146     * @exception DBException if the operation cannot proceed
147     */

148     public void setCatalog(DBIdentifier catalog) throws DBException {
149         _catalog = catalog;
150     }
151   
152     /** Get the catalog name of this schema snapshot.
153     * @return the catalog name, or <code>null</code> if this snapshot does
154     * not have a catalog name
155     */

156     public DBIdentifier getCatalog() {
157         return _catalog;
158     }
159   
160     /** Change the set of tables.
161      * @param elems the tables to change
162      * @param action one of {@link #ADD}, {@link #REMOVE}, or {@link #SET}
163      * @exception DBException if the action cannot be handled
164      */

165     public void changeTables(TableElement[] elems,int action) throws DBException {
166         tables.changeElements(elems, action);
167     }
168     
169     /** Get all tables.
170      * @return the tables
171      */

172     public TableElement[] getTables() {
173         DBElement[] dbe = tables.getElements();
174         return (TableElement[]) Arrays.asList(dbe).toArray(new TableElement[dbe.length]);
175     }
176
177     /** Find a table by name.
178     * @param name the name for which to look
179     * @return the table, or <code>null</code> if it does not exist
180     */

181     public TableElement getTable(DBIdentifier name) {
182         return (TableElement) tables.find(name);
183     }
184     
185     public void initTables(ConnectionProvider cp) {
186         initTables(cp, null, null, false); //false = check tables references
187
}
188     
189     public void initTables(ConnectionProvider cp, LinkedList t, LinkedList v) {
190         initTables(cp, t, v, false); //false = check tables references
191
}
192         
193     public void initTables(ConnectionProvider cp, LinkedList t, LinkedList v, boolean allTables) {
194         if (cp !=null)
195             try {
196                 progress = 0;
197                 LinkedList tables = new LinkedList();
198                 LinkedList views = new LinkedList();
199                 LinkedList tablesTmp = new LinkedList();
200                 LinkedList viewsTmp = new LinkedList();
201 // String user = dmd.getUserName().trim();
202
String JavaDoc user = cp.getSchema();
203                 List recycleBinTables;
204                 ResultSet rs;
205                 
206                 DDLBridge bridge = null;
207                 if (IDEUtil.isIDERunning())
208                     bridge = new DDLBridge(cp.getConnection(), cp.getSchema(), dmd);
209                 
210                 // issue 76953: do not display tables from the Recycle Bin on Oracle 10 and higher
211
if ("Oracle".equals(dmd.getDatabaseProductName()) && dmd.getDatabaseMajorVersion() >= 10) { // NOI18N
212
recycleBinTables = getOracleRecycleBinTables();
213                 } else {
214                     recycleBinTables = Collections.EMPTY_LIST;
215                 }
216
217 //get the list of all tables and views
218
if (bridge != null) {
219                     bridge.getDriverSpecification().getTables("%", new String JavaDoc[] {"TABLE"}); //NOI18N
220
rs = bridge.getDriverSpecification().getResultSet();
221                 } else
222                     rs = dmd.getTables(catalog, user, "%", new String JavaDoc[] {"TABLE"}); //NOI18N
223

224                 if (rs != null) {
225                     while (rs.next()) {
226                         if (isStop()) {
227                             rs.close();
228                             return;
229                         }
230
231                         String JavaDoc tableTmp;
232                         if (bridge != null)
233                             tableTmp = (String JavaDoc) bridge.getDriverSpecification().getRow().get(new Integer JavaDoc(3));
234                         else
235                             tableTmp = rs.getString("TABLE_NAME").trim(); //NOI18N
236

237                         if (!recycleBinTables.contains(tableTmp)) {
238                             tablesTmp.add(tableTmp);
239                         }
240                     }
241                     rs.close();
242                 }
243
244                 rs = null;
245                 if (bridge != null)
246                     if (bridge.getDriverSpecification().areViewsSupported()) {
247                         bridge.getDriverSpecification().getTables("%", new String JavaDoc[] {"VIEW"}); //NOI18N
248
rs = bridge.getDriverSpecification().getResultSet();
249                     }
250                 else
251                     if (MetaDataUtil.areViewsSupported(dmd.getDatabaseProductName()))
252                         rs = dmd.getTables(catalog, user, "%", new String JavaDoc[] {"VIEW"}); //NOI18N
253

254                 if (rs != null) {
255                     while (rs.next()) {
256                         if (isStop()) {
257                             rs.close();
258                             return;
259                         }
260
261                         if (bridge != null)
262                             viewsTmp.add((String JavaDoc) bridge.getDriverSpecification().getRow().get(new Integer JavaDoc(3)));
263                         else
264                             viewsTmp.add(rs.getString("TABLE_NAME").trim()); //NOI18N
265
}
266                     rs.close();
267                 }
268 //list of all tables and views collected
269

270                 if (t == null && v == null) {
271                     tables = tablesTmp;
272                     views = viewsTmp;
273                 } else {
274                     t = checkNames(t, tablesTmp);
275                     v = checkNames(v, viewsTmp);
276                     
277                     if (allTables)
278                         tables = t;
279                     else
280                         tables = checkReferences(t, bridge, user);
281                     
282                     views = v;
283                 }
284                 
285                 // the tables are included twice because for each table
286
// the progress is incremented twice (once for the table itself and once for the keys)
287
propertySupport.firePropertyChange("totalCount", null, new Integer JavaDoc(2 * tables.size() + views.size())); //NOI18N
288

289                 initTables(cp, tables);
290                 initViews(cp, views, bridge);
291             } catch (Exception JavaDoc exc) {
292                 if (Boolean.getBoolean("netbeans.debug.exceptions")) // NOI18N
293
exc.printStackTrace();
294             }
295     }
296     
297     private LinkedList checkNames(LinkedList toCheck, LinkedList names) {
298         LinkedList result = new LinkedList();
299         
300         for (int i = 0; i < toCheck.size(); i++) {
301             Object JavaDoc table = toCheck.get(i);
302             
303             if (names.contains(table))
304                 result.add(table);
305             else
306                 if (Boolean.getBoolean("netbeans.debug.exceptions")) // NOI18N
307
System.out.println("Cannot find " + table + " table in the database."); //NOI18N
308
}
309         
310         return result;
311     }
312     
313     private LinkedList checkReferences(LinkedList tables, DDLBridge bridge, String JavaDoc schema) throws SQLException {
314         ResultSet rs;
315         String JavaDoc pkSchema;
316         String JavaDoc fkSchema;
317         String JavaDoc refTable;
318
319         for (int i = 0; i < tables.size(); i++) { //add all referenced tables
320
if (bridge != null) {
321                 bridge.getDriverSpecification().getImportedKeys(tables.get(i).toString());
322                 rs = bridge.getDriverSpecification().getResultSet();
323             } else
324                 rs = dmd.getImportedKeys(catalog, schema, tables.get(i).toString());
325
326             if (rs != null) {
327                 HashMap rset = new HashMap();
328                 String JavaDoc c1, c2, s1, s2;
329                 while (rs.next()) {
330                     if (bridge != null) {
331                         rset = bridge.getDriverSpecification().getRow();
332                         
333                         //test references between two schemas
334
c1 = (String JavaDoc) rset.get(new Integer JavaDoc(1));
335                         s1 = (String JavaDoc) rset.get(new Integer JavaDoc(2));
336                         c2 = (String JavaDoc) rset.get(new Integer JavaDoc(5));
337                         s2 = (String JavaDoc) rset.get(new Integer JavaDoc(6));
338                         
339                         if (comp(c1, c2)) {
340                             if (! comp(s1, s2))
341                                 continue;
342                         } else
343                             continue;
344                         
345                         pkSchema = (String JavaDoc) rset.get(new Integer JavaDoc(2));
346                         fkSchema = (String JavaDoc) rset.get(new Integer JavaDoc(6));
347                         if ((pkSchema == fkSchema) || (pkSchema.equals(fkSchema))) {
348                             refTable = (String JavaDoc) rset.get(new Integer JavaDoc(3));
349                             if (! tables.contains(refTable))
350                                 tables.add(refTable);
351                         }
352                         rset.clear();
353                     } else {
354                         //test references between two schemas
355
c1 = rs.getString("PKTABLE_CAT"); //NOI18N
356
s1 = rs.getString("PKTABLE_SCHEM"); //NOI18N
357
c2 = rs.getString("FKTABLE_CAT"); //NOI18N
358
s2 = rs.getString("FKTABLE_SCHEM"); //NOI18N
359

360                         if (comp(c1, c2)) {
361                             if (! comp(s1, s2))
362                                 continue;
363                         } else
364                             continue;
365                         
366                         pkSchema = rs.getString("PKTABLE_SCHEM"); //NOI18N
367
if (pkSchema != null) {
368                             pkSchema = pkSchema.trim();
369                         }
370                         fkSchema = rs.getString("FKTABLE_SCHEM"); //NOI18N
371
if (fkSchema != null) {
372                             fkSchema = fkSchema.trim();
373                         }
374                         if ((pkSchema == fkSchema) || (pkSchema.equals(fkSchema))) {
375                             refTable = rs.getString("PKTABLE_NAME").trim(); //NOI18N
376
if (! tables.contains(refTable))
377                                 tables.add(refTable);
378                         }
379                     }
380                 }
381                 rs.close();
382             }
383         }
384         
385         return tables;
386     }
387     
388     private void initTables(ConnectionProvider cp, LinkedList tables) throws DBException {
389         String JavaDoc name;
390         
391         for (int i = 0; i < tables.size(); i++) {
392             if (isStop())
393                 return;
394
395             name = tables.get(i).toString();
396             propertySupport.firePropertyChange("tableName", null, name); //NOI18N
397
TableElementImpl tei = new TableElementImpl(name);
398             tei.setTableOrView(true);
399             TableElement[] te = {new TableElement(tei, (SchemaElement) element)};
400
401             tei.initColumns(cp);
402             tei.initIndexes(cp);
403             changeTables(te, DBElement.Impl.ADD);
404
405             progress++;
406             propertySupport.firePropertyChange("progress", null, new Integer JavaDoc(progress)); //NOI18N
407
}
408
409         TableElement te;
410         String JavaDoc tableName;
411         for (int i = 0; i < tables.size(); i++) {
412             if (isStop())
413                 return;
414
415             tableName = tables.get(i).toString();
416             te = getTable(DBIdentifier.create(tableName));
417             if (te != null) {
418                 propertySupport.firePropertyChange("FKt", null, tableName); //NOI18N
419
((TableElementImpl) te.getElementImpl()).initKeys(cp, 0, tableName);
420             }
421
422             progress++;
423             propertySupport.firePropertyChange("progress", null, new Integer JavaDoc(progress)); //NOI18N
424
}
425     }
426     
427     private void initViews(ConnectionProvider cp, LinkedList views, DDLBridge bridge) throws DBException, SQLException {
428         String JavaDoc name;
429         ResultSet rs;
430         
431         for (int i = 0; i < views.size(); i++) {
432             if (isStop())
433                 return;
434
435             name = views.get(i).toString();
436             propertySupport.firePropertyChange("viewName", null, name); //NOI18N
437
TableElementImpl tei = new TableElementImpl(name);
438             tei.setTableOrView(false);
439             TableElement te = new TableElement(tei, (SchemaElement) element);
440             tei.initColumns(cp);
441
442             String JavaDoc database = dmd.getDatabaseProductName();
443             if (database!=null){ //could be null see bug 53887
444
database = database.trim();
445             }
446             else{
447                 database=""; //NOI18N
448
}
449             if (database.equalsIgnoreCase("Oracle") || database.equalsIgnoreCase("Microsoft SQL Server")) { //NOI18N
450
propertySupport.firePropertyChange("FKv", null, name); //NOI18N
451

452                 ViewDependency vd = new ViewDependency(cp, cp.getSchema(), name);
453                 LinkedList tables = new LinkedList();
454                 LinkedList columns = new LinkedList();
455
456                 tables.clear();
457                 columns.clear();
458                 vd.constructPK();
459                 tables = vd.getTables();
460                 columns = vd.getColumns();
461                 if (! columns.isEmpty()) {
462                     boolean all = false;
463                     for (int k = 0; k < columns.size(); k++)
464                         //test if the view is created over all columns and try to eliminate agregation functions
465
if (((String JavaDoc) columns.get(k)).trim().endsWith("*")) {
466                             all = true;
467                             break;
468                         }
469                             
470                     boolean capture = true;
471                     LinkedList pkTables = new LinkedList();
472                     for (int j = 0; j < tables.size(); j++) {
473                         if (isStop())
474                             return;
475
476                         //compute PK
477
if (bridge != null) {
478                             bridge.getDriverSpecification().getPrimaryKeys(tables.get(j).toString());
479                             rs = bridge.getDriverSpecification().getResultSet();
480                         } else
481                             rs = cp.getDatabaseMetaData().getPrimaryKeys(cp.getConnection().getCatalog(), cp.getSchema(), tables.get(j).toString());
482
483                         if (rs != null) {
484                             if (! all) {
485                                 String JavaDoc colName;
486                                 HashMap rset = new HashMap();
487                                 while (rs.next()) {
488                                     if (bridge != null) {
489                                         rset = bridge.getDriverSpecification().getRow();
490                                         colName = (String JavaDoc) rset.get(new Integer JavaDoc(4));
491                                         rset.clear();
492                                     } else
493                                         colName = rs.getString("COLUMN_NAME").trim(); //NOI18N
494

495                                     if (columns.contains(colName.toLowerCase()) || columns.contains(tables.get(j).toString().toLowerCase() + "." + colName.toLowerCase()))
496                                         continue;
497                                     else {
498                                         capture = false;
499                                         break;
500                                     }
501                                 }
502                             }
503
504                             if (capture)
505                                 pkTables.add(tables.get(j).toString());
506
507                             rs.close();
508                         }
509                     }
510
511                     if (capture)
512                         for (int j = 0; j < pkTables.size(); j++) {
513                             //capture PK
514
tei.initIndexes(cp, pkTables.get(j).toString());
515                             tei.initKeys(cp, 1, pkTables.get(j).toString());
516
517                             LinkedList tempList = new LinkedList();
518                             UniqueKeyElement[] keys = te.getUniqueKeys();
519                             for (int k = 0; k < keys.length; k++)
520                                 if (keys[k].isPrimaryKey())
521                                     tempList.add(keys[k]);
522
523                             keys = new UniqueKeyElement[tempList.size()];
524                             for (int k = 0; k < tempList.size(); k++)
525                                 keys[k] = (UniqueKeyElement) tempList.get(k);
526                             te.setKeys(keys);
527
528                             IndexElement[] indexes = new IndexElement[keys.length];
529                             for (int k = 0; k < keys.length; k++)
530                                 indexes[k] = ((UniqueKeyElement) keys[k]).getAssociatedIndex();
531                             te.setIndexes(indexes);
532                         }
533
534                     //compound PKs
535
if (te.getUniqueKeys().length > 1) {
536                         IndexElementImpl iei = new IndexElementImpl(tei, "GENERATED_PK_" + tei.getName().getName(), true);
537                         IndexElement[] ie = {new IndexElement(iei, te)};
538                         IndexElement[] ies = te.getIndexes();
539                         for (int j = 0; j < ies.length; j++)
540                             iei.changeColumns(ies[j].getColumns(), DBElement.Impl.ADD);
541                         te.setIndexes(ie);
542                         
543                         IndexElement ii = te.getIndexes()[0];
544                         UniqueKeyElementImpl ukei = new UniqueKeyElementImpl(ii.getName().getName(), true);
545                         UniqueKeyElement uke = new UniqueKeyElement(ukei, te, ii);
546                         uke.setColumns(ii.getColumns());
547                         tei.changeKeys(new UniqueKeyElement[] {uke}, DBElement.Impl.SET);
548                     }
549                     
550                     //compute FKs
551
LinkedList toCapture = new LinkedList();
552                     LinkedList validFKs = new LinkedList();
553                     LinkedList fkTables = new LinkedList();
554                     for (int j = 0; j < tables.size(); j++) {
555                         if (isStop())
556                             return;
557
558                         if (bridge != null) {
559                             bridge.getDriverSpecification().getImportedKeys(tables.get(j).toString());
560                             rs = bridge.getDriverSpecification().getResultSet();
561                         } else
562                             rs = cp.getDatabaseMetaData().getImportedKeys(cp.getConnection().getCatalog(), cp.getSchema(), tables.get(j).toString());
563                         
564                         if (rs != null) {
565                             HashMap rset = new HashMap();
566                             LinkedList local = new LinkedList();
567                             LinkedList ref = new LinkedList();
568                             LinkedList fk = new LinkedList();
569                             String JavaDoc fkName, c1, c2, s1, s2;
570                             while (rs.next()) {
571                                 if (bridge != null) {
572                                     rset = bridge.getDriverSpecification().getRow();
573                                     
574                                     //test references between two schemas
575
c1 = (String JavaDoc) rset.get(new Integer JavaDoc(1));
576                                     s1 = (String JavaDoc) rset.get(new Integer JavaDoc(2));
577                                     c2 = (String JavaDoc) rset.get(new Integer JavaDoc(5));
578                                     s2 = (String JavaDoc) rset.get(new Integer JavaDoc(6));
579
580                                     if (comp(c1, c2)) {
581                                         if (! comp(s1, s2))
582                                             continue;
583                                     } else
584                                         continue;
585                                     
586                                     fkName = (String JavaDoc) rset.get(new Integer JavaDoc(12));
587                                     if (fkName == null)
588                                         continue;
589                                     else
590                                         fkName = fkName.trim();
591 // schemas = ((rset.get(new Integer(6)) == rset.get(new Integer(2))) || rset.get(new Integer(6)).equals(rset.get(new Integer(2)))) ? true : false;
592
local.add(fkName + "." + ((String JavaDoc) rset.get(new Integer JavaDoc(7))) + "." + ((String JavaDoc) rset.get(new Integer JavaDoc(8)))); //NOI18N
593
ref.add(fkName + "." + ((String JavaDoc) rset.get(new Integer JavaDoc(3))) + "." + ((String JavaDoc) rset.get(new Integer JavaDoc(4)))); //NOI18N
594
if (! fk.contains(fkName))
595                                         fk.add(fkName);
596                                     rset.clear();
597                                 } else {
598                                     //test references between two schemas
599
c1 = rs.getString("PKTABLE_CAT"); //NOI18N
600
s1 = rs.getString("PKTABLE_SCHEM"); //NOI18N
601
c2 = rs.getString("FKTABLE_CAT"); //NOI18N
602
s2 = rs.getString("FKTABLE_SCHEM"); //NOI18N
603

604                                     if (comp(c1, c2)) {
605                                         if (! comp(s1, s2))
606                                             continue;
607                                     } else
608                                         continue;
609                         
610                                     fkName = rs.getString("FK_NAME"); //NOI18N
611
if (fkName == null)
612                                         continue;
613                                     else
614                                         fkName = fkName.trim();
615 // schemas = ((rs.getString("FKTABLE_SCHEM") == rs.getString("PKTABLE_SCHEM")) || rs.getString("FKTABLE_SCHEM").equals(rs.getString("PKTABLE_SCHEM"))) ? true : false;
616
local.add(fkName + "." + rs.getString("FKTABLE_NAME").trim() + "." + rs.getString("FKCOLUMN_NAME").trim()); //NOI18N
617
ref.add(fkName + "." + rs.getString("PKTABLE_NAME").trim() + "." + rs.getString("PKCOLUMN_NAME").trim()); //NOI18N
618
if (! fk.contains(fkName))
619                                         fk.add(fkName);
620                                 }
621                             }
622                             rs.close();
623                             
624                             String JavaDoc colName;
625                             for (int k = 0; k < fk.size(); k++) {
626                                 fkName = fk.get(k).toString();
627                                 for (int l = 0; l < local.size(); l++) {
628                                     colName = local.get(l).toString();
629                                     if (colName.startsWith(fkName))
630                                         colName = colName.substring(colName.lastIndexOf(".") + 1).toLowerCase();
631                                     else
632                                         continue;
633                                     
634                                     if (all || columns.contains(colName) || columns.contains(tables.get(j).toString().toLowerCase() + "." + colName)) {
635                                         continue;
636                                     } else {
637                                         fk.set(k, null);
638                                         break;
639                                     }
640                                 }
641                                     
642                                 if (fk.get(k) != null)
643                                     for (int l = 0; l < ref.size(); l++) {
644                                         colName = ref.get(l).toString();
645                                         if (colName.startsWith(fkName)) {
646                                             colName = colName.substring(colName.indexOf(".") + 1, colName.lastIndexOf("."));
647                                             if (getTable(DBIdentifier.create(colName)) == null)
648                                                 toCapture.add(colName);
649                                             break;
650                                         }
651                                     }
652                             }
653                                 
654                             String JavaDoc tblName = tables.get(j).toString();
655                             for (int k = 0; k < fk.size(); k++) {
656                                 Object JavaDoc o = fk.get(k);
657                                 if (o != null) {
658                                     validFKs.add(o);
659                                     if (! fkTables.contains(tblName))
660                                         fkTables.add(tblName);
661                                 }
662                             }
663                         }
664                     }
665
666                     initTables(cp, checkReferences(toCapture, bridge, cp.getSchema()));
667
668                     for (int j = 0; j < fkTables.size(); j++)
669                         tei.initKeys(cp, 2, fkTables.get(j).toString());
670                     
671                     LinkedList tempList = new LinkedList();
672                     ForeignKeyElement[] fke = te.getForeignKeys();
673                     UniqueKeyElement[] uke = te.getUniqueKeys();
674                     for (int j = 0; j < fke.length; j++)
675                         if (validFKs.contains(fke[j].getName().getName()))
676                             tempList.add(fke[j]);
677                     KeyElement[] ke = new KeyElement[uke.length + tempList.size()];
678                     for (int j = 0; j < uke.length; j++)
679                         ke[j] = uke[j];
680                     int idx = uke.length;
681                     for (int j = 0; j < tempList.size(); j++)
682                         ke[j + idx] = (ForeignKeyElement) tempList.get(j);
683                     
684                     te.setKeys(ke);
685
686                 }
687             }
688
689             changeTables(new TableElement[] {te}, DBElement.Impl.ADD);
690
691             progress++;
692             propertySupport.firePropertyChange("progress", null, new Integer JavaDoc(progress)); //NOI18N
693
}
694     }
695     
696     private List getOracleRecycleBinTables() {
697         List result = new ArrayList();
698         try {
699             Statement stmt = dmd.getConnection().createStatement();
700             try {
701                 ResultSet rs = stmt.executeQuery("SELECT OBJECT_NAME FROM RECYCLEBIN WHERE TYPE = 'TABLE'"); // NOI18N
702
try {
703                     while (rs.next()) {
704                         result.add(rs.getString("OBJECT_NAME")); // NOI18N
705
}
706                 } finally {
707                     rs.close();
708                 }
709             } finally {
710                 stmt.close();
711             }
712         } catch (SQLException exc) {
713             // not critical, logging is enough
714
if (Boolean.getBoolean("netbeans.debug.exceptions")) { // NOI18N
715
exc.printStackTrace();
716             }
717             result = Collections.EMPTY_LIST;
718         }
719         return result;
720     }
721   
722     /** Getter for property url.
723     * @return Value of property url.
724     */

725     public String JavaDoc getUrl() {
726         return _url;
727     }
728   
729     /** Setter for property url.
730     * @param url New value of property url.
731     */

732     public void setUrl(String JavaDoc url) throws DBException {
733         _url = url;
734     }
735   
736     /** Getter for property username.
737     * @return Value of property username.
738     */

739     public String JavaDoc getUsername() {
740         return _username;
741     }
742   
743     /** Setter for property username.
744     * @param username New value of property username.
745     */

746     public void setUsername(String JavaDoc username) throws DBException {
747         _username = username;
748     }
749   
750     /** Getter for property driver.
751      * @return Value of property driver.
752      */

753     public String JavaDoc getDriver() {
754         return _driver;
755     }
756   
757     /** Setter for property driver.
758      * @param driver New value of property driver.
759      */

760     public void setDriver(String JavaDoc driver) {
761         _driver = driver;
762     }
763   
764     /** Getter for property databaseProductName.
765      * @return Value of property databaseProductName.
766      */

767     public String JavaDoc getDatabaseProductName() {
768         return _databaseProductName;
769     }
770   
771     /** Setter for property databaseProductName.
772      * @param databaseProductName New value of property databaseProductName.
773      */

774     public void setDatabaseProductName(String JavaDoc databaseProductName) throws DBException {
775         _databaseProductName = databaseProductName;
776     }
777   
778     /** Getter for property databaseProductVersion.
779      * @return Value of property databaseProductVersion.
780      */

781     public String JavaDoc getDatabaseProductVersion() {
782         return _databaseProductVersion;
783     }
784   
785     /** Setter for property databaseProductVersion.
786      * @param databaseProductVersion New value of property databaseProductVersion.
787      */

788     public void setDatabaseProductVersion(String JavaDoc databaseProductVersion) throws DBException {
789         _databaseProductVersion = databaseProductVersion;
790     }
791   
792     /** Getter for property driverName.
793      * @return Value of property driverName.
794      */

795     public String JavaDoc getDriverName() {
796         return _driverName;
797     }
798   
799     /** Setter for property driverName.
800      * @param driverName New value of property driverName.
801      */

802     public void setDriverName(String JavaDoc driverName) throws DBException {
803         _driverName = driverName;
804     }
805   
806     /** Getter for property driverVersion.
807      * @return Value of property driverVersion.
808      */

809     public String JavaDoc getDriverVersion() {
810         return _driverVersion;
811     }
812   
813     /** Setter for property driverVersion.
814      * @param driverVersion New value of property driverVersion.
815      */

816     public void setDriverVersion(String JavaDoc driverVersion) throws DBException {
817         _driverVersion = driverVersion;
818     }
819     
820     public boolean isStop() {
821         return stop;
822     }
823     
824     public void setStop(boolean stop) {
825         this.stop = stop;
826     }
827
828     //========== property change support needed for progressbar ==========
829
public void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
830         propertySupport.addPropertyChangeListener (l);
831     }
832     
833     public void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
834         propertySupport.removePropertyChangeListener (l);
835     }
836
837     //=============== extra methods needed for xml archiver ==============
838

839     /** Returns the table collection of this schema element. This method
840      * should only be used internally and for cloning and archiving.
841      * @return the table collection of this schema element
842      */

843     public DBElementsCollection getTableCollection ()
844     {
845         return tables;
846     }
847
848     /** Set the table collection of this claschemass element to the supplied
849      * collection. This method should only be used internally and for
850      * cloning and archiving.
851      * @param collection the table collection of this schema element
852      */

853     public void setTableCollection (DBElementsCollection collection)
854     {
855         tables = collection;
856     }
857 }
858
Popular Tags