View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software 
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License.
16   */
17  
18  package org.apache.jdo.impl.model.jdo.caching;
19  
20  import org.apache.jdo.model.ModelException;
21  import org.apache.jdo.model.java.JavaField;
22  import org.apache.jdo.model.jdo.JDOArray;
23  import org.apache.jdo.model.jdo.JDOClass;
24  import org.apache.jdo.model.jdo.JDOCollection;
25  import org.apache.jdo.model.jdo.JDOMap;
26  import org.apache.jdo.model.jdo.JDOReference;
27  import org.apache.jdo.model.jdo.JDORelationship;
28  import org.apache.jdo.model.jdo.PersistenceModifier;
29  import org.apache.jdo.impl.model.jdo.JDOFieldImplDynamic;
30  
31  /***
32   * An instance of this class represents the JDO metadata of a managed
33   * field of a persistence capable class. This caching implementation
34   * caches any calulated value to avoid re-calculating it if it is
35   * requested again. 
36   * <p>
37   * Please note, this implementation does not support
38   * changing the relationship property once it is defined (either
39   * explicitly by the setter or internally calculated by the
40   * getter). The second attempt to define the relationship will result
41   * in an exception.
42   *
43   * @author Michael Bouschen
44   * @since 1.1
45   * @version 2.0
46   */
47  public class JDOFieldImplCaching extends JDOFieldImplDynamic {
48  
49      /*** Relative field number. */
50      private int relativeFieldNumber = -1;
51  
52      /*** Constructor. */
53      protected JDOFieldImplCaching(String name, JDOClass declaringClass) {
54          super(name, declaringClass);
55      }
56  
57      /***
58       * Get the persistence modifier of this JDOField.
59       * @return the persistence modifier, one of 
60       * {@link PersistenceModifier#NONE}, 
61       * {@link PersistenceModifier#PERSISTENT},
62       * {@link PersistenceModifier#TRANSACTIONAL}, or
63       * {@link PersistenceModifier#POSSIBLY_PERSISTENT}.
64       */
65      public int getPersistenceModifier() {
66          if (persistenceModifier == PersistenceModifier.UNSPECIFIED) {
67              persistenceModifier = super.getPersistenceModifier();
68          }
69          return persistenceModifier;
70      }
71  
72      /***
73       * Returns the relative field number of this JDOField.
74       * @return the relative field number
75       */
76      public int getRelativeFieldNumber() {
77          ((JDOClassImplCaching)getDeclaringClass()).calculateFieldNumbers();
78          return relativeFieldNumber;
79      }
80  
81      /***
82       * Determines whether this JDOField is part of the default fetch group or 
83       * not.
84       * @return <code>true</code> if the field is part of the default fetch 
85       * group, <code>false</code> otherwise
86       */
87      public boolean isDefaultFetchGroup() {
88          if (defaultFetchGroup == null) {
89              defaultFetchGroup = 
90                  super.isDefaultFetchGroup() ? Boolean.TRUE : Boolean.FALSE;
91          }
92          return (defaultFetchGroup == null) ? 
93              false : defaultFetchGroup.booleanValue();
94      }
95  
96      /***
97       * Determines whether the field should be stored if possible as part of
98       * the instance instead of as its own instance in the datastore.
99       * @return <code>true</code> if the field is stored as part of the instance;
100      * <code>false</code> otherwise
101      */
102     public boolean isEmbedded() {
103         if (embedded == null) {
104             embedded = super.isEmbedded() ? Boolean.TRUE : Boolean.FALSE;
105         }
106         return (embedded == null) ? false : embedded.booleanValue();
107     }
108     
109     /***
110      * Get the corresponding JavaField representation for this JDOField.
111      * @return the corresponding Java field representation
112      */
113     public JavaField getJavaField() {
114         if (javaField == null) {
115             javaField = super.getJavaField();
116         }
117         return javaField;
118     }
119 
120     /***
121      * Get the relationship information for this JDOField. The method 
122      * returns null if the field is not part of a relationship 
123      * (e.g. it is a primitive type field).
124      * @return relationship info of this JDOField or <code>null</code> if 
125      * this JDOField is not a relationship
126      */
127     public JDORelationship getRelationship() {
128         if (relationship == null) {
129             relationship = super.getRelationship();
130         }
131         return relationship;
132     }
133     
134     /***
135      * Creates and returns a new JDOReference instance. 
136      * This method automatically binds the new JDOReference to this JDOField. 
137      * It throws a ModelException, if this JDOField is already bound to 
138      * another JDORelationship instance. Otherwise the following holds true:
139      * <ul>
140      * <li> Method {@link #getRelationship} returns the new created instance
141      * <li> <code>this.getRelationship().getDeclaringField() == this</code>
142      * </ul> 
143      * @return a new JDOReference instance bound to this JDOField
144      * @exception ModelException if impossible
145      */
146     public JDOReference createJDOReference() throws ModelException {
147         if (relationship != null)
148             throw new ModelException(
149                 msg.msg("EXC_RelationshipAlreadyDefined", //NOI18N
150                         getName(), relationship));
151         return super.createJDOReference();
152     }
153 
154     /***
155      * Creates and returns a new JDOCollection instance. 
156      * This method automatically binds the new JDOCollection to this JDOField. 
157      * It throws a ModelException, if this JDOField is already bound to 
158      * another JDORelationship instance. Otherwise the following holds true:
159      * <ul>
160      * <li> Method {@link #getRelationship} returns the new created instance
161      * <li> <code>this.getRelationship().getDeclaringField() == this</code>
162      * </ul> 
163      * @return a new JDOCollection instance bound to this JDOField
164      * @exception ModelException if impossible
165      */
166     public JDOCollection createJDOCollection() throws ModelException {
167         if (relationship != null)
168             throw new ModelException(
169                 msg.msg("EXC_RelationshipAlreadyDefined", //NOI18N
170                         getName(), relationship));
171         return super.createJDOCollection();
172     }
173 
174     /***
175      * Creates and returns a new JDOArray instance. 
176      * This method automatically binds the new JDOArray to this JDOField. 
177      * It throws a ModelException, if this JDOField is already bound to 
178      * another JDORelationship instance. Otherwise the following holds true:
179      * <ul>
180      * <li> Method {@link #getRelationship} returns the new created instance
181      * <li> <code>this.getRelationship().getDeclaringField() == this</code>
182      * </ul> 
183      * @return a new JDOArray instance bound to this JDOField
184      * @exception ModelException if impossible
185      */
186     public JDOArray createJDOArray() throws ModelException {
187         if (relationship != null)
188             throw new ModelException(
189                 msg.msg("EXC_RelationshipAlreadyDefined", //NOI18N
190                         getName(), relationship));
191         return super.createJDOArray();
192     }
193 
194     /***
195      * Creates and returns a new JDOMap instance. 
196      * This method automatically binds the new JDOMap to this JDOField. 
197      * It throws a ModelException, if this JDOField is already bound to 
198      * another JDORelationship instance. Otherwise the following holds true:
199      * <ul>
200      * <li> Method {@link #getRelationship} returns the new created instance
201      * <li> <code>this.getRelationship().getDeclaringField() == this</code>
202      * </ul> 
203      * @return a new JDOMap instance bound to this JDOField
204      * @exception ModelException if impossible
205      */
206     public JDOMap createJDOMap() throws ModelException {
207         if (relationship != null)
208             throw new ModelException(
209                 msg.msg("EXC_RelationshipAlreadyDefined", //NOI18N
210                         getName(), relationship));
211         return super.createJDOMap();
212     }
213 
214     //========= Internal helper methods ==========
215 
216     /***
217      * Creates and returns a new JDOReference instance. 
218      * This method automatically sets this JDOField as the declaring field of 
219      * the returned instance.
220      * @return a new JDOReference instance bound to this JDOField
221      */
222     protected JDOReference createJDOReferenceInternal() {
223         JDOReferenceImplCaching ref = new JDOReferenceImplCaching();
224         // update relationship JDORelationship->JDOField
225         ref.setDeclaringField(this);
226         return ref;
227     }
228 
229     /***
230      * Creates and returns a new JDOCollection instance. 
231      * This method automatically this JDOField as the declarinmg field of 
232      * the returned instance.
233      * @return a new JDOCollection instance bound to this JDOField
234      */
235     protected JDOCollection createJDOCollectionInternal() {
236         JDOCollectionImplCaching collection = new JDOCollectionImplCaching();
237         // update relationship JDORelationship->JDOField
238         collection.setDeclaringField(this);
239         return collection;
240     }
241 
242     /***
243      * Creates and returns a new JDOArray instance. 
244      * This method automatically this JDOField as the declarinmg field of 
245      * the returned instance.
246      * @return a new JDOArray instance bound to this JDOField
247      */
248     protected JDOArray createJDOArrayInternal() {
249         JDOArrayImplCaching array = new JDOArrayImplCaching();
250         // update relationship JDORelationship->JDOField
251         array.setDeclaringField(this);
252         return array;
253     }
254 
255     /***
256      * Creates and returns a new JDOMap instance. 
257      * This method automatically this JDOField as the declarinmg field of 
258      * the returned instance.
259      * @return a new JDOMap instance bound to this JDOField
260      */
261     protected JDOMap createJDOMapInternal() {
262         JDOMapImplCaching map = new JDOMapImplCaching();
263         // update relationship JDORelationship->JDOField
264         map.setDeclaringField(this);
265         return map;
266     }
267 
268     /***
269      * Sets the relative field number of this JDOField.
270      * @param number the relative field number
271      */
272     void setRelativeFieldNumber(int number) {
273         this.relativeFieldNumber = number;
274     }
275 }