KickJava   Java API By Example, From Geeks To Geeks.

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


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 schema of the database. Its children are DBMetaTableNode
19  * objects. Not all databases support schemas (e.g. MySQL), so the schema name
20  * may be null.
21  * @author <a HREF="mailto:bfl@florianbruckner.com">Florian Bruckner</a>
22  * @version $Id: DBMetaSchemaNode.java,v 1.1.2.1 2005/12/21 22:32:42 tomdz Exp $
23  */

24 public class DBMetaSchemaNode extends ReverseDbTreeNode
25     implements java.io.Serializable JavaDoc
26 {
27     static final long serialVersionUID = 2430983502951445144L; /** Key for accessing the schema name in the attributes Map */
28     public static final String JavaDoc ATT_SCHEMA_NAME = "Schema Name";
29     /** Creates a new instance of DBMetaSchemaNode
30      * @param pdbMeta DatabaseMetaData implementation where this node gets its data from.
31      * @param pdbMetaTreeModel The TreeModel this node is associated to.
32      * @param pcatalogNode The parent node for this node.
33      * @param pstrSchemaName The name of the schema this node is representing. Some databases do not support
34      * schemas, therefore null values are allowed for this parameter
35      */

36     public DBMetaSchemaNode(java.sql.DatabaseMetaData JavaDoc pdbMeta,
37                              DatabaseMetaDataTreeModel pdbMetaTreeModel,
38                              DBMetaCatalogNode pcatalogNode,
39                              String JavaDoc pstrSchemaName)
40     {
41         super(pdbMeta, pdbMetaTreeModel, pcatalogNode);
42         this.setAttribute(ATT_SCHEMA_NAME, pstrSchemaName);
43     }
44     
45     /**
46      * @see ReverseDbTreeNode#isLeaf()
47      */

48     public boolean getAllowsChildren()
49     {
50         return true;
51     }
52
53     /**
54      * @see ReverseDbTreeNode#getAllowsChildren()
55      */

56     public boolean isLeaf()
57     {
58         return false;
59     }
60
61     /**
62      * Convenience access method for the schema name. Accesses the
63      * attributes HashMap to retrieve the value.
64      */

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

76     public String JavaDoc toString()
77     {
78         if (this.getAttribute(ATT_SCHEMA_NAME) == null) return "Schema not specified";
79         else return this.getAttribute(ATT_SCHEMA_NAME).toString();
80     }
81     
82     /**
83      * Convenience access method to the catalog this schema is associated to.
84      */

85     public DBMetaCatalogNode getCatalog()
86     {
87         return (DBMetaCatalogNode ) getParent();
88     }
89
90     public Class JavaDoc getPropertyEditorClass()
91     {
92         return org.apache.ojb.tools.mapping.reversedb2.propertyEditors.JPnlPropertyEditorDBMetaSchema.class;
93     }
94     
95     /**
96      * Fills the children list with the tables this schema contains.
97      */

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