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: package org.netbeans.modules.jdbcwizard.builder.dbmodel.impl;
038:
039: import java.util.logging.Logger;
040:
041: import org.netbeans.modules.jdbcwizard.builder.DBMetaData;
042: import org.netbeans.modules.jdbcwizard.builder.dbmodel.DBColumn;
043: import org.netbeans.modules.jdbcwizard.builder.dbmodel.DBTable;
044:
045: /**
046: * Interface describing column metadata for JDBC data sources providing information in a database or
047: * database-like format.
048: *
049: * @author
050: */
051: public class DBColumnImpl implements DBColumn, Cloneable, Comparable {
052:
053: /** String constant for unknown SQL type */
054: public static final String UNKNOWN_TYPE = "unknown";
055:
056: /** Constant for this jdbc type not being assigned. */
057: public static final int NOT_ASSIGNED = -562471;
058:
059: /* Log4J category name */
060: private static final String LOG_CATEGORY = DBColumnImpl.class
061: .getName();
062:
063: public void setName(final String name) {
064: this .name = name;
065: }
066:
067: /** name of column */
068: protected String name;
069:
070: /** whether this column is part of a primary key */
071: protected boolean pkFlag;
072:
073: /** whether this column is part of a foreign key */
074: protected boolean fkFlag;
075:
076: /** whether this column is indexed */
077: protected boolean indexed;
078:
079: /** whether this column can accept null as a valid value */
080: protected boolean nullable;
081:
082: /** editable */
083: protected boolean editable = true;
084:
085: /** insert editable */
086: protected boolean insertEditable = true;
087:
088: /** selected */
089: protected boolean insertSelected = true;
090:
091: protected boolean updateSelected = true;
092:
093: protected boolean chooseSelected = true;
094:
095: protected boolean pollSelected = true;
096:
097: /** JDBC SQL type, as enumerated in java.sql.Types */
098: protected int jdbcType;
099:
100: /** column precision (for numeric types) / width (for char types) */
101: protected int precision;
102:
103: /** column scale (meaningful only for numeric types) */
104: protected int scale;
105:
106: /* DBTable to which this PK belongs */
107: protected DBTable parent;
108:
109: /* Java name of this column */
110: protected String javaName;
111:
112: /* Java type for this column */
113: protected String javaType;
114:
115: /* Default Value */
116: protected String defaultValue;
117:
118: /* Cardinal Position */
119: protected int cardinalPosition;
120:
121: /**
122: * @param sqlType
123: */
124: public void setSqlType(final String sqlType) {
125: this .sqlType = sqlType;
126: }
127:
128: /**
129: * @return
130: */
131: public String getSqlType() {
132: return this .sqlType;
133: }
134:
135: protected String sqlType;
136:
137: private static final Logger mLogger = Logger
138: .getLogger(DBColumnImpl.LOG_CATEGORY);
139:
140: /**
141: * @param colName
142: * @param sqlJdbcType
143: * @param colScale
144: * @param colPrecision
145: * @param isNullable
146: */
147: public DBColumnImpl(final String colName, final int sqlJdbcType,
148: final int colScale, final int colPrecision,
149: final boolean isNullable) {
150: this .name = colName;
151: this .jdbcType = sqlJdbcType;
152: this .precision = colPrecision;
153: this .scale = colScale;
154: this .nullable = isNullable;
155: }
156:
157: /**
158: * @param colName
159: * @param sqlJdbcType
160: * @param colScale
161: * @param colPrecision
162: * @param isPrimaryKey
163: * @param isForeignKey
164: * @param isIndexed
165: * @param isNullable
166: */
167: public DBColumnImpl(final String colName, final int sqlJdbcType,
168: final int colScale, final int colPrecision,
169: final boolean isPrimaryKey, final boolean isForeignKey,
170: final boolean isIndexed, final boolean isNullable) {
171: this (colName, sqlJdbcType, colScale, colPrecision, isNullable);
172:
173: this .pkFlag = isPrimaryKey;
174: this .fkFlag = isForeignKey;
175: this .indexed = isIndexed;
176: }
177:
178: /**
179: * Creates a new instance of DBColumnImpl, cloning the contents of the given DBColumn
180: * implementation instance.
181: *
182: * @param src DBColumn instance to be cloned
183: */
184: public DBColumnImpl() {
185: }
186:
187: /**
188: * @see com.stc.model.database.DBColumn#getName
189: */
190: public String getName() {
191: return this .name;
192: }
193:
194: /**
195: * @see com.stc.model.database.DBColumn#isPrimaryKey
196: */
197: public boolean isPrimaryKey() {
198: return this .pkFlag;
199: }
200:
201: /**
202: * @see com.stc.model.database.DBColumn#isForeignKey
203: */
204: public boolean isForeignKey() {
205: return this .fkFlag;
206: }
207:
208: /**
209: * @see com.stc.model.database.DBColumn#isIndexed
210: */
211: public boolean isIndexed() {
212: return this .indexed;
213: }
214:
215: /**
216: * @see com.stc.model.database.DBColumn#isNullable
217: */
218: public boolean isNullable() {
219: return this .nullable;
220: }
221:
222: /**
223: * @see com.stc.model.database.DBColumn#getParent
224: */
225: public DBTable getParent() {
226: return this .parent;
227: }
228:
229: /**
230: * @see com.stc.model.database.DBColumn#getJdbcType
231: */
232: public int getJdbcType() {
233: return this .jdbcType;
234: }
235:
236: /**
237: * @see com.stc.model.database.DBColumn#getJdbcTypeString
238: */
239: public String getJdbcTypeString() {
240: return DBMetaData.getSQLTypeDescription(this .jdbcType);
241: }
242:
243: /**
244: * @see com.stc.model.database.DBColumn#getScale
245: */
246: public int getScale() {
247: return this .scale;
248: }
249:
250: /**
251: * @see com.stc.model.database.DBColumn#getPrecision
252: */
253: public int getPrecision() {
254: return this .precision;
255: }
256:
257: /**
258: * Indicates whether this DBColumn references the given DBColumn in a FK -> PK relationship.
259: *
260: * @param column PK whose relationship to this column is to be checked
261: * @return true if this column is a FK reference to column; false otherwise
262: */
263: // public boolean references(DBColumn column);
264: /**
265: * Indicates whether this DBColumn is referenced by the given DBColumn in a FK -> PK
266: * relationship.
267: *
268: * @param column potential FK reference to be checked
269: * @return true if column is referenced as a PK by the given column, false otherwise
270: */
271: // public boolean isReferencedBy(DBColumn column);
272: /*
273: * Setters and non-API helper methods.
274: */
275: public void setPrecision(final int newPrec) {
276: this .precision = newPrec;
277: }
278:
279: public void setScale(final int newScale) {
280: this .scale = newScale;
281: }
282:
283: public void setJdbcType(final int newType) {
284: this .jdbcType = newType;
285: }
286:
287: public void setPrimaryKey(final boolean newFlag) {
288: this .pkFlag = newFlag;
289: }
290:
291: public void setForeignKey(final boolean newFlag) {
292: this .fkFlag = newFlag;
293: }
294:
295: public void setIndexed(final boolean newFlag) {
296: this .indexed = newFlag;
297: }
298:
299: public void setNullable(final boolean newFlag) {
300: this .nullable = newFlag;
301: }
302:
303: public void setParent(final DBTable newParent) {
304: this .parent = newParent;
305: }
306:
307: /**
308: * Clone a deep copy of DBColumn.
309: *
310: * @return a copy of DBColumn.
311: */
312: public Object clone() {
313: try {
314: final DBColumnImpl column = (DBColumnImpl) super .clone();
315:
316: return column;
317: } catch (final CloneNotSupportedException e) {
318: throw new InternalError(e.toString());
319: }
320: }
321:
322: /**
323: * Overrides default implementation to return value based on memberwise comparison.
324: *
325: * @param refObj Object against which we compare this instance
326: * @return true if refObj is functionally identical to this instance; false otherwise
327: */
328: public boolean equals(final Object refObj) {
329: if (this == refObj) {
330: return true;
331: }
332:
333: if (!(refObj instanceof DBColumn)) {
334: return false;
335: }
336:
337: final DBColumnImpl refMeta = (DBColumnImpl) refObj;
338:
339: boolean result = this .name != null ? this .name
340: .equals(refMeta.name) : refMeta.name == null;
341:
342: result &= this .jdbcType == refMeta.jdbcType
343: && this .pkFlag == refMeta.pkFlag
344: && this .fkFlag == refMeta.fkFlag
345: && this .indexed == refMeta.indexed
346: && this .nullable == refMeta.nullable
347: && this .scale == refMeta.scale
348: && this .precision == refMeta.precision;
349:
350: result &= this .parent != null ? this .parent
351: .equals(refMeta.parent) : refMeta.parent == null;
352:
353: return result;
354: }
355:
356: /**
357: * Returns the hashCode for this object.
358: *
359: * @return the hashCode of this object.
360: */
361: public int hashCode() {
362: int myHash = this .name != null ? this .name.hashCode() : 0;
363: myHash += this .jdbcType + 10 * this .scale + 100
364: * this .precision;
365:
366: myHash += this .pkFlag ? 1 : 0;
367: myHash += this .fkFlag ? 2 : 0;
368: myHash += this .indexed ? 4 : 0;
369: myHash += this .nullable ? 8 : 0;
370:
371: myHash += this .parent != null ? this .parent.hashCode() : 0;
372:
373: return myHash;
374: }
375:
376: /**
377: * Compares DBColumn with another object for lexicographical ordering. Null objects and those
378: * DBColumns with null names are placed at the end of any ordered collection using this method.
379: *
380: * @param refObj Object to be compared.
381: * @return -1 if the column name is less than obj to be compared. 0 if the column name is the
382: * same. 1 if the column name is greater than obj to be compared.
383: */
384: public int compareTo(final Object refObj) {
385: if (refObj == null) {
386: return -1;
387: }
388:
389: if (refObj == this ) {
390: return 0;
391: }
392:
393: final String refName = ((DBColumn) refObj).getName();
394: return this .name != null ? this .name.compareTo(refName)
395: : refName != null ? 1 : -1;
396: }
397:
398: /*
399: * Sets the various member variables and collections using the given DBColumn instance as a
400: * source object. @param source DBColumn from which to obtain values for member variables and
401: * collections
402: */
403: public void copyFrom(final DBColumn source) {
404: this .name = source.getName();
405: this .jdbcType = source.getJdbcType();
406:
407: this .scale = source.getScale();
408: this .precision = source.getPrecision();
409:
410: this .pkFlag = source.isPrimaryKey();
411: this .fkFlag = source.isForeignKey();
412: this .indexed = source.isIndexed();
413: this .nullable = source.isNullable();
414:
415: this .parent = source.getParent();
416: }
417:
418: /**
419: * Gets Java name for this table.
420: *
421: * @return normalized Java name for this table
422: */
423: public String getJavaName() {
424: return this .javaName != null ? this .javaName : this .name;
425: }
426:
427: /**
428: * Sets Java name for this table.
429: *
430: * @param newName new normalized Java name for this table, or null if original name is to be
431: * used.
432: */
433: public void setJavaName(final String newName) {
434: this .javaName = newName;
435: }
436:
437: /**
438: * Gets Java type for this table.
439: *
440: * @return normalized Java type for this table
441: */
442: public String getJavaType() {
443: return this .javaType;
444: }
445:
446: /**
447: * Sets Java type for this table.
448: *
449: * @param newType new normalized Java name for this table
450: */
451: public void setJavaType(final String newType) {
452: this .javaType = newType;
453: }
454:
455: /**
456: * Gets the default value
457: *
458: * @return defaultValue
459: */
460: public String getDefaultValue() {
461: return this .defaultValue;
462: }
463:
464: /**
465: * Gets the Ordinal Position
466: *
467: * @param cardinalPosition to be used
468: */
469: public int getOrdinalPosition() {
470: return this .cardinalPosition;
471: }
472:
473: /**
474: * sets the default value
475: *
476: * @param defaultValue to be set
477: */
478: public void setDefaultValue(final String defValue) {
479: this .defaultValue = defValue;
480: }
481:
482: /**
483: * Gets the Ordinal Position
484: *
485: * @param cardinalPosition to be used
486: */
487: public void setOrdinalPosition(final int cardinalPos) {
488: this .cardinalPosition = cardinalPos;
489: }
490:
491: /**
492: * Gets the status of selection of the column
493: *
494: * @return seleted
495: */
496: public boolean isInsertSelected() {
497: return this .insertSelected;
498: }
499:
500: /**
501: * Gets the status of selection of the column
502: *
503: * @return seleted
504: */
505: public boolean isUpdateSelected() {
506: return this .updateSelected;
507: }
508:
509: /**
510: * Gets the status of selection of the column
511: *
512: * @return seleted
513: */
514: public boolean isChooseSelected() {
515: return this .chooseSelected;
516: }
517:
518: /**
519: * Gets the status of selection of the column
520: *
521: * @return seleted
522: */
523: public boolean isPollSelected() {
524: return this .pollSelected;
525: }
526:
527: public boolean isSelected() {
528: return this .insertSelected && this .updateSelected
529: && this .chooseSelected && this .pollSelected;
530: }
531:
532: /**
533: * Gets the status of editing
534: *
535: * @return editable
536: */
537: public boolean isEditable() {
538: return this .editable;
539: }
540:
541: /**
542: * Gets the status of Insert editing
543: *
544: * @return InsertEditable
545: */
546: public boolean isInsertEditable() {
547: return this .insertEditable;
548: }
549:
550: /**
551: * Sets status of editing the column table
552: *
553: * @param the editing status is from now that of cedit
554: */
555:
556: public void setEditable(final boolean cedit) {
557: this .editable = cedit;
558: }
559:
560: /**
561: * Sets status of editing the column table at Insert tab
562: *
563: * @param the editing status is from now that of cedit
564: */
565:
566: public void setInsertEditable(final boolean cedit) {
567: this .insertEditable = cedit;
568: }
569:
570: /**
571: * Sets status of selection of the column table
572: *
573: * @param the selection status is set from now on to that of cselect
574: */
575:
576: public void setInsertSelected(final boolean cselect) {
577: this .insertSelected = cselect;
578: }
579:
580: /**
581: * Sets status of selection of the column table
582: *
583: * @param the selection status is set from now on to that of cselect
584: */
585:
586: public void setUpdateSelected(final boolean cselect) {
587: this .updateSelected = cselect;
588: }
589:
590: /**
591: * Sets status of selection of the column table
592: *
593: * @param the selection status is set from now on to that of cselect
594: */
595:
596: public void setChooseSelected(final boolean cselect) {
597: this .chooseSelected = cselect;
598: }
599:
600: /**
601: * Sets status of selection of the column table
602: *
603: * @param the selection status is set from now on to that of cselect
604: */
605:
606: public void setPollSelected(final boolean cselect) {
607: this .pollSelected = cselect;
608: }
609:
610: public void setSelected(final boolean cselect) {
611: this.setInsertSelected(cselect);
612: this.setUpdateSelected(cselect);
613: this.setChooseSelected(cselect);
614: this.setPollSelected(cselect);
615: }
616:
617: }
|