1 19 20 package org.netbeans.modules.db.explorer.actions; 21 22 import java.sql.ResultSet ; 23 import java.text.MessageFormat ; 24 import java.util.HashMap ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.Vector ; 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 tablename = (String )nfo.get(DatabaseNode.TABLE); 62 63 Specification spec = (Specification)nfo.getSpecification(); 64 DriverSpecification drvSpec = info.getDriverSpecification(); 65 String index = (String )nfo.get(DatabaseNode.INDEX); 66 67 HashSet ixrm = new HashSet (); 69 70 drvSpec.getIndexInfo(tablename, false, true); 71 ResultSet rs = drvSpec.getResultSet(); 72 HashMap rset = new HashMap (); 73 boolean isUQ = false; 74 String ixname; 75 while (rs.next()) { 76 rset = drvSpec.getRow(); 77 ixname = (String ) rset.get(new Integer (6)); 78 79 if (!index.equals(ixname)) 80 continue; 81 82 String colname = (String ) rset.get(new Integer (9)); 83 ixrm.add(colname); 84 85 String val = (String ) rset.get(new Integer (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 Vector cols = new Vector (5); 97 98 drvSpec.getColumns(tablename, "%"); 99 rs = drvSpec.getResultSet(); 100 while (rs.next()) { 101 rset = drvSpec.getRow(); 102 String colname = (String ) rset.get(new Integer (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 (bundle().getString("EXC_NoUsableColumnInPlace")); 111 113 LabeledComboDialog dlg = new LabeledComboDialog(bundle().getString("AddToIndexTitle"), bundle().getString("AddToIndexLabel"), cols); if (dlg.run()) { 115 CreateIndex icmd = spec.createCommandCreateIndex(tablename); 116 icmd.setIndexName(index); 117 icmd.setObjectOwner((String )info.get(DatabaseNodeInfo.SCHEMA)); 118 if(isUQ) 119 icmd.setIndexType(ColumnItem.UNIQUE); 120 else 121 icmd.setIndexType(new String ()); 122 123 Iterator enu = ixrm.iterator(); 124 while (enu.hasNext()) 125 icmd.specifyColumn((String )enu.next()); 126 127 icmd.specifyColumn((String )dlg.getSelectedItem()); 128 DropIndex dicmd = spec.createCommandDropIndex(index); 129 dicmd.setObjectOwner((String )info.get(DatabaseNodeInfo.SCHEMA)); 130 dicmd.setTableName(tablename); 131 dicmd.execute(); 132 icmd.execute(); 133 info.getParent(DatabaseNode.TABLE).refreshChildren(); 134 } 136 137 } catch(Exception exc) { 138 String message = MessageFormat.format(bundle().getString("ERR_UnableToPerformOperation"), new String [] {node.getName(), exc.getMessage()}); DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE)); 140 } 141 } 142 } 143 | Popular Tags |