View Javadoc

1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software 
11   * distributed under the License is distributed on an "AS IS" BASIS, 
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  
17  package org.apache.jdo.impl.model.jdo;
18  
19  import org.apache.jdo.model.ModelException;
20  import org.apache.jdo.model.java.JavaField;
21  import org.apache.jdo.model.java.JavaProperty;
22  import org.apache.jdo.model.java.JavaType;
23  import org.apache.jdo.model.jdo.JDOArray;
24  import org.apache.jdo.model.jdo.JDOClass;
25  import org.apache.jdo.model.jdo.JDOCollection;
26  import org.apache.jdo.model.jdo.JDOField;
27  import org.apache.jdo.model.jdo.JDOMap;
28  import org.apache.jdo.model.jdo.JDOProperty;
29  import org.apache.jdo.model.jdo.JDOReference;
30  import org.apache.jdo.model.jdo.JDORelationship;
31  import org.apache.jdo.util.I18NHelper;
32  
33  /***
34   * An instance of this class represents the JDO metadata of a managed property
35   * of a persistence capable class. This JDOProperty implementation is used for
36   * persistent properties with an associated JDOField. All JDOField getter
37   * methods delegate to the associated JDOField, except methods getName,
38   * getDeclaringClass and getJavaField. All JDOField setter method throw a
39   * ModelException to avoid changing the associated JDOField through this
40   * JDOProperty instance. This dynamic implementation only stores values
41   * explicitly set by setter method. 
42   *
43   * @author Michael Bouschen
44   * @since 2.0
45   * @version 2.0
46   */
47  public class JDOAssociatedPropertyImplDynamic
48      extends JDOMemberImpl 
49      implements JDOProperty
50  {
51      /*** The associated JDOField instance. */
52      private final JDOField associatedJDOField;
53  
54      /*** The corresponding JavaProperty instance. */
55      protected JavaProperty javaProperty;
56  
57     /*** I18N support. */
58      protected final static I18NHelper msg =  
59          I18NHelper.getInstance(JDOAssociatedPropertyImplDynamic.class);
60      
61      /*** Constrcutor. */
62      protected JDOAssociatedPropertyImplDynamic(
63          String name, JDOClass declaringClass, JDOField associatedJDOField)
64          throws ModelException {
65          super(name, declaringClass);
66          this.associatedJDOField = associatedJDOField;
67          if (associatedJDOField == null) {
68              throw new ModelException(
69                  msg.msg("EXC_InvalidNullAssociatedJDOField")); //NOI18N
70          }
71      }
72      
73      // ===== Methods specified in JDOField =====
74  
75      /***
76       * Get the corresponding JavaProperty representation for this JDOProperty.
77       * @return the corresponding JavaProperty representation
78       */
79      public JavaField getJavaField() {
80          if (javaProperty != null) {
81              // return javaProperty, if explicitly set by the setter
82              return javaProperty;
83          }
84          
85          // not set => calculate
86          JavaType javaType = getDeclaringClass().getJavaType();
87          return javaType.getJavaProperty(getName());
88      }
89  
90      /***
91       * Sets the corresponding JavaProperty representation for this JDOProperty.
92       * @param javaField the corresponding JavaProperty representation
93       * @throws ModelException if impossible
94       */
95      public void setJavaField(JavaField javaField) throws ModelException {
96          
97          if (javaField instanceof JavaProperty) {
98              this.javaProperty = (JavaProperty)javaField;
99          }
100         else {
101             throw new ModelException(msg.msg(
102                 "EXC_InvalidJavaFieldForJDOProperty", javaField)); //NOI18N
103         }
104     }
105 
106     /***
107      * Convenience method to check whether this field represents a property.
108      * @return <code>true</code> if this field represents a property; 
109      * <code>false</code> otherwise
110      */
111     public boolean isProperty() {
112         return true;
113     }
114 
115     // ===== Methods specified in JDOField delegate to associatedJDOField =====
116 
117     /*** Deletegate to associatedJDOField. */
118     public int getPersistenceModifier() {
119         return associatedJDOField.getPersistenceModifier();
120     }
121 
122     /*** Throws ModelException. */
123     public void setPersistenceModifier (int persistenceModifier)
124         throws ModelException {
125         throw new ModelException(
126             msg.msg("EXC_CannotModifyJDOProperty")); //NOI18N
127     }
128     
129     /*** Deletegate to associatedJDOField. */
130     public boolean isPrimaryKey() {
131         return associatedJDOField.isPrimaryKey();
132     }
133     
134     /*** Throws ModelException. */
135     public void setPrimaryKey(boolean primaryKey)
136         throws ModelException {
137         throw new ModelException(
138             msg.msg("EXC_CannotModifyJDOProperty")); //NOI18N
139     }
140     
141     /*** Deletegate to associatedJDOField. */
142     public int getNullValueTreatment() {
143         return associatedJDOField.getNullValueTreatment();
144     }
145     
146     /*** Throws ModelException. */
147     public void setNullValueTreatment(int nullValueTreament)
148         throws ModelException {
149         throw new ModelException(
150             msg.msg("EXC_CannotModifyJDOProperty")); //NOI18N
151     }
152     
153     /*** Deletegate to associatedJDOField. */
154     public boolean isDefaultFetchGroup() {
155         return associatedJDOField.isDefaultFetchGroup();
156     }
157 
158     /*** Throws ModelException. */
159     public void setDefaultFetchGroup(boolean defaultFetchGroup)
160         throws ModelException {
161         throw new ModelException(
162             msg.msg("EXC_CannotModifyJDOProperty")); //NOI18N
163     }
164 
165     /*** Deletegate to associatedJDOField. */
166     public boolean isEmbedded() {
167         return associatedJDOField.isEmbedded();
168     }
169     
170     /*** Throws ModelException. */
171     public void setEmbedded(boolean embedded)
172         throws ModelException {
173         throw new ModelException(
174             msg.msg("EXC_CannotModifyJDOProperty")); //NOI18N
175     }
176 
177     /*** Deletegate to associatedJDOField. */
178     public boolean isSerializable() {
179         return associatedJDOField.isSerializable();
180     }
181 
182     /*** Throws ModelException. */
183     public void setSerializable(boolean serializable)
184         throws ModelException {
185         throw new ModelException(
186             msg.msg("EXC_CannotModifyJDOProperty")); //NOI18N
187     }
188 
189     /*** Deletegate to associatedJDOField. */
190     public String getMappedByName() {
191         return associatedJDOField.getMappedByName();
192     }
193 
194     /*** Throws ModelException. */
195     public void setMappedByName(String mappedByName)
196         throws ModelException {
197         throw new ModelException (
198             msg.msg("EXC_CannotModifyJDOProperty")); //NOI18N
199     }
200 
201     /*** Deletegate to associatedJDOField. */
202     public JDORelationship getRelationship() {
203         return associatedJDOField.getRelationship();
204     }
205 
206     /*** Throws ModelException. */
207     public void setRelationship(JDORelationship relationship)
208         throws ModelException {
209         throw new ModelException(
210             msg.msg("EXC_CannotModifyJDOProperty")); //NOI18N
211     }
212 
213     /*** Throws ModelException. */
214     public JDOReference createJDOReference()
215         throws ModelException {
216         throw new ModelException(
217             msg.msg("EXC_CannotModifyJDOProperty")); //NOI18N
218     }
219 
220     /*** Throws ModelException. */
221     public JDOCollection createJDOCollection()
222         throws ModelException {
223         throw new ModelException(
224             msg.msg("EXC_CannotModifyJDOProperty")); //NOI18N
225     }
226 
227     /*** Throws ModelException. */
228     public JDOArray createJDOArray()
229         throws ModelException {
230         throw new ModelException(
231             msg.msg("EXC_CannotModifyJDOProperty")); //NOI18N
232     }
233     
234     /*** Throws ModelException. */
235     public JDOMap createJDOMap()
236         throws ModelException {
237         throw new ModelException(
238             msg.msg("EXC_CannotModifyJDOProperty")); //NOI18N
239     }
240     
241     /*** Deletegate to associatedJDOField. */
242     public boolean isPersistent() {
243         return associatedJDOField.isPersistent();
244     }
245     
246     /*** Deletegate to associatedJDOField. */
247     public boolean isTransactional() {
248         return associatedJDOField.isTransactional();
249     }
250 
251     /*** Deletegate to associatedJDOField. */
252     public boolean isManaged() {
253         return associatedJDOField.isManaged();
254     }
255 
256     /*** Deletegate to associatedJDOField. */
257     public boolean isRelationship() {
258         return associatedJDOField.isRelationship();
259     }
260 
261     /*** Deletegate to associatedJDOField. */
262     public JavaType getType() {
263         return associatedJDOField.getType();
264     }
265 
266     /*** Deletegate to associatedJDOField. */
267     public int getFieldNumber() {
268         return associatedJDOField.getFieldNumber();
269     }
270 
271     /*** Deletegate to associatedJDOField. */
272     public int getRelativeFieldNumber() {
273         return associatedJDOField.getRelativeFieldNumber();
274     }
275 
276     // ===== Methods specified in JDOProperty =====
277 
278     /*** 
279      * Return the JDOField instance associated with this property, if
280      * available. If there is no JDOField instance associated, then the method
281      * returns <code>null</code>.
282      * @return associated JDOField instance or <code>null</code> if not
283      * available.
284      */
285     public JDOField getAssociatedJDOField() {
286         return associatedJDOField;
287     }
288 }