KickJava   Java API By Example, From Geeks To Geeks.

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


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  * RelationshipElementImpl.java
26  *
27  * Created on March 2, 2000, 6:21 PM
28  */

29
30 package com.sun.jdo.api.persistence.model.jdo.impl;
31
32 import java.beans.PropertyVetoException JavaDoc;
33
34 import com.sun.jdo.api.persistence.model.ModelException;
35 import com.sun.jdo.api.persistence.model.ModelVetoException;
36 import com.sun.jdo.api.persistence.model.jdo.RelationshipElement;
37 import com.sun.jdo.spi.persistence.utility.JavaTypeHelper;
38
39 /* TODO:
40     1. upper and lower bound defaults/ get method constraints based on field
41      type (whether it's a collection or nullable etc.); pseudo-code:
42      defaults:
43          boolean isCollection =
44          boolean isNullable =
45
46         _lowerBound = (isNullable) ? 0 : 1;
47         _upperBound = (isCollection) ? Integer.MAX_VALUE : 1);
48     getElement/CollectionClass:
49         return (isCollection) ? element/collectionClass : null;
50     2. What is the default for Collection Class?
51  */

52
53 /**
54  *
55  * @author raccah
56  * @version %I%
57  */

58 public class RelationshipElementImpl extends PersistenceFieldElementImpl
59     implements RelationshipElement.Impl
60 {
61     /** Update action of the relationship element. */
62     private int _updateAction;
63
64     /** Delete action of the relationship element. */
65     private int _deleteAction;
66
67     /** Flag indicating whether this relationship element should prefetch. */
68     private boolean _isPrefetch;
69
70     /** Lower cardinality bound of the relationship element. */
71     private int _lowerBound;
72
73     /** Upper cardinality bound of the relationship element. */
74     private int _upperBound;
75
76     /** Collection lass of the relationship element. */
77     private String JavaDoc _collectionClass;
78
79     /** Element class of the relationship element. */
80     private String JavaDoc _elementClass;
81
82     /** Relative name of the inverse relationship. */
83     private String JavaDoc _inverseRelationshipName;
84
85     /** Create new RelationshipElementImpl with no corresponding name. This
86      * constructor should only be used for cloning and archiving.
87      */

88     public RelationshipElementImpl ()
89     {
90         this(null);
91     }
92
93     /** Creates new RelationshipElementImpl with the corresponding name
94      * @param name the name of the element
95      */

96     public RelationshipElementImpl (String JavaDoc name)
97     {
98         super(name);
99         _updateAction = RelationshipElement.NONE_ACTION;
100         _deleteAction = RelationshipElement.NONE_ACTION;
101         _isPrefetch = false;
102         _lowerBound = 0;
103         _upperBound = Integer.MAX_VALUE;
104     }
105
106     /** Get the update action for this relationship element.
107      * @return the update action, one of
108      * {@link RelationshipElement#NONE_ACTION},
109      * {@link RelationshipElement#NULLIFY_ACTION},
110      * {@link RelationshipElement#RESTRICT_ACTION},
111      * {@link RelationshipElement#CASCADE_ACTION}, or
112      * {@link RelationshipElement#AGGREGATE_ACTION}. The default is
113      * NONE_ACTION.
114      */

115     public int getUpdateAction () { return _updateAction; }
116
117     /** Set the update action for this relationship element.
118      * @param action - an integer indicating the update action, one of:
119      * {@link RelationshipElement#NONE_ACTION},
120      * {@link RelationshipElement#NULLIFY_ACTION},
121      * {@link RelationshipElement#RESTRICT_ACTION},
122      * {@link RelationshipElement#CASCADE_ACTION}, or
123      * {@link RelationshipElement#AGGREGATE_ACTION}
124      * @exception ModelException if impossible
125      */

126     public void setUpdateAction (int action) throws ModelException
127     {
128         Integer JavaDoc old = new Integer JavaDoc(getUpdateAction());
129         Integer JavaDoc newAction = new Integer JavaDoc(action);
130
131         try
132         {
133             fireVetoableChange(PROP_UPDATE_ACTION, old, newAction);
134             _updateAction = action;
135             firePropertyChange(PROP_UPDATE_ACTION, old, newAction);
136         }
137         catch (PropertyVetoException JavaDoc e)
138         {
139             throw new ModelVetoException(e);
140         }
141     }
142
143     /** Get the delete action for this relationship element.
144      * @return the delete action, one of
145      * {@link RelationshipElement#NONE_ACTION},
146      * {@link RelationshipElement#NULLIFY_ACTION},
147      * {@link RelationshipElement#RESTRICT_ACTION},
148      * {@link RelationshipElement#CASCADE_ACTION}, or
149      * {@link RelationshipElement#AGGREGATE_ACTION}. The default is
150      * NONE_ACTION.
151      */

152     public int getDeleteAction () { return _deleteAction; }
153
154     /** Set the delete action for this relationship element.
155      * @param action - an integer indicating the delete action, one of:
156      * {@link RelationshipElement#NONE_ACTION},
157      * {@link RelationshipElement#NULLIFY_ACTION},
158      * {@link RelationshipElement#RESTRICT_ACTION},
159      * {@link RelationshipElement#CASCADE_ACTION}, or
160      * {@link RelationshipElement#AGGREGATE_ACTION}
161      * @exception ModelException if impossible
162      */

163     public void setDeleteAction (int action) throws ModelException
164     {
165         Integer JavaDoc old = new Integer JavaDoc(getDeleteAction());
166         Integer JavaDoc newAction = new Integer JavaDoc(action);
167
168         try
169         {
170             fireVetoableChange(PROP_DELETE_ACTION, old, newAction);
171             _deleteAction = action;
172             firePropertyChange(PROP_DELETE_ACTION, old, newAction);
173         }
174         catch (PropertyVetoException JavaDoc e)
175         {
176             throw new ModelVetoException(e);
177         }
178     }
179
180     /** Determines whether this relationship element should prefetch or not.
181      * @return <code>true</code> if the relationship should prefetch,
182      * <code>false</code> otherwise. The default is <code>false</code>.
183      */

184     public boolean isPrefetch () { return _isPrefetch; }
185
186     /** Set whether this relationship element should prefetch or not.
187      * @param flag - if <code>true</code>, the relationship is set to
188      * prefetch; otherwise, it is not
189      * @exception ModelException if impossible
190      */

191     public void setPrefetch (boolean flag) throws ModelException
192     {
193         Boolean JavaDoc old = JavaTypeHelper.valueOf(isPrefetch());
194         Boolean JavaDoc newFlag = JavaTypeHelper.valueOf(flag);
195
196         try
197         {
198             fireVetoableChange(PROP_PREFETCH, old, newFlag);
199             _isPrefetch = flag;
200             firePropertyChange(PROP_PREFETCH, old, newFlag);
201         }
202         catch (PropertyVetoException JavaDoc e)
203         {
204             throw new ModelVetoException(e);
205         }
206     }
207
208     /** Get the lower cardinality bound for this relationship element. The
209      * default is 0.
210      * @return the lower cardinality bound
211      */

212     public int getLowerBound () { return _lowerBound; }
213
214     /** Set the lower cardinality bound for this relationship element.
215      * @param lowerBound - an integer indicating the lower cardinality bound
216      * @exception ModelException if impossible
217      */

218     public void setLowerBound (int lowerBound) throws ModelException
219     {
220         Integer JavaDoc old = new Integer JavaDoc(getLowerBound());
221         Integer JavaDoc newBound = new Integer JavaDoc(lowerBound);
222
223         try
224         {
225             fireVetoableChange(PROP_CARDINALITY, old, newBound);
226             _lowerBound = lowerBound;
227             firePropertyChange(PROP_CARDINALITY, old, newBound);
228         }
229         catch (PropertyVetoException JavaDoc e)
230         {
231             throw new ModelVetoException(e);
232         }
233     }
234
235     /** Get the upper cardinality bound for this relationship element. The
236      * default is Integer.MAX_VALUE.
237      * Returns {@link java.lang.Integer#MAX_VALUE} for <code>n</code>
238      * @return the upper cardinality bound
239      */

240     public int getUpperBound () { return _upperBound; }
241
242     /** Set the upper cardinality bound for this relationship element.
243      * @param upperBound - an integer indicating the upper cardinality bound
244      * (use {@link java.lang.Integer#MAX_VALUE} for <code>n</code>)
245      * @exception ModelException if impossible
246      */

247     public void setUpperBound (int upperBound) throws ModelException
248     {
249         Integer JavaDoc old = new Integer JavaDoc(getUpperBound());
250         Integer JavaDoc newBound = new Integer JavaDoc(upperBound);
251
252         try
253         {
254             fireVetoableChange(PROP_CARDINALITY, old, newBound);
255             _upperBound = upperBound;
256             firePropertyChange(PROP_CARDINALITY, old, newBound);
257         }
258         catch (PropertyVetoException JavaDoc e)
259         {
260             throw new ModelVetoException(e);
261         }
262     }
263
264     /** Get the collection class (for example Set, List, Vector, etc.)
265      * for this relationship element.
266      * @return the collection class
267      */

268     public String JavaDoc getCollectionClass () { return _collectionClass; }
269
270     /** Set the collection class for this relationship element.
271      * @param collectionClass - a string indicating the type of
272      * collection (for example Set, List, Vector, etc.)
273      * @exception ModelException if impossible
274      */

275     public void setCollectionClass (String JavaDoc collectionClass)
276         throws ModelException
277     {
278         String JavaDoc old = getCollectionClass();
279
280         try
281         {
282             fireVetoableChange(PROP_COLLECTION_CLASS, old, collectionClass);
283             _collectionClass = collectionClass;
284             firePropertyChange(PROP_COLLECTION_CLASS, old, collectionClass);
285         }
286         catch (PropertyVetoException JavaDoc e)
287         {
288             throw new ModelVetoException(e);
289         }
290     }
291
292     /** Get the element class for this relationship element. If primitive
293      * types are supported, you can use
294      * <code><i>wrapperclass</i>.TYPE.toString()</code> to specify them.
295      * @return the element class
296      */

297     public String JavaDoc getElementClass () { return _elementClass; }
298
299     /** Set the element class for this relationship element.
300      * @param elementClass - a string indicating the type of elements
301      * in the collection. If primitive types are supported, you can use
302      * <code><i>wrapperclass</i>.TYPE.toString()</code> to specify them.
303      * @exception ModelException if impossible
304      */

305     public void setElementClass (String JavaDoc elementClass) throws ModelException
306     {
307         String JavaDoc old = getElementClass();
308
309         try
310         {
311             fireVetoableChange(PROP_ELEMENT_CLASS, old, elementClass);
312             _elementClass = elementClass;
313             firePropertyChange(PROP_ELEMENT_CLASS, old, elementClass);
314         }
315         catch (PropertyVetoException JavaDoc e)
316         {
317             throw new ModelVetoException(e);
318         }
319     }
320
321     /** Get the relative name of the inverse relationship field for this
322      * relationship element. In the case of two-way relationships, the two
323      * relationship elements involved are inverses of each other. If this
324      * relationship element does not participate in a two-way relationship,
325      * this returns <code>null</code>. Note that it is possible to have this
326      * method return a value, but because of the combination of related class
327      * and lookup, there may be no corresponding RelationshipElement which can
328      * be found.
329      * @return the relative name of the inverse relationship element
330      * @see #getInverseRelationship
331      */

332     public String JavaDoc getInverseRelationshipName ()
333     {
334         return _inverseRelationshipName;
335     }
336
337     /** Changes the inverse relationship element for this relationship element.
338      * This method is invoked for both sides from
339      * {@link RelationshipElement#setInverseRelationship} and should handle the
340      * vetoable change events, property change events, and setting the internal
341      * variable.
342      * @param inverseRelationship - a relationship element to be used as the
343      * inverse for this relationship element or <code>null</code> if this
344      * relationship element does not participate in a two-way relationship.
345      * @exception ModelException if impossible
346      */

347     public void changeInverseRelationship (
348         RelationshipElement inverseRelationship) throws ModelException
349     {
350         String JavaDoc newName = ((inverseRelationship != null) ?
351             inverseRelationship.getName() : null);
352         String JavaDoc oldName = getInverseRelationshipName();
353
354         try
355         {
356             fireVetoableChange(PROP_INVERSE_FIELD, oldName, newName);
357             _inverseRelationshipName = newName;
358             firePropertyChange(PROP_INVERSE_FIELD, oldName, newName);
359         }
360         catch (PropertyVetoException JavaDoc e)
361         {
362             throw new ModelVetoException(e);
363         }
364     }
365 }
366
Popular Tags