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