KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.text.MessageFormat JavaDoc;
27
28 import org.openide.DialogDisplayer;
29 import org.openide.NotifyDescriptor;
30
31 import org.netbeans.lib.ddl.impl.AbstractCommand;
32 import org.netbeans.lib.ddl.impl.CreateTable;
33 import org.netbeans.lib.ddl.impl.DriverSpecification;
34 import org.netbeans.lib.ddl.impl.ModifyColumn;
35 import org.netbeans.lib.ddl.impl.RemoveColumn;
36 import org.netbeans.lib.ddl.impl.Specification;
37 import org.netbeans.lib.ddl.impl.TableColumn;
38
39 import org.netbeans.api.db.explorer.DatabaseException;
40 import org.netbeans.modules.db.explorer.nodes.DatabaseNode;
41
42 public class ColumnNodeInfo extends DatabaseNodeInfo {
43     static final long serialVersionUID =-1470704512178901918L;
44     
45     public boolean canAdd(Map JavaDoc propmap, String JavaDoc propname) {
46         if (propname.equals("decdigits")) { //NOI18N
47
int type = ((Integer JavaDoc) get("datatype")).intValue(); //NOI18N
48
return (type == java.sql.Types.FLOAT || type == java.sql.Types.REAL || type == java.sql.Types.DOUBLE);
49         }
50
51         return super.canAdd(propmap, propname);
52     }
53
54     public Object JavaDoc getProperty(String JavaDoc key) {
55         if (key.equals("columnsize") || key.equals("decdigits") || key.equals("ordpos") || key.equals("key_seq")) { //NOI18N
56
Object JavaDoc val = get(key);
57             if (val instanceof String JavaDoc)
58                 return Integer.valueOf((String JavaDoc) val);
59         }
60         if (key.equals("isnullable")) { //NOI18N
61
String JavaDoc nullable = (String JavaDoc) get(key);
62             boolean eq = (nullable == null) ? false : (nullable).toUpperCase().equals("YES"); //NOI18N
63
return eq ? Boolean.TRUE : Boolean.FALSE;
64         }
65         return super.getProperty(key);
66     }
67
68     public void delete() throws IOException JavaDoc {
69         try {
70             String JavaDoc code = getCode();
71             String JavaDoc table = (String JavaDoc) get(DatabaseNode.TABLE);
72             Specification spec = (Specification) getSpecification();
73             RemoveColumn cmd = (RemoveColumn) spec.createCommandRemoveColumn(table);
74             cmd.removeColumn((String JavaDoc) get(code));
75             cmd.setObjectOwner((String JavaDoc) get(DatabaseNodeInfo.SCHEMA));
76             cmd.execute();
77
78             // refresh list of columns after column drop
79
//getParent().refreshChildren();
80
fireRefresh();
81         //} catch(DatabaseException exc) {
82
//String message = MessageFormat.format(bundle.getString("ERR_UnableToDeleteColumn"), new String[] {exc.getMessage()}); // NOI18N
83
//Topmanager.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE));
84
} catch (Exception JavaDoc exc) {
85             DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(exc.getMessage(), NotifyDescriptor.ERROR_MESSAGE));
86 // throw new IOException(exc.getMessage());
87
}
88     }
89
90     public TableColumn getColumnSpecification() throws DatabaseException {
91         TableColumn col = null;
92
93         try {
94             Specification spec = (Specification) getSpecification();
95             CreateTable cmd = (CreateTable) spec.createCommandCreateTable("DUMMY"); //NOI18N
96
String JavaDoc code = getCode();
97
98             if (code.equals(DatabaseNode.PRIMARY_KEY)) {
99                 col = (TableColumn)cmd.createPrimaryKeyColumn(getName());
100             } else if (code.equals(DatabaseNode.INDEXED_COLUMN)) {
101                 col = (TableColumn)cmd.createUniqueColumn(getName());
102             } else if (code.equals(DatabaseNode.FOREIGN_KEY)) {
103                 col = null;
104             } else if (code.equals(DatabaseNode.COLUMN)) {
105                 col = (TableColumn)cmd.createColumn(getName());
106             } else {
107                 String JavaDoc message = MessageFormat.format(bundle().getString("EXC_UnknownCode"), new String JavaDoc[] {code}); // NOI18N
108
throw new DatabaseException(message);
109             }
110
111             DriverSpecification drvSpec = getDriverSpecification();
112             drvSpec.getColumns((String JavaDoc) get(DatabaseNode.TABLE), (String JavaDoc)get(code));
113             ResultSet JavaDoc rs = drvSpec.getResultSet();
114             if (rs != null) {
115                 rs.next();
116                 HashMap JavaDoc rset = drvSpec.getRow();
117                 
118                 try {
119                     //hack because of MSSQL ODBC problems - see DriverSpecification.getRow() for more info - shouln't be thrown
120
col.setColumnType(Integer.parseInt((String JavaDoc) rset.get(new Integer JavaDoc(5))));
121                     col.setColumnSize(Integer.parseInt((String JavaDoc) rset.get(new Integer JavaDoc(7))));
122                 } catch (NumberFormatException JavaDoc exc) {
123                     col.setColumnType(0);
124                     col.setColumnSize(0);
125                 }
126
127                 col.setNullAllowed(((String JavaDoc) rset.get(new Integer JavaDoc(18))).toUpperCase().equals("YES")); //NOI18N
128
col.setDefaultValue((String JavaDoc) rset.get(new Integer JavaDoc(13)));
129                 rset.clear();
130
131                 rs.close();
132             }
133         } catch (Exception JavaDoc e) {
134             throw new DatabaseException(e.getMessage());
135         }
136
137         return col;
138     }
139
140     // catalog,schema,tablename,name,datatype,typename,
141
// columnsize,bufflen,decdigits,radix,nullable,remarks,coldef,
142
// reserved1,reserved2,octetlen,ordpos,isnullable
143

144     public void setProperty(String JavaDoc key, Object JavaDoc obj) {
145         try {
146             if (key.equals("remarks")) //NOI18N
147
setRemarks((String JavaDoc)obj);
148             else if (key.equals("isnullable")) { //NOI18N
149
setNullAllowed(((Boolean JavaDoc) obj).booleanValue());
150                 obj = (((Boolean JavaDoc) obj).equals(Boolean.TRUE) ? "YES" : "NO"); //NOI18N
151
} else if (key.equals("columnsize")) //NOI18N
152
setColumnSize((Integer JavaDoc) obj);
153             else if (key.equals("decdigits")) //NOI18N
154
setDecimalDigits((Integer JavaDoc) obj);
155             else if (key.equals("coldef")) //NOI18N
156
setDefaultValue((String JavaDoc) obj);
157             else if (key.equals("datatype")) //NOI18N
158
setDataType((Integer JavaDoc) obj);
159             
160             super.setProperty(key, obj);
161         } catch (Exception JavaDoc e) {
162             e.printStackTrace();
163         }
164     }
165
166     public void setRemarks(String JavaDoc rem) throws DatabaseException {
167         String JavaDoc tablename = (String JavaDoc) get(DatabaseNode.TABLE);
168         Specification spec = (Specification) getSpecification();
169         try {
170             AbstractCommand cmd = spec.createCommandCommentTable(tablename, rem);
171             cmd.setObjectOwner((String JavaDoc) get(DatabaseNodeInfo.SCHEMA));
172             cmd.execute();
173         } catch (Exception JavaDoc e) {
174             throw new DatabaseException(e.getMessage());
175         }
176     }
177
178     public void setColumnSize(Integer JavaDoc size) throws DatabaseException {
179         try {
180             Specification spec = (Specification) getSpecification();
181             ModifyColumn cmd = (ModifyColumn) spec.createCommandModifyColumn(getTable());
182             TableColumn col = getColumnSpecification();
183             col.setColumnSize(size.intValue());
184             cmd.setColumn(col);
185             cmd.setObjectOwner((String JavaDoc) get(DatabaseNodeInfo.SCHEMA));
186             cmd.execute();
187         } catch (Exception JavaDoc e) {
188             throw new DatabaseException(e.getMessage());
189         }
190     }
191
192     public void setDecimalDigits(Integer JavaDoc size) throws DatabaseException {
193         try {
194             Specification spec = (Specification) getSpecification();
195             ModifyColumn cmd = (ModifyColumn) spec.createCommandModifyColumn(getTable());
196             TableColumn col = getColumnSpecification();
197             col.setDecimalSize(size.intValue());
198             cmd.setColumn(col);
199             cmd.setObjectOwner((String JavaDoc) get(DatabaseNodeInfo.SCHEMA));
200             cmd.execute();
201         } catch (Exception JavaDoc e) {
202             throw new DatabaseException(e.getMessage());
203         }
204     }
205
206     public void setDefaultValue(String JavaDoc val) throws DatabaseException {
207         try {
208             Specification spec = (Specification) getSpecification();
209             ModifyColumn cmd = (ModifyColumn) spec.createCommandModifyColumn(getTable());
210             TableColumn col = getColumnSpecification();
211             col.setDefaultValue(val);
212             cmd.setColumn(col);
213             cmd.setObjectOwner((String JavaDoc) get(DatabaseNodeInfo.SCHEMA));
214             cmd.execute();
215         } catch (Exception JavaDoc e) {
216             throw new DatabaseException(e.getMessage());
217         }
218     }
219
220     public void setNullAllowed(boolean flag) throws DatabaseException {
221         try {
222             Specification spec = (Specification) getSpecification();
223             ModifyColumn cmd = (ModifyColumn) spec.createCommandModifyColumn(getTable());
224             TableColumn col = getColumnSpecification();
225             col.setNullAllowed(flag);
226             cmd.setColumn(col);
227             cmd.setObjectOwner((String JavaDoc) get(DatabaseNodeInfo.SCHEMA));
228             cmd.execute();
229         } catch (Exception JavaDoc e) {
230             throw new DatabaseException(e.getMessage());
231         }
232     }
233
234     public void setDataType(Integer JavaDoc type) throws DatabaseException {
235         try {
236             Specification spec = (Specification) getSpecification();
237             ModifyColumn cmd = (ModifyColumn) spec.createCommandModifyColumn(getTable());
238             TableColumn col = getColumnSpecification();
239             col.setColumnType(type.intValue());
240             cmd.setColumn(col);
241             cmd.setObjectOwner((String JavaDoc) get(DatabaseNodeInfo.SCHEMA));
242             cmd.execute();
243         } catch (Exception JavaDoc e) {
244             throw new DatabaseException(e.getMessage());
245         }
246     }
247
248     /**
249      * Using name of column for hashCode computation.
250      *
251      * @return computed hashCode based on name of column
252      */

253     public int hashCode() {
254         return getName().hashCode();
255     }
256     
257 }
258
Popular Tags