KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > api > persistence > model > mapping > impl > MappingRelationshipElementImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * MappingRelationshipElementImpl.java
26  *
27  * Created on March 3, 2000, 1:11 PM
28  */

29
30 package com.sun.jdo.api.persistence.model.mapping.impl;
31
32 import java.util.*;
33 import java.beans.PropertyVetoException JavaDoc;
34
35 import org.netbeans.modules.dbschema.*;
36 import org.netbeans.modules.dbschema.util.NameUtil;
37
38 import com.sun.jdo.api.persistence.model.ModelException;
39 import com.sun.jdo.api.persistence.model.ModelVetoException;
40 import com.sun.jdo.api.persistence.model.mapping.*;
41 import com.sun.jdo.api.persistence.model.jdo.RelationshipElement;
42 import com.sun.jdo.spi.persistence.utility.I18NHelper;
43
44 /**
45  *
46  * @author Mark Munro
47  * @author Rochelle Raccah
48  * @version %I%
49  */

50 public class MappingRelationshipElementImpl extends MappingFieldElementImpl
51     implements MappingRelationshipElement
52 {
53     // for join tables -- traverse through the middle table
54
private ArrayList _associatedColumns; // of column pair names
55
//@olsen: made transient to prevent from serializing into mapping files
56
private transient ArrayList _associatedColumnObjects;// of ColumnPairElement (for runtime)
57

58     /*
59     // possibly for EJB use later
60     // private String EJBType;
61     // public String EJBReference;
62     // end possibly for EJB use later
63     */

64
65     /** Create new MappingRelationshipElementImpl with no corresponding name or
66      * declaring class. This constructor should only be used for cloning and
67      * archiving.
68      */

69     public MappingRelationshipElementImpl ()
70     {
71         this(null, null);
72     }
73
74     /** Create new MappingRelationshipElementImpl with the corresponding name
75      * and declaring class.
76      * @param name the name of the element
77      * @param declaringClass the class to attach to
78      */

79     public MappingRelationshipElementImpl (String JavaDoc name,
80         MappingClassElement declaringClass)
81     {
82         super(name, declaringClass);
83         setFetchGroupInternal(GROUP_NONE);
84     }
85
86 // TBD?
87
/* public void clear ()
88     {
89         super.clear();
90         _associatedColumns = null;
91     }*/

92 // end TBD
93

94     //=================== column handling for join tables ====================
95

96     /** Returns the list of associated column names to which this
97      * mapping field is mapped. This is used for join tables.
98      * @return the names of the columns mapped by this mapping field
99      * @see MappingFieldElement#getColumns
100      */

101     public ArrayList getAssociatedColumns ()
102     {
103         if (_associatedColumns == null)
104             _associatedColumns = new ArrayList();
105
106         return _associatedColumns;
107     }
108
109     /** Adds a column to the list of columns mapped by this mapping field.
110      * Call this method instead of <code>addColumn</code> when mapping join
111      * tables. This method is used to map between the local column and the
112      * join table, while <code>addAssociatedColumn</code> is used to
113      * map between the join table and the foreign table.
114      * @param column column pair element to be added to the mapping
115      * @exception ModelException if impossible
116      * @see MappingFieldElement#addColumn
117      * @see #addAssociatedColumn
118      */

119     public void addLocalColumn (ColumnPairElement column)
120         throws ModelException
121     {
122         // can't call addColumn in this class because there will be an
123
// exception since the associated columns will be (legally) populated
124
super.addColumn(column);
125     }
126
127     /** Adds a column to the list of associated columns mapped by this mapping
128      * field. Call this method instead of <code>addColumn</code> when mapping
129      * join tables. This method is used to map between the join table column
130      * and the foreign table column, while <code>addLocalColumn</code> is used
131      * to map between the local table and the join table.
132      * @param column column pair element to be added to the mapping
133      * @exception ModelException if impossible
134      * @see MappingFieldElement#addColumn
135      * @see #addLocalColumn
136      */

137     public void addAssociatedColumn (ColumnPairElement column)
138         throws ModelException
139     {
140         if (column != null)
141         {
142             ArrayList columns = getAssociatedColumns();
143             String JavaDoc columnName = NameUtil.getRelativeMemberName(
144                 column.getName().getFullName());
145
146             // double check that this pair is not already in the column list
147
if (!columns.contains(columnName))
148             {
149                 try
150                 {
151                     fireVetoableChange(PROP_ASSOCIATED_COLUMNS, null, null);
152                     columns.add(columnName);
153                     firePropertyChange(PROP_ASSOCIATED_COLUMNS, null, null);
154
155                     // sync up runtime's object list too
156
_associatedColumnObjects = null;
157                 }
158                 catch (PropertyVetoException JavaDoc e)
159                 {
160                     throw new ModelVetoException(e);
161                 }
162             }
163             else
164             {
165                 throw new ModelException(I18NHelper.getMessage(getMessages(),
166                     "mapping.column.column_defined", columnName)); // NOI18N
167
}
168         }
169         else
170         {
171             throw new ModelException(I18NHelper.getMessage(getMessages(),
172                 "mapping.element.null_argument")); // NOI18N
173
}
174     }
175
176     //================= overridden column handling methods ===================
177

178     /** Adds a column to the list of columns mapped by this mapping
179      * relationship. This method overrides the one in MappingFieldElement to
180      * check that the argument is a ColumnPairElement.
181      * @param column column element to be added to the mapping
182      * @exception ModelException if impossible
183      */

184     public void addColumn (DBMemberElement column) throws ModelException
185     {
186         if (column instanceof ColumnPairElement)
187         {
188             if (!getAssociatedColumns().isEmpty())
189             {
190                 throw new ModelException(I18NHelper.getMessage(getMessages(),
191                     "mapping.column.associated_columns_defined", // NOI18N
192
NameUtil.getRelativeMemberName(
193                     column.getName().getFullName())));
194             }
195
196             super.addColumn(column);
197         }
198         else
199         {
200             throw new ModelException(I18NHelper.getMessage(getMessages(),
201                 "mapping.column.column_invalid", // NOI18N
202
NameUtil.getRelativeMemberName(
203                 column.getName().getFullName())));
204         }
205     }
206
207     /** Removes a column from the list of columns mapped by this mapping field.
208      * This method overrides the one in MappingFieldElement to
209      * remove the argument from the associated columns if necessary.
210      * @param columnName the relative name of the column to be removed from
211      * the mapping
212      * @exception ModelException if impossible
213      */

214     public void removeColumn (String JavaDoc columnName) throws ModelException
215     {
216         try
217         {
218             super.removeColumn(columnName);
219         }
220         catch (ModelException e) // not found in regular columns
221
{
222             try
223             {
224                 fireVetoableChange(PROP_ASSOCIATED_COLUMNS, null, null);
225
226                 if (!getAssociatedColumns().remove(columnName))
227                 {
228                     throw new ModelException(
229                         I18NHelper.getMessage(getMessages(),
230                         "mapping.element.element_not_removed", // NOI18N
231
columnName));
232                 }
233
234                 firePropertyChange(PROP_ASSOCIATED_COLUMNS, null, null);
235
236                 // sync up runtime's object list too
237
_associatedColumnObjects = null;
238             }
239             catch (PropertyVetoException JavaDoc ve)
240             {
241                 throw new ModelVetoException(ve);
242             }
243         }
244     }
245
246     //============= extra object support for runtime ========================
247

248     /** Returns the list of associated columns (ColumnPairElements) to
249      * which this mapping field is mapped. This is used for join tables.
250      * This method should only be used by the runtime.
251      * @return the columns mapped by this mapping field
252      * @see MappingFieldElement#getColumns
253      */

254     public ArrayList getAssociatedColumnObjects ()
255     {
256         if (_associatedColumnObjects == null)
257         {
258             _associatedColumnObjects = MappingClassElementImpl.
259                 toColumnObjects(getDeclaringClass().getDatabaseRoot(),
260                 getAssociatedColumns());
261         }
262
263         return _associatedColumnObjects;
264     }
265
266     //============= delegation to RelationshipElement ===========
267

268     final RelationshipElement getRelationshipElement ()
269     {
270         return ((MappingClassElementImpl)getDeclaringClass()).
271             getPersistenceElement().getRelationship(getName());
272     }
273
274     /** Get the element class for this relationship element. If primitive
275      * types are supported, you can use <code><i>wrapperclass</i>.TYPE</code>
276      * to specify them.
277      * @return the element class
278      */

279     public String JavaDoc getElementClass ()
280     {
281         return getRelationshipElement().getElementClass();
282     }
283
284     /** Get the update action for this relationship element.
285      * @return the update action, one of
286      * {@link RelationshipElement#NONE_ACTION},
287      * {@link RelationshipElement#NULLIFY_ACTION},
288      * {@link RelationshipElement#RESTRICT_ACTION},
289      * {@link RelationshipElement#CASCADE_ACTION}, or
290      * {@link RelationshipElement#AGGREGATE_ACTION}
291      */

292     public int getUpdateAction ()
293     {
294         return getRelationshipElement().getUpdateAction();
295     }
296
297     /** Get the delete action for this relationship element.
298      * @return the delete action, one of
299      * {@link RelationshipElement#NONE_ACTION},
300      * {@link RelationshipElement#NULLIFY_ACTION},
301      * {@link RelationshipElement#RESTRICT_ACTION},
302      * {@link RelationshipElement#CASCADE_ACTION}, or
303      * {@link RelationshipElement#AGGREGATE_ACTION}
304      */

305     public int getDeleteAction ()
306     {
307         return getRelationshipElement().getDeleteAction();
308     }
309
310     /** Get the upper cardinality bound for this relationship element. Returns
311      * {@link java.lang.Integer#MAX_VALUE} for <code>n</code>
312      * @return the upper cardinality bound
313      */

314     public int getUpperBound ()
315     {
316         return getRelationshipElement().getUpperBound();
317     }
318
319     /** Get the lower cardinality bound for this relationship element.
320      * @return the lower cardinality bound
321      */

322     public int getLowerBound ()
323     {
324         return getRelationshipElement().getLowerBound();
325     }
326
327     //=============== extra set methods needed for xml archiver ==============
328

329     /** Set the list of associated column names to which this mapping field is
330      * mapped. This method should only be used internally and for cloning
331      * and archiving.
332      * @param associatedColumns the list of names of the columns mapped by
333      * this mapping field
334      */

335     public void setAssociatedColumns (ArrayList associatedColumns)
336     {
337         _associatedColumns = associatedColumns;
338     }
339
340     //================== possibly for EJB use later ===========================
341

342     //public String getEJBType() { return this.EJBType; }
343

344     //============== extra method for Boston -> Pilsen conversion ============
345

346     /** Boston to Pilsen conversion.
347      * This method converts the absolute column names to relative names.
348      */

349     protected void stripSchemaName ()
350     {
351         // call super to handle the columns stored in _columns
352
super.stripSchemaName();
353
354         // handle _associatedColumns
355
if (_associatedColumns != null)
356         {
357             // Use ListIterator here, because I want to replace the value
358
// stored in the ArrayList. The ListIterator returned by
359
// ArrayList.listIterator() supports the set method.
360
ListIterator i = _associatedColumns.listIterator();
361
362             while (i.hasNext())
363                 i.set(NameUtil.getRelativeMemberName((String JavaDoc)i.next()));
364         }
365     }
366 }
367
Popular Tags