KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > tools > mapping > reversedb2 > dbmetatreemodel > DBMetaCatalogNode


1 package org.apache.ojb.tools.mapping.reversedb2.dbmetatreemodel;
2 /* Copyright 2002-2005 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 /**
18  * This node represents a catalog of the database. Its children are DBMetaSchemaNode
19  * objects.
20  *
21  * @author <a HREF="mailto:bfl@florianbruckner.com">Florian Bruckner</a>
22  * @version $Id: DBMetaCatalogNode.java,v 1.1.2.1 2005/12/21 22:32:42 tomdz Exp $
23  */

24 public class DBMetaCatalogNode extends ReverseDbTreeNode
25     implements java.io.Serializable JavaDoc
26 {
27     static final long serialVersionUID = -2455228985120104948L; /** Attribute key for the accessing the catalog name */
28     public static final String JavaDoc ATT_CATALOG_NAME = "Catalog Name";
29
30   
31     /** Creates a new instance of DBMetaCatalogNode.
32      * @param pdbMeta DatabaseMetaData implementation where this node gets its data from.
33      * @param pdbMetaTreeModel The TreeModel this node is associated to.
34      * @param prootNode The parent node for this node.
35      * @param pstrCatalogName The name of the catalog this node is representing. Some databases do not supports
36      * catalogs, therefore null values are allowed for this parameter
37      */

38     public DBMetaCatalogNode(java.sql.DatabaseMetaData JavaDoc pdbMeta,
39                              DatabaseMetaDataTreeModel pdbMetaTreeModel,
40                              DBMetaRootNode prootNode,
41                              String JavaDoc pstrCatalogName)
42     {
43         super(pdbMeta, pdbMetaTreeModel, prootNode);
44         this.setAttribute(ATT_CATALOG_NAME, pstrCatalogName);
45     }
46     
47     /**
48      * @see ReverseDbTreeNode#isLeaf()
49      */

50     public boolean isLeaf()
51     {
52         return false;
53     }
54     
55     /**
56      * @see ReverseDbTreeNode#getAllowsChildren()
57      */

58     public boolean getAllowsChildren()
59     {
60         return false;
61     }
62     
63     /**
64      * Convenience access method for the catalog name. Accesses the
65      * attributes HashMap to retrieve the value.
66      */

67     public String JavaDoc getCatalogName()
68     {
69         return (String JavaDoc)this.getAttribute(ATT_CATALOG_NAME);
70     }
71     
72     /**
73      * If the catalog name is specified, returns the catalog name,
74      * otherwise a constant string indicating that the catalog name
75      * is emtpy (which is legal for some databases, e.g. Oracle)
76      * @see Object#toString()
77      */

78     public String JavaDoc toString()
79     {
80         if (this.getAttribute(ATT_CATALOG_NAME) != null)
81             return this.getAttribute(ATT_CATALOG_NAME).toString();
82         else return "catalog not specified";
83     }
84     
85     public Class JavaDoc getPropertyEditorClass()
86     {
87         return org.apache.ojb.tools.mapping.reversedb2.propertyEditors.JPnlPropertyEditorDBMetaCatalog.class;
88     }
89     
90     /**
91      * Loads the schemas associated to this catalog.
92      */

93     protected boolean _load ()
94     {
95         java.sql.ResultSet JavaDoc rs = null;
96         try
97         {
98             
99             // This synchronization is necessary for Oracle JDBC drivers 8.1.7, 9.0.1, 9.2.0.1
100
// The documentation says synchronization is done within the driver, but they
101
// must have overlooked something. Without the lock we'd get mysterious error
102
// messages.
103
synchronized(getDbMeta())
104             {
105             
106                 getDbMetaTreeModel().setStatusBarMessage("Reading schemas for catalog "
107                     + this.getAttribute(ATT_CATALOG_NAME));
108                 rs = getDbMeta().getSchemas();
109                 final java.util.ArrayList JavaDoc alNew = new java.util.ArrayList JavaDoc();
110                 int count = 0;
111                 while (rs.next())
112                 {
113                     getDbMetaTreeModel().setStatusBarMessage("Creating schema " + getCatalogName() + "." + rs.getString("TABLE_SCHEM"));
114                     alNew.add(new DBMetaSchemaNode(getDbMeta(),
115                                                    getDbMetaTreeModel(),
116                                                    DBMetaCatalogNode.this,
117                                                    rs.getString("TABLE_SCHEM")));
118                     count++;
119                 }
120                 if (count == 0)
121                     alNew.add(new DBMetaSchemaNode(getDbMeta(),
122                                                    getDbMetaTreeModel(),
123                                                    DBMetaCatalogNode.this, null));
124                 alChildren = alNew;
125                 javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc()
126                 {
127                     public void run()
128                     {
129                         getDbMetaTreeModel().nodeStructureChanged(DBMetaCatalogNode.this);
130                     }
131                 });
132                 rs.close();
133             }
134         }
135         catch (java.sql.SQLException JavaDoc sqlEx)
136         {
137             getDbMetaTreeModel().reportSqlError("Error retrieving schemas", sqlEx);
138             try
139             {
140                 if (rs != null) rs.close ();
141             }
142             catch (java.sql.SQLException JavaDoc sqlEx2)
143             {
144                 this.getDbMetaTreeModel().reportSqlError("Error retrieving schemas", sqlEx2);
145             }
146             return false;
147         }
148         return true;
149     }
150
151 }
152
Popular Tags