KickJava   Java API By Example, From Geeks To Geeks.

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


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.Connection JavaDoc;
23 import java.sql.DatabaseMetaData JavaDoc;
24 import java.sql.ResultSet JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.Vector JavaDoc;
30
31 import org.openide.ErrorManager;
32
33 import org.netbeans.lib.ddl.impl.DriverSpecification;
34 import org.netbeans.api.db.explorer.DatabaseException;
35 import org.netbeans.modules.db.explorer.DatabaseNodeChildren;
36 import org.netbeans.modules.db.explorer.nodes.DatabaseNode;
37
38 public class IndexListNodeInfo extends DatabaseNodeInfo {
39     static final long serialVersionUID =5809643799834921044L;
40
41     public void initChildren(Vector JavaDoc children) throws DatabaseException {
42         try {
43             String JavaDoc table = (String JavaDoc) get(DatabaseNode.TABLE);
44             DriverSpecification drvSpec = getDriverSpecification();
45             Connection JavaDoc con = getConnection();
46             DatabaseMetaData JavaDoc dmd = con.getMetaData();
47             ResultSet JavaDoc rs = dmd.getIndexInfo(drvSpec.getCatalog(), drvSpec.getSchema(), table, false, true);
48             if (rs != null) {
49                 Set JavaDoc ixmap = new HashSet JavaDoc();
50                 IndexNodeInfo info;
51                 while (rs.next()) {
52                     HashMap JavaDoc rset = getRow(rs);
53                     if (rset == null)
54                         continue;
55                     if (rset.get(new Integer JavaDoc(6)) != null) {
56                         info = (IndexNodeInfo)DatabaseNodeInfo.createNodeInfo(this, DatabaseNode.INDEX, rset);
57                         if (info != null) {
58                             if (!ixmap.contains(info.getName())) {
59                                 ixmap.add(info.getName());
60                                 info.put("index", info.getName()); //NOI18N
61
children.add(info);
62                             }
63                         } else
64                             throw new Exception JavaDoc(bundle().getString("EXC_UnableToCreateIndexNodeInfo")); //NOI18N
65
}
66                 }
67                 rs.close();
68             }
69         } catch (Exception JavaDoc e) {
70             throw new DatabaseException(e.getMessage());
71         }
72     }
73
74     public HashMap JavaDoc getRow(ResultSet JavaDoc rs) {
75         HashMap JavaDoc rset = new HashMap JavaDoc();
76         Object JavaDoc value;
77
78         try {
79             int count = rs.getMetaData().getColumnCount();
80
81             for (int i = 1; i <= count; i++) {
82                 value = null;
83                 try {
84                     value = rs.getString(i);
85                 } catch (SQLException JavaDoc exc) {
86                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc);
87                     rset = null;
88                     break;
89                 }
90                 rset.put(new Integer JavaDoc(i), value);
91             }
92         } catch (SQLException JavaDoc exc) {
93             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, exc);
94             rset = null;
95         }
96
97         return rset;
98     }
99     
100     public void addIndex(String JavaDoc name) throws DatabaseException {
101         try {
102             String JavaDoc table = (String JavaDoc)get(DatabaseNode.TABLE);
103
104             DriverSpecification drvSpec = getDriverSpecification();
105             drvSpec.getIndexInfo(table, false, true);
106             ResultSet JavaDoc rs = drvSpec.getResultSet();
107             if (rs != null) {
108                 HashMap JavaDoc rset = new HashMap JavaDoc();
109                 IndexNodeInfo info = null;
110                 String JavaDoc findex;
111                 while (rs.next()) {
112                     rset = drvSpec.getRow();
113                     findex = (String JavaDoc) rset.get(new Integer JavaDoc(6));
114                     if (findex != null)
115                         if(findex.equalsIgnoreCase(name))
116                             info = (IndexNodeInfo)DatabaseNodeInfo.createNodeInfo(this, DatabaseNode.INDEX, rset);
117                     rset.clear();
118                 }
119                 rs.close();
120
121                 if (info != null) ((DatabaseNodeChildren)getNode().getChildren()).createSubnode(info,true);
122                 //refresh list of columns due to the column's icons
123
getParent().refreshChildren();
124             }
125         } catch (Exception JavaDoc e) {
126             e.printStackTrace();
127             throw new DatabaseException(e.getMessage());
128         }
129     }
130
131 }
132
Popular Tags