1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.JDOField;
24 import org.apache.jdo.model.jdo.JDOMap;
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 * (the treatment of keys and values) of a map relationship field.
31 * This dynamic implementation only stores property values explicitly
32 * set by setter method.
33 *
34 * @author Michael Bouschen
35 * @since 1.1
36 * @version 1.1
37 */
38 public class JDOMapImplDynamic extends JDORelationshipImpl implements JDOMap {
39
40 /*** Property embeddedKey. */
41 protected Boolean embeddedKey;
42
43 /*** Property keyType. No default. */
44 protected transient JavaType keyType;
45
46 /*** Property keyTypeName. Defaults to java.lang.Object. */
47 private String keyTypeName = "java.lang.Object";
48
49 /*** Property embeddedValue. */
50 protected Boolean embeddedValue;
51
52 /*** Property valueType. No default. */
53 protected transient JavaType valueType;
54
55 /*** Property valueTypeName. Defaults to java.lang.Object. */
56 private String valueTypeName = "java.lang.Object";
57
58 /*** I18N support */
59 private final static I18NHelper msg =
60 I18NHelper.getInstance(JDOMapImplDynamic.class);
61
62 /***
63 * Determines whether the keys of the map should be stored if possible as
64 * part of the instance instead of as their own instances in the datastore.
65 * @return <code>true</code> if the keys are stored as part of this instance;
66 * <code>false</code> otherwise
67 */
68 public boolean isEmbeddedKey() {
69 if (embeddedKey != null) {
70
71 return embeddedKey.booleanValue();
72 }
73
74
75 JavaType type = getKeyType();
76 return (type != null) ?
77 TypeSupport.isEmbeddedElementType(type) : false;
78 }
79
80 /***
81 * Set whether the keys of the map should be stored if possible as part
82 * of the instance instead of as their own instances in the datastore.
83 * @param embeddedKey <code>true</code> if the keys are stored as part of
84 * this instance; <code>false</code> otherwise
85 */
86 public void setEmbeddedKey(boolean embeddedKey) {
87 this.embeddedKey = (embeddedKey ? Boolean.TRUE : Boolean.FALSE);
88 }
89
90 /***
91 * Get the type representation of the keys for this JDOMap.
92 * @return the type of the keys of this JDOMap
93 */
94 public JavaType getKeyType() {
95 if (keyType != null) {
96
97 return keyType;
98 }
99
100
101 JavaType type = null;
102 if (keyTypeName != null) {
103 JDOField jdoField = getDeclaringField();
104 JDOClass jdoClass = jdoField.getDeclaringClass();
105 JDOModel jdoModel = jdoClass.getDeclaringModel();
106 type = TypeSupport.resolveType(jdoModel, keyTypeName,
107 jdoClass.getPackagePrefix());
108 if (type == null) {
109 throw new ModelFatalException(
110 msg.msg("EXC_CannotResolveKeyType", keyTypeName,
111 jdoField.getName(), jdoClass.getName()));
112 }
113 }
114
115 return type;
116 }
117
118 /***
119 * Set the type representation of the keys for this JDOMap.
120 * @param keyType the type representation of the keys
121 */
122 public void setKeyType(JavaType keyType) {
123 this.keyType = keyType;
124 if (keyType != null) {
125 setKeyTypeName(keyType.getName());
126 }
127 }
128
129 /***
130 * Get the string representation of the type of the keys for this JDOMap.
131 * @return the key type as string
132 */
133 public String getKeyTypeName() {
134 return keyTypeName;
135 }
136
137 /***
138 * Set string representation of the type of the keys for this JDOMap.
139 * @param keyTypeName the name of the key type
140 */
141 public void setKeyTypeName(String keyTypeName) {
142 this.keyTypeName = keyTypeName;
143 }
144
145 /***
146 * Determines whether the values of the map should be stored if possible as
147 * part of the instance instead of as their own instances in the datastore.
148 * @return <code>true</code> if the values are stored as part of this
149 * instance; <code>false</code> otherwise
150 */
151 public boolean isEmbeddedValue() {
152 if (embeddedValue != null) {
153
154 return embeddedValue.booleanValue();
155 }
156
157
158 JavaType type = getValueType();
159 return (type != null) ?
160 TypeSupport.isEmbeddedElementType(type) : false;
161 }
162
163 /***
164 * Set whether the values of the map should be stored if possible as part
165 * of the instance instead of as their own instances in the datastore.
166 * @param embeddedValue <code>true</code> if the values are stored as part
167 * of this instance; <code>false</code> otherwise
168 */
169 public void setEmbeddedValue(boolean embeddedValue) {
170 this.embeddedValue = (embeddedValue ? Boolean.TRUE : Boolean.FALSE);
171 }
172
173 /***
174 * Get the type representation of the values for this JDOMap.
175 * @return the type of the values of this JDOMap
176 */
177 public JavaType getValueType() {
178 if (valueType != null) {
179
180 return valueType;
181 }
182
183
184 JavaType type = null;
185 if (valueTypeName != null) {
186 JDOField jdoField = getDeclaringField();
187 JDOClass jdoClass = jdoField.getDeclaringClass();
188 JDOModel jdoModel = jdoClass.getDeclaringModel();
189 type = TypeSupport.resolveType(jdoModel, valueTypeName,
190 jdoClass.getPackagePrefix());
191 if (type == null) {
192 throw new ModelFatalException(
193 msg.msg("EXC_CannotResolveValueType", valueTypeName,
194 jdoField.getName(), jdoClass.getName()));
195 }
196 }
197
198 return type;
199 }
200
201 /***
202 * Set the type representation of the values for this JDOMap.
203 * @param valueType the type representation of the values
204 */
205 public void setValueType(JavaType valueType) {
206 this.valueType = valueType;
207 if (valueType != null) {
208 setKeyTypeName(valueType.getName());
209 }
210 }
211
212 /***
213 * Get the string representation of the type of the values for this JDOMap.
214 * @return the key value as string
215 */
216 public String getValueTypeName() {
217 return valueTypeName;
218 }
219
220 /***
221 * Set string representation of the type of the values for this JDOMap.
222 * @param valueTypeName the name of the value type
223 */
224 public void setValueTypeName(String valueTypeName) {
225 this.valueTypeName = valueTypeName;
226 }
227
228 /***
229 * Determines whether this JDORelationship represents a map
230 * relationship or not. A return of <code>true</code> means this
231 * JDORelationship is a JDOMap instance.
232 * @return <code>true</code> if this JDORelationship represents a
233 * map relationship; <code>false</code> otherwise.
234 */
235 public boolean isJDOMap() {
236 return true;
237 }
238
239
240
241 /***
242 * Get the type representation of the relationship. This will be
243 * the JavaType for references, the element type for collections
244 * and arrays, and the value type for maps.
245 * @return the relationship type
246 */
247 public JavaType getRelatedJavaType() {
248 return getValueType();
249 }
250
251 }