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.impl.model.jdo.util.TypeSupport;
20  import org.apache.jdo.model.ModelFatalException;
21  import org.apache.jdo.model.java.JavaType;
22  import org.apache.jdo.model.jdo.JDOClass;
23  import org.apache.jdo.model.jdo.JDOCollection;
24  import org.apache.jdo.model.jdo.JDOField;
25  import org.apache.jdo.model.jdo.JDOModel;
26  import org.apache.jdo.util.I18NHelper;
27  
28  /***
29   * An instance of this class represents the JDO relationship metadata 
30   * of a collection relationship field. This dynamic implementation only
31   * stores property values explicitly set by setter method. 
32   *
33   * @author Michael Bouschen
34   * @since 1.1
35   * @version 1.1
36   */
37  public class JDOCollectionImplDynamic extends JDORelationshipImpl
38      implements JDOCollection {
39      
40      /*** Property embeddedElement. */
41      protected Boolean embeddedElement;
42  
43      /*** Property elementType. */
44      protected transient JavaType elementType;
45  
46      /*** Property elementTypeName. Defaults to java.lang.Object. */
47      private String elementTypeName = "java.lang.Object"; //NOI18N
48  
49      /*** I18N support */
50      private final static I18NHelper msg =  
51          I18NHelper.getInstance(JDOCollectionImplDynamic.class);
52      
53      /***
54       * Determines whether the values of the elements should be stored if 
55       * possible as part of the instance instead of as their own instances 
56       * in the datastore.
57       * @return <code>true</code> if the elements should be stored as part of 
58       * the instance; <code>false</code> otherwise
59       */
60      public boolean isEmbeddedElement() {
61          if (embeddedElement != null) {
62              // return embeddedElement, if explicitly set by the setter
63              return embeddedElement.booleanValue();
64          }
65          
66          // not set => calculate
67          JavaType type = getElementType();
68          return (type != null) ? 
69              TypeSupport.isEmbeddedElementType(type) : false;
70      }
71      
72      /***
73       * Set whether the values of the elements should be stored if possible as 
74       * part of the instance instead of as their own instances in the datastore.
75       * @param embeddedElement <code>true</code> if elements should be stored 
76       * as part of the instance
77       */
78      public void setEmbeddedElement(boolean embeddedElement) {
79          this.embeddedElement = (embeddedElement ? Boolean.TRUE : Boolean.FALSE);
80      }
81  
82      /*** 
83       * Get the type representation of the collection elements. 
84       * @return the element type
85       */
86      public JavaType getElementType() {
87          if (elementType != null) {
88              // return elementType, if explicitly set by the setter
89              return elementType;
90          }
91      
92          // not set => calculate
93          JavaType type = null;
94          if (elementTypeName != null) {
95              JDOField jdoField = getDeclaringField();
96              JDOClass jdoClass = jdoField.getDeclaringClass();
97              JDOModel jdoModel = jdoClass.getDeclaringModel();
98              type = TypeSupport.resolveType(jdoModel, elementTypeName,
99                                             jdoClass.getPackagePrefix());
100             if (type == null) {
101                 throw new ModelFatalException(
102                     msg.msg("EXC_CannotResolveElementType", elementTypeName, //NOI18N
103                             jdoField.getName(), jdoClass.getName())); //NOI18N
104             }
105         }
106         
107         return type;
108     }
109 
110     /*** 
111      * Set the type representation of the collection elements.
112      * @param elementType the type representation of the collection elements
113      */
114     public void setElementType(JavaType elementType) {
115         this.elementType = elementType;
116         if (elementType != null) {
117             setElementTypeName(elementType.getName());
118         }
119     }
120 
121     /*** 
122      * Get the type of collection elements as string.
123      * @return the element type as string
124      */
125     public String getElementTypeName() {
126         return elementTypeName;
127     }
128 
129     /*** 
130      * Set string representation of the type of collection elements.
131      * @param elementTypeName a string representation of the type of elements in
132      * the collection. 
133      */
134     public void setElementTypeName(String elementTypeName) {
135         this.elementTypeName = elementTypeName;
136     }
137 
138     /***
139      * Determines whether this JDORelationship represents a collection
140      * relationship or not. A return of <code>true</code> means this
141      * JDORelationship is a JDOCollection instance.
142      * @return <code>true</code> if this JDORelationship represents a
143      * collection relationship; <code>false</code> otherwise.
144      */
145     public boolean isJDOCollection() {
146         return true;
147     }
148 
149     //========= Internal helper methods ==========
150 
151     /*** 
152      * Get the type representation of the relationship. This will be 
153      * the JavaType for references, the element type for collections
154      * and arrays, and the value type for maps.
155      * @return the relationship type
156      */
157     public JavaType getRelatedJavaType() {
158         return getElementType();
159     }
160 
161 }