KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > dbschema > ForeignKeyElement


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.dbschema;
21
22 /** Describes a foreign key in a table.
23  */

24 public final class ForeignKeyElement extends KeyElement implements ReferenceKey, ColumnPairElementHolder {
25     /** Creates a new foreign key element represented in memory.
26      */

27     public ForeignKeyElement() {
28         this(new Memory(), null);
29     }
30
31     /** Creates a new foreign key element.
32      * @param impl the pluggable implementation
33      * @param declaringTable declaring table of this foreign key, or
34      * <code>null</code>
35      */

36     public ForeignKeyElement(Impl impl, TableElement declaringTable) {
37         super(impl, declaringTable);
38     }
39
40     /** Returns the implementation for the foreign key.
41      * @return implementation for the foreign key
42      */

43     final Impl getForeignKeyImpl() {
44         return (Impl)getElementImpl();
45     }
46
47     /** Gets the referenced table of the foreign key.
48      * @return the referenced table
49      */

50     public TableElement getReferencedTable() {
51         ColumnPairElement[] columnPairs = getColumnPairs();
52
53         if ((columnPairs != null) && (columnPairs.length > 0))
54             return columnPairs[0].getReferencedColumn().getDeclaringTable();
55
56         return null;
57     }
58
59     /** Gets all referenced columns in this foreign key.
60      * @return the referenced columns
61      */

62     public ColumnElement[] getReferencedColumns() {
63         ColumnPairElement[] columnPairs = getColumnPairs();
64         int count = ((columnPairs != null) ? columnPairs.length : 0);
65         ColumnElement[] ce = new ColumnElement[count];
66         
67         for (int i = 0; i < count; i++)
68             ce[i] = columnPairs[i].getReferencedColumn();
69         
70         return ce;
71     }
72     
73     /** Gets the name of this element.
74      * @return the name
75      */

76     public String JavaDoc getKeyName() {
77         return getName().getFullName();
78     }
79     
80     /** Sets the name of this element.
81      * @param name the name
82      * @throws DBException if impossible
83      */

84     public void setKeyName(String JavaDoc name) throws DBException {
85         setName(DBIdentifier.create(name));
86     }
87     
88     /** Adds a new column pair to the holder.
89      * @param pair the pair to add
90      * @throws DBException if impossible
91      */

92     public void addColumnPair(ColumnPairElement pair) throws DBException {
93         addColumnPairs(new ColumnPairElement[] {pair});
94     }
95     
96     /** Adds some new column pairs to the holder.
97      * @param pairs the column pairs to add
98      * @throws DBException if impossible
99      */

100     public void addColumnPairs(ColumnPairElement[] pairs) throws DBException {
101         getForeignKeyImpl().changeColumnPairs(pairs, Impl.ADD);
102     }
103     
104     /** Removes a column pair from the holder.
105      * @param pair the column pair to remove
106      * @throws DBException if impossible
107      */

108     public void removeColumnPair(ColumnPairElement pair) throws DBException {
109         removeColumnPairs(new ColumnPairElement[] {pair});
110     }
111     
112     /** Removes some column pairs from the holder.
113      * @param pairs the column pairs to remove
114      * @throws DBException if impossible
115      */

116     public void removeColumnPairs(ColumnPairElement[] pairs) throws DBException {
117         getForeignKeyImpl().changeColumnPairs(pairs, Impl.REMOVE);
118     }
119     
120     /** Sets the column pairs for this holder. Previous column pairs are removed.
121      * @param pairs the new column pairs
122      * @throws DBException if impossible
123      */

124     public void setColumnPairs(ColumnPairElement[] pairs) throws DBException {
125         getForeignKeyImpl().changeColumnPairs(pairs, Impl.SET);
126     }
127     
128     /** Gets all column pairs in this holder.
129      * @return the column pairs
130      */

131     public ColumnPairElement[] getColumnPairs() {
132         return getForeignKeyImpl().getColumnPairs();
133     }
134     
135     /** Finds a column pair by name.
136      * @param name the name of the column pair for which to look
137      * @return the column pair or <code>null</code> if not found
138      */

139     public ColumnPairElement getColumnPair(DBIdentifier name) {
140         return getForeignKeyImpl().getColumnPair(name);
141     }
142
143     /** Gets all local columns in this reference key.
144      * @return the local columns
145      */

146     public ColumnElement[] getLocalColumns() {
147         return getForeignKeyImpl().getColumns();
148     }
149     
150 //-- Unsupported methods ---------------------------------------------------
151

152     /** Adds a column to the holder. It is unsupported operation.
153      * @param el the column to add
154      * @throws UnsupportedOperationException is always thrown
155      */

156     public void addColumn (ColumnElement el) throws UnsupportedOperationException JavaDoc {
157         throw new UnsupportedOperationException JavaDoc();
158     }
159
160     /** Adds a collection of columns to the holder. It is unsupported operation.
161      * @param els the columns to add
162      * @throws UnsupportedOperationException is always thrown
163      */

164     public void addColumns (ColumnElement[] els) throws UnsupportedOperationException JavaDoc {
165         throw new UnsupportedOperationException JavaDoc();
166     }
167
168     /** Removes a column from the holder. It is unsupported operation.
169      * @param el the column to remove
170      * @throws UnsupportedOperationException is always thrown
171      */

172     public void removeColumn (ColumnElement el) throws UnsupportedOperationException JavaDoc {
173         throw new UnsupportedOperationException JavaDoc();
174     }
175
176     /** Removes a collection of columns from the holder. It is unsupported operation.
177      * @param els the columns to remove
178      * @throws UnsupportedOperationException is always thrown
179      */

180     public void removeColumns (ColumnElement[] els) throws UnsupportedOperationException JavaDoc {
181         throw new UnsupportedOperationException JavaDoc();
182     }
183
184     /** Sets a collection of columns to the holder. It is unsupported operation.
185      * Previous columns are removed.
186      * @param els the column to set
187      * @throws UnsupportedOperationException is always thrown
188      */

189     public void setColumns (ColumnElement[] els) throws UnsupportedOperationException JavaDoc {
190         throw new UnsupportedOperationException JavaDoc();
191     }
192     
193 //-------------------------------------------------------------------------
194

195     /** Gets all columns in this key.
196      * @return the columns
197      */

198     public ColumnElement[] getColumns () {
199         return getForeignKeyImpl().getColumns();
200     }
201
202     /** Gets a column by name.
203      * @param name the name
204      * @return the column
205      */

206     public ColumnElement getColumn (DBIdentifier name) {
207         return getForeignKeyImpl().getColumn(name);
208     }
209
210     
211     /** Implementation of a foreign key element.
212      * @see KeyElement
213      */

214     public interface Impl extends KeyElement.Impl {
215         /** Changes the content of this object.
216          * @param arr array of objects to change
217          * @param action the action to do
218          */

219         public void changeColumnPairs(ColumnPairElement[] pairs, int action) throws DBException;
220         
221         /** Gets all column pairs.
222          * @return the column pairs
223          */

224         public ColumnPairElement[] getColumnPairs();
225
226         /** Finds a column pair by name.
227          * @param name the name of the column pair for which to look
228          * @return the column pair or <code>null</code> if not found
229          */

230         public ColumnPairElement getColumnPair(DBIdentifier name);
231     }
232
233     static class Memory extends KeyElement.Memory implements Impl {
234         /** collection of column pairs */
235         private DBMemoryCollection.ColumnPair pairs;
236         
237         /** Default constructor
238          */

239         Memory() {
240             super();
241         }
242
243         /** Copy constructor.
244         * @param fk the object from which to read values
245         */

246         Memory(ForeignKeyElement fk) {
247             super(fk);
248         }
249
250         /** Gets all column pairs.
251          * @return the column pairs
252          */

253         public synchronized ColumnPairElement[] getColumnPairs() {
254             initColumnPairs();
255             return (ColumnPairElement[]) pairs.getElements();
256         }
257         
258         /** Finds a column pair by name.
259          * @param name the name of the column pair for which to look
260          * @return the column pair or <code>null</code> if not found
261          */

262         public synchronized ColumnPairElement getColumnPair(DBIdentifier name) {
263             initColumnPairs();
264             return (ColumnPairElement) pairs.getElement(name);
265         }
266         
267         /** Changes the content of this object.
268          * @param arr array of objects to change
269          * @param action the action to do
270          */

271         public synchronized void changeColumnPairs(ColumnPairElement[] pairs, int action) throws DBException {
272             initColumnPairs();
273             this.pairs.change(pairs, action);
274         }
275
276         /** Initializes the collection of column pairs.
277          */

278         void initColumnPairs() {
279             if (pairs == null)
280                 pairs = new DBMemoryCollection.ColumnPair(this);
281         }
282
283     }
284 }
285
Popular Tags