KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
23 import java.sql.ResultSet JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import java.util.Vector JavaDoc;
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 JavaDoc children) throws DatabaseException {
43         initChildren(children, null);
44     }
45
46     private void initChildren(Vector JavaDoc children, String JavaDoc columnname) throws DatabaseException {
47         try {
48             String JavaDoc table = (String JavaDoc)get(DatabaseNode.TABLE);
49             DriverSpecification drvSpec = getDriverSpecification();
50
51             // Primary keys
52
Hashtable JavaDoc ihash = new Hashtable JavaDoc();
53             drvSpec.getPrimaryKeys(table);
54             ResultSet JavaDoc rs = drvSpec.getResultSet();
55             if (rs != null) {
56                 HashMap JavaDoc rset = new HashMap JavaDoc();
57                 DatabaseNodeInfo iinfo;
58                 while (rs.next()) {
59                     rset = drvSpec.getRow();
60                     iinfo = DatabaseNodeInfo.createNodeInfo(this, DatabaseNode.PRIMARY_KEY, rset);
61                     String JavaDoc iname = (String JavaDoc)iinfo.get("name"); //NOI18N
62
ihash.put(iname,iinfo);
63                     rset.clear();
64                 }
65                 rs.close();
66             }
67
68             // Indexes
69
Hashtable JavaDoc ixhash = new Hashtable JavaDoc();
70             drvSpec.getIndexInfo(table, true, true);
71             rs = drvSpec.getResultSet();
72             if (rs != null) {
73                 HashMap JavaDoc rset = new HashMap JavaDoc();
74                 DatabaseNodeInfo iinfo;
75                 while (rs.next()) {
76                     rset = drvSpec.getRow();
77                     if (rset.get(new Integer JavaDoc(9)) == null)
78                         continue;
79                     iinfo = DatabaseNodeInfo.createNodeInfo(this, DatabaseNode.INDEXED_COLUMN, rset);
80                     String JavaDoc iname = (String JavaDoc)iinfo.get("name"); //NOI18N
81
ixhash.put(iname,iinfo);
82                     rset.clear();
83                 }
84                 rs.close();
85             }
86
87             /*
88                         // Foreign keys
89                         Hashtable fhash = new Hashtable();
90                         rs = dmd.getImportedKeys(catalog,user,table);
91                         while (rs.next()) {
92                             DatabaseNodeInfo finfo = DatabaseNodeInfo.createNodeInfo(this, DatabaseNode.FOREIGN_KEY, rs);
93                             String iname = (String)finfo.get("name"); //NOI18N
94                             fhash.put(iname,finfo);
95                         }
96                         rs.close();
97             */

98
99             // Columns
100
drvSpec.getColumns(table, columnname);
101             rs = drvSpec.getResultSet();
102             if (rs != null) {
103                 HashMap JavaDoc rset = new HashMap JavaDoc();
104                 DatabaseNodeInfo nfo;
105                 while (rs.next()) {
106                     rset = drvSpec.getRow();
107                     String JavaDoc cname = (String JavaDoc) rset.get(new Integer JavaDoc(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
120
// if (fhash.containsKey(cname)) {
121
// nfo = (DatabaseNodeInfo)fhash.get(cname);
122
else
123 // nfo = DatabaseNodeInfo.createNodeInfo(this, DatabaseNode.COLUMN, drvSpec.rsTemp);
124
nfo = DatabaseNodeInfo.createNodeInfo(this, DatabaseNode.COLUMN, rset);
125
126                     children.add(nfo);
127                     rset.clear();
128                 }
129                 rs.close();
130             }
131         } catch (Exception JavaDoc e) {
132             DatabaseException dbe = new DatabaseException(e.getMessage());
133             dbe.initCause(e);
134             throw dbe;
135         }
136     }
137
138     /**
139      * Copies all properties from soure to target. Existing properties are not
140      * overwritten
141      */

142     private void copyProperties(DatabaseNodeInfo source, DatabaseNodeInfo target) {
143         Enumeration JavaDoc keys = source.keys();
144         while (keys.hasMoreElements()) {
145             String JavaDoc nextKey = keys.nextElement().toString();
146             
147             /* existing properties are not overwritten*/
148             if (target.get(nextKey) == null)
149                 target.put(nextKey, source.get(nextKey));
150         }
151     }
152     
153     public void setProperty(String JavaDoc key, Object JavaDoc obj) {
154         try {
155             if (key.equals("remarks"))
156                 setRemarks((String JavaDoc)obj); //NOI18N
157
put(key, obj);
158         } catch (Exception JavaDoc ex) {
159             ex.printStackTrace();
160         }
161     }
162
163     public void setRemarks(String JavaDoc rem) throws DatabaseException {
164         String JavaDoc tablename = (String JavaDoc)get(DatabaseNode.TABLE);
165         Specification spec = (Specification)getSpecification();
166         try {
167             AbstractCommand cmd = spec.createCommandCommentTable(tablename, rem);
168             cmd.setObjectOwner((String JavaDoc)get(DatabaseNodeInfo.SCHEMA));
169             cmd.execute();
170         } catch (Exception JavaDoc 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         //???
179
}
180
181     public void refreshChildren() throws DatabaseException {
182         // force init collection
183
getNode().getChildren().getNodes();
184         
185         // create list of columns (infos)
186
Vector JavaDoc charr = new Vector JavaDoc();
187         put(DatabaseNodeInfo.CHILDREN, charr);
188         initChildren(charr);
189         
190         // create sub-tree (by infos)
191
try {
192             Node[] subTreeNodes = new Node[charr.size()+1/*special node Foreign keys*/+/*special node Indexes*/1];
193
194             // current sub-tree
195
DatabaseNodeChildren children = (DatabaseNodeChildren) getNode().getChildren();
196             final Node[] childrenNodes = children.getNodes();
197             for (int i=0; i < childrenNodes.length; i++)
198                 // is it node Indexes
199
if ((childrenNodes[i]).getCookie(IndexListNodeInfo.class) != null) {
200                     final int j = i;
201                     javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc() {
202                         public void run() {
203                             try {
204                                 // refresh indexes
205
((DatabaseNode) childrenNodes[j]).getInfo().refreshChildren();
206                             } catch(Exception JavaDoc ex) {
207                                 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex);
208                             }
209                         }
210                     });
211                     // add into refreshed sub-tree
212
subTreeNodes[charr.size()] = childrenNodes[i];
213                 } else
214                 // is it node Foreign keys or column?
215
if ((childrenNodes[i]).getCookie(ForeignKeyListNodeInfo.class) != null) {
216                     final int j = i;
217                     javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc() {
218                         public void run() {
219                             try {
220                                 // refresh foreign keys
221
((DatabaseNode) childrenNodes[j]).getInfo().refreshChildren();
222                             } catch(Exception JavaDoc ex) {
223                                 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex);
224                             }
225                         }
226                     });
227                     // add into refreshed sub-tree
228
subTreeNodes[charr.size()+1] = childrenNodes[i];
229                 }
230
231             // remove current sub-tree
232
children.remove(childrenNodes);
233
234             // build refreshed sub-tree
235
for (int i=0; i<charr.size(); i++)
236                 subTreeNodes[i] = children.createNode((DatabaseNodeInfo) charr.elementAt(i));
237
238             // add built sub-tree
239
children.add(subTreeNodes);
240             
241             fireRefresh();
242         } catch (Exception JavaDoc ex) {
243             org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex);
244         }
245     }
246
247     public void delete() throws IOException JavaDoc {
248         try {
249             Specification spec = (Specification)getSpecification();
250             AbstractCommand cmd = spec.createCommandDropTable(getTable());
251             cmd.setObjectOwner((String JavaDoc)get(DatabaseNodeInfo.SCHEMA));
252             cmd.execute();
253             
254             fireRefresh();
255         } catch (Exception JavaDoc e) {
256             org.openide.DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(e.getMessage(), NotifyDescriptor.ERROR_MESSAGE));
257         }
258     }
259
260     /** Returns ColumnNodeInfo specified by info
261     * Compares code and name only.
262     */

263     public ColumnNodeInfo getChildrenColumnInfo(ColumnNodeInfo info) {
264         String JavaDoc scode = info.getCode();
265         String JavaDoc sname = info.getName();
266
267         try {
268             Enumeration JavaDoc 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 JavaDoc e) {
275             //PENDING
276
}
277         
278         return null;
279     }
280
281     public void addColumn(String JavaDoc tname) throws DatabaseException {
282         try {
283             Vector JavaDoc chvec = new Vector JavaDoc(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             // refresh list of columns
291
refreshChildren();
292         } catch (Exception JavaDoc e) {
293             DatabaseException dbe = new DatabaseException(e.getMessage());
294             dbe.initCause(e);
295             throw dbe;
296         }
297     }
298 }
299
Popular Tags