001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.db.explorer.infos;
043:
044: import java.io.IOException;
045: import java.sql.ResultSet;
046: import java.util.Enumeration;
047: import java.util.HashMap;
048: import java.util.Hashtable;
049: import java.util.Vector;
050: import java.util.logging.Level;
051: import java.util.logging.Logger;
052:
053: import org.openide.NotifyDescriptor;
054: import org.openide.nodes.Node;
055:
056: import org.netbeans.lib.ddl.impl.AbstractCommand;
057: import org.netbeans.lib.ddl.impl.DriverSpecification;
058: import org.netbeans.lib.ddl.impl.Specification;
059: import org.netbeans.api.db.explorer.DatabaseException;
060: import org.netbeans.modules.db.explorer.DatabaseNodeChildren;
061: import org.netbeans.modules.db.explorer.nodes.DatabaseNode;
062:
063: public class TableNodeInfo extends DatabaseNodeInfo {
064: static final long serialVersionUID = -632875098783935367L;
065:
066: public void initChildren(Vector children) throws DatabaseException {
067: initChildren(children, null);
068: }
069:
070: private void initChildren(Vector children, String columnname)
071: throws DatabaseException {
072: try {
073: String table = (String) get(DatabaseNode.TABLE);
074: DriverSpecification drvSpec = getDriverSpecification();
075:
076: // Primary keys
077: Hashtable ihash = new Hashtable();
078: drvSpec.getPrimaryKeys(table);
079: ResultSet rs = drvSpec.getResultSet();
080: if (rs != null) {
081: HashMap rset = new HashMap();
082: DatabaseNodeInfo iinfo;
083: while (rs.next()) {
084: rset = drvSpec.getRow();
085: iinfo = DatabaseNodeInfo.createNodeInfo(this ,
086: DatabaseNode.PRIMARY_KEY, rset);
087: String iname = (String) iinfo.get("name"); //NOI18N
088: ihash.put(iname, iinfo);
089: rset.clear();
090: }
091: rs.close();
092: }
093:
094: // Indexes
095: Hashtable ixhash = new Hashtable();
096: drvSpec.getIndexInfo(table, true, true);
097: rs = drvSpec.getResultSet();
098: if (rs != null) {
099: HashMap rset = new HashMap();
100: DatabaseNodeInfo iinfo;
101: while (rs.next()) {
102: rset = drvSpec.getRow();
103: if (rset.get(new Integer(9)) == null)
104: continue;
105: iinfo = DatabaseNodeInfo.createNodeInfo(this ,
106: DatabaseNode.INDEXED_COLUMN, rset);
107: String iname = (String) iinfo.get("name"); //NOI18N
108: ixhash.put(iname, iinfo);
109: rset.clear();
110: }
111: rs.close();
112: }
113:
114: /*
115: // Foreign keys
116: Hashtable fhash = new Hashtable();
117: rs = dmd.getImportedKeys(catalog,user,table);
118: while (rs.next()) {
119: DatabaseNodeInfo finfo = DatabaseNodeInfo.createNodeInfo(this, DatabaseNode.FOREIGN_KEY, rs);
120: String iname = (String)finfo.get("name"); //NOI18N
121: fhash.put(iname,finfo);
122: }
123: rs.close();
124: */
125:
126: // Columns
127: drvSpec.getColumns(table, columnname);
128: rs = drvSpec.getResultSet();
129: if (rs != null) {
130: HashMap rset = new HashMap();
131: DatabaseNodeInfo nfo;
132: while (rs.next()) {
133: rset = drvSpec.getRow();
134: String cname = (String) rset.get(new Integer(4));
135:
136: if (ihash.containsKey(cname)) {
137: nfo = (DatabaseNodeInfo) ihash.get(cname);
138: DatabaseNodeInfo tempInfo = DatabaseNodeInfo
139: .createNodeInfo(this ,
140: DatabaseNode.COLUMN, rset);
141: copyProperties(tempInfo, nfo);
142: } else if (ixhash.containsKey(cname)) {
143: nfo = (DatabaseNodeInfo) ixhash.get(cname);
144: DatabaseNodeInfo tempInfo = DatabaseNodeInfo
145: .createNodeInfo(this ,
146: DatabaseNode.COLUMN, rset);
147: copyProperties(tempInfo, nfo);
148: }
149: // else
150: // if (fhash.containsKey(cname)) {
151: // nfo = (DatabaseNodeInfo)fhash.get(cname);
152: else
153: // nfo = DatabaseNodeInfo.createNodeInfo(this, DatabaseNode.COLUMN, drvSpec.rsTemp);
154: nfo = DatabaseNodeInfo.createNodeInfo(this ,
155: DatabaseNode.COLUMN, rset);
156:
157: children.add(nfo);
158: rset.clear();
159: }
160: rs.close();
161: }
162: } catch (Exception e) {
163: DatabaseException dbe = new DatabaseException(e
164: .getMessage());
165: dbe.initCause(e);
166: throw dbe;
167: }
168: }
169:
170: /**
171: * Copies all properties from soure to target. Existing properties are not
172: * overwritten
173: */
174: private void copyProperties(DatabaseNodeInfo source,
175: DatabaseNodeInfo target) {
176: Enumeration keys = source.keys();
177: while (keys.hasMoreElements()) {
178: String nextKey = keys.nextElement().toString();
179:
180: /* existing properties are not overwritten*/
181: if (target.get(nextKey) == null)
182: target.put(nextKey, source.get(nextKey));
183: }
184: }
185:
186: public void setProperty(String key, Object obj) {
187: try {
188: if (key.equals("remarks"))
189: setRemarks((String) obj); //NOI18N
190: put(key, obj);
191: } catch (Exception ex) {
192: ex.printStackTrace();
193: }
194: }
195:
196: public void setRemarks(String rem) throws DatabaseException {
197: String tablename = (String) get(DatabaseNode.TABLE);
198: Specification spec = (Specification) getSpecification();
199: try {
200: AbstractCommand cmd = spec.createCommandCommentTable(
201: tablename, rem);
202: cmd.setObjectOwner((String) get(DatabaseNodeInfo.SCHEMA));
203: cmd.execute();
204: } catch (Exception e) {
205: DatabaseException dbe = new DatabaseException(e
206: .getMessage());
207: dbe.initCause(e);
208: throw dbe;
209: }
210: }
211:
212: public void dropIndex(DatabaseNodeInfo tinfo)
213: throws DatabaseException {
214: //???
215: }
216:
217: public void refreshChildren() throws DatabaseException {
218: // force init collection
219: getNode().getChildren().getNodes();
220:
221: // create list of columns (infos)
222: Vector charr = new Vector();
223: put(DatabaseNodeInfo.CHILDREN, charr);
224: initChildren(charr);
225:
226: // create sub-tree (by infos)
227: try {
228: Node[] subTreeNodes = new Node[charr.size() + 1/*special node Foreign keys*/+ /*special node Indexes*/1];
229:
230: // current sub-tree
231: DatabaseNodeChildren children = (DatabaseNodeChildren) getNode()
232: .getChildren();
233: final Node[] childrenNodes = children.getNodes();
234: for (int i = 0; i < childrenNodes.length; i++)
235: // is it node Indexes
236: if ((childrenNodes[i])
237: .getCookie(IndexListNodeInfo.class) != null) {
238: final int j = i;
239: javax.swing.SwingUtilities
240: .invokeLater(new Runnable() {
241: public void run() {
242: try {
243: // refresh indexes
244: ((DatabaseNode) childrenNodes[j])
245: .getInfo()
246: .refreshChildren();
247: } catch (Exception ex) {
248: Logger.getLogger("global").log(
249: Level.INFO, null, ex);
250: }
251: }
252: });
253: // add into refreshed sub-tree
254: subTreeNodes[charr.size()] = childrenNodes[i];
255: } else
256: // is it node Foreign keys or column?
257: if ((childrenNodes[i])
258: .getCookie(ForeignKeyListNodeInfo.class) != null) {
259: final int j = i;
260: javax.swing.SwingUtilities
261: .invokeLater(new Runnable() {
262: public void run() {
263: try {
264: // refresh foreign keys
265: ((DatabaseNode) childrenNodes[j])
266: .getInfo()
267: .refreshChildren();
268: } catch (Exception ex) {
269: Logger.getLogger("global").log(
270: Level.INFO, null, ex);
271: }
272: }
273: });
274: // add into refreshed sub-tree
275: subTreeNodes[charr.size() + 1] = childrenNodes[i];
276: }
277:
278: // remove current sub-tree
279: children.remove(childrenNodes);
280:
281: // build refreshed sub-tree
282: for (int i = 0; i < charr.size(); i++)
283: subTreeNodes[i] = children
284: .createNode((DatabaseNodeInfo) charr
285: .elementAt(i));
286:
287: // add built sub-tree
288: children.add(subTreeNodes);
289:
290: fireRefresh();
291: } catch (Exception ex) {
292: Logger.getLogger("global").log(Level.INFO, null, ex);
293: }
294: }
295:
296: public void delete() throws IOException {
297: try {
298: DDLHelper.deleteTable((Specification) getSpecification(),
299: (String) get(DatabaseNodeInfo.SCHEMA), getTable());
300:
301: fireRefresh();
302: } catch (Exception e) {
303: org.openide.DialogDisplayer.getDefault().notify(
304: new NotifyDescriptor.Message(e.getMessage(),
305: NotifyDescriptor.ERROR_MESSAGE));
306: }
307: }
308:
309: /** Returns ColumnNodeInfo specified by info
310: * Compares code and name only.
311: */
312: public ColumnNodeInfo getChildrenColumnInfo(ColumnNodeInfo info) {
313: String scode = info.getCode();
314: String sname = info.getName();
315:
316: try {
317: Enumeration enu = getChildren().elements();
318: while (enu.hasMoreElements()) {
319: ColumnNodeInfo elem = (ColumnNodeInfo) enu
320: .nextElement();
321: if (elem.getCode().equals(scode)
322: && elem.getName().equals(sname))
323: return elem;
324: }
325: } catch (Exception e) {
326: //PENDING
327: }
328:
329: return null;
330: }
331:
332: public void addColumn(String tname) throws DatabaseException {
333: try {
334: Vector chvec = new Vector(1);
335: initChildren(chvec, tname);
336: if (chvec.size() == 1) {
337: DatabaseNodeInfo nfo = (DatabaseNodeInfo) chvec
338: .elementAt(0);
339: DatabaseNodeChildren chld = (DatabaseNodeChildren) getNode()
340: .getChildren();
341: chld.createSubnode(nfo, true);
342: }
343: // refresh list of columns
344: refreshChildren();
345: } catch (Exception e) {
346: DatabaseException dbe = new DatabaseException(e
347: .getMessage());
348: dbe.initCause(e);
349: throw dbe;
350: }
351: }
352: }
|