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 node represents a schema of the database. Its children are DBMetaTableNode
020: * objects. Not all databases support schemas (e.g. MySQL), so the schema name
021: * may be null.
022: * @author <a href="mailto:bfl@florianbruckner.com">Florian Bruckner</a>
023: * @version $Id: DBMetaSchemaNode.java,v 1.1.2.1 2005/12/21 22:32:42 tomdz Exp $
024: */
025: public class DBMetaSchemaNode extends ReverseDbTreeNode implements
026: java.io.Serializable {
027: static final long serialVersionUID = 2430983502951445144L;
028: /** Key for accessing the schema name in the attributes Map */
029: public static final String ATT_SCHEMA_NAME = "Schema Name";
030:
031: /** Creates a new instance of DBMetaSchemaNode
032: * @param pdbMeta DatabaseMetaData implementation where this node gets its data from.
033: * @param pdbMetaTreeModel The TreeModel this node is associated to.
034: * @param pcatalogNode The parent node for this node.
035: * @param pstrSchemaName The name of the schema this node is representing. Some databases do not support
036: * schemas, therefore null values are allowed for this parameter
037: */
038: public DBMetaSchemaNode(java.sql.DatabaseMetaData pdbMeta,
039: DatabaseMetaDataTreeModel pdbMetaTreeModel,
040: DBMetaCatalogNode pcatalogNode, String pstrSchemaName) {
041: super (pdbMeta, pdbMetaTreeModel, pcatalogNode);
042: this .setAttribute(ATT_SCHEMA_NAME, pstrSchemaName);
043: }
044:
045: /**
046: * @see ReverseDbTreeNode#isLeaf()
047: */
048: public boolean getAllowsChildren() {
049: return true;
050: }
051:
052: /**
053: * @see ReverseDbTreeNode#getAllowsChildren()
054: */
055: public boolean isLeaf() {
056: return false;
057: }
058:
059: /**
060: * Convenience access method for the schema name. Accesses the
061: * attributes HashMap to retrieve the value.
062: */
063: public String getSchemaName() {
064: return (String) this .getAttribute(ATT_SCHEMA_NAME);
065: }
066:
067: /**
068: * If the schema name is specified, returns the schema name,
069: * otherwise a constant string indicating that the schema name
070: * is emtpy (which is legal for some databases, e.g. MySQL)
071: * @see Object#toString()
072: */
073: public String toString() {
074: if (this .getAttribute(ATT_SCHEMA_NAME) == null)
075: return "Schema not specified";
076: else
077: return this .getAttribute(ATT_SCHEMA_NAME).toString();
078: }
079:
080: /**
081: * Convenience access method to the catalog this schema is associated to.
082: */
083: public DBMetaCatalogNode getCatalog() {
084: return (DBMetaCatalogNode) getParent();
085: }
086:
087: public Class getPropertyEditorClass() {
088: return org.apache.ojb.tools.mapping.reversedb2.propertyEditors.JPnlPropertyEditorDBMetaSchema.class;
089: }
090:
091: /**
092: * Fills the children list with the tables this schema contains.
093: */
094: protected boolean _load() {
095: java.sql.ResultSet rs = null;
096: try {
097: // This synchronization is necessary for Oracle JDBC drivers 8.1.7, 9.0.1, 9.2.0.1
098: // The documentation says synchronization is done within the driver, but they
099: // must have overlooked something. Without the lock we'd get mysterious error
100: // messages.
101: synchronized (getDbMeta()) {
102: getDbMetaTreeModel().setStatusBarMessage(
103: "Reading tables for schema "
104: + getCatalog().getCatalogName() + "."
105: + getSchemaName());
106: rs = getDbMeta().getTables(
107: getCatalog().getCatalogName(), getSchemaName(),
108: "%", null);
109: final java.util.ArrayList alNew = new java.util.ArrayList();
110: while (rs.next()) {
111: getDbMetaTreeModel().setStatusBarMessage(
112: "Creating table "
113: + getCatalog().getCatalogName()
114: + "." + getSchemaName() + "."
115: + rs.getString("TABLE_NAME"));
116: alNew.add(new DBMetaTableNode(getDbMeta(),
117: getDbMetaTreeModel(),
118: DBMetaSchemaNode.this , rs
119: .getString("TABLE_NAME")));
120: }
121: alChildren = alNew;
122: javax.swing.SwingUtilities.invokeLater(new Runnable() {
123: public void run() {
124: getDbMetaTreeModel().nodeStructureChanged(
125: DBMetaSchemaNode.this );
126: }
127: });
128: rs.close();
129: }
130: } catch (java.sql.SQLException sqlEx) {
131: getDbMetaTreeModel().reportSqlError(
132: "Error retrieving tables", sqlEx);
133: try {
134: if (rs != null)
135: rs.close();
136: } catch (java.sql.SQLException sqlEx2) {
137: this .getDbMetaTreeModel().reportSqlError(
138: "Error retrieving tables", sqlEx2);
139: }
140: return false;
141: }
142: return true;
143: }
144: }
|