KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > explorer > infos > TableListNodeInfo


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.db.explorer.infos;
21
22 import java.sql.DatabaseMetaData JavaDoc;
23 import java.sql.ResultSet JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.sql.Statement JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Vector JavaDoc;
32 import org.netbeans.api.db.explorer.DatabaseException;
33 import org.netbeans.lib.ddl.impl.DriverSpecification;
34 import org.netbeans.modules.db.explorer.nodes.DatabaseNode;
35 import org.openide.ErrorManager;
36
37 public class TableListNodeInfo extends DatabaseNodeInfo implements TableOwnerOperations {
38     static final long serialVersionUID =-6156362126513404875L;
39
40     protected void initChildren(Vector JavaDoc children) throws DatabaseException {
41         try {
42             String JavaDoc[] types = new String JavaDoc[] {"TABLE"}; // NOI18N
43
List JavaDoc recycleBinTables;
44             
45             DriverSpecification drvSpec = getDriverSpecification();
46             
47             // issue 76953: do not display tables from the Recycle Bin on Oracle 10 and higher
48
DatabaseMetaData JavaDoc dmd = drvSpec.getMetaData();
49             if ("Oracle".equals(dmd.getDatabaseProductName()) && dmd.getDatabaseMajorVersion() >= 10) { // NOI18N
50
recycleBinTables = getOracleRecycleBinTables(dmd);
51             } else {
52                 recycleBinTables = Collections.EMPTY_LIST;
53             }
54             
55             drvSpec.getTables("%", types);
56             ResultSet JavaDoc rs = drvSpec.getResultSet();
57             if (rs != null) {
58                 HashMap JavaDoc rset = new HashMap JavaDoc();
59                 DatabaseNodeInfo info;
60                 while (rs.next()) {
61                     rset = drvSpec.getRow();
62                     info = DatabaseNodeInfo.createNodeInfo(this, DatabaseNode.TABLE, rset);
63                     if (info != null) {
64                         if (!recycleBinTables.contains(info.getName())) {
65                             info.put(DatabaseNode.TABLE, info.getName());
66                             children.add(info);
67                         }
68                     } else
69                         throw new Exception JavaDoc(bundle().getString("EXC_UnableToCreateNodeInformationForTable")); // NOI18N
70
rset.clear();
71                 }
72                 rs.close();
73             }
74         } catch (Exception JavaDoc e) {
75             DatabaseException dbe = new DatabaseException(e.getMessage());
76             dbe.initCause(e);
77             throw dbe;
78         }
79     }
80
81     /** Adds driver specified in drv into list.
82     * Creates new node info and adds node into node children.
83     */

84     public void addTable(String JavaDoc tname) throws DatabaseException {
85         try {
86             String JavaDoc[] types = new String JavaDoc[] {"TABLE", "BASE"}; // NOI18N
87

88             DriverSpecification drvSpec = getDriverSpecification();
89             drvSpec.getTables(tname, types);
90             ResultSet JavaDoc rs = drvSpec.getResultSet();
91             if (rs != null) {
92                 HashMap JavaDoc rset = new HashMap JavaDoc();
93                 rs.next();
94                 rset = drvSpec.getRow();
95                 if (rset == null)
96                     throw new NullPointerException JavaDoc();
97                 DatabaseNodeInfo info = DatabaseNodeInfo.createNodeInfo(this, DatabaseNode.TABLE, rset);
98                 rset.clear();
99                 rs.close();
100
101                 if (info != null)
102                     info.put(DatabaseNode.TABLE, info.getName());
103                 else
104                     throw new Exception JavaDoc(bundle().getString("EXC_UnableToCreateNodeInformationForTable")); // NOI18N
105
// refersh list of tables
106
refreshChildren();
107             }
108         } catch (Exception JavaDoc e) {
109             DatabaseException dbe = new DatabaseException(e.getMessage());
110             dbe.initCause(e);
111             throw dbe;
112         }
113     }
114
115     /** Returns tablenodeinfo specified by info
116     * Compares code and name only.
117     */

118     public TableNodeInfo getChildrenTableInfo(TableNodeInfo info) {
119         String JavaDoc scode = info.getCode();
120         String JavaDoc sname = info.getName();
121
122         try {
123             Enumeration JavaDoc enu = getChildren().elements();
124             while (enu.hasMoreElements()) {
125                 TableNodeInfo elem = (TableNodeInfo)enu.nextElement();
126                 if (elem.getCode().equals(scode) && elem.getName().equals(sname))
127                     return elem;
128             }
129         } catch (Exception JavaDoc e) {
130             //PENDING
131
}
132         
133         return null;
134     }
135     
136     private List JavaDoc getOracleRecycleBinTables(DatabaseMetaData JavaDoc dmd) {
137         List JavaDoc result = new ArrayList JavaDoc();
138         try {
139             Statement JavaDoc stmt = dmd.getConnection().createStatement();
140             try {
141                 ResultSet JavaDoc rs = stmt.executeQuery("SELECT OBJECT_NAME FROM RECYCLEBIN WHERE TYPE = 'TABLE'"); // NOI18N
142
try {
143                     while (rs.next()) {
144                         result.add(rs.getString("OBJECT_NAME")); // NOI18N
145
}
146                 } finally {
147                     rs.close();
148                 }
149             } finally {
150                 stmt.close();
151             }
152         } catch (SQLException JavaDoc e) {
153             // not critical, logging is enough
154
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
155             result = Collections.EMPTY_LIST;
156         }
157         return result;
158     }
159     
160 /*
161     public void dropIndex(DatabaseNodeInfo tinfo) throws DatabaseException {
162         DatabaseNode node = (DatabaseNode)tinfo.getNode();
163         DatabaseNodeChildren chld = (DatabaseNodeChildren)getNode().getChildren();
164         try {
165             String tname = tinfo.getName();
166             Specification spec = (Specification)getSpecification();
167             AbstractCommand cmd = spec.createCommandDropIndex(tname);
168             cmd.execute();
169             getNode().getChildren().remove(new Node[]{node});
170         } catch (Exception e) {
171             throw new DatabaseException(e.getMessage());
172         }
173     }
174 */

175 }
176
Popular Tags