001: package org.apache.ojb.tools.mapping.reversedb2.dbmetatreemodel;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * This class represents a table within a database.
020: * @author <a href="mailto:bfl@florianbruckner.com">Florian Bruckner</a>
021: * @version $Id: DBMetaTableNode.java,v 1.1.2.1 2005/12/21 22:32:42 tomdz Exp $
022: */
023: public class DBMetaTableNode extends ReverseDbTreeNode implements
024: java.io.Serializable {
025: static final long serialVersionUID = 7091783312165332145L;
026: /** Key for accessing the table name in the attributes Map */
027: public static final String ATT_TABLE_NAME = "Table Name";
028:
029: /** Creates a new instance of DBMetaSchemaNode
030: * @param pdbMeta DatabaseMetaData implementation where this node gets its data from.
031: * @param pdbMetaTreeModel The TreeModel this node is associated to.
032: * @param pschemaNode The parent node for this node.
033: * @param pstrTableName The name of the table this node is representing.
034: */
035: public DBMetaTableNode(java.sql.DatabaseMetaData pdbMeta,
036: DatabaseMetaDataTreeModel pdbMetaTreeModel,
037: DBMetaSchemaNode pschemaNode, String pstrTableName) {
038: super (pdbMeta, pdbMetaTreeModel, pschemaNode);
039: this .setAttribute(ATT_TABLE_NAME, pstrTableName);
040: }
041:
042: /**
043: * @see ReverseDbTreeNode#isLeaf()
044: */
045: public boolean getAllowsChildren() {
046: return true;
047: }
048:
049: /**
050: * @see ReverseDbTreeNode#getAllowsChildren()
051: */
052: public boolean isLeaf() {
053: return false;
054: }
055:
056: /**
057: * Convenience access method for the table name. Accesses the
058: * attributes HashMap to retrieve the value.
059: */
060: public String getTableName() {
061: return (String) this .getAttribute(ATT_TABLE_NAME);
062: }
063:
064: /**
065: * @see Object#toString()
066: */
067: public String toString() {
068: return (String) getAttribute(ATT_TABLE_NAME);
069: }
070:
071: /**
072: * Convenience access method to the schema this table is associated to.
073: */
074: public DBMetaSchemaNode getSchema() {
075: return (DBMetaSchemaNode) this .getParent();
076: }
077:
078: public Class getPropertyEditorClass() {
079: return org.apache.ojb.tools.mapping.reversedb2.propertyEditors.JPnlPropertyEditorDBMetaTable.class;
080: }
081:
082: /**
083: * Loads the columns for this table into the alChildren list.
084: */
085: protected boolean _load() {
086: java.sql.ResultSet rs = null;
087: try {
088: // This synchronization is necessary for Oracle JDBC drivers 8.1.7, 9.0.1, 9.2.0.1
089: // The documentation says synchronization is done within the driver, but they
090: // must have overlooked something. Without the lock we'd get mysterious error
091: // messages.
092: synchronized (getDbMeta()) {
093: getDbMetaTreeModel().setStatusBarMessage(
094: "Reading columns for table "
095: + getSchema().getCatalog()
096: .getCatalogName() + "."
097: + getSchema().getSchemaName() + "."
098: + getTableName());
099: rs = getDbMeta().getColumns(
100: getSchema().getCatalog().getCatalogName(),
101: getSchema().getSchemaName(), getTableName(),
102: "%");
103: final java.util.ArrayList alNew = new java.util.ArrayList();
104: while (rs.next()) {
105: alNew.add(new DBMetaColumnNode(getDbMeta(),
106: getDbMetaTreeModel(), DBMetaTableNode.this ,
107: rs.getString("COLUMN_NAME")));
108: }
109: alChildren = alNew;
110: javax.swing.SwingUtilities.invokeLater(new Runnable() {
111: public void run() {
112: getDbMetaTreeModel().nodeStructureChanged(
113: DBMetaTableNode.this );
114: }
115: });
116: rs.close();
117: }
118: } catch (java.sql.SQLException sqlEx) {
119: this .getDbMetaTreeModel().reportSqlError(
120: "Error retrieving columns", sqlEx);
121: try {
122: if (rs != null)
123: rs.close();
124: } catch (java.sql.SQLException sqlEx2) {
125: this .getDbMetaTreeModel().reportSqlError(
126: "Error retrieving columns", sqlEx2);
127: }
128: return false;
129: }
130: return true;
131: }
132:
133: }
|