KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > metadata > parser > JdoField


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.metadata.parser;
13
14 import com.versant.core.metadata.MDStaticUtils;
15 import com.versant.core.metadata.MDStatics;
16 import com.versant.core.common.Debug;
17 import com.versant.core.common.BindingSupportImpl;
18
19 import java.io.PrintStream JavaDoc;
20 import java.util.*;
21
22 /**
23  * Field element from a .jdo file.
24  */

25 public final class JdoField extends JdoElement {
26     private final static Set VENDOR_IGNORE_SET = new HashSet();
27
28     public String JavaDoc name;
29     public String JavaDoc origName;
30     public int persistenceModifier;
31     public boolean primaryKey;
32     public int nullValue;
33     public int defaultFetchGroup;
34     public int embedded;
35
36     /**
37      * Temp list to hold extensions
38      */

39     public ArrayList extensionList = new ArrayList();
40
41     public List embeddedFields;
42     public JdoField parentField;
43     public boolean nullIndicator;
44
45     public JdoCollection collection;
46     public JdoMap map;
47     public JdoArray array;
48
49     public JdoExtension[] extensions;
50
51     public JdoClass parent;
52     public int cascadeType;
53
54     public JdoField() {
55         VENDOR_IGNORE_SET.add(JdoExtension.toKeyString(JdoExtensionKeys.COLLECTION));
56         VENDOR_IGNORE_SET.add(JdoExtension.toKeyString(JdoExtensionKeys.EMBEDDED));
57         VENDOR_IGNORE_SET.add(JdoExtension.toKeyString(JdoExtensionKeys.DEFAULT_FETCH_GROUP));
58         VENDOR_IGNORE_SET.add(JdoExtension.toKeyString(JdoExtensionKeys.MAP));
59         VENDOR_IGNORE_SET.add(JdoExtension.toKeyString(JdoExtensionKeys.ARRAY));
60     }
61
62     public JdoField createCopy(JdoField parentField, JdoClass parentClass) {
63         JdoField jf = new JdoField(parentField);
64         jf.name = name;
65         jf.persistenceModifier = persistenceModifier;
66         jf.primaryKey = primaryKey;
67         jf.nullValue = nullValue;
68         jf.defaultFetchGroup = defaultFetchGroup;
69         jf.embedded = embedded;
70
71         jf.parent = parentClass;
72         if (collection != null) jf.collection = collection.createCopy(jf);
73         if (map != null) jf.map = map.createCopy(jf);
74         if (array != null) jf.array = array.createCopy(jf);
75
76         if (extensions != null) {
77             jf.extensions = new JdoExtension[extensions.length];
78             for (int i = 0; i < extensions.length; i++) {
79                 jf.extensions[i] = extensions[i].createCopy(jf);
80             }
81         }
82         return jf;
83     }
84
85     public JdoField createCopy(JdoField parentField) {
86         return createCopy(parentField, null);
87     }
88
89     public JdoField createCopy(JdoClass parentClass) {
90         return createCopy(null, parentClass);
91     }
92
93     public JdoField(JdoField parentField) {
94         this.parentField = parentField;
95         if (parentField != null) parentField.addEmbeddedField(this);
96     }
97
98     public JdoElement getParent() { return parent; }
99
100     public void addEmbeddedField(JdoField embeddedField) {
101         if (embeddedFields == null) embeddedFields = new ArrayList();
102         embeddedFields.add(embeddedField);
103     }
104
105     public boolean isExternalized() {
106         if (extensions != null){
107             for (int i = 0; i < extensions.length; i++) {
108                 if (extensions[i].key==JdoExtensionKeys.EXTERNALIZER) {
109                     return true;
110                 }
111             }
112         }
113         return false;
114     }
115
116
117     /**
118      * Get information for this element to be used in building up a
119      * context string.
120      * @see #getContext
121      */

122     public String JavaDoc getSubContext() {
123         return "field[" + name + "]";
124     }
125
126     public String JavaDoc toString() {
127         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
128         s.append("field[");
129         s.append(name);
130         s.append("] persistenceModifier=");
131         s.append(MDStaticUtils.toPersistenceModifierString(persistenceModifier));
132         s.append(" primaryKey=");
133         s.append(primaryKey);
134         s.append(" nullValue=");
135         s.append(MDStaticUtils.toNullValueString(nullValue));
136         s.append(" defaultFetchGroup=");
137         s.append(MDStaticUtils.toTriStateString(defaultFetchGroup));
138         s.append(" embedded=");
139         s.append(MDStaticUtils.toTriStateString(embedded));
140         return s.toString();
141     }
142
143     public void dump() {
144         dump(Debug.OUT, "");
145     }
146
147     public void dump(PrintStream JavaDoc out, String JavaDoc indent) {
148         out.println(indent + this);
149         String JavaDoc is = indent + " ";
150         if (collection != null) collection.dump(out, is);
151         if (map != null) map.dump(out, is);
152         if (array != null) array.dump(out, is);
153         if (extensions != null) {
154             for (int i = 0; i < extensions.length; i++) {
155                 extensions[i].dump(out, is);
156             }
157         }
158     }
159
160     /**
161      * Update this with non conflicting metadat from the supplied instance.
162      */

163     public void synchWith(JdoField updateFrom, Set exclude, boolean errorOnExclude) {
164         if (persistenceModifier == MDStatics.NOT_SET) {
165             persistenceModifier = updateFrom.persistenceModifier;
166         }
167         if (embedded == MDStatics.NOT_SET) {
168             embedded = updateFrom.embedded;
169         }
170         if (defaultFetchGroup == MDStatics.NOT_SET) {
171             defaultFetchGroup = updateFrom.defaultFetchGroup;
172         }
173         if (nullValue == MDStatics.NOT_SET) {
174             nullValue = updateFrom.nullValue;
175         }
176
177         if (collection != null) {
178             if (updateFrom.collection == null) {
179                 throw BindingSupportImpl.getInstance().invalidOperation(
180                         "Not allowed to change the type of field '"
181                         + updateFrom.name + "' to a 'collection'");
182             } else {
183                 collection.synchronizeForHorizontal(updateFrom.collection);
184             }
185         } else if (updateFrom.collection != null) {
186             collection = updateFrom.collection.createCopy(this);
187         }
188
189         if (map != null) {
190             if (updateFrom.map == null) {
191                 throw BindingSupportImpl.getInstance().invalidOperation(
192                         "Not allowed to change the type of field '"
193                         + updateFrom.name + "' to a 'map'");
194             } else {
195                 map.synchronizeForHorizontal(updateFrom.map);
196             }
197         } else if (updateFrom.map != null) {
198             map = updateFrom.map.createCopy(this);
199         }
200
201         if (array != null) {
202             if (updateFrom.array == null) {
203                 throw BindingSupportImpl.getInstance().invalidOperation(
204                         "Not allowed to change the type of field '"
205                         + updateFrom.name + "' to a 'map'");
206             } else {
207                 array.synchronizeForHorizontal(updateFrom.array);
208             }
209         } else if (updateFrom.array != null) {
210             array = updateFrom.array.createCopy(this);
211         }
212
213         JdoExtension[] copy = updateFrom.getExtensionCopy(this);
214         JdoExtension.synchronize3(extensions, copy, exclude, errorOnExclude);
215         extensions = copy;
216     }
217
218     public JdoExtension[] getExtensionCopy(JdoField owner) {
219         if (extensions == null) return null;
220         JdoExtension[] copy = new JdoExtension[extensions.length];
221         for (int i = 0; i < extensions.length; i++) {
222             copy[i] = extensions[i].createCopy(owner);
223         }
224         return copy;
225     }
226
227     private void processAttributeExtensions(JdoExtension exts[]) {
228         if (exts == null) return;
229         for (int i = 0; i < exts.length; i++) {
230             JdoExtension extension = exts[i];
231             if (extension.isFieldAttribute()) {
232                 if (extension.key == JdoExtensionKeys.DEFAULT_FETCH_GROUP) {
233                     String JavaDoc value = extension.value;
234                     if ("true".equals(value)) {
235                         defaultFetchGroup = JdoField.TRUE;
236                     } else if ("false".equals(value)) {
237                         defaultFetchGroup = JdoField.FALSE;
238                     }
239                 } else if (extension.key == JdoExtensionKeys.EMBEDDED) {
240                     String JavaDoc value = extension.value;
241                     if ("true".equals(value)) {
242                         embedded = JdoField.TRUE;
243                     } else if ("false".equals(value)) {
244                         embedded = JdoField.FALSE;
245                     }
246                 }
247             }
248         }
249     }
250
251     public void applyEmbeddedExtensions(JdoExtension[] nested) {
252         clearAllJdbcColumnNames();
253
254         processAttributeExtensions(nested);
255         processVendorExtensions(nested);
256         processCollectionExtensions(nested);
257         processMapExtensions(nested);
258         processArrayExtensions(nested);
259
260     }
261
262     private void clearAllJdbcColumnNames() {
263         if (extensions != null) {
264             JdoExtension.clearKey(JdoExtensionKeys.JDBC_COLUMN_NAME, 3, extensions);
265         }
266         if (collection != null && collection.extensions != null) {
267             JdoExtension.clearKey(JdoExtensionKeys.JDBC_COLUMN_NAME, 3, collection.extensions);
268         }
269     }
270
271     private void processVendorExtensions(JdoExtension[] nested) {
272         extensions = JdoExtension.synchronize4(nested, extensions, VENDOR_IGNORE_SET);
273         if (extensions != null) {
274             JdoExtension ext = JdoExtension.find(JdoExtensionKeys.NULL_INDICATOR, extensions);
275             if (ext != null) {
276                 nullIndicator = true;
277             }
278         }
279     }
280
281     private void processArrayExtensions(JdoExtension[] nested) {
282         if (array == null) return;
283         JdoExtension ext = JdoExtension.find(JdoExtensionKeys.ARRAY, nested);
284         if (ext != null && ext.nested != null) {
285             array.extensions = JdoExtension.synchronize4(ext.nested, array.extensions, Collections.EMPTY_SET);
286         }
287     }
288
289     private void processMapExtensions(JdoExtension[] nested) {
290         if (map == null) return;
291         JdoExtension ext = JdoExtension.find(JdoExtensionKeys.MAP, nested);
292         if (ext != null && ext.nested != null) {
293             checkForTypeOverride(ext.nested, JdoExtensionKeys.VALUE_TYPE, map.valueType);
294             checkForTypeOverride(ext.nested, JdoExtensionKeys.KEY_TYPE, map.keyType);
295             map.extensions = JdoExtension.synchronize4(ext.nested, map.extensions, Collections.EMPTY_SET);
296         }
297     }
298
299     private void processCollectionExtensions(JdoExtension[] nested) {
300         if (collection == null) return;
301         JdoExtension ext = JdoExtension.find(JdoExtensionKeys.COLLECTION, nested);
302         if (ext != null && ext.nested != null) {
303             if (ext.nested == null) return;
304             checkForTypeOverride(ext.nested, JdoExtensionKeys.ELEMENT_TYPE, collection.elementType);
305             collection.extensions = JdoExtension.synchronize4(ext.nested, collection.extensions, Collections.EMPTY_SET);
306         }
307     }
308
309     private void checkForTypeOverride(JdoExtension[] exts, int key, String JavaDoc value) {
310         //may not specify a different element type
311
JdoExtension typeExt = JdoExtension.find(key, exts);
312         if (typeExt != null && !value.equals(typeExt.value)) {
313             throw BindingSupportImpl.getInstance().invalidOperation(
314                     "Key/Values types for Collection/Maps may not be changed in embedded fields");
315         }
316     }
317
318     /**
319      * Create the extension if it does not exist, else update it if overwrite is true
320      * @param key
321      * @param value
322      * @param overwrite
323      */

324     public JdoExtension findCreate(int key, String JavaDoc value, boolean overwrite) {
325         if (extensions == null) {
326             extensions = new JdoExtension[] {createChild(key, value, this)};
327             return extensions[0];
328         }
329
330         JdoExtension ext = JdoExtension.find(key, extensions);
331         if (ext == null) {
332             extensions = addExtension(extensions, (ext = createChild(key, value, this)));
333         } else if (overwrite) {
334             ext.value = value;
335         }
336         return ext;
337     }
338     
339 }
340
341
Popular Tags