001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.db.explorer.infos;
043:
044: import java.sql.DatabaseMetaData;
045: import java.sql.ResultSet;
046: import java.sql.SQLException;
047: import java.sql.Statement;
048: import java.util.ArrayList;
049: import java.util.Collections;
050: import java.util.Enumeration;
051: import java.util.HashMap;
052: import java.util.List;
053: import java.util.Vector;
054: import java.util.logging.Level;
055: import java.util.logging.Logger;
056: import org.netbeans.api.db.explorer.DatabaseException;
057: import org.netbeans.lib.ddl.impl.DriverSpecification;
058: import org.netbeans.modules.db.explorer.nodes.DatabaseNode;
059:
060: public class TableListNodeInfo extends DatabaseNodeInfo implements
061: TableOwnerOperations {
062: static final long serialVersionUID = -6156362126513404875L;
063: public static final Logger LOGGER = Logger
064: .getLogger(TableListNodeInfo.class.getName());
065:
066: protected void initChildren(Vector children)
067: throws DatabaseException {
068: try {
069: String[] types = new String[] { "TABLE" }; // NOI18N
070: List recycleBinTables;
071:
072: DriverSpecification drvSpec = getDriverSpecification();
073:
074: // issue 76953: do not display tables from the Recycle Bin on Oracle 10 and higher
075: DatabaseMetaData dmd = drvSpec.getMetaData();
076: if ("Oracle".equals(dmd.getDatabaseProductName())) { // NOI18N
077: try {
078: if (dmd.getDatabaseMajorVersion() >= 10) {
079: recycleBinTables = getOracleRecycleBinTables(dmd);
080: } else {
081: recycleBinTables = Collections.EMPTY_LIST;
082: }
083: } catch (AbstractMethodError ame) {
084: // Some older versions of Oracle driver throw an exception on getDatabaseMajorVersion()
085: LOGGER
086: .log(
087: Level.WARNING,
088: "Some older versions of the Oracle driver do not support getDatabaseMajorVersion(). Setting recycleBinTables to an empty list.",
089: ame); // NOI18N
090: recycleBinTables = Collections.EMPTY_LIST;
091: } catch (SQLException e) {
092: LOGGER
093: .log(
094: Level.WARNING,
095: "Some older versions of the Oracle driver do not support getDatabaseMajorVersion(). Setting recycleBinTables to an empty list.",
096: e); // NOI18N
097: recycleBinTables = Collections.EMPTY_LIST;
098: }
099:
100: } else {
101: recycleBinTables = Collections.EMPTY_LIST;
102: }
103:
104: drvSpec.getTables("%", types);
105: ResultSet rs = drvSpec.getResultSet();
106: if (rs != null) {
107: HashMap rset = new HashMap();
108: DatabaseNodeInfo info;
109: while (rs.next()) {
110: rset = drvSpec.getRow();
111: info = DatabaseNodeInfo.createNodeInfo(this ,
112: DatabaseNode.TABLE, rset);
113: if (info != null) {
114: if (!recycleBinTables.contains(info.getName())) {
115: info
116: .put(DatabaseNode.TABLE, info
117: .getName());
118: children.add(info);
119: }
120: } else
121: throw new Exception(
122: bundle()
123: .getString(
124: "EXC_UnableToCreateNodeInformationForTable")); // NOI18N
125: rset.clear();
126: }
127: rs.close();
128: }
129: } catch (Exception e) {
130: DatabaseException dbe = new DatabaseException(e
131: .getMessage());
132: dbe.initCause(e);
133: throw dbe;
134: }
135: }
136:
137: /** Adds driver specified in drv into list.
138: * Creates new node info and adds node into node children.
139: */
140: public void addTable(String tname) throws DatabaseException {
141: refreshChildren();
142: }
143:
144: /** Returns tablenodeinfo specified by info
145: * Compares code and name only.
146: */
147: public TableNodeInfo getChildrenTableInfo(TableNodeInfo info) {
148: String scode = info.getCode();
149: String sname = info.getName();
150:
151: try {
152: Enumeration enu = getChildren().elements();
153: while (enu.hasMoreElements()) {
154: TableNodeInfo elem = (TableNodeInfo) enu.nextElement();
155: if (elem.getCode().equals(scode)
156: && elem.getName().equals(sname))
157: return elem;
158: }
159: } catch (Exception e) {
160: //PENDING
161: }
162:
163: return null;
164: }
165:
166: private List getOracleRecycleBinTables(DatabaseMetaData dmd) {
167: List result = new ArrayList();
168: try {
169: Statement stmt = dmd.getConnection().createStatement();
170: try {
171: ResultSet rs = stmt
172: .executeQuery("SELECT OBJECT_NAME FROM RECYCLEBIN WHERE TYPE = 'TABLE'"); // NOI18N
173: try {
174: while (rs.next()) {
175: result.add(rs.getString("OBJECT_NAME")); // NOI18N
176: }
177: } finally {
178: rs.close();
179: }
180: } finally {
181: stmt.close();
182: }
183: } catch (SQLException e) {
184: // not critical, logging is enough
185: Logger.getLogger("global").log(Level.INFO, null, e);
186: result = Collections.EMPTY_LIST;
187: }
188: return result;
189: }
190:
191: /*
192: public void dropIndex(DatabaseNodeInfo tinfo) throws DatabaseException {
193: DatabaseNode node = (DatabaseNode)tinfo.getNode();
194: DatabaseNodeChildren chld = (DatabaseNodeChildren)getNode().getChildren();
195: try {
196: String tname = tinfo.getName();
197: Specification spec = (Specification)getSpecification();
198: AbstractCommand cmd = spec.createCommandDropIndex(tname);
199: cmd.execute();
200: getNode().getChildren().remove(new Node[]{node});
201: } catch (Exception e) {
202: throw new DatabaseException(e.getMessage());
203: }
204: }
205: */
206: }
|