KickJava   Java API By Example, From Geeks To Geeks.

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


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  * PersistenceClassElementImpl.java
26  *
27  * Created on March 2, 2000, 5:33 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.jdo.*;
35 import com.sun.jdo.api.persistence.model.ModelException;
36 import com.sun.jdo.api.persistence.model.ModelVetoException;
37 import com.sun.jdo.spi.persistence.utility.JavaTypeHelper;
38
39 /**
40  *
41  * @author raccah
42  * @version %I%
43  */

44 public class PersistenceClassElementImpl extends PersistenceElementImpl
45     implements PersistenceClassElement.Impl
46 {
47     /** Flag used to keep track of changes to this class element. */
48     private boolean _isModified;
49
50     /** Object identity type of the class element. */
51     private int _objectIdentityType;
52
53     /** Primary key class of the class element. */
54     private String JavaDoc _keyClass;
55
56     /** Fields of the class element. */
57     private PersistenceElementCollection _fields;
58
59     /** Concurrency groups of the class element. */
60     private PersistenceElementCollection _groups;
61
62     /** Create new PersistenceClassElementImpl with no corresponding name.
63      * This constructor should only be used for cloning and archiving.
64      */

65     public PersistenceClassElementImpl ()
66     {
67         this(null);
68     }
69
70     /** Creates new PersistenceClassElementImpl with the corresponding name
71      * @param name the name of the element
72      */

73     public PersistenceClassElementImpl (String JavaDoc name)
74     {
75         super(name);
76
77         if (name != null)
78             _keyClass = name + ".Oid"; // NOI18N
79

80         _objectIdentityType = PersistenceClassElement.APPLICATION_IDENTITY;
81         _fields = new PersistenceElementCollection(this, PROP_FIELDS,
82             new PersistenceFieldElement[0]);
83         _groups = new PersistenceElementCollection(this, PROP_GROUPS,
84             new ConcurrencyGroupElement[0]);
85     }
86
87     /** Fires property change event. This method overrides that of
88      * PersistenceElementImpl to update the persistence class element's
89      * modified status.
90      * @param name property name
91      * @param o old value
92      * @param n new value
93      */

94     protected final void firePropertyChange (String JavaDoc name, Object JavaDoc o, Object JavaDoc n)
95     {
96         // even though o == null and n == null will signify a change, that
97
// is consistent with PropertyChangeSupport's behavior and is
98
// necessary for this to work
99
boolean noChange = ((o != null) && (n != null) && o.equals(n));
100
101         super.firePropertyChange(name, o, n);
102
103         if (!(PROP_MODIFIED.equals(name)) && !noChange)
104             setModified(true);
105     }
106
107     /** Fires vetoable change event. This method overrides that of
108      * PersistenceElementImpl to give listeners a chance to block
109      * changes on the persistence class element modified status.
110      * @param name property name
111      * @param o old value
112      * @param n new value
113      * @exception PropertyVetoException when the change is vetoed by a listener
114      */

115     protected final void fireVetoableChange (String JavaDoc name, Object JavaDoc o, Object JavaDoc n)
116         throws PropertyVetoException JavaDoc
117     {
118         // even though o == null and n == null will signify a change, that
119
// is consistent with PropertyChangeSupport's behavior and is
120
// necessary for this to work
121
boolean noChange = ((o != null) && (n != null) && o.equals(n));
122
123         super.fireVetoableChange(name, o, n);
124
125         if (!(PROP_MODIFIED.equals(name)) && !noChange)
126             fireVetoableChange(PROP_MODIFIED, Boolean.FALSE, Boolean.TRUE);
127     }
128
129     /** Gets the modified flag for this persistence class.
130      * @return <code>true</code> if there have been (property) changes to this
131      * class, <code>false</code> otherwise.
132      */

133     public boolean isModified () { return _isModified; }
134
135     /** Set the modified flag for this persistence class to flag. This is
136      * usually set to <code>true</code> by property changes and
137      * <code>false</code> after a save.
138      * @param flag if <code>true</code>, this class is marked as modified;
139      * if <code>false</code>, it is marked as unmodified.
140      */

141     public void setModified (boolean flag)
142     {
143         boolean oldFlag = isModified();
144
145         if (flag != oldFlag)
146         {
147             _isModified = flag;
148             firePropertyChange(PROP_MODIFIED, JavaTypeHelper.valueOf(oldFlag),
149                 JavaTypeHelper.valueOf(flag));
150         }
151     }
152
153     /** Get the object identity type of this class element.
154      * @return the object identity type, one of
155      * {@link PersistenceClassElement#APPLICATION_IDENTITY},
156      * {@link PersistenceClassElement#DATABASE_IDENTITY}, or
157      * {@link PersistenceClassElement#UNMANAGED_IDENTITY}. The default is
158      * APPLICATION_IDENTITY.
159      */

160     public int getObjectIdentityType ()
161     {
162         return _objectIdentityType;
163     }
164
165     /** Set the object identity type of this class element.
166      * @param type - an integer indicating the object identity type, one of:
167      * {@link PersistenceClassElement#APPLICATION_IDENTITY},
168      * {@link PersistenceClassElement#DATABASE_IDENTITY}, or
169      * {@link PersistenceClassElement#UNMANAGED_IDENTITY}
170      * @exception ModelException if impossible
171      */

172     public void setObjectIdentityType (int type) throws ModelException
173     {
174         Integer JavaDoc old = new Integer JavaDoc(getObjectIdentityType());
175         Integer JavaDoc newType = new Integer JavaDoc(type);
176
177         try
178         {
179             fireVetoableChange(PROP_IDENTITY, old, newType);
180             _objectIdentityType = type;
181             firePropertyChange(PROP_IDENTITY, old, newType);
182         }
183         catch (PropertyVetoException JavaDoc e)
184         {
185             throw new ModelVetoException(e);
186         }
187     }
188
189
190     /** Get the fully qualified name of the primary key class for this class
191      * element. This value is only used if <code>getObjectIdentityType</code>
192      * returns <code>APPLICATION_IDENTITY</code>
193      * @return the fully qualified key class name, <code>null</code> if the
194      * identity type is not managed by the application
195      * @see #setObjectIdentityType
196      * @see PersistenceClassElement#APPLICATION_IDENTITY
197      *
198      */

199     public String JavaDoc getKeyClass ()
200     {
201         return ((PersistenceClassElement.APPLICATION_IDENTITY ==
202             getObjectIdentityType()) ? _keyClass : null);
203     }
204
205     /** Set the primary key class for this class element.
206      * @param name - the fully qualified name which represents the primary key
207      * class. This value is only used if <code>getObjectIdentityType</code>
208      * returns <code>APPLICATION_IDENTITY</code>
209      * @exception ModelException if impossible
210      * @see #setObjectIdentityType
211      * @see PersistenceClassElement#APPLICATION_IDENTITY
212      */

213     public void setKeyClass (String JavaDoc name) throws ModelException
214     {
215         String JavaDoc old = getKeyClass();
216         
217         try
218         {
219             fireVetoableChange(PROP_KEY_CLASS, old, name);
220             _keyClass = name;
221             firePropertyChange(PROP_KEY_CLASS, old, name);
222         }
223         catch (PropertyVetoException JavaDoc e)
224         {
225             throw new ModelVetoException(e);
226         }
227     }
228
229     //================== Fields ===============================
230

231     /** Change the set of fields.
232      * @param fields the new fields
233      * @param action {@link #ADD}, {@link #REMOVE}, or {@link #SET}
234      * @exception ModelException if impossible
235      */

236     public void changeFields (PersistenceFieldElement[] fields, int action)
237         throws ModelException
238     {
239         _fields.changeElements(fields, action);
240     }
241
242     /** Get all fields.
243      * @return the fields
244      */

245     public PersistenceFieldElement[] getFields ()
246     {
247         return (PersistenceFieldElement[])_fields.getElements();
248     }
249
250     /** Find a field by name.
251      * @param name the name to match
252      * @return the field, or <code>null</code> if it does not exist
253      */

254     public PersistenceFieldElement getField (String JavaDoc name)
255     {
256         return (PersistenceFieldElement)_fields.getElement(name);
257     }
258
259     //================== ConcurrencyGroups ===============================
260

261     /** Change the set of concurrency groups.
262      * @param groups the new concurrency groups
263      * @param action {@link #ADD}, {@link #REMOVE}, or {@link #SET}
264      * @exception ModelException if impossible
265      */

266     public void changeConcurrencyGroups (ConcurrencyGroupElement[] groups,
267         int action) throws ModelException
268     {
269         _groups.changeElements(groups, action);
270     }
271
272     /** Get all concurrency groups.
273      * @return the concurrency groups
274      */

275     public ConcurrencyGroupElement[] getConcurrencyGroups ()
276     {
277         return (ConcurrencyGroupElement[])_groups.getElements();
278     }
279
280     /** Find a concurrency group by name.
281      * @param name the name to match
282      * @return the concurrency group, or <code>null</code> if it does not exist
283      */

284     public ConcurrencyGroupElement getConcurrencyGroup (String JavaDoc name)
285     {
286         return (ConcurrencyGroupElement)_groups.getElement(name);
287     }
288
289     //=============== extra methods needed for xml archiver ==============
290

291     /** Returns the field collection of this class element. This method
292      * should only be used internally and for cloning and archiving.
293      * @return the field collection of this class element
294      */

295     public PersistenceElementCollection getFieldCollection ()
296     {
297         return _fields;
298     }
299
300     /** Set the field collection of this class element to the supplied
301      * collection. This method should only be used internally and for
302      * cloning and archiving.
303      * @param collection the field collection of this class element
304      */

305     public void setFieldCollection (PersistenceElementCollection collection)
306     {
307         _fields = collection;
308     }
309
310     /** Returns the concurrency group collection of this class element.
311      * This method should only be used internally and for cloning and
312      * archiving.
313      * @return the concurrency group collection of this class element
314      */

315     public PersistenceElementCollection getGroupCollection ()
316     {
317         return _groups;
318     }
319
320     /** Set the concurrency group collection of this class element to the
321      * supplied collection. This method should only be used internally
322      * and for cloning and archiving.
323      * @param collection the concurrency group collection of this class element
324      */

325     public void setGroupCollection (PersistenceElementCollection collection)
326     {
327         _groups = collection;
328     }
329 }
330
Popular Tags