KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > explorer > actions > AddToIndexAction


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.actions;
21
22 import java.sql.ResultSet JavaDoc;
23 import java.text.MessageFormat JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import org.openide.DialogDisplayer;
30 import org.openide.NotifyDescriptor;
31 import org.openide.nodes.Node;
32
33 import org.netbeans.lib.ddl.impl.CreateIndex;
34 import org.netbeans.lib.ddl.impl.DriverSpecification;
35 import org.netbeans.lib.ddl.impl.DropIndex;
36 import org.netbeans.lib.ddl.impl.Specification;
37
38 import org.netbeans.modules.db.explorer.dlg.ColumnItem;
39 import org.netbeans.modules.db.explorer.dlg.LabeledComboDialog;
40 import org.netbeans.modules.db.explorer.nodes.DatabaseNode;
41 import org.netbeans.modules.db.explorer.infos.DatabaseNodeInfo;
42
43 public class AddToIndexAction extends DatabaseAction {
44     static final long serialVersionUID =-1416260930649261633L;
45     
46     protected boolean enable(Node[] activatedNodes) {
47         return (activatedNodes != null && activatedNodes.length == 1);
48     }
49
50     public void performAction (Node[] activatedNodes) {
51         Node node;
52         if (activatedNodes != null && activatedNodes.length>0)
53             node = activatedNodes[0];
54         else
55             return;
56
57         try {
58             DatabaseNodeInfo info = (DatabaseNodeInfo)node.getCookie(DatabaseNodeInfo.class);
59             DatabaseNodeInfo nfo = info.getParent(nodename);
60
61             String JavaDoc tablename = (String JavaDoc)nfo.get(DatabaseNode.TABLE);
62
63             Specification spec = (Specification)nfo.getSpecification();
64             DriverSpecification drvSpec = info.getDriverSpecification();
65             String JavaDoc index = (String JavaDoc)nfo.get(DatabaseNode.INDEX);
66
67             // List columns used in current index (do not show)
68
HashSet JavaDoc ixrm = new HashSet JavaDoc();
69
70             drvSpec.getIndexInfo(tablename, false, true);
71             ResultSet JavaDoc rs = drvSpec.getResultSet();
72             HashMap JavaDoc rset = new HashMap JavaDoc();
73             boolean isUQ = false;
74             String JavaDoc ixname;
75             while (rs.next()) {
76                 rset = drvSpec.getRow();
77                 ixname = (String JavaDoc) rset.get(new Integer JavaDoc(6));
78
79                 if (!index.equals(ixname))
80                     continue;
81
82                 String JavaDoc colname = (String JavaDoc) rset.get(new Integer JavaDoc(9));
83                 ixrm.add(colname);
84
85                 String JavaDoc val = (String JavaDoc) rset.get(new Integer JavaDoc(4));
86                 if (val.equals("1"))
87                     isUQ = false;
88                 else
89                     isUQ = !(Boolean.valueOf(val).booleanValue());
90                 
91                 rset.clear();
92             }
93             rs.close();
94
95             // List columns not present in current index
96
Vector JavaDoc cols = new Vector JavaDoc(5);
97
98             drvSpec.getColumns(tablename, "%");
99             rs = drvSpec.getResultSet();
100             while (rs.next()) {
101                 rset = drvSpec.getRow();
102                 String JavaDoc colname = (String JavaDoc) rset.get(new Integer JavaDoc(4));
103                 if (!ixrm.contains(colname))
104                     cols.add(colname);
105                 rset.clear();
106             }
107             rs.close();
108             if (cols.size() == 0)
109                 throw new Exception JavaDoc(bundle().getString("EXC_NoUsableColumnInPlace")); // NOI18N
110

111             // Create and execute command
112

113             LabeledComboDialog dlg = new LabeledComboDialog(bundle().getString("AddToIndexTitle"), bundle().getString("AddToIndexLabel"), cols); // NOI18N
114
if (dlg.run()) {
115                 CreateIndex icmd = spec.createCommandCreateIndex(tablename);
116                 icmd.setIndexName(index);
117                 icmd.setObjectOwner((String JavaDoc)info.get(DatabaseNodeInfo.SCHEMA));
118                 if(isUQ)
119                     icmd.setIndexType(ColumnItem.UNIQUE);
120                 else
121                     icmd.setIndexType(new String JavaDoc());
122                 
123                 Iterator JavaDoc enu = ixrm.iterator();
124                 while (enu.hasNext())
125                     icmd.specifyColumn((String JavaDoc)enu.next());
126
127                 icmd.specifyColumn((String JavaDoc)dlg.getSelectedItem());
128                 DropIndex dicmd = spec.createCommandDropIndex(index);
129                 dicmd.setObjectOwner((String JavaDoc)info.get(DatabaseNodeInfo.SCHEMA));
130                 dicmd.setTableName(tablename);
131                 dicmd.execute();
132                 icmd.execute();
133                 info.getParent(DatabaseNode.TABLE).refreshChildren();
134 // ((DatabaseNodeChildren)nfo.getNode().getChildren()).createSubnode(info,true);
135
}
136
137         } catch(Exception JavaDoc exc) {
138             String JavaDoc message = MessageFormat.format(bundle().getString("ERR_UnableToPerformOperation"), new String JavaDoc[] {node.getName(), exc.getMessage()}); // NOI18N
139
DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE));
140         }
141     }
142 }
143
Popular Tags