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-2007 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: package org.netbeans.modules.sql.framework.ui.view.property;
042:
043: import com.sun.sql.framework.utils.Attribute;
044: import java.beans.PropertyEditor;
045: import java.util.ArrayList;
046: import java.util.Collections;
047: import java.util.HashSet;
048: import java.util.Iterator;
049: import java.util.List;
050: import java.util.Set;
051:
052: import java.util.Vector;
053: import org.netbeans.modules.sql.framework.model.DBColumn;
054: import org.netbeans.modules.sql.framework.model.DBTable;
055: import org.netbeans.modules.sql.framework.model.ForeignKey;
056: import org.netbeans.modules.sql.framework.model.Index;
057: import org.netbeans.modules.sql.framework.model.SQLDBTable;
058: import org.netbeans.modules.sql.framework.ui.editor.property.impl.DefaultPropertyEditor;
059: import org.netbeans.modules.sql.framework.ui.view.BasicTopView;
060: import org.netbeans.modules.sql.framework.ui.view.IGraphViewContainer;
061: import org.netbeans.modules.sql.framework.ui.view.graph.SQLBasicTableArea;
062: import org.openide.nodes.Node;
063:
064: /**
065: * @author Ritesh Adval
066: */
067: public class TableProperties {
068: /** ColumnPropertySupport instance for foreign keys. */
069: protected ColumnPropertySupport fKeys;
070:
071: /** ColumnPropertySupport instance for indices. */
072: protected ColumnPropertySupport indices;
073:
074: /** ColumnPropertySupport instance for unique columns. */
075: protected ColumnPropertySupport uniqueCols;
076:
077: /** ColumnPropertySupport instance for primary keys. */
078: protected ColumnPropertySupport pKeys;
079:
080: protected IGraphViewContainer editor;
081: protected SQLBasicTableArea gNode;
082:
083: private SQLDBTable table;
084:
085: /**
086: * Gets the alias name for this table.
087: *
088: * @return alias name
089: */
090: public String getAliasName() {
091: return table.getAliasName();
092: }
093:
094: /**
095: * Gets the batch size for this table.
096: *
097: * @return Integer representing batch size for the table
098: */
099: public Integer getBatchSize() {
100: return new Integer(this .table.getBatchSize());
101: }
102:
103: /**
104: * Gets appropriate custom editor, if any, for the given property.
105: *
106: * @param property Node.Property whose custom editor is sought
107: * @return PropertyEditor associated with <code>property</code>, or null if none
108: * exists.
109: */
110: public PropertyEditor getCustomEditor(Node.Property property) {
111: if (property.getName().equals("primaryKeys")) {
112: return new DefaultPropertyEditor.ListEditor(pKeys
113: .getDisplayVector());
114: } else if (property.getName().equals("modelName")) {
115: Vector str = new Vector();
116: str.add(table.getParent().getModelName());
117: return new DefaultPropertyEditor.ListEditor(str);
118: } else if (property.getName().equals("foreignKeys")) {
119: return new DefaultPropertyEditor.ListEditor(fKeys
120: .getDisplayVector());
121: } else if (property.getName().equals("indices")) {
122: return new DefaultPropertyEditor.ListEditor(indices
123: .getDisplayVector());
124: }
125:
126: return null;
127: }
128:
129: /**
130: * Gets display name of this table.
131: *
132: * @return table disply name.
133: */
134: public String getDisplayName() {
135: return table.getDisplayName();
136: }
137:
138: private List<String> getForeignKeyList(DBTable tbl, DBColumn column) {
139: ArrayList<String> optionList = new ArrayList<String>();
140: String refString = column.getName() + " --> ";
141:
142: List list = tbl.getForeignKeys();
143: Iterator it = list.iterator();
144: while (it.hasNext()) {
145: ForeignKey fk = (ForeignKey) it.next();
146: if (fk.contains(column)) {
147: List pkColumnList = fk.getPKColumnNames();
148: Iterator it1 = pkColumnList.iterator();
149: while (it1.hasNext()) {
150: String pkColName = (String) it1.next();
151: String optStr = refString.toString() + pkColName;
152: optionList.add(optStr);
153: }
154: }
155: }
156:
157: return optionList;
158: }
159:
160: /**
161: * Gets delimited String list of foreign keys associated with this table.
162: *
163: * @return delimited String list of foreign keys
164: */
165: public String getForeignKeys() {
166: return fKeys.getDisplayString();
167: }
168:
169: /**
170: * Gets delimited String list of indices associated with this table.
171: *
172: * @return delimited String list of indices
173: */
174: public String getIndices() {
175: return indices.getDisplayString();
176: }
177:
178: /**
179: * Gets name of parent DatabaseModel for this table
180: *
181: * @return name of parent DatabaseModel
182: */
183: public String getModelName() {
184: return table.getParent().getModelName();
185: }
186:
187: /**
188: * Gets delimited String list of primary key columns associated with this table.
189: *
190: * @return delimited String list of primary key columns
191: */
192: public String getPrimaryKeys() {
193: return pKeys.getDisplayString();
194: }
195:
196: /**
197: * Gets the current database-derived schema name.
198: *
199: * @return current schema name as supplied by Database.
200: */
201: public String getSchema() {
202: return table.getSchema();
203: }
204:
205: /**
206: * Gets the current Database-derived catalog name.
207: *
208: * @return current catalog name as supplied by Database.
209: */
210: public String getCatalog() {
211: return table.getCatalog();
212: }
213:
214: /**
215: * Gets the table prefix, if any, associated with this table.
216: *
217: * @return table prefix
218: */
219: public String getTablePrefix() {
220: return table.getTablePrefix();
221: }
222:
223: /**
224: * Gets the user defined table name.
225: *
226: * @return user defined table
227: */
228: public String getUserDefinedTableName() {
229: return table.getUserDefinedTableName();
230: }
231:
232: /**
233: * Gets the user defined schema name, if any.
234: *
235: * @return user defined schema name
236: */
237: public String getUserDefinedSchemaName() {
238: return table.getUserDefinedSchemaName();
239: }
240:
241: /**
242: * Gets the user defined catalog name, if any.
243: *
244: * @return user defined catalog name
245: */
246: public String getUserDefinedCatalogName() {
247: return table.getUserDefinedCatalogName();
248: }
249:
250: // RFE-102428
251: /**
252: * Gets the Staging Table Name
253: *
254: * @return Staging Table Name
255: */
256: public String getStagingTableName() {
257: return table.getStagingTableName();
258: }
259:
260: /**
261: * Indicates whether to use fully-qualified form in resolving table name.
262: *
263: * @return true to use fully-qualified form, false otherwise.
264: */
265: public boolean isUseFullyQualifiedName() {
266: return table.isUsingFullyQualifiedName();
267: }
268:
269: /**
270: * Initializes display properties using values from the given SQLDBTable.
271: *
272: * @param tbl SQLDBTable from which to initialize values
273: */
274: protected void initializeProperties(SQLDBTable tbl) {
275: this .table = tbl;
276: ArrayList<String> pkList = new ArrayList<String>();
277: ArrayList<String> fkList = new ArrayList<String>();
278: ArrayList<String> idxList = new ArrayList<String>();
279: Set<String> indexedUniqueCols = new HashSet<String>();
280:
281: List columnList = table.getColumnList();
282: Iterator it = columnList.iterator();
283: while (it.hasNext()) {
284: DBColumn column = (DBColumn) it.next();
285: boolean pk = column.isPrimaryKey();
286: boolean fk = column.isForeignKey();
287: boolean indexed = column.isIndexed();
288:
289: //create pk option
290: if (pk) {
291: pkList.add(column.getName());
292: }
293:
294: //get fk options
295: if (fk) {
296: List<String> fkListForColumn = getForeignKeyList(table,
297: column);
298: if (fkListForColumn.size() > 0) {
299: fkList.addAll(fkListForColumn);
300: }
301: }
302:
303: //create idx option
304: if (indexed) {
305: idxList.add(column.getName());
306: }
307: }
308:
309: List indexList = table.getIndexes();
310: it = indexList.iterator();
311: while (it.hasNext()) {
312: Index idx = (Index) it.next();
313: if (idx.isUnique()) {
314: indexedUniqueCols.addAll(idx.getColumnNames());
315: }
316: }
317:
318: //sort options
319: Collections.sort(pkList);
320: Collections.sort(fkList);
321: Collections.sort(idxList);
322:
323: //create objects
324: pKeys = new ColumnPropertySupport(pkList);
325: fKeys = new ColumnPropertySupport(fkList);
326: indices = new ColumnPropertySupport(idxList);
327: List<String> ndxCols = new ArrayList<String>(indexedUniqueCols);
328: uniqueCols = new ColumnPropertySupport(ndxCols);
329:
330: }
331:
332: /**
333: * Sets the alias name for this table
334: *
335: * @param aName alias name
336: */
337: public void setAliasName(String aName) {
338: this .table.setAliasName(aName);
339: }
340:
341: /**
342: * Sets the batch size for this table
343: *
344: * @param newSize new value for batch size
345: */
346: public void setBatchSize(Integer newSize) {
347: table.setBatchSize(newSize.intValue());
348: setDirty(true);
349: }
350:
351: /**
352: * Sets the table prefix.
353: *
354: * @param tPrefix new table prefix
355: */
356: public void setTablePrefix(String tPrefix) {
357: table.setTablePrefix(tPrefix);
358: setDirty(true);
359: }
360:
361: /**
362: * Sets the user defined table name.
363: *
364: * @param newName new user defined table name
365: */
366: public void setUserDefinedTableName(String newName) {
367: table.setUserDefinedTableName(newName);
368: setDirty(true);
369: }
370:
371: /**
372: * Sets the user defined schema name.
373: *
374: * @param newName user defined schema name
375: */
376: public void setUserDefinedSchemaName(String newName) {
377: table.setUserDefinedSchemaName(newName);
378: setDirty(true);
379: }
380:
381: /**
382: * Sets the user defined catalog name.
383: *
384: * @param newName user defined catalog name
385: */
386: public void setUserDefinedCatalogName(String newName) {
387: table.setUserDefinedCatalogName(newName);
388: setDirty(true);
389: }
390:
391: /**
392: * Sets whether to use fully-qualified form in resolving table name.
393: *
394: * @param useFullName true to use fully-qualified form, false otherwise.
395: */
396: public void setUseFullyQualifiedName(boolean useFullName) {
397: table.setUsingFullyQualifiedName(useFullName);
398: setDirty(true);
399: }
400:
401: // RFE-102428
402: /**
403: * Sets the Staging Table Name.
404: *
405: * @param stgTbleName user defined catalog name
406: */
407: public void setStagingTableName(String stgTbleName) {
408: table.setStagingTableName(stgTbleName);
409: setDirty(true);
410: }
411:
412: public void setOrgProperty(String attrName, String newFileType) {
413: table.setAttribute("ORGPROP_" + attrName, newFileType);
414: setDirty(true);
415: }
416:
417: public Attribute getOrgProperty(String attrName) {
418: return table.getAttribute("ORGPROP_" + attrName);
419: }
420:
421: protected void setDirty(boolean dirty) {
422: if (editor instanceof BasicTopView) {
423: BasicTopView topView = (BasicTopView) editor;
424: topView.setDirty(dirty);
425: }
426: }
427: }
|