001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: /*
021: *
022: * Copyright 2005 Sun Microsystems, Inc.
023: *
024: * Licensed under the Apache License, Version 2.0 (the "License");
025: * you may not use this file except in compliance with the License.
026: * You may obtain a copy of the License at
027: *
028: * http://www.apache.org/licenses/LICENSE-2.0
029: *
030: * Unless required by applicable law or agreed to in writing, software
031: * distributed under the License is distributed on an "AS IS" BASIS,
032: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
033: * See the License for the specific language governing permissions and
034: * limitations under the License.
035: *
036: */
037:
038: package org.netbeans.modules.jdbcwizard.builder;
039:
040: import java.util.Collections;
041: import java.util.List;
042: import java.util.ArrayList;
043: import java.util.ResourceBundle;
044: import org.openide.util.NbBundle;
045:
046: /**
047: * Class to hold procedure metadata.
048: *
049: * @author
050: */
051: public class Table {
052: private String name = ""; // name of table
053:
054: private String javaName = ""; // java name of table
055:
056: private String catalog = ""; // catalog
057:
058: private String schema = ""; // schema
059:
060: private int numColumns = 0; // number of table columns
061:
062: private int numColumnsSelected = 0; // number of table columns selected
063:
064: private TableColumn[] columns; // array of table columns
065:
066: private String type = "TABLE"; // TABLE, SYSTEM TABLE, VIEW - from driver
067:
068: private List indexList; // List of IndexColumn objects
069:
070: private List fkColumnList; // List of KeyColumn objects (PK cols)
071:
072: private List pkColumnList; // List of ForeignKeyColumn objects (FK cols)
073:
074: private boolean selected;
075:
076: /**
077: * Creates an instance of Table with the given attributes.
078: *
079: * @param tname Table name
080: * @param tcatalog Catalog name
081: * @param tschema Schema name
082: * @param ttype Table type
083: */
084: public Table(final String tname, final String tcatalog,
085: final String tschema, final String ttype) {
086: this .name = tname;
087: this .catalog = tcatalog;
088: this .schema = tschema;
089: this .type = ttype;
090:
091: this .indexList = Collections.EMPTY_LIST;
092: this .fkColumnList = Collections.EMPTY_LIST;
093: this .pkColumnList = Collections.EMPTY_LIST;
094: }
095:
096: /**
097: * Creates an instance of Table with the given attributes.
098: *
099: * @param tname Table name
100: * @param jname Table java name
101: * @param tcatalog Catalog name
102: * @param tschema Schema name
103: * @param ttype Table type
104: */
105: public Table(final String tname, final String jname,
106: final String tcatalog, final String tschema,
107: final String ttype) {
108: this .name = tname;
109: this .javaName = jname;
110: this .catalog = tcatalog;
111: this .schema = tschema;
112: this .type = ttype;
113:
114: this .indexList = Collections.EMPTY_LIST;
115: this .fkColumnList = Collections.EMPTY_LIST;
116: this .pkColumnList = Collections.EMPTY_LIST;
117: }
118:
119: /**
120: * Creates an instance of Table with the given attributes.
121: */
122: public Table(final Table nTable) {
123: this .name = nTable.getName();
124: this .javaName = nTable.getJavaName();
125: this .catalog = nTable.getCatalog();
126: this .schema = nTable.getSchema();
127: this .numColumns = nTable.getNumColumns();
128: this .numColumnsSelected = nTable.getNumColumnsSelected();
129: this .cloneColumns(nTable.getColumns());
130: this .type = nTable.getType();
131: this .cloneIndexList(nTable.getIndexList());
132: this
133: .cloneForeignKeyColumnList(nTable
134: .getForeignKeyColumnList());
135: this
136: .clonePrimaryKeyColumnList(nTable
137: .getPrimaryKeyColumnList());
138: this .selected = nTable.isSelected();
139: }
140:
141: /**
142: * Get the table name.
143: *
144: * @return Table name
145: */
146: public String getName() {
147: return this .name;
148: }
149:
150: /**
151: * Get the table java name.
152: *
153: * @return Table java name
154: */
155: public String getJavaName() {
156: return this .javaName;
157: }
158:
159: /**
160: * Get the catalog name.
161: *
162: * @return Catalog name
163: */
164: public String getCatalog() {
165: return this .catalog;
166: }
167:
168: /**
169: * Get the schema name.
170: *
171: * @return Schema name
172: */
173: public String getSchema() {
174: return this .schema;
175: }
176:
177: /**
178: * Get the number of table columns.
179: *
180: * @return Number of table columns.
181: */
182: public int getNumColumns() {
183: return this .numColumns;
184: }
185:
186: /**
187: * Get the number of columns selected.
188: *
189: * @return Number of columns selected.
190: */
191: public int getNumColumnsSelected() {
192: return this .numColumnsSelected;
193: }
194:
195: /**
196: * Get the list of table columns.
197: *
198: * @return List of table columns
199: */
200: public TableColumn[] getColumns() {
201: return this .columns;
202: }
203:
204: /**
205: * Get the table type.
206: *
207: * @return Table type
208: */
209: public String getType() {
210: return this .type;
211: }
212:
213: /**
214: * Set the table name.
215: *
216: * @param newName Table name
217: */
218: public void setName(final String newName) {
219: this .name = newName;
220: }
221:
222: /**
223: * Set the table java name.
224: *
225: * @param newName Table java name
226: */
227: public void setJavaName(final String newJavaName) {
228: this .javaName = newJavaName;
229: }
230:
231: /**
232: * Set the catalog name.
233: *
234: * @param newCatalog Catalog name
235: */
236: public void setCatalog(final String newCatalog) {
237: this .catalog = newCatalog;
238: }
239:
240: /**
241: * Set the schema name.
242: *
243: * @param newSchema Schema name
244: */
245: public void setSchema(final String newSchema) {
246: this .schema = newSchema;
247: }
248:
249: /**
250: * Set the table columns.
251: *
252: * @param newColumns Table columns
253: */
254: public void setColumns(final TableColumn[] newColumns) {
255: this .columns = newColumns;
256:
257: // update the number of columns and columns selected
258: if (this .columns != null) {
259: this .numColumns = this .columns.length;
260:
261: int count = 0;
262: for (int i = 0; i < this .columns.length; i++) {
263: if (this .columns[i].getIsSelected()) {
264: count++;
265: }
266: }
267: this .numColumnsSelected = count;
268: } else {
269: this .numColumns = 0;
270: this .numColumnsSelected = 0;
271: }
272: }
273:
274: /**
275: * Clone the table columns.
276: *
277: * @param newColumns Table columns
278: */
279: public void cloneColumns(final TableColumn[] newColumns) {
280: this .numColumns = 0;
281: this .numColumnsSelected = 0;
282:
283: int count = 0;
284: if (newColumns != null) {
285: this .numColumns = newColumns.length;
286: if (this .numColumns > 0) {
287: this .columns = new TableColumn[this .numColumns];
288: for (int i = 0; i < this .numColumns; i++) {
289: this .columns[i] = new TableColumn(newColumns[i]);
290: if (this .columns[i].getIsSelected()) {
291: count++;
292: }
293: }
294: }
295: this .numColumnsSelected = count;
296: }
297: }
298:
299: public void addColumn(final TableColumn col) {
300: if (null == col) {
301: return;
302: }
303:
304: int numCols = 0;
305: if (null != this .columns) {
306: numCols = this .columns.length;
307: }
308: final TableColumn[] newTable = new TableColumn[numCols + 1];
309: for (int i = 0; i < numCols; i++) {
310: newTable[i] = this .columns[i];
311: }
312: newTable[numCols] = col;
313: this .setColumns(newTable);
314: }
315:
316: public void removeColumn(final int index) {
317: if (null == this .columns || index > this .columns.length) {
318: return;
319: }
320:
321: final int numCols = this .columns.length;
322:
323: final TableColumn[] newTable = new TableColumn[numCols - 1];
324: for (int i = 0, j = 0; i < numCols; i++, j++) {
325: if (i == index) {
326: j--;
327: } else {
328: newTable[j] = this .columns[i];
329: }
330: }
331: this .setColumns(newTable);
332: }
333:
334: /**
335: * Set the table type.
336: *
337: * @param newType Table type
338: */
339: public void setType(final String newType) {
340: this .type = newType;
341: }
342:
343: /**
344: * Get the index list.
345: *
346: * @return Index list
347: */
348: public List getIndexList() {
349: return this .indexList;
350: }
351:
352: /**
353: * Set the index list.
354: *
355: * @param newList Index list
356: */
357: public void setIndexList(final List newList) {
358: if (newList != null && newList.size() != 0) {
359: try {
360: // Test to ensure that List contains nothing but Index objects.
361: final IndexColumn[] dummy = (IndexColumn[]) newList
362: .toArray(new IndexColumn[newList.size()]);
363: } catch (final ArrayStoreException e) {
364: throw new IllegalArgumentException(
365: "newList does not contain Index objects!");
366: }
367:
368: this .indexList = newList;
369: }
370: }
371:
372: public void cloneIndexList(final List newList) {
373: this .indexList = Collections.EMPTY_LIST;
374:
375: if (newList != null && newList.size() != 0) {
376: this .indexList = new ArrayList();
377:
378: try {
379: // Test to ensure that List contains nothing but Index objects.
380: final IndexColumn[] dummy = (IndexColumn[]) newList
381: .toArray(new IndexColumn[newList.size()]);
382: for (int i = 0; i < newList.size(); i++) {
383: final IndexColumn iCol = (IndexColumn) newList
384: .get(i);
385: this .indexList.add(new IndexColumn(iCol));
386: }
387: } catch (final ArrayStoreException e) {
388: throw new IllegalArgumentException(
389: "newList does not contain Index objects!");
390: }
391: }
392: }
393:
394: // added by Neena
395: // to set the selection state of the table
396:
397: public void setSelected(final boolean selected) {
398: this .selected = selected;
399: }
400:
401: // added by Neena
402: // to get the selection state of the object
403:
404: public boolean isSelected() {
405: return this .selected;
406: }
407:
408: /**
409: * Gets current List of KeyColumn objects, representing primary key columns in this table.
410: *
411: * @return List (possibly empty) of KeyColumn instances
412: */
413: public List getPrimaryKeyColumnList() {
414: return this .pkColumnList;
415: }
416:
417: /**
418: * Sets List of primary key column objects to the given List.
419: *
420: * @param newList List containing new collection of KeyColumn objects representing primary key
421: * columns within this table
422: */
423: public void setPrimaryKeyColumnList(final List newList) {
424: if (newList != null && newList.size() != 0) {
425: try {
426: // Test to ensure that List contains nothing but Index objects.
427: final KeyColumn[] dummy = (KeyColumn[]) newList
428: .toArray(new KeyColumn[newList.size()]);
429: } catch (final ArrayStoreException e) {
430: final ResourceBundle cMessages = NbBundle
431: .getBundle(Table.class);
432: throw new IllegalArgumentException(cMessages
433: .getString("ERROR_KEY")
434: + "(ERROR_KEY)");// NO
435: // i18n
436: }
437:
438: this .pkColumnList = newList;
439: }
440: }
441:
442: public void clonePrimaryKeyColumnList(final List newList) {
443: this .pkColumnList = Collections.EMPTY_LIST;
444:
445: if (newList != null && newList.size() != 0) {
446: this .pkColumnList = new ArrayList();
447:
448: try {
449: // Test to ensure that List contains nothing but Index objects.
450: final KeyColumn[] dummy = (KeyColumn[]) newList
451: .toArray(new KeyColumn[newList.size()]);
452: for (int i = 0; i < newList.size(); i++) {
453: final KeyColumn tPK = (KeyColumn) newList.get(i);
454: this .pkColumnList.add(new KeyColumn(tPK.getName(),
455: tPK.getColumnName(), tPK
456: .getColumnSequence()));
457: }
458: } catch (final ArrayStoreException e) {
459: final ResourceBundle cMessages = NbBundle
460: .getBundle(Table.class);
461: throw new IllegalArgumentException(cMessages
462: .getString("ERROR_KEY")
463: + "(ERROR_KEY)");// NO
464: // i18n
465: }
466: }
467: }
468:
469: /**
470: * Gets current List of ForeignKeyColumn objects, representing foreign key columns in this
471: * table.
472: *
473: * @return List (possibly empty) of ForeignKeyColumn instances
474: */
475: public List getForeignKeyColumnList() {
476: return this .fkColumnList;
477: }
478:
479: /**
480: * Sets List of foreign key column objects to the given List.
481: *
482: * @param newList List containing new collection of ForeignKeyColumn objects representing
483: * foreign key columns within this table
484: */
485: public void setForeignKeyColumnList(final List newList) {
486: if (newList != null && newList.size() != 0) {
487: try {
488: // Test to ensure that List contains nothing but Index objects.
489: final ForeignKeyColumn[] dummy = (ForeignKeyColumn[]) newList
490: .toArray(new ForeignKeyColumn[newList.size()]);
491: } catch (final ArrayStoreException e) {
492: final ResourceBundle cMessages = NbBundle
493: .getBundle(Table.class);
494: throw new IllegalArgumentException(cMessages
495: .getString("ERROR_FK_KEY")
496: + "(ERROR_FK_KEY)");// NO
497: // i18n
498: }
499:
500: this .fkColumnList = newList;
501: }
502: }
503:
504: public void cloneForeignKeyColumnList(final List newList) {
505: this .fkColumnList = Collections.EMPTY_LIST;
506:
507: if (newList != null && newList.size() != 0) {
508: this .fkColumnList = new ArrayList();
509:
510: try {
511: // Test to ensure that List contains nothing but Index objects.
512: final ForeignKeyColumn[] dummy = (ForeignKeyColumn[]) newList
513: .toArray(new ForeignKeyColumn[newList.size()]);
514: for (int i = 0; i < newList.size(); i++) {
515: final ForeignKeyColumn fkCol = (ForeignKeyColumn) newList
516: .get(i);
517: this .fkColumnList.add(new ForeignKeyColumn(fkCol));
518: }
519: } catch (final ArrayStoreException e) {
520: final ResourceBundle cMessages = NbBundle
521: .getBundle(Table.class);
522: throw new IllegalArgumentException(cMessages
523: .getString("ERROR_FK_KEY")
524: + "(ERROR_FK_KEY)");// NO
525: // i18n
526: }
527: }
528: }
529: }
|