KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > sql > visualeditor > querybuilder > TableNode


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.db.sql.visualeditor.querybuilder;
20
21 import org.openide.ErrorManager;
22
23 import org.openide.nodes.AbstractNode;
24 import org.openide.nodes.Sheet;
25 import org.openide.nodes.Children;
26 import org.openide.nodes.PropertySupport;
27 import org.openide.nodes.PropertySupport.Reflection;
28
29 import org.openide.util.NbBundle;
30 import org.openide.ErrorManager;
31 import org.openide.NotifyDescriptor;
32 import org.openide.DialogDisplayer;
33
34 import org.netbeans.modules.db.sql.visualeditor.Log;
35
36 /**
37  * Provides a place to hang properties for a node (table) in the query graph.
38  * <p>
39  *
40  * @author Jim Davidson
41  */

42
43 public class TableNode extends AbstractNode
44 {
45     // Private variable
46
private int SQL_IDENTIFIER_LENGTH = 32;
47
48     private boolean DEBUG = false;
49     private String JavaDoc _fullTableName=null;
50     private String JavaDoc _corrName=null;
51     private QueryBuilder _queryBuilder;
52
53     // Constructor
54

55     TableNode(String JavaDoc fullTableName)
56     {
57         super(Children.LEAF);
58         _fullTableName = fullTableName;
59     }
60
61     TableNode(String JavaDoc fullTableName, String JavaDoc corrName, QueryBuilder queryBuilder)
62     {
63         super(Children.LEAF);
64         _fullTableName = fullTableName;
65         _corrName = corrName;
66         _queryBuilder = queryBuilder;
67     }
68
69     // Accessors/mutators
70

71     public String JavaDoc getTableName() {
72         return _fullTableName;
73     }
74
75     public String JavaDoc getCorrName() {
76         return _corrName;
77     }
78
79     public void setCorrName(String JavaDoc corrName) {
80
81         Log.err.log(ErrorManager.INFORMATIONAL, "Entering TableNode.setCorrName, corrname: " + corrName); // NOI18N
82

83         // Note the old value
84
String JavaDoc oldCorrName = getCorrName();
85         String JavaDoc oldTableSpec = (oldCorrName==null) ? getTableName() : oldCorrName;
86
87         // Sometimes we are called when the user has not made any changes. Just return
88
if ( ((corrName == null) && (oldCorrName==null)) ||
89              ((corrName !=null) && (corrName.equals(oldCorrName))))
90             return;
91
92         // Save the new value
93
if (corrName.trim().length()==0) {
94             _corrName=null;
95         }
96         else {
97             // Modify the corrName if necessary, to ensure that it's unique
98
// Addresses 5005528 Setting same alias for two tables produces incorrect query.
99
// A return value of null means the name was already unique
100
if ( ! isAliasValid ( corrName.trim() ) ) {
101                 // display an error message "Invalid alias name"
102
// return without changing anything.
103
String JavaDoc msg = NbBundle.getMessage(TableNode.class, "INVALID_ALIAS"); // NOI18N
104
NotifyDescriptor d = new NotifyDescriptor.Message(msg + "\n\n" +corrName, NotifyDescriptor.ERROR_MESSAGE); // NOI18N
105
DialogDisplayer.getDefault().notify(d);
106                 return;
107             }
108             String JavaDoc tmp = _queryBuilder._queryModel.genUniqueName(corrName);
109             _corrName= (tmp==null) ? corrName : tmp;
110         }
111
112         // Update the entire model
113
_queryBuilder._queryModel.renameTableSpec(oldTableSpec, _corrName);
114         // _queryBuilder.setTableColumnCorrName(oldTableSpec, _corrName);
115

116         // Clear all panes and generate fresh
117
_queryBuilder.generate();
118     }
119
120     // Create minimal property sheet
121

122     protected Sheet createSheet() {
123         Sheet s = Sheet.createDefault();
124         Sheet.Set ss = s.get(Sheet.PROPERTIES);
125         try {
126             PropertySupport.Reflection p;
127             p = new Reflection(this, String JavaDoc.class, "getTableName", null); // NOI18N
128
p.setName("tableName"); // NOI18N
129
String JavaDoc tableDisplayName = NbBundle.getMessage(TableNode.class, "TABLE_DISPLAY_NAME"); // NOI18N
130
// p.setDisplayName("Table Name"); // NOI18N
131
p.setDisplayName(tableDisplayName);
132
133             String JavaDoc tableShortDescription = NbBundle.getMessage(TableNode.class, "TABLE_SHORT_DESCRIPTION"); // NOI18N
134
// p.setShortDescription("Table name"); // NOI18N
135
p.setShortDescription(tableShortDescription);
136             ss.put(p);
137             p = new Reflection(this, String JavaDoc.class, "getCorrName", "setCorrName"); // NOI18N
138
p.setName("aliasName"); // NOI18N
139
String JavaDoc aliasDisplayName = NbBundle.getMessage(TableNode.class, "ALIAS_DISPLAY_NAME"); // NOI18N
140
// p.setDisplayName("Table Alias"); // NOI18N
141
p.setDisplayName(aliasDisplayName);
142             String JavaDoc aliasShortDescription = NbBundle.getMessage(TableNode.class, "ALIAS_SHORT_DESCRIPTION"); // NOI18N
143
// p.setShortDescription("Alias name for the table"); // NOI18N
144
p.setShortDescription(aliasShortDescription);
145             ss.put(p);
146         } catch (NoSuchMethodException JavaDoc nsme) {
147             ErrorManager.getDefault().notify(nsme);
148         }
149         return s;
150     }
151
152     public boolean isAliasValid ( String JavaDoc aliasName ) {
153         // As per the SQL 92,
154
// SQL syntax requires users to supply names for elements such as
155
// tables, aliases, views, cursors, and columns when they define
156
// them. SQL statements must use those names to refer to the table,
157
// view, or other element.
158
//
159
// The maximum length for SQL identifiers is 32 characters.
160

161         // There are two types of SQL identifiers:
162
//
163
// * Conventional identifiers
164
// Conventional SQL identifiers must:
165
// * Begin with an uppercase or lowercase letter.
166
// * Contain only letters, digits, or the underscore character ( _ ).
167
// * Not be reserved words.
168
// * Use ASCII characters only.
169
// * SQL does not distinguish between uppercase and lowercase
170
// letters in SQL identifiers. It converts all names specified
171
// as conventional identifiers to uppercase, but statements
172
// can refer to the names in mixed case.
173
//
174
// * Delimited identifiers enclosed in double quotation marks
175
// * Delimited identifiers are strings of no more than 32 ASCII
176
// characters enclosed in double quotation marks ( " " ).
177
// Enclosing a name in double quotation marks preserves the
178
// case of the name and allows it to be a reserved word or to
179
// contain special characters. Special characters are any
180
// characters other than letters, digits, or the underscore
181
// character. Subsequent references to a delimited identifier
182
// must also use enclosing double quotation marks. To include
183
// a double quotation mark character in a delimited identifier,
184
// precede it with another double quotation mark.
185
//
186

187         if ( aliasName.startsWith("\"") && aliasName.endsWith("\"") ) { // NOI18N
188
return isValidDelimitedIdentifier ( aliasName.substring (1, (aliasName.length()-1) ) ) ;
189         }
190         else {
191             return isValidConventionalIdentifier ( aliasName ) ;
192         }
193     }
194
195     boolean isValidDelimitedIdentifier ( String JavaDoc identifier ) {
196         if ( identifier.length() > SQL_IDENTIFIER_LENGTH )
197             return false;
198
199         return true;
200     }
201
202     boolean isValidConventionalIdentifier ( String JavaDoc identifier ) {
203
204         if ( identifier.length() > SQL_IDENTIFIER_LENGTH )
205             return false;
206
207         char[] charArray = identifier.toCharArray();
208
209         // has to begin with uppercase or lower case letter
210
if (! Character.isLetter ( charArray[0] ) ) {
211             if (DEBUG)
212                 System.out.println("isValidConventionalIdentifier called. charArray[0] = " + charArray[0] + "\n" ); // NOI18N
213
return false;
214         }
215
216         for ( int i=1; i<charArray.length; i++ ) {
217             // Contain only letters, digits, or the underscore character ( _ ).
218
if ( ( ! Character.isLetter ( charArray [i] ) ) &&
219                  ( ! Character.isDigit ( charArray [i] ) ) &&
220                  ( charArray [i] != '_' ) ) {
221                 return false;
222             }
223         }
224         return true;
225     }
226 }
227
Popular Tags