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