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-2006 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:
042: package org.netbeans.modules.dbschema;
043:
044: /** Describes a foreign key in a table.
045: */
046: public final class ForeignKeyElement extends KeyElement implements
047: ReferenceKey, ColumnPairElementHolder {
048: /** Creates a new foreign key element represented in memory.
049: */
050: public ForeignKeyElement() {
051: this (new Memory(), null);
052: }
053:
054: /** Creates a new foreign key element.
055: * @param impl the pluggable implementation
056: * @param declaringTable declaring table of this foreign key, or
057: * <code>null</code>
058: */
059: public ForeignKeyElement(Impl impl, TableElement declaringTable) {
060: super (impl, declaringTable);
061: }
062:
063: /** Returns the implementation for the foreign key.
064: * @return implementation for the foreign key
065: */
066: final Impl getForeignKeyImpl() {
067: return (Impl) getElementImpl();
068: }
069:
070: /** Gets the referenced table of the foreign key.
071: * @return the referenced table
072: */
073: public TableElement getReferencedTable() {
074: ColumnPairElement[] columnPairs = getColumnPairs();
075:
076: if ((columnPairs != null) && (columnPairs.length > 0))
077: return columnPairs[0].getReferencedColumn()
078: .getDeclaringTable();
079:
080: return null;
081: }
082:
083: /** Gets all referenced columns in this foreign key.
084: * @return the referenced columns
085: */
086: public ColumnElement[] getReferencedColumns() {
087: ColumnPairElement[] columnPairs = getColumnPairs();
088: int count = ((columnPairs != null) ? columnPairs.length : 0);
089: ColumnElement[] ce = new ColumnElement[count];
090:
091: for (int i = 0; i < count; i++)
092: ce[i] = columnPairs[i].getReferencedColumn();
093:
094: return ce;
095: }
096:
097: /** Gets the name of this element.
098: * @return the name
099: */
100: public String getKeyName() {
101: return getName().getFullName();
102: }
103:
104: /** Sets the name of this element.
105: * @param name the name
106: * @throws DBException if impossible
107: */
108: public void setKeyName(String name) throws DBException {
109: setName(DBIdentifier.create(name));
110: }
111:
112: /** Adds a new column pair to the holder.
113: * @param pair the pair to add
114: * @throws DBException if impossible
115: */
116: public void addColumnPair(ColumnPairElement pair)
117: throws DBException {
118: addColumnPairs(new ColumnPairElement[] { pair });
119: }
120:
121: /** Adds some new column pairs to the holder.
122: * @param pairs the column pairs to add
123: * @throws DBException if impossible
124: */
125: public void addColumnPairs(ColumnPairElement[] pairs)
126: throws DBException {
127: getForeignKeyImpl().changeColumnPairs(pairs, Impl.ADD);
128: }
129:
130: /** Removes a column pair from the holder.
131: * @param pair the column pair to remove
132: * @throws DBException if impossible
133: */
134: public void removeColumnPair(ColumnPairElement pair)
135: throws DBException {
136: removeColumnPairs(new ColumnPairElement[] { pair });
137: }
138:
139: /** Removes some column pairs from the holder.
140: * @param pairs the column pairs to remove
141: * @throws DBException if impossible
142: */
143: public void removeColumnPairs(ColumnPairElement[] pairs)
144: throws DBException {
145: getForeignKeyImpl().changeColumnPairs(pairs, Impl.REMOVE);
146: }
147:
148: /** Sets the column pairs for this holder. Previous column pairs are removed.
149: * @param pairs the new column pairs
150: * @throws DBException if impossible
151: */
152: public void setColumnPairs(ColumnPairElement[] pairs)
153: throws DBException {
154: getForeignKeyImpl().changeColumnPairs(pairs, Impl.SET);
155: }
156:
157: /** Gets all column pairs in this holder.
158: * @return the column pairs
159: */
160: public ColumnPairElement[] getColumnPairs() {
161: return getForeignKeyImpl().getColumnPairs();
162: }
163:
164: /** Finds a column pair by name.
165: * @param name the name of the column pair for which to look
166: * @return the column pair or <code>null</code> if not found
167: */
168: public ColumnPairElement getColumnPair(DBIdentifier name) {
169: return getForeignKeyImpl().getColumnPair(name);
170: }
171:
172: /** Gets all local columns in this reference key.
173: * @return the local columns
174: */
175: public ColumnElement[] getLocalColumns() {
176: return getForeignKeyImpl().getColumns();
177: }
178:
179: //-- Unsupported methods ---------------------------------------------------
180:
181: /** Adds a column to the holder. It is unsupported operation.
182: * @param el the column to add
183: * @throws UnsupportedOperationException is always thrown
184: */
185: public void addColumn(ColumnElement el)
186: throws UnsupportedOperationException {
187: throw new UnsupportedOperationException();
188: }
189:
190: /** Adds a collection of columns to the holder. It is unsupported operation.
191: * @param els the columns to add
192: * @throws UnsupportedOperationException is always thrown
193: */
194: public void addColumns(ColumnElement[] els)
195: throws UnsupportedOperationException {
196: throw new UnsupportedOperationException();
197: }
198:
199: /** Removes a column from the holder. It is unsupported operation.
200: * @param el the column to remove
201: * @throws UnsupportedOperationException is always thrown
202: */
203: public void removeColumn(ColumnElement el)
204: throws UnsupportedOperationException {
205: throw new UnsupportedOperationException();
206: }
207:
208: /** Removes a collection of columns from the holder. It is unsupported operation.
209: * @param els the columns to remove
210: * @throws UnsupportedOperationException is always thrown
211: */
212: public void removeColumns(ColumnElement[] els)
213: throws UnsupportedOperationException {
214: throw new UnsupportedOperationException();
215: }
216:
217: /** Sets a collection of columns to the holder. It is unsupported operation.
218: * Previous columns are removed.
219: * @param els the column to set
220: * @throws UnsupportedOperationException is always thrown
221: */
222: public void setColumns(ColumnElement[] els)
223: throws UnsupportedOperationException {
224: throw new UnsupportedOperationException();
225: }
226:
227: //-------------------------------------------------------------------------
228:
229: /** Gets all columns in this key.
230: * @return the columns
231: */
232: public ColumnElement[] getColumns() {
233: return getForeignKeyImpl().getColumns();
234: }
235:
236: /** Gets a column by name.
237: * @param name the name
238: * @return the column
239: */
240: public ColumnElement getColumn(DBIdentifier name) {
241: return getForeignKeyImpl().getColumn(name);
242: }
243:
244: /** Implementation of a foreign key element.
245: * @see KeyElement
246: */
247: public interface Impl extends KeyElement.Impl {
248: /** Changes the content of this object.
249: * @param arr array of objects to change
250: * @param action the action to do
251: */
252: public void changeColumnPairs(ColumnPairElement[] pairs,
253: int action) throws DBException;
254:
255: /** Gets all column pairs.
256: * @return the column pairs
257: */
258: public ColumnPairElement[] getColumnPairs();
259:
260: /** Finds a column pair by name.
261: * @param name the name of the column pair for which to look
262: * @return the column pair or <code>null</code> if not found
263: */
264: public ColumnPairElement getColumnPair(DBIdentifier name);
265: }
266:
267: static class Memory extends KeyElement.Memory implements Impl {
268: /** collection of column pairs */
269: private DBMemoryCollection.ColumnPair pairs;
270:
271: /** Default constructor
272: */
273: Memory() {
274: super ();
275: }
276:
277: /** Copy constructor.
278: * @param fk the object from which to read values
279: */
280: Memory(ForeignKeyElement fk) {
281: super (fk);
282: }
283:
284: /** Gets all column pairs.
285: * @return the column pairs
286: */
287: public synchronized ColumnPairElement[] getColumnPairs() {
288: initColumnPairs();
289: return (ColumnPairElement[]) pairs.getElements();
290: }
291:
292: /** Finds a column pair by name.
293: * @param name the name of the column pair for which to look
294: * @return the column pair or <code>null</code> if not found
295: */
296: public synchronized ColumnPairElement getColumnPair(
297: DBIdentifier name) {
298: initColumnPairs();
299: return (ColumnPairElement) pairs.getElement(name);
300: }
301:
302: /** Changes the content of this object.
303: * @param arr array of objects to change
304: * @param action the action to do
305: */
306: public synchronized void changeColumnPairs(
307: ColumnPairElement[] pairs, int action)
308: throws DBException {
309: initColumnPairs();
310: this .pairs.change(pairs, action);
311: }
312:
313: /** Initializes the collection of column pairs.
314: */
315: void initColumnPairs() {
316: if (pairs == null)
317: pairs = new DBMemoryCollection.ColumnPair(this);
318: }
319:
320: }
321: }
|