0001: /*
0002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
0003: *
0004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
0005: *
0006: * The contents of this file are subject to the terms of either the GNU
0007: * General Public License Version 2 only ("GPL") or the Common
0008: * Development and Distribution License("CDDL") (collectively, the
0009: * "License"). You may not use this file except in compliance with the
0010: * License. You can obtain a copy of the License at
0011: * http://www.netbeans.org/cddl-gplv2.html
0012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
0013: * specific language governing permissions and limitations under the
0014: * License. When distributing the software, include this License Header
0015: * Notice in each file and include the License file at
0016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
0017: * particular file as subject to the "Classpath" exception as provided
0018: * by Sun in the GPL Version 2 section of the License file that
0019: * accompanied this code. If applicable, add the following below the
0020: * License Header, with the fields enclosed by brackets [] replaced by
0021: * your own identifying information:
0022: * "Portions Copyrighted [year] [name of copyright owner]"
0023: *
0024: * Contributor(s):
0025: *
0026: * The Original Software is NetBeans. The Initial Developer of the Original
0027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
0028: * Microsystems, Inc. All Rights Reserved.
0029: *
0030: * If you wish your version of this file to be governed by only the CDDL
0031: * or only the GPL Version 2, indicate your decision by adding
0032: * "[Contributor] elects to include this software in this distribution
0033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
0034: * single choice of license, a recipient has the option to distribute
0035: * your version of this file under either the CDDL, the GPL Version 2 or
0036: * to extend the choice of license to its licensees as provided above.
0037: * However, if you add GPL Version 2 code and therefore, elected the GPL
0038: * Version 2 license, then the option applies only if the new code is
0039: * made subject to such option by the copyright holder.
0040: */
0041: package org.netbeans.modules.sql.framework.model.impl;
0042:
0043: import java.util.ArrayList;
0044: import java.util.Collection;
0045: import java.util.Collections;
0046: import java.util.Comparator;
0047: import java.util.HashMap;
0048: import java.util.HashSet;
0049: import java.util.Iterator;
0050: import java.util.List;
0051: import java.util.Map;
0052: import java.util.Set;
0053:
0054: import org.netbeans.modules.sql.framework.model.DBColumn;
0055: import org.netbeans.modules.sql.framework.common.utils.NativeColumnOrderComparator;
0056: import org.netbeans.modules.sql.framework.model.GUIInfo;
0057: import org.netbeans.modules.sql.framework.model.SQLDBColumn;
0058: import org.netbeans.modules.sql.framework.model.SQLDBModel;
0059: import org.netbeans.modules.sql.framework.model.SQLDBTable;
0060: import org.netbeans.modules.sql.framework.model.SQLObject;
0061: import org.w3c.dom.Element;
0062: import org.w3c.dom.NodeList;
0063:
0064: import com.sun.sql.framework.exception.BaseException;
0065: import java.util.LinkedHashMap;
0066: import org.netbeans.modules.sql.framework.model.DBTable;
0067: import org.netbeans.modules.sql.framework.model.DatabaseModel;
0068: import org.netbeans.modules.sql.framework.model.ForeignKey;
0069: import org.netbeans.modules.sql.framework.model.Index;
0070: import org.netbeans.modules.sql.framework.model.PrimaryKey;
0071:
0072: /**
0073: * Abstract implementation for org.netbeans.modules.model.database.DBTable and SQLObject interfaces.
0074: *
0075: * @author Sudhendra Seshachala, Jonathan Giron
0076: * @version $Revision$
0077: */
0078: public abstract class AbstractDBTable extends AbstractSQLObject
0079: implements SQLDBTable {
0080:
0081: static class StringComparator implements Comparator {
0082: public int compare(Object o1, Object o2) {
0083: if (o1 instanceof String && o2 instanceof String) {
0084: return ((String) o1).compareTo((String) o2);
0085: }
0086: throw new ClassCastException(
0087: "StringComparator cannot compare non-String objects.");
0088: }
0089: }
0090:
0091: /** Attribute name for commit batch size. */
0092: protected static final String ATTR_COMMIT_BATCH_SIZE = "commitBatchSize";
0093:
0094: /** String constant for table catalog name attribute. */
0095: protected static final String CATALOG_NAME_ATTR = "catalog"; // NOI18N
0096:
0097: /** String constants for dbTableRef tag. */
0098: protected static final String DB_TABLE_REF = "dbTableRef"; // NOI18N
0099:
0100: /** String constant for table name attribute. */
0101: protected static final String DISPLAY_NAME_ATTR = "displayName"; // NOI18N
0102:
0103: /** String constant for table ID attribute. */
0104: protected static final String ID_ATTR = "id"; // NOI18N
0105:
0106: /** String to use in prefixing each line of a generated XML document */
0107: protected static final String INDENT = "\t";
0108:
0109: /** Initial buffer size for StringBuilder used in marshalling SQLTable to XML */
0110: protected static final int INIT_XMLBUF_SIZE = 500;
0111:
0112: /** Constant for column model name tag. */
0113: protected static final String MODEL_NAME_TAG = "dbModelName"; // NOI18N
0114:
0115: /** String onstant for table schema attribute. */
0116: protected static final String SCHEMA_NAME_ATTR = "schema"; // NOI18N
0117:
0118: /** String constant for table name attribute. */
0119: protected static final String TABLE_NAME_ATTR = "name"; // NOI18N
0120:
0121: private static final String ATTR_ALIAS_NAME = "aliasName";
0122:
0123: private static final String ATTR_FLATFILE_LOCATION_RUNTIME_INPUT_NAME = "flatFileLocationRuntimeInputName";
0124:
0125: private static final String ATTR_TABLE_PREFIX = "tablePrefix";
0126:
0127: private static final String ATTR_USERDEFINED_CATALOG_NAME = "userDefinedCatalogName";
0128:
0129: private static final String ATTR_USERDEFINED_SCHEMA_NAME = "userDefinedSchemaName";
0130:
0131: private static final String ATTR_USERDEFINED_TABLE_NAME = "userDefinedTableName";
0132:
0133: private static final String ATTR_USING_FULLYQUALIFIED_NAME = "usingFullyQualifiedName";
0134:
0135: private static final int DEFAULT_COMMIT_BATCH_SIZE = 5000;
0136:
0137: private static final String FQ_TBL_NAME_SEPARATOR = ".";
0138:
0139: // RFE-102428
0140: private static final String ATTR_STAGING_TABLE_NAME = "stagingTableName";
0141:
0142: /** use alias is required : transient variable */
0143: protected boolean aliasUsed = false;
0144:
0145: /** catalog to which this table belongs. */
0146: protected String catalog;
0147:
0148: /** Map of column metadata. */
0149: protected Map<String, DBColumn> columns;
0150:
0151: /** User-defined description. */
0152: protected String description;
0153: /** editable */
0154: protected boolean editable = true;
0155:
0156: /** Map of names to ForeignKey instances for this table; may be empty. */
0157: protected Map<String, ForeignKey> foreignKeys;
0158:
0159: /** Contains UI state information */
0160: protected GUIInfo guiInfo;
0161:
0162: /** Map of names to Index instances for this table; may be empty. */
0163: protected Map<String, Index> indexes;
0164:
0165: /** Table name as supplied by data source. */
0166: protected String name;
0167:
0168: protected boolean overrideCatalogName = false;
0169:
0170: protected String overridenCatalogName = null;
0171:
0172: protected String overridenSchemaName = null;
0173: protected boolean overrideSchemaName = false;
0174:
0175: /** Model instance that "owns" this table */
0176: protected DatabaseModel parentDBModel;
0177: /** PrimaryKey for this table; may be null. */
0178: protected PrimaryKeyImpl primaryKey;
0179: /** schema to which this table belongs. */
0180: protected String schema;
0181: /** selected */
0182: protected boolean selected;
0183:
0184: /** No-arg constructor; initializes Collections-related member variables. */
0185: protected AbstractDBTable() {
0186: columns = new LinkedHashMap<String, DBColumn>();
0187: foreignKeys = new HashMap<String, ForeignKey>();
0188: indexes = new HashMap<String, Index>();
0189: guiInfo = new GUIInfo();
0190: setDefaultAttributes();
0191: }
0192:
0193: /**
0194: * Creates a new instance of AbstractDBTable, cloning the contents of the given
0195: * DBTable implementation instance.
0196: *
0197: * @param src DBTable instance to be 43d
0198: */
0199: protected AbstractDBTable(DBTable src) {
0200: this ();
0201:
0202: if (src == null) {
0203: throw new IllegalArgumentException(
0204: "Must supply non-null DBTable instance for src param.");
0205: }
0206: copyFrom(src);
0207: }
0208:
0209: /**
0210: * Creates a new instance of AbstractDBTable with the given name.
0211: *
0212: * @param aName name of new DBTable instance
0213: * @param aSchema schema of new DBTable instance; may be null
0214: * @param aCatalog catalog of new DBTable instance; may be null
0215: */
0216: protected AbstractDBTable(String aName, String aSchema,
0217: String aCatalog) {
0218: this ();
0219:
0220: name = (aName != null) ? aName.trim() : null;
0221: schema = (aSchema != null) ? aSchema.trim() : null;
0222: catalog = (aCatalog != null) ? aCatalog.trim() : null;
0223: }
0224:
0225: /**
0226: * Adds an AbstractDBColumn instance to this table.
0227: *
0228: * @param theColumn column to be added.
0229: * @return true if successful. false if failed.
0230: */
0231: public boolean addColumn(SQLDBColumn theColumn) {
0232: if (theColumn != null) {
0233: theColumn.setParent(this );
0234: columns.put(theColumn.getName(), theColumn);
0235: return true;
0236: }
0237:
0238: return false;
0239: }
0240:
0241: /**
0242: * Adds the given ForeignKeyImpl, associating it with this AbstractDBTable instance.
0243: *
0244: * @param newFk new ForeignKeyImpl instance to be added
0245: * @return return true if addition succeeded, false otherwise
0246: */
0247: public boolean addForeignKey(ForeignKeyImpl newFk) {
0248: if (newFk != null) {
0249: newFk.setParent(this );
0250: foreignKeys.put(newFk.getName(), newFk);
0251: return true;
0252: }
0253: return false;
0254: }
0255:
0256: /**
0257: * Adds the given IndexImpl, associating it with this AbstractDBTable instance.
0258: *
0259: * @param newIndex new IndexImpl instance to be added
0260: * @return return true if addition succeeded, false otherwise
0261: */
0262: public boolean addIndex(IndexImpl newIndex) {
0263: if (newIndex != null) {
0264: newIndex.setParent(this );
0265: indexes.put(newIndex.getName(), newIndex);
0266:
0267: return true;
0268: }
0269: return false;
0270: }
0271:
0272: /**
0273: * Clears list of foreign keys.
0274: */
0275: public void clearForeignKeys() {
0276: foreignKeys.clear();
0277: }
0278:
0279: /**
0280: * Clears list of indexes.
0281: */
0282: public void clearIndexes() {
0283: indexes.clear();
0284: }
0285:
0286: public void clearOverride(boolean clearCatalogOverride,
0287: boolean clearSchemaOverride) {
0288: if (clearCatalogOverride) {
0289: this .overrideCatalogName = false;
0290: this .overridenCatalogName = null;
0291: }
0292:
0293: if (clearSchemaOverride) {
0294: this .overrideSchemaName = false;
0295: this .overridenSchemaName = null;
0296: }
0297: }
0298:
0299: /**
0300: * Compares DBTable with another object for lexicographical ordering. Null objects and
0301: * those DBTables with null names are placed at the end of any ordered collection
0302: * using this method.
0303: *
0304: * @param refObj Object to be compared.
0305: * @return -1 if the column name is less than obj to be compared. 0 if the column name
0306: * is the same. 1 if the column name is greater than obj to be compared.
0307: */
0308: public int compareTo(Object refObj) {
0309: if (refObj == null) {
0310: return -1;
0311: }
0312:
0313: if (refObj == this ) {
0314: return 0;
0315: }
0316:
0317: String refName = (parentDBModel != null) ? parentDBModel
0318: .getFullyQualifiedTableName((DBTable) refObj)
0319: : ((DBTable) refObj).getName();
0320:
0321: String myName = (parentDBModel != null) ? parentDBModel
0322: .getFullyQualifiedTableName(this ) : name;
0323:
0324: return (myName != null) ? myName.compareTo(refName)
0325: : (refName != null) ? 1 : -1;
0326: }
0327:
0328: /**
0329: * Sets the various member variables and collections using the given DBTable instance
0330: * as a source object. Concrete implementations should override this method, call
0331: * super.copyFrom(DBColumn) to pick up member variables defined in this class and then
0332: * implement its own logic for copying member variables defined within itself.
0333: *
0334: * @param source DBTable from which to obtain values for member variables and
0335: * collections
0336: */
0337: public void copyFrom(DBTable source) {
0338: if (source == null) {
0339: throw new IllegalArgumentException(
0340: "Must supply non-null ref for source");
0341: } else if (source == this ) {
0342: return;
0343: }
0344:
0345: name = source.getName();
0346: description = source.getDescription();
0347: schema = source.getSchema();
0348: catalog = source.getCatalog();
0349:
0350: parentDBModel = source.getParent();
0351:
0352: if (source instanceof SQLDBTable) {
0353: SQLDBTable abstractTbl = (SQLDBTable) source;
0354: super .copyFromSource(abstractTbl);
0355: displayName = abstractTbl.getDisplayName();
0356: guiInfo = abstractTbl.getGUIInfo();
0357: aliasUsed = abstractTbl.isAliasUsed();
0358: }
0359:
0360: deepCopyReferences(source);
0361: }
0362:
0363: /**
0364: * Deletes all columns associated with this table.
0365: *
0366: * @return true if all columns were deleted successfully, false otherwise.
0367: */
0368: public boolean deleteAllColumns() {
0369: columns.clear();
0370: return false;
0371: }
0372:
0373: /**
0374: * Deletes DBColumn, if any, associated with the given name from this table.
0375: *
0376: * @param columnName column name to be removed.
0377: * @return true if successful. false if failed.
0378: */
0379: public boolean deleteColumn(String columnName) {
0380: if (columnName != null && columnName.trim().length() != 0) {
0381: return (columns.remove(columnName) != null);
0382: }
0383: return false;
0384: }
0385:
0386: /**
0387: * Overrides default implementation to return value based on memberwise comparison.
0388: *
0389: * @param obj Object against which we compare this instance
0390: * @return true if obj is functionally identical to this SQLTable instance; false
0391: * otherwise
0392: */
0393: @Override
0394: public boolean equals(Object obj) {
0395: boolean result = false;
0396:
0397: // Check for reflexivity first.
0398: if (this == obj) {
0399: return true;
0400: }
0401: if (!(obj instanceof SQLDBTable)) {
0402: return false;
0403: }
0404:
0405: result = super .equals(obj);
0406:
0407: if (!result) {
0408: return result;
0409: }
0410:
0411: SQLDBTable target = (SQLDBTable) obj;
0412:
0413: // since now we allow duplicate source tables we need to check the id and if id
0414: // is not equal then table is not equal
0415: result &= target.getId() != null ? target.getId().equals(
0416: this .getId()) : this .getId() == null;
0417:
0418: // Check for castability (also deals with null obj)
0419: if (obj instanceof DBTable) {
0420: DBTable aTable = (DBTable) obj;
0421: String aTableName = aTable.getName();
0422: DatabaseModel aTableParent = aTable.getParent();
0423: Map<String, DBColumn> aTableColumns = aTable.getColumns();
0424: PrimaryKey aTablePK = aTable.getPrimaryKey();
0425: List<ForeignKey> aTableFKs = aTable.getForeignKeys();
0426: List<Index> aTableIdxs = aTable.getIndexes();
0427:
0428: result &= (aTableName != null && name != null && name
0429: .equals(aTableName))
0430: && (parentDBModel != null && aTableParent != null && parentDBModel
0431: .equals(aTableParent));
0432:
0433: if (columns != null && aTableColumns != null) {
0434: Set<String> objCols = aTableColumns.keySet();
0435: Set<String> myCols = columns.keySet();
0436:
0437: // Must be identical (no subsetting), hence the pair of tests.
0438: result &= myCols.containsAll(objCols)
0439: && objCols.containsAll(myCols);
0440: } else if (!(columns == null && aTableColumns == null)) {
0441: result = false;
0442: }
0443:
0444: result &= (primaryKey != null) ? primaryKey
0445: .equals(aTablePK) : aTablePK == null;
0446:
0447: if (foreignKeys != null && aTableFKs != null) {
0448: Collection<ForeignKey> myFKs = foreignKeys.values();
0449: // Must be identical (no subsetting), hence the pair of tests.
0450: result &= myFKs.containsAll(aTableFKs)
0451: && aTableFKs.containsAll(myFKs);
0452: } else if (!(foreignKeys == null && aTableFKs == null)) {
0453: result = false;
0454: }
0455:
0456: if (indexes != null && aTableIdxs != null) {
0457: Collection<Index> myIdxs = indexes.values();
0458: // Must be identical (no subsetting), hence the pair of tests.
0459: result &= myIdxs.containsAll(aTableIdxs)
0460: && aTableIdxs.containsAll(myIdxs);
0461: } else if (!(indexes == null && aTableIdxs == null)) {
0462: result = false;
0463: }
0464: }
0465: return result;
0466: }
0467:
0468: /**
0469: * @see org.netbeans.modules.sql.framework.model.SQLDBTable#getAliasName()
0470: */
0471: public String getAliasName() {
0472: return (String) this .getAttributeObject(ATTR_ALIAS_NAME);
0473: }
0474:
0475: /**
0476: * @see org.netbeans.modules.sql.framework.model.SQLDBTable#getBatchSize()
0477: */
0478: public int getBatchSize() {
0479: Integer batchSize = (Integer) this
0480: .getAttributeObject(ATTR_COMMIT_BATCH_SIZE);
0481: return (batchSize != null) ? batchSize.intValue()
0482: : DEFAULT_COMMIT_BATCH_SIZE;
0483: }
0484:
0485: /**
0486: * @see org.netbeans.modules.model.database.DBTable#getCatalog
0487: */
0488: public String getCatalog() {
0489: return catalog;
0490: }
0491:
0492: /**
0493: * Gets List of child SQLObjects belonging to this instance.
0494: *
0495: * @return List of child SQLObjects
0496: */
0497: @Override
0498: public List<DBColumn> getChildSQLObjects() {
0499: return this .getColumnList();
0500: }
0501:
0502: /**
0503: * Gets the DBColumn, if any, associated with the given name
0504: *
0505: * @param columnName column name
0506: * @return DBColumn associated with columnName, or null if none exists
0507: */
0508: public DBColumn getColumn(String columnName) {
0509: return columns.get(columnName);
0510: }
0511:
0512: /**
0513: * @see org.netbeans.modules.model.database.DBTable#getColumnList
0514: */
0515: public List<DBColumn> getColumnList() {
0516: List<DBColumn> list = new ArrayList<DBColumn>();
0517: list.addAll(columns.values());
0518: Collections.sort(list, NativeColumnOrderComparator
0519: .getInstance());
0520:
0521: return list;
0522: }
0523:
0524: /**
0525: * @see org.netbeans.modules.model.database.DBTable#getColumns
0526: */
0527: public Map<String, DBColumn> getColumns() {
0528: return columns;
0529: }
0530:
0531: /**
0532: * @see org.netbeans.modules.model.database.DBTable#getDescription
0533: */
0534: public String getDescription() {
0535: return description;
0536: }
0537:
0538: /**
0539: * Get display name
0540: *
0541: * @return display name
0542: */
0543: @Override
0544: public String getDisplayName() {
0545: return this .getQualifiedName();
0546: }
0547:
0548: /**
0549: * Gets the flat file location runtime input name which is generate when a flat file
0550: * table is added to collaboration. use this name at runtime for file location passed
0551: * by eInsight
0552: *
0553: * @return String representing flatfile location runtime input name
0554: */
0555: public String getFlatFileLocationRuntimeInputName() {
0556: return (String) this
0557: .getAttributeObject(ATTR_FLATFILE_LOCATION_RUNTIME_INPUT_NAME);
0558: }
0559:
0560: /**
0561: * @see org.netbeans.modules.model.database.DBTable#getForeignKey(java.lang.String)
0562: */
0563: public ForeignKey getForeignKey(String fkName) {
0564: return foreignKeys.get(fkName);
0565: }
0566:
0567: /**
0568: * @see org.netbeans.modules.model.database.DBTable#getForeignKeys
0569: */
0570: public List<ForeignKey> getForeignKeys() {
0571: return new ArrayList<ForeignKey>(foreignKeys.values());
0572: }
0573:
0574: /**
0575: * get table fully qualified name including schema , catalog info
0576: *
0577: * @return fully qualified table name prefixed with alias
0578: */
0579: public String getFullyQualifiedName() {
0580:
0581: String tblName = getName();
0582: String schName = getSchema();
0583: String catName = getCatalog();
0584:
0585: if (tblName == null) {
0586: throw new IllegalArgumentException(
0587: "can not construct fully qualified table name, table name is null.");
0588: }
0589:
0590: StringBuilder buf = new StringBuilder(50);
0591:
0592: if (catName != null && catName.trim().length() != 0) {
0593: buf.append(catName.trim());
0594: buf.append(FQ_TBL_NAME_SEPARATOR);
0595: }
0596:
0597: if (schName != null && schName.trim().length() != 0) {
0598: buf.append(schName.trim());
0599: buf.append(FQ_TBL_NAME_SEPARATOR);
0600: }
0601:
0602: buf.append(tblName.trim());
0603:
0604: return buf.toString();
0605: }
0606:
0607: /**
0608: * @see SQLCanvasObject#getGUIInfo
0609: */
0610: public GUIInfo getGUIInfo() {
0611: return guiInfo;
0612: }
0613:
0614: /**
0615: * @see org.netbeans.modules.model.database.DBTable#getIndex
0616: */
0617: public Index getIndex(String indexName) {
0618: return indexes.get(indexName);
0619: }
0620:
0621: /**
0622: * @see org.netbeans.modules.model.database.DBTable#getIndexes
0623: */
0624: public List<Index> getIndexes() {
0625: return new ArrayList<Index>(indexes.values());
0626: }
0627:
0628: /**
0629: * @see org.netbeans.modules.model.database.DBTable#getName
0630: */
0631: public synchronized String getName() {
0632: return name;
0633: }
0634:
0635: /**
0636: * Get specified SQL object
0637: *
0638: * @param objectId - object ID
0639: * @return SQLObject
0640: */
0641: public SQLObject getObject(String objectId) {
0642: List list = this .getColumnList();
0643: Iterator it = list.iterator();
0644:
0645: while (it.hasNext()) {
0646: SQLDBColumn dbColumn = (SQLDBColumn) it.next();
0647: // if looking for table then return table
0648: if (objectId.equals(dbColumn.getId())) {
0649: return dbColumn;
0650: }
0651: }
0652: return null;
0653: }
0654:
0655: /**
0656: * @see org.netbeans.modules.model.database.DBTable#getParent
0657: */
0658: public DatabaseModel getParent() {
0659: return parentDBModel;
0660: }
0661:
0662: /**
0663: * @see org.netbeans.modules.model.database.DBTable#getPrimaryKey
0664: */
0665: public PrimaryKey getPrimaryKey() {
0666: return primaryKey;
0667: }
0668:
0669: /**
0670: * get table qualified name
0671: *
0672: * @return qualified table name prefixed with alias
0673: */
0674: public String getQualifiedName() {
0675: StringBuilder buf = new StringBuilder(50);
0676: String aName = this .getAliasName();
0677: if (aName != null && !aName.trim().equals("")) {
0678: buf.append("(");
0679: buf.append(aName);
0680: buf.append(") ");
0681: buf.append(this .getName());
0682: } else {
0683: buf.append(this .getFullyQualifiedName());
0684: }
0685:
0686: return buf.toString();
0687: }
0688:
0689: /**
0690: * @see org.netbeans.modules.model.database.DBTable#getReferencedTables
0691: */
0692: public Set getReferencedTables() {
0693: List keys = getForeignKeys();
0694: Set<DBTable> tables = new HashSet<DBTable>(keys.size());
0695:
0696: if (keys.size() != 0) {
0697: Iterator iter = keys.iterator();
0698: while (iter.hasNext()) {
0699: ForeignKeyImpl fk = (ForeignKeyImpl) iter.next();
0700: DBTable pkTable = parentDBModel.getTable(fk
0701: .getPKTable(), fk.getPKSchema(), fk
0702: .getPKCatalog());
0703: if (pkTable != null
0704: && fk.references(pkTable.getPrimaryKey())) {
0705: tables.add(pkTable);
0706: }
0707: }
0708:
0709: if (tables.size() == 0) {
0710: tables.clear();
0711: tables = Collections.emptySet();
0712: }
0713: }
0714:
0715: return tables;
0716: }
0717:
0718: /**
0719: * @see org.netbeans.modules.model.database.DBTable#getReferenceFor
0720: */
0721: public ForeignKey getReferenceFor(DBTable target) {
0722: if (target == null) {
0723: return null;
0724: }
0725:
0726: PrimaryKey targetPK = target.getPrimaryKey();
0727: if (targetPK == null) {
0728: return null;
0729: }
0730:
0731: Iterator iter = foreignKeys.values().iterator();
0732: while (iter.hasNext()) {
0733: ForeignKey myFK = (ForeignKey) iter.next();
0734: if (myFK.references(targetPK)) {
0735: return myFK;
0736: }
0737: }
0738:
0739: return null;
0740: }
0741:
0742: public String getRuntimeArgumentName() {
0743: return this .getFlatFileLocationRuntimeInputName();
0744: }
0745:
0746: /**
0747: * @see org.netbeans.modules.model.database.DBTable#getSchema
0748: */
0749: public String getSchema() {
0750: return schema;
0751: }
0752:
0753: /**
0754: * @see org.netbeans.modules.sql.framework.model.SQLDBTable#getTablePrefix()
0755: */
0756: public String getTablePrefix() {
0757: return (String) this .getAttributeObject(ATTR_TABLE_PREFIX);
0758: }
0759:
0760: //RFE-102428
0761: /**
0762: * Gets the staging table name.
0763: *
0764: * @return staging table name
0765: */
0766: public String getStagingTableName() {
0767: return (String) this
0768: .getAttributeObject(ATTR_STAGING_TABLE_NAME);
0769: }
0770:
0771: /**
0772: * @see org.netbeans.modules.sql.framework.model.SQLDBTable#getUniqueTableName()
0773: */
0774: public String getUniqueTableName() {
0775: // Use alias name + given name to make this name consistent with the existing
0776: // name formats used in 5.0.x for flatfile runtime arguments.
0777: return this .getAliasName() + "_" + this .getName();
0778: }
0779:
0780: /**
0781: * @see org.netbeans.modules.sql.framework.model.SQLDBTable#getUserDefinedCatalogName()
0782: */
0783: public String getUserDefinedCatalogName() {
0784: if (overrideCatalogName) {
0785: return overridenCatalogName;
0786: } else {
0787: return (String) this
0788: .getAttributeObject(ATTR_USERDEFINED_CATALOG_NAME);
0789: }
0790: }
0791:
0792: /**
0793: * @see org.netbeans.modules.sql.framework.model.SQLDBTable#getUserDefinedSchemaName()
0794: */
0795: public String getUserDefinedSchemaName() {
0796: if (overrideSchemaName) {
0797: return overridenSchemaName;
0798: } else {
0799: return (String) this
0800: .getAttributeObject(ATTR_USERDEFINED_SCHEMA_NAME);
0801: }
0802: }
0803:
0804: /**
0805: * @see org.netbeans.modules.sql.framework.model.SQLDBTable#getUserDefinedTableName()
0806: */
0807: public String getUserDefinedTableName() {
0808: return (String) this
0809: .getAttributeObject(ATTR_USERDEFINED_TABLE_NAME);
0810: }
0811:
0812: /**
0813: * Overrides default implementation to compute hashCode value for those members used
0814: * in equals() for comparison.
0815: *
0816: * @return hash code for this object
0817: * @see java.lang.Object#hashCode
0818: */
0819: @Override
0820: public int hashCode() {
0821: int myHash = super .hashCode();
0822: myHash = (name != null) ? name.hashCode() : 0;
0823: myHash += (parentDBModel != null) ? parentDBModel.hashCode()
0824: : 0;
0825: myHash += (schema != null) ? schema.hashCode() : 0;
0826: myHash += (catalog != null) ? catalog.hashCode() : 0;
0827:
0828: // Include hashCodes of all column names.
0829: if (columns != null) {
0830: myHash += columns.keySet().hashCode();
0831: }
0832:
0833: if (primaryKey != null) {
0834: myHash += primaryKey.hashCode();
0835: }
0836:
0837: if (foreignKeys != null) {
0838: myHash += foreignKeys.keySet().hashCode();
0839: }
0840:
0841: if (indexes != null) {
0842: myHash += indexes.keySet().hashCode();
0843: }
0844:
0845: myHash += (displayName != null) ? displayName.hashCode() : 0;
0846:
0847: return myHash;
0848: }
0849:
0850: /**
0851: * @return Returns the aliasUsed.
0852: */
0853: public boolean isAliasUsed() {
0854: return aliasUsed;
0855: }
0856:
0857: /**
0858: * Get editable
0859: *
0860: * @return true/false
0861: */
0862: public boolean isEditable() {
0863: return this .editable;
0864: }
0865:
0866: public boolean isInputStatic(String inputName) {
0867: return false;
0868: }
0869:
0870: /**
0871: * Get selected
0872: *
0873: * @return selected
0874: */
0875: public boolean isSelected() {
0876: return this .selected;
0877: }
0878:
0879: /**
0880: * @see org.netbeans.modules.sql.framework.model.SQLDBTable#isUsingFullyQualifiedName()
0881: */
0882: public boolean isUsingFullyQualifiedName() {
0883: Boolean isUsing = (Boolean) getAttributeObject(ATTR_USING_FULLYQUALIFIED_NAME);
0884: return (isUsing != null) ? isUsing.booleanValue() : true;
0885: }
0886:
0887: public void overrideCatalogName(String nName) {
0888: this .overrideCatalogName = true;
0889: this .overridenCatalogName = nName;
0890: }
0891:
0892: public void overrideSchemaName(String nName) {
0893: this .overrideSchemaName = true;
0894: this .overridenSchemaName = nName;
0895: }
0896:
0897: /**
0898: * Parses the XML content, if any, using the given Element as a source for
0899: * reconstituting the member variables and collections of this instance.
0900: *
0901: * @param tableElement DOM element containing XML marshalled version of a
0902: * @exception BaseException thrown while parsing XML, or if member variable element is
0903: * null
0904: */
0905: @Override
0906: public void parseXML(Element tableElement) throws BaseException {
0907: if (tableElement == null) {
0908: throw new BaseException("Null ref for tableElement.");
0909: }
0910:
0911: if (!(tableElement.getNodeName().equals(getElementTagName()))) {
0912: throw new BaseException("No <" + getElementTagName()
0913: + "> element found.");
0914: }
0915:
0916: super .parseXML(tableElement);
0917:
0918: name = tableElement.getAttribute(TABLE_NAME_ATTR);
0919: schema = tableElement.getAttribute(SCHEMA_NAME_ATTR);
0920: catalog = tableElement.getAttribute(CATALOG_NAME_ATTR);
0921:
0922: NodeList childNodeList = tableElement.getChildNodes();
0923: parseChildren(childNodeList);
0924: }
0925:
0926: /**
0927: * @see org.netbeans.modules.model.database.DBTable#references
0928: */
0929: public boolean references(DBTable pkTarget) {
0930: return (getReferenceFor(pkTarget) != null);
0931: }
0932:
0933: /**
0934: * Dissociates the given ForeignKeyImpl from this AbstractDBTable instance, removing
0935: * it from its internal FK collection.
0936: *
0937: * @param oldKey new ForeignKeyImpl instance to be removed
0938: * @return return true if removal succeeded, false otherwise
0939: */
0940: public boolean removeForeignKey(ForeignKeyImpl oldKey) {
0941: if (oldKey != null) {
0942: return (foreignKeys.remove(oldKey.getName()) != null);
0943: }
0944:
0945: return false;
0946: }
0947:
0948: /**
0949: * set the alias name for this table
0950: *
0951: * @param aName alias name
0952: */
0953: public void setAliasName(String aName) {
0954: this .setAttribute(ATTR_ALIAS_NAME, aName);
0955: }
0956:
0957: /**
0958: * @param aliasUsed The aliasUsed to set.
0959: */
0960: public void setAliasUsed(boolean aliasUsed) {
0961: this .aliasUsed = aliasUsed;
0962: }
0963:
0964: /**
0965: * Clones contents of the given Map to this table's internal column map, overwriting
0966: * any previous mappings.
0967: *
0968: * @param theColumns Map of columns to be substituted
0969: * @return true if successful. false if failed.
0970: */
0971: public boolean setAllColumns(Map<String, DBColumn> theColumns) {
0972: columns.clear();
0973: if (theColumns != null) {
0974: columns.putAll(theColumns);
0975: }
0976: return true;
0977: }
0978:
0979: public void setBatchSize(int newSize) {
0980: if (newSize < 0) {
0981: newSize = DEFAULT_COMMIT_BATCH_SIZE;
0982: }
0983:
0984: this .setAttribute(ATTR_COMMIT_BATCH_SIZE, new Integer(newSize));
0985: }
0986:
0987: /**
0988: * Sets catalog name to new value.
0989: *
0990: * @param newCatalog new value for catalog name
0991: */
0992: public void setCatalog(String newCatalog) {
0993: catalog = newCatalog;
0994: }
0995:
0996: /**
0997: * Sets description text for this instance.
0998: *
0999: * @param newDesc new descriptive text
1000: */
1001: public void setDescription(String newDesc) {
1002: description = newDesc;
1003: }
1004:
1005: /**
1006: * Set editable
1007: *
1008: * @param edit - editable
1009: */
1010: public void setEditable(boolean edit) {
1011: this .editable = edit;
1012: }
1013:
1014: /**
1015: * set flat file location runtime input name which is generate when a flat file table
1016: * is added to collaboration
1017: *
1018: * @param runtimeArgName name of runtime input argument for flat file location
1019: */
1020: public void setFlatFileLocationRuntimeInputName(
1021: String runtimeArgName) {
1022: this .setAttribute(ATTR_FLATFILE_LOCATION_RUNTIME_INPUT_NAME,
1023: runtimeArgName);
1024: }
1025:
1026: /**
1027: * Sets table name to new value.
1028: *
1029: * @param newName new value for table name
1030: */
1031: public void setName(String newName) {
1032: name = newName;
1033: }
1034:
1035: /**
1036: * Sets parentDBModel DatabaseModel to the given reference.
1037: *
1038: * @param newParent new DatabaseModel parentDBModel
1039: */
1040: public void setParent(SQLDBModel newParent) {
1041: parentDBModel = newParent;
1042: try {
1043: setParentObject(newParent);
1044: } catch (BaseException ex) {
1045: // do nothing
1046: }
1047: }
1048:
1049: /**
1050: * Sets PrimaryKey instance for this DBTable to the given instance.
1051: *
1052: * @param newPk new PrimaryKey instance to be associated
1053: * @return true if association succeeded, false otherwise
1054: */
1055: public boolean setPrimaryKey(PrimaryKeyImpl newPk) {
1056: if (newPk != null) {
1057: newPk.setParent(this );
1058: }
1059:
1060: primaryKey = newPk;
1061: return true;
1062: }
1063:
1064: public void setForeignKeyMap(Map<String, ForeignKey> fkMap) {
1065: foreignKeys = fkMap;
1066: }
1067:
1068: /**
1069: * Sets schema name to new value.
1070: *
1071: * @param newSchema new value for schema name
1072: */
1073: public void setSchema(String newSchema) {
1074: schema = newSchema;
1075: }
1076:
1077: /**
1078: * Set selected
1079: *
1080: * @param sel - selected
1081: */
1082: public void setSelected(boolean sel) {
1083: this .selected = sel;
1084: }
1085:
1086: public void setTablePrefix(String tPrefix) {
1087: this .setAttribute(ATTR_TABLE_PREFIX, tPrefix);
1088: }
1089:
1090: //RFE-102428
1091: /**
1092: * Sets the staging table name.
1093: *
1094: * @param stName staging table name
1095: */
1096: public void setStagingTableName(String stName) {
1097: this .setAttribute(ATTR_STAGING_TABLE_NAME, stName);
1098: }
1099:
1100: /**
1101: * @see org.netbeans.modules.sql.framework.model.SQLDBTable#setUserDefinedCatalogName(java.lang.String)
1102: */
1103: public void setUserDefinedCatalogName(String newName) {
1104: this .setAttribute(ATTR_USERDEFINED_CATALOG_NAME, newName);
1105: }
1106:
1107: /**
1108: * @see org.netbeans.modules.sql.framework.model.SQLDBTable#setUserDefinedSchemaName(java.lang.String)
1109: */
1110: public void setUserDefinedSchemaName(String newName) {
1111: this .setAttribute(ATTR_USERDEFINED_SCHEMA_NAME, newName);
1112: }
1113:
1114: /**
1115: * @see org.netbeans.modules.sql.framework.model.SQLDBTable#setUserDefinedTableName(java.lang.String)
1116: */
1117: public void setUserDefinedTableName(String newName) {
1118: this .setAttribute(ATTR_USERDEFINED_TABLE_NAME, newName);
1119: }
1120:
1121: /**
1122: * @see org.netbeans.modules.sql.framework.model.SQLDBTable#setUsingFullyQualifiedName(boolean)
1123: */
1124: public void setUsingFullyQualifiedName(boolean usesFullName) {
1125: this .setAttribute(ATTR_USING_FULLYQUALIFIED_NAME,
1126: (usesFullName ? Boolean.TRUE : Boolean.FALSE));
1127: }
1128:
1129: /**
1130: * Overrides default implementation to return appropriate display name of this DBTable
1131: *
1132: * @return qualified table name.
1133: */
1134: @Override
1135: public String toString() {
1136: return getQualifiedName();
1137: }
1138:
1139: /**
1140: * @see SQLObject#toXMLString
1141: */
1142: @Override
1143: public String toXMLString(String prefix) throws BaseException {
1144: return toXMLString(prefix, false);
1145: }
1146:
1147: /**
1148: * Returns XML representation of table metadata.
1149: *
1150: * @param prefix prefix for the xml.
1151: * @param tableOnly flag for generating table only metadata.
1152: * @return XML representation of the table metadata.
1153: * @exception BaseException - exception
1154: */
1155: public String toXMLString(String prefix, boolean tableOnly)
1156: throws BaseException {
1157: throw new UnsupportedOperationException("Not supported yet.");
1158: }
1159:
1160: /**
1161: * Perform deep copy of columns.
1162: *
1163: * @param source SQLTable whose columns are to be copied.
1164: */
1165: protected void deepCopyReferences(DBTable source) {
1166: if (source != null && source != this ) {
1167: primaryKey = null;
1168: PrimaryKey srcPk = source.getPrimaryKey();
1169: if (srcPk != null) {
1170: primaryKey = new PrimaryKeyImpl(source.getPrimaryKey());
1171: primaryKey.setParent(this );
1172: }
1173:
1174: foreignKeys.clear();
1175: Iterator iter = source.getForeignKeys().iterator();
1176: while (iter.hasNext()) {
1177: ForeignKeyImpl impl = new ForeignKeyImpl(
1178: (ForeignKey) iter.next());
1179: impl.setParent(this );
1180: foreignKeys.put(impl.getName(), impl);
1181: }
1182:
1183: indexes.clear();
1184: iter = source.getIndexes().iterator();
1185:
1186: while (iter.hasNext()) {
1187: IndexImpl impl = new IndexImpl((Index) iter.next());
1188: impl.setParent(this );
1189: indexes.put(impl.getName(), impl);
1190: }
1191:
1192: columns.clear();
1193: iter = source.getColumnList().iterator();
1194: while (iter.hasNext()) {
1195: try {
1196: SQLDBColumn column = (SQLDBColumn) iter.next();
1197: SQLDBColumn clonedColumn = (SQLDBColumn) column
1198: .cloneSQLObject();
1199: columns.put(clonedColumn.getName(), clonedColumn);
1200: } catch (Exception ex) {
1201: // TODO Log this exception
1202: }
1203: }
1204: }
1205: }
1206:
1207: /**
1208: * Gets String representing tag name for this table class.
1209: *
1210: * @return String representing element tag for this class
1211: */
1212: protected String getElementTagName() {
1213: throw new UnsupportedOperationException("Not supported yet.");
1214: }
1215:
1216: /**
1217: * Parses node elements to extract child components to various collections (columns,
1218: * PK, FK, indexes).
1219: *
1220: * @param childNodeList Nodes to be unmarshalled
1221: * @throws BaseException if error occurs while parsing
1222: */
1223: protected void parseChildren(NodeList childNodeList)
1224: throws BaseException {
1225: throw new UnsupportedOperationException("Not supported yet.");
1226: }
1227:
1228: /**
1229: * Sets default values for attributes defined in this abstract class.
1230: */
1231: protected void setDefaultAttributes() {
1232: setUserDefinedTableName("");
1233: setUserDefinedSchemaName("");
1234: setUserDefinedCatalogName("");
1235: setTablePrefix("");
1236: setAliasName("");
1237: setUsingFullyQualifiedName(true);
1238: setStagingTableName("");
1239: }
1240: }
|