KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > api > persistence > enhancer > meta > JDOMetaDataModelImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24
25 package com.sun.jdo.api.persistence.enhancer.meta;
26
27 import java.io.PrintWriter JavaDoc;
28
29 import com.sun.jdo.api.persistence.enhancer.util.Support;
30 import com.sun.jdo.api.persistence.model.Model;
31 import com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement;
32 import com.sun.jdo.api.persistence.model.jdo.PersistenceClassElement;
33 import com.sun.jdo.api.persistence.model.jdo.RelationshipElement;
34
35
36 /**
37  * Provides the JDO meta information based on a JDO meta model.
38  */

39 //@olsen: new class
40
public class JDOMetaDataModelImpl extends Support
41     implements JDOMetaData
42 {
43     // misc
44
protected final PrintWriter JavaDoc out;
45
46     // model
47
protected Model model;
48
49     /**
50      * Creates an instance.
51      */

52     public JDOMetaDataModelImpl(Model model) {
53         this(model, null);
54     }
55
56     public JDOMetaDataModelImpl(Model model,
57                                 PrintWriter JavaDoc out)
58         throws JDOMetaDataUserException, JDOMetaDataFatalError
59     {
60         // check arguments
61
if (model == null) {
62             final String JavaDoc msg
63                 = "Initializing meta data: model == null";//NOI18N
64
throw new JDOMetaDataFatalError(msg);
65         }
66         this.model = model;
67         this.out = out;
68     }
69
70     /**
71      * Tests whether a class is known to be transient.
72      */

73     public boolean isTransientClass(String JavaDoc classPath)
74         throws JDOMetaDataUserException, JDOMetaDataFatalError
75     {
76         //^olsen: delegate to Model once supported
77
if (classPath.startsWith("java/"))//NOI18N
78
return true;
79         if (classPath.startsWith("javax/"))//NOI18N
80
return true;
81         if (classPath.startsWith("com/sun/jdo/"))//NOI18N
82
return true;
83         return false;
84     }
85
86     /**
87      * Tests whether a class is known to be persistence-capable.
88      */

89     public boolean isPersistenceCapableClass(String JavaDoc classPath)
90         throws JDOMetaDataUserException, JDOMetaDataFatalError
91     {
92         //^olsen: delegate to Model once supported
93
if (isTransientClass(classPath))
94             return false;
95         final String JavaDoc className = pathToName(classPath);
96         return model.isPersistent(className);
97     }
98
99     /**
100      * Tests whether a class is known as a persistence-capable root class.
101      */

102     public boolean isPersistenceCapableRootClass(String JavaDoc classPath)
103         throws JDOMetaDataUserException, JDOMetaDataFatalError
104     {
105         //@olsen: 4631388 - do not attempt to support inheritance right now
106
final String JavaDoc className = pathToName(classPath);
107         return model.isPersistent(className);
108         //return (model.isPersistent(className)
109
// && !model.hasPersistentSuperclass(className));
110
}
111
112     /**
113      * Returns the name of the persistence-capable root class of a class.
114      */

115     public String JavaDoc getPersistenceCapableRootClass(String JavaDoc classPath)
116         throws JDOMetaDataUserException, JDOMetaDataFatalError
117     {
118         //^olsen: exchange dummy implementation once supported by Model
119
return (isPersistenceCapableClass(classPath) ? classPath : null);
120     }
121
122     /**
123      * Returns the name of the superclass of a class.
124      * <P>
125      * @param classPath the JVM-qualified name of the class
126      * @return the name of the superclass.
127      */

128     public String JavaDoc getSuperClass(String JavaDoc classPath)
129         throws JDOMetaDataUserException, JDOMetaDataFatalError
130     {
131         //throw new UnsupportedOperationException ("not implemented yet");
132
return null;
133     }
134
135     /**
136      * Tests whether a type is known for Second Class Objects.
137      */

138     public boolean isSecondClassObjectType(String JavaDoc classPath)
139         throws JDOMetaDataUserException, JDOMetaDataFatalError
140     {
141         final String JavaDoc className = pathToName(classPath);
142         return model.isSecondClassObject(className);
143     }
144
145     /**
146      * Tests whether a type is known for Mutable Second Class Objects.
147      */

148     public boolean isMutableSecondClassObjectType(String JavaDoc classPath)
149         throws JDOMetaDataUserException, JDOMetaDataFatalError
150     {
151         final String JavaDoc className = pathToName(classPath);
152         return model.isMutableSecondClassObject(className);
153     }
154
155     /**
156      * Tests whether a field of a class is known to be persistent.
157      */

158     public boolean isPersistentField(String JavaDoc classPath, String JavaDoc fieldName)
159         throws JDOMetaDataUserException, JDOMetaDataFatalError
160     {
161         final String JavaDoc className = pathToName(classPath);
162         return model.isPersistent(className, fieldName);
163     }
164
165     /**
166      * Tests whether a field of a class is known to be transactional.
167      */

168     public boolean isTransactionalField(String JavaDoc classPath, String JavaDoc fieldName)
169         throws JDOMetaDataUserException, JDOMetaDataFatalError
170     {
171         //throw new UnsupportedOperationException ("not implemented yet");
172
return false;
173     }
174
175     /**
176      * Tests whether a field of a class is known to be Primary Key.
177      */

178     public boolean isPrimaryKeyField(String JavaDoc classPath, String JavaDoc fieldName)
179         throws JDOMetaDataUserException, JDOMetaDataFatalError
180     {
181         final String JavaDoc className = pathToName(classPath);
182         return model.isKey(className, fieldName);
183     }
184
185     /**
186      * Tests whether a field of a class is known to be part of the
187      * Default Fetch Group. Please note that for a relationship field, this
188      * method always returns false.
189      */

190     public boolean isDefaultFetchGroupField(String JavaDoc classPath, String JavaDoc fieldName)
191         throws JDOMetaDataUserException, JDOMetaDataFatalError
192     {
193         final String JavaDoc className = pathToName(classPath);
194         boolean isdfgField = model.isDefaultFetchGroup(className, fieldName);
195         if(isdfgField) {
196             final PersistenceFieldElement pfe
197                 = model.getPersistenceField(className, fieldName);
198             if (pfe instanceof RelationshipElement) {
199                 // This is a relationship field. Flag it as not belonging
200
// to dfg.
201
// Relationship fields are always flaged as not belonging to dfg
202
// This assures that access to a relationship fields is always
203
// mediated.
204
// Please see call to this method from following for more details.
205
// 1. EJBMetaDataModelImpl#getFieldFlags()
206
// 2. MethodAnnotater#notePutFieldAnnotation()
207
// 3. MethodAnnotater#noteGetFieldAnnotation()
208
isdfgField = false;
209             }
210         }
211         return isdfgField;
212     }
213
214     /**
215      * Returns the unique field index of a declared, persistent field of a
216      * class.
217      */

218     public int getFieldNo(String JavaDoc classPath, String JavaDoc fieldName)
219         throws JDOMetaDataUserException, JDOMetaDataFatalError
220     {
221         final String JavaDoc className = pathToName(classPath);
222         final PersistenceFieldElement pfe
223             = model.getPersistenceField(className, fieldName);
224         if (pfe == null
225             || pfe.getPersistenceType() != PersistenceFieldElement.PERSISTENT)
226             return -1;
227
228         return pfe.getFieldNumber();
229     }
230
231     /**
232      * Returns an array of field names of all declared, persistent fields
233      * of a class.
234      */

235     private final String JavaDoc[] getPersistentFields(String JavaDoc classPath)
236         throws JDOMetaDataUserException, JDOMetaDataFatalError
237     {
238         final String JavaDoc className = pathToName(classPath);
239         final PersistenceClassElement pce
240             = model.getPersistenceClass(className);
241         if (pce == null)
242             return new String JavaDoc[]{};
243
244         // exctract field names into result array
245
final PersistenceFieldElement[] pfes = pce.getFields();
246         final int nofFields = (pfes != null ? pfes.length : 0);
247         final String JavaDoc[] names = new String JavaDoc[nofFields];
248         for (int i = 0; i < nofFields; i++) {
249             final PersistenceFieldElement pfe = pfes[i];
250             names[i] = pfe.getName();
251
252             //@olsen: debugging check
253
if (false) {
254                 if (pfe.getPersistenceType()
255                     != PersistenceFieldElement.PERSISTENT) {
256                     final String JavaDoc msg
257                         = ("Getting persistent field names: " //NOI18N
258
+ "Encountered non-persistent field '"//NOI18N
259
+ names[i] + "' for class " + classPath);//NOI18N
260
throw new JDOMetaDataFatalError(msg);
261                     //out.println(msg);
262
//names[i] = null;
263
}
264             }
265         }
266         return names;
267     }
268
269     /**
270      * Returns an array of field names of all declared persistent and
271      * transactional fields of a class.
272      */

273     public String JavaDoc[] getManagedFields(String JavaDoc classPath)
274         throws JDOMetaDataUserException, JDOMetaDataFatalError
275     {
276         return getPersistentFields(classPath);
277     }
278
279     static protected String JavaDoc pathToName(String JavaDoc classPath) {
280         if (classPath != null) {
281             return classPath.replace('/', '.');
282         } else {
283             return null;
284         }
285     }
286
287     static protected String JavaDoc nameToPath(String JavaDoc className) {
288         if (className != null) {
289             return className.replace('.', '/');
290         } else {
291             return null;
292         }
293     }
294 }
295
Popular Tags