KickJava   Java API By Example, From Geeks To Geeks.

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


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 class represents a table within a database.
19  * @author <a HREF="mailto:bfl@florianbruckner.com">Florian Bruckner</a>
20  * @version $Id: DBMetaTableNode.java,v 1.1.2.1 2005/12/21 22:32:42 tomdz Exp $
21  */

22 public class DBMetaTableNode extends ReverseDbTreeNode
23     implements java.io.Serializable JavaDoc
24 {
25     static final long serialVersionUID = 7091783312165332145L;
26     /** Key for accessing the table name in the attributes Map */
27     public static final String JavaDoc ATT_TABLE_NAME = "Table Name";
28     
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 pschemaNode The parent node for this node.
33      * @param pstrTableName The name of the table this node is representing.
34      */

35     public DBMetaTableNode(java.sql.DatabaseMetaData JavaDoc pdbMeta,
36                            DatabaseMetaDataTreeModel pdbMetaTreeModel,
37                            DBMetaSchemaNode pschemaNode,
38                            String JavaDoc pstrTableName)
39     {
40         super(pdbMeta, pdbMetaTreeModel, pschemaNode);
41         this.setAttribute(ATT_TABLE_NAME, pstrTableName);
42     }
43     
44     /**
45      * @see ReverseDbTreeNode#isLeaf()
46      */

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

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

64     public String JavaDoc getTableName()
65     {
66         return (String JavaDoc)this.getAttribute(ATT_TABLE_NAME);
67     }
68     
69     /**
70      * @see Object#toString()
71      */

72     public String JavaDoc toString()
73     {
74         return (String JavaDoc)getAttribute(ATT_TABLE_NAME);
75     }
76     
77     /**
78      * Convenience access method to the schema this table is associated to.
79      */

80     public DBMetaSchemaNode getSchema()
81     {
82         return (DBMetaSchemaNode)this.getParent();
83     }
84
85     public Class JavaDoc getPropertyEditorClass()
86     {
87         return org.apache.ojb.tools.mapping.reversedb2.propertyEditors.JPnlPropertyEditorDBMetaTable.class;
88     }
89     
90     
91     /**
92      * Loads the columns for this table into the alChildren list.
93      */

94     protected boolean _load ()
95     {
96         java.sql.ResultSet JavaDoc rs = null;
97         try
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                 getDbMetaTreeModel().setStatusBarMessage("Reading columns for table " + getSchema().getCatalog().getCatalogName() + "." + getSchema().getSchemaName() + "." + getTableName());
106                 rs = getDbMeta().getColumns(getSchema().getCatalog().getCatalogName(),
107                                                           getSchema().getSchemaName(),
108                                                           getTableName(), "%");
109                 final java.util.ArrayList JavaDoc alNew = new java.util.ArrayList JavaDoc();
110                 while (rs.next())
111                 {
112                     alNew.add(new DBMetaColumnNode(getDbMeta(), getDbMetaTreeModel(), DBMetaTableNode.this, rs.getString("COLUMN_NAME")));
113                 }
114                 alChildren = alNew;
115                 javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc()
116                 {
117                     public void run()
118                     {
119                         getDbMetaTreeModel().nodeStructureChanged(DBMetaTableNode.this);
120                     }
121                 });
122                 rs.close();
123             }
124         }
125         catch (java.sql.SQLException JavaDoc sqlEx)
126         {
127             this.getDbMetaTreeModel().reportSqlError("Error retrieving columns", sqlEx);
128             try
129             {
130                 if (rs != null) rs.close ();
131             }
132             catch (java.sql.SQLException JavaDoc sqlEx2)
133             {
134                 this.getDbMetaTreeModel().reportSqlError("Error retrieving columns", sqlEx2);
135             }
136             return false;
137         }
138         return true;
139     }
140
141 }
142
Popular Tags