1 19 20 package org.netbeans.modules.db.explorer.infos; 21 22 import java.io.IOException ; 23 import java.sql.ResultSet ; 24 import java.util.Enumeration ; 25 import java.util.HashMap ; 26 import java.util.Hashtable ; 27 import java.util.Vector ; 28 29 import org.openide.NotifyDescriptor; 30 import org.openide.nodes.Node; 31 32 import org.netbeans.lib.ddl.impl.AbstractCommand; 33 import org.netbeans.lib.ddl.impl.DriverSpecification; 34 import org.netbeans.lib.ddl.impl.Specification; 35 import org.netbeans.api.db.explorer.DatabaseException; 36 import org.netbeans.modules.db.explorer.DatabaseNodeChildren; 37 import org.netbeans.modules.db.explorer.nodes.DatabaseNode; 38 39 public class TableNodeInfo extends DatabaseNodeInfo { 40 static final long serialVersionUID =-632875098783935367L; 41 42 public void initChildren(Vector children) throws DatabaseException { 43 initChildren(children, null); 44 } 45 46 private void initChildren(Vector children, String columnname) throws DatabaseException { 47 try { 48 String table = (String )get(DatabaseNode.TABLE); 49 DriverSpecification drvSpec = getDriverSpecification(); 50 51 Hashtable ihash = new Hashtable (); 53 drvSpec.getPrimaryKeys(table); 54 ResultSet rs = drvSpec.getResultSet(); 55 if (rs != null) { 56 HashMap rset = new HashMap (); 57 DatabaseNodeInfo iinfo; 58 while (rs.next()) { 59 rset = drvSpec.getRow(); 60 iinfo = DatabaseNodeInfo.createNodeInfo(this, DatabaseNode.PRIMARY_KEY, rset); 61 String iname = (String )iinfo.get("name"); ihash.put(iname,iinfo); 63 rset.clear(); 64 } 65 rs.close(); 66 } 67 68 Hashtable ixhash = new Hashtable (); 70 drvSpec.getIndexInfo(table, true, true); 71 rs = drvSpec.getResultSet(); 72 if (rs != null) { 73 HashMap rset = new HashMap (); 74 DatabaseNodeInfo iinfo; 75 while (rs.next()) { 76 rset = drvSpec.getRow(); 77 if (rset.get(new Integer (9)) == null) 78 continue; 79 iinfo = DatabaseNodeInfo.createNodeInfo(this, DatabaseNode.INDEXED_COLUMN, rset); 80 String iname = (String )iinfo.get("name"); ixhash.put(iname,iinfo); 82 rset.clear(); 83 } 84 rs.close(); 85 } 86 87 98 99 drvSpec.getColumns(table, columnname); 101 rs = drvSpec.getResultSet(); 102 if (rs != null) { 103 HashMap rset = new HashMap (); 104 DatabaseNodeInfo nfo; 105 while (rs.next()) { 106 rset = drvSpec.getRow(); 107 String cname = (String ) rset.get(new Integer (4)); 108 109 if (ihash.containsKey(cname)) { 110 nfo = (DatabaseNodeInfo)ihash.get(cname); 111 DatabaseNodeInfo tempInfo = DatabaseNodeInfo.createNodeInfo(this, DatabaseNode.COLUMN, rset); 112 copyProperties(tempInfo, nfo); 113 } else 114 if (ixhash.containsKey(cname)) { 115 nfo = (DatabaseNodeInfo)ixhash.get(cname); 116 DatabaseNodeInfo tempInfo = DatabaseNodeInfo.createNodeInfo(this, DatabaseNode.COLUMN, rset); 117 copyProperties(tempInfo, nfo); 118 } 119 else 123 nfo = DatabaseNodeInfo.createNodeInfo(this, DatabaseNode.COLUMN, rset); 125 126 children.add(nfo); 127 rset.clear(); 128 } 129 rs.close(); 130 } 131 } catch (Exception e) { 132 DatabaseException dbe = new DatabaseException(e.getMessage()); 133 dbe.initCause(e); 134 throw dbe; 135 } 136 } 137 138 142 private void copyProperties(DatabaseNodeInfo source, DatabaseNodeInfo target) { 143 Enumeration keys = source.keys(); 144 while (keys.hasMoreElements()) { 145 String nextKey = keys.nextElement().toString(); 146 147 148 if (target.get(nextKey) == null) 149 target.put(nextKey, source.get(nextKey)); 150 } 151 } 152 153 public void setProperty(String key, Object obj) { 154 try { 155 if (key.equals("remarks")) 156 setRemarks((String )obj); put(key, obj); 158 } catch (Exception ex) { 159 ex.printStackTrace(); 160 } 161 } 162 163 public void setRemarks(String rem) throws DatabaseException { 164 String tablename = (String )get(DatabaseNode.TABLE); 165 Specification spec = (Specification)getSpecification(); 166 try { 167 AbstractCommand cmd = spec.createCommandCommentTable(tablename, rem); 168 cmd.setObjectOwner((String )get(DatabaseNodeInfo.SCHEMA)); 169 cmd.execute(); 170 } catch (Exception e) { 171 DatabaseException dbe = new DatabaseException(e.getMessage()); 172 dbe.initCause(e); 173 throw dbe; 174 } 175 } 176 177 public void dropIndex(DatabaseNodeInfo tinfo) throws DatabaseException { 178 } 180 181 public void refreshChildren() throws DatabaseException { 182 getNode().getChildren().getNodes(); 184 185 Vector charr = new Vector (); 187 put(DatabaseNodeInfo.CHILDREN, charr); 188 initChildren(charr); 189 190 try { 192 Node[] subTreeNodes = new Node[charr.size()+1+1]; 193 194 DatabaseNodeChildren children = (DatabaseNodeChildren) getNode().getChildren(); 196 final Node[] childrenNodes = children.getNodes(); 197 for (int i=0; i < childrenNodes.length; i++) 198 if ((childrenNodes[i]).getCookie(IndexListNodeInfo.class) != null) { 200 final int j = i; 201 javax.swing.SwingUtilities.invokeLater(new Runnable () { 202 public void run() { 203 try { 204 ((DatabaseNode) childrenNodes[j]).getInfo().refreshChildren(); 206 } catch(Exception ex) { 207 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex); 208 } 209 } 210 }); 211 subTreeNodes[charr.size()] = childrenNodes[i]; 213 } else 214 if ((childrenNodes[i]).getCookie(ForeignKeyListNodeInfo.class) != null) { 216 final int j = i; 217 javax.swing.SwingUtilities.invokeLater(new Runnable () { 218 public void run() { 219 try { 220 ((DatabaseNode) childrenNodes[j]).getInfo().refreshChildren(); 222 } catch(Exception ex) { 223 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex); 224 } 225 } 226 }); 227 subTreeNodes[charr.size()+1] = childrenNodes[i]; 229 } 230 231 children.remove(childrenNodes); 233 234 for (int i=0; i<charr.size(); i++) 236 subTreeNodes[i] = children.createNode((DatabaseNodeInfo) charr.elementAt(i)); 237 238 children.add(subTreeNodes); 240 241 fireRefresh(); 242 } catch (Exception ex) { 243 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex); 244 } 245 } 246 247 public void delete() throws IOException { 248 try { 249 Specification spec = (Specification)getSpecification(); 250 AbstractCommand cmd = spec.createCommandDropTable(getTable()); 251 cmd.setObjectOwner((String )get(DatabaseNodeInfo.SCHEMA)); 252 cmd.execute(); 253 254 fireRefresh(); 255 } catch (Exception e) { 256 org.openide.DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(e.getMessage(), NotifyDescriptor.ERROR_MESSAGE)); 257 } 258 } 259 260 263 public ColumnNodeInfo getChildrenColumnInfo(ColumnNodeInfo info) { 264 String scode = info.getCode(); 265 String sname = info.getName(); 266 267 try { 268 Enumeration enu = getChildren().elements(); 269 while (enu.hasMoreElements()) { 270 ColumnNodeInfo elem = (ColumnNodeInfo)enu.nextElement(); 271 if (elem.getCode().equals(scode) && elem.getName().equals(sname)) 272 return elem; 273 } 274 } catch (Exception e) { 275 } 277 278 return null; 279 } 280 281 public void addColumn(String tname) throws DatabaseException { 282 try { 283 Vector chvec = new Vector (1); 284 initChildren(chvec, tname); 285 if (chvec.size() == 1) { 286 DatabaseNodeInfo nfo = (DatabaseNodeInfo)chvec.elementAt(0); 287 DatabaseNodeChildren chld = (DatabaseNodeChildren)getNode().getChildren(); 288 chld.createSubnode(nfo, true); 289 } 290 refreshChildren(); 292 } catch (Exception e) { 293 DatabaseException dbe = new DatabaseException(e.getMessage()); 294 dbe.initCause(e); 295 throw dbe; 296 } 297 } 298 } 299 | Popular Tags |