KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > model > MapMetaData


1 /*
2  * Copyright 2002 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: MapMetaData.java,v 1.6 2004/01/25 20:44:32 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.model;
12
13 import java.util.List JavaDoc;
14 import java.util.Set JavaDoc;
15 import javax.jdo.JDOFatalInternalException;
16 import org.w3c.dom.Element JavaDoc;
17
18
19 public class MapMetaData extends MetaData
20 {
21     protected final Class JavaDoc keyType;
22     protected final Class JavaDoc valueType;
23     protected final boolean embeddedKey;
24     protected final boolean embeddedValue;
25     protected final boolean allowsNullValues;
26
27     public MapMetaData(FieldMetaData fmd, Element JavaDoc elem)
28     {
29         super(fmd.getClassMetaData().getSourceURL(), elem);
30
31         String JavaDoc typeAttr = elem.getAttribute("key-type");
32         String JavaDoc embeddedAttr = elem.getAttribute("embedded-key");
33
34         keyType = getReferencedType(typeAttr, fmd.getClassMetaData());
35
36         if (embeddedAttr.length() > 0)
37             embeddedKey = Boolean.valueOf(embeddedAttr).booleanValue();
38         else
39             embeddedKey = Types.isDefaultEmbeddedType(keyType);
40
41         typeAttr = elem.getAttribute("value-type");
42         embeddedAttr = elem.getAttribute("embedded-value");
43
44         valueType = getReferencedType(typeAttr, fmd.getClassMetaData());
45
46         if (embeddedAttr.length() > 0)
47             embeddedValue = Boolean.valueOf(embeddedAttr).booleanValue();
48         else
49             embeddedValue = Types.isDefaultEmbeddedType(valueType);
50
51         if (fmd.getType() == java.util.Hashtable JavaDoc.class)
52             allowsNullValues = false;
53         else
54             allowsNullValues = true;
55     }
56
57     public String JavaDoc getJavaName()
58     {
59         throw new JDOFatalInternalException("MapMetaData has no equivalent Java identifier");
60     }
61
62     public Class JavaDoc getKeyType()
63     {
64         return keyType;
65     }
66
67     public Class JavaDoc getValueType()
68     {
69         return valueType;
70     }
71
72     public boolean isEmbeddedKey()
73     {
74         return embeddedKey;
75     }
76
77     public boolean isEmbeddedValue()
78     {
79         return embeddedValue;
80     }
81
82     public boolean allowsNullValues()
83     {
84         return allowsNullValues;
85     }
86
87     public ColumnOptions getKeyColumnOptions()
88     {
89         return new ColumnOptions()
90         {
91             public String JavaDoc getLength()
92             {
93                 String JavaDoc length = getVendorExtension(MY_VENDOR, "key-length");
94
95                 if (length == null)
96                     length = getVendorExtension(MY_VENDOR, "length");
97
98                 return length;
99             }
100
101             public String JavaDoc getPrecision()
102             {
103                 String JavaDoc precision = getVendorExtension(MY_VENDOR, "key-precision");
104
105                 if (precision == null)
106                     precision = getVendorExtension(MY_VENDOR, "precision");
107
108                 return precision;
109             }
110
111             public String JavaDoc getScale()
112             {
113                 String JavaDoc scale = getVendorExtension(MY_VENDOR, "key-scale");
114
115                 if (scale == null)
116                     scale = getVendorExtension(MY_VENDOR, "scale");
117
118                 return scale;
119             }
120         };
121     }
122
123     public ColumnOptions getValueColumnOptions()
124     {
125         return new ColumnOptions()
126         {
127             public String JavaDoc getLength()
128             {
129                 return getVendorExtension(MY_VENDOR, "value-length");
130             }
131
132             public String JavaDoc getPrecision()
133             {
134                 return getVendorExtension(MY_VENDOR, "value-precision");
135             }
136
137             public String JavaDoc getScale()
138             {
139                 return getVendorExtension(MY_VENDOR, "value-scale");
140             }
141         };
142     }
143
144     public boolean isInverseMap()
145     {
146         return getOwnerField() != null;
147     }
148
149     public boolean clearOnDelete()
150     {
151         String JavaDoc codAttr = getVendorExtension(MY_VENDOR, "clear-on-delete");
152
153         return codAttr == null ? true : Boolean.valueOf(codAttr).booleanValue();
154     }
155
156     public FieldMetaData getOwnerField()
157     {
158         String JavaDoc ownerFieldName = getVendorExtension(MY_VENDOR, "owner-field");
159
160         return ownerFieldName == null ? null : FieldMetaData.forField(valueType, ownerFieldName);
161     }
162
163     public FieldMetaData getKeyField()
164     {
165         String JavaDoc keyFieldName = getVendorExtension(MY_VENDOR, "key-field");
166
167         return keyFieldName == null ? null : FieldMetaData.forField(valueType, keyFieldName);
168     }
169
170     void getReferencedClasses(final String JavaDoc vendorID, final List JavaDoc ordered, final Set JavaDoc referenced)
171     {
172         ClassMetaData cmd = ClassMetaData.forClass(keyType);
173
174         if (cmd != null)
175             cmd.getReferencedClasses(vendorID, ordered, referenced);
176
177         cmd = ClassMetaData.forClass(valueType);
178
179         if (cmd != null)
180             cmd.getReferencedClasses(vendorID, ordered, referenced);
181     }
182 }
183
Popular Tags