KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > metadata > SpeedoClass


1 /**
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  *
21  *
22  * Contact: speedo@objectweb.org
23  *
24  * Authors: S.Chassande-Barrioz.
25  *
26  */

27
28 package org.objectweb.speedo.metadata;
29
30 import org.objectweb.jorm.metainfo.api.Class;
31 import org.objectweb.speedo.api.SpeedoException;
32 import org.objectweb.util.monolog.api.BasicLevel;
33 import org.objectweb.util.monolog.api.Logger;
34
35 import java.io.File JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.Map JavaDoc;
39
40 import javax.jdo.JDOUserException;
41
42 /**
43  * Describes a persistence capable class.
44  * @author S.Chassande-Barrioz
45  */

46 public class SpeedoClass extends SpeedoElement {
47     /**
48      * Class name.
49      */

50     public String JavaDoc name;
51
52     /**
53      * Attribute identity-type.
54      */

55     public byte identityType = SpeedoIdentity.CONTAINER_ID;
56
57     /**
58      * Description of the version.
59      */

60     public SpeedoVersion version;
61     
62     /**
63      * The datastore-id.
64      */

65     public SpeedoDatastoreId datastoreId;
66     
67     /**
68      * Attribute objectid-class.
69      */

70     public String JavaDoc objectidClass;
71
72     /**
73      * Attribute requires-extent.
74      */

75     public boolean requiresExtent = true;
76
77     /**
78      * Attribute detachable.
79      */

80     public boolean isDetachable = false;
81         
82     /**
83      * Super class name.
84      */

85     public String JavaDoc superClassName = "";
86
87     /**
88      * Description of persistent capable fields of this class. The HashMap key
89      * is the field name.
90      */

91     public Map JavaDoc jdoField = new HashMap JavaDoc();
92     
93     /**
94      * Description of fetchgroups defined for this class. The HashMap key
95      * is the field name.
96      */

97     public Map JavaDoc jdoFetchGroup = new HashMap JavaDoc();
98
99     /**
100      * Descriptor of this class code.
101      */

102     public ClassCode classCode = new ClassCode();
103
104     /**
105      * Description of the package which contains this class.
106      */

107     public SpeedoPackage jdoPackage;
108
109     /**
110      * Indicates if the class is abstract.
111      */

112     public boolean isAbstract = false;
113
114     /**
115      * Indicates if the class implements InstanceCallbacks
116      */

117     public boolean isInstanceCallbacks = false;
118
119     /**
120      * Indicates if the class implements InstanceCallbacks
121      */

122     public boolean isSerializable = false;
123
124     /**
125      * Class signature.
126      */

127     public String JavaDoc signature;
128
129     /**
130      * Version UID for serialization.
131      */

132     public long VersionUID = 0;
133
134     /**
135      * Indicates whether something goes wrong during the JDO Instances generation.
136      */

137     public boolean failed = false;
138
139     /**
140      * Class jorm corresponding to this jdoclass
141      */

142     public Class JavaDoc jormclass;
143
144     /**
145      * The name of the datastore sequence
146      */

147     public String JavaDoc datastoreSequence;
148     
149     /**
150      * Number of fields of this class (including inherited fields).
151      */

152     private int fieldsCount = -1;
153
154     private int pkFieldsCount = -1;
155
156     public boolean classAlreadyEnhanced = false;
157     public boolean requireEnhancement = true;
158     
159     public Map JavaDoc name2query = new HashMap JavaDoc();
160
161     public final static byte NO_NO_ARG_CONSTRUCTOR = 1;
162     public final static byte NON_PUBLIC_NO_ARG_CONSTRUCTOR = 2;
163     public final static byte PUBLIC_NO_ARG_CONSTRUCTOR = 3;
164
165     public byte noArgConstructorStatus = NO_NO_ARG_CONSTRUCTOR;
166     
167     public final static byte SUBCLASS_TABLE = 1;
168     public final static byte NEW_TABLE = 2;
169     public final static byte SUPERCLASS_TABLE = 3;
170     
171     public byte inheritanceStrategy = 0;
172
173     public String JavaDoc getFQName() {
174         if (jdoPackage.name == null || jdoPackage.name.length() == 0) {
175             return name;
176         } else {
177             return jdoPackage.name + "." + name;
178         }
179     }
180
181     /**
182      * Transforms a SpeedoClass into a String.
183      * @return the Sting corresponding to the SpeedoClass.
184      */

185     public String JavaDoc toString() {
186         String JavaDoc s = ("\n class name : " + name +
187                 ", identityType : " + identityType +
188                 ", objectidClass : " + objectidClass +
189                 ", requiresExtent : " + requiresExtent +
190                 ",version : " + version +
191                 ", persistenceCapableSuperClass : " + superClassName);
192         s += ", \t fields:[";
193         Iterator JavaDoc it = jdoField.values().iterator();
194         while (it.hasNext()) {
195             s = s + "\t" + it.next().toString();
196         }
197         s += "], \t fetchGroups:[";
198         Iterator JavaDoc it2 = jdoFetchGroup.values().iterator();
199             while (it2.hasNext()) {
200                 s += "\t" + it2.next().toString();
201         }
202         s += "]";
203         return s;
204     }
205
206     /**
207      * Adds a SpeedoField to the class.
208      * Precondition: this field doesn't exist in the jdoFields HashMap.
209      * @param field field to add.
210      */

211     public void add(Object JavaDoc field) {
212         SpeedoField f = (SpeedoField) field;
213         f.jdoClass = this;
214         f.number = fieldsCount++;
215         jdoField.put(f.name, field);
216     }
217
218     /**
219      * Adds a SpeedoField to the class.
220      * @param field field to add.
221      * @param failsOnError if an error must be thrown or creates a warning.
222      * @param logger logger for writting warn message if necessary.
223      * @exception SpeedoException if the field was already defined into the class.
224      */

225     public void add(Object JavaDoc field, boolean failsOnError, Logger logger) throws SpeedoException {
226         SpeedoField f = (SpeedoField) field;
227         if (jdoField.containsKey(f.name)) {
228             if (failsOnError)
229                 throw new SpeedoException("The field " + f.name + " of class "
230                         + name + " is defined twice.");
231             else
232                 logger.log(BasicLevel.ERROR, "The field " + f.name + " of class "
233                         + name + " is defined twice.");
234         } else {
235             logger.log(BasicLevel.DEBUG, "Add the field '" + f.name
236                     + "' in class '" + name + "'.");
237             f.jdoClass = this;
238             jdoField.put(f.name, field);
239         }
240     }
241
242     /**
243      * Set the versioning strategy of the class.
244      * @param version : the version to set
245      */

246     public void addVersion(Object JavaDoc version) {
247         SpeedoVersion v = (SpeedoVersion) version;
248         v.jdoClass = this;
249         this.version = v;
250     }
251     
252     public void addDatastoreId(Object JavaDoc datastoreId) {
253         SpeedoDatastoreId d = (SpeedoDatastoreId) datastoreId;
254         d.jdoClass = this;
255         this.datastoreId = d;
256         //add the datasore sequence: can be null
257
this.datastoreSequence = d.sequence;
258     }
259     
260     /**
261      * Adds a SpeedoFetchGroup to the class.
262      * Precondition: this fetchgroup doesn't exist in the jdoFtechGroups HashMap.
263      * @param fetchGroup the fetchgroup to add.
264      */

265     public void addFetchGroup(Object JavaDoc fetchGroup) {
266         SpeedoFetchGroup fg = (SpeedoFetchGroup) fetchGroup;
267         jdoFetchGroup.put(fg.name, fetchGroup);
268     }
269
270     /**
271      * Adds a SpeedoFetchgroup to the class.
272      * @param fetchGroup the fetchgroup to add.
273      * @param failsOnError if an error must be thrown or creates a warning.
274      * @param logger logger for writting warn message if necessary.
275      * @exception SpeedoException if the field was already defined into the class.
276      */

277     public void addFetchGroup(Object JavaDoc fetchGroup, boolean failsOnError, Logger logger) throws SpeedoException {
278         SpeedoFetchGroup fg = (SpeedoFetchGroup) fetchGroup;
279         if (jdoFetchGroup.containsKey(fg.name)) {
280             if (failsOnError)
281                 throw new SpeedoException("The fetchgroup " + fg.name + " of class "
282                         + name + " is defined twice.");
283             else
284                 logger.log(BasicLevel.ERROR, "The fetchgroup " + fg.name + " of class "
285                         + name + " is defined twice.");
286         } else {
287             logger.log(BasicLevel.DEBUG, "Add the fetchgroup '" + fg.name
288                     + "' in class '" + name + "'.");
289             jdoFetchGroup.put(fg.name, fetchGroup);
290         }
291     }
292     
293     
294     /**
295      * Computes the field numbers for the fields of this class.
296      *
297      * @return the number of fields of this class (including inherited fields).
298      */

299     public int computeFieldNumbers() {
300         if (fieldsCount != -1) {
301             return fieldsCount;
302         }
303         if (superClassName.length() == 0) {
304             fieldsCount = 0;
305         } else {
306             SpeedoClass jdoSuperClass = jdoPackage.jdoXMLDescriptor.smi
307                 .getSpeedoClass(superClassName, jdoPackage);
308             if (jdoSuperClass == null) {
309                 throw new JDOUserException(
310                         "The persistence-capable-superclass field has a bad value: '"
311                         + superClassName
312                         + "'. See the description of the class '" + getFQName()
313                         + "' from the file '" + getJDOFileName() + "'.");
314             }
315             fieldsCount = jdoSuperClass.computeFieldNumbers();
316         }
317         Iterator JavaDoc i = jdoField.values().iterator();
318         while (i.hasNext()) {
319             SpeedoField jf = (SpeedoField) i.next();
320             jf.number = fieldsCount++;
321         }
322         return fieldsCount;
323     }
324
325     public String JavaDoc getJormFileName() {
326         return getFQName().replace('.', File.separatorChar) + ".pd";
327     }
328
329     public boolean generateObjectId() {
330         return getPkFieldCount() > 1;
331     }
332
333     public int getPkFieldCount() {
334         if (pkFieldsCount == -1) {
335             pkFieldsCount = 0;
336             if (identityType == SpeedoIdentity.USER_ID &&
337                     (objectidClass == null || objectidClass.length() == 0)) {
338                 Iterator JavaDoc it = jdoField.values().iterator();
339                 while(it.hasNext()) {
340                     if (((SpeedoField) it.next()).primaryKey) {
341                         pkFieldsCount++;
342                     }
343                 }
344             }
345         }
346         return pkFieldsCount;
347     }
348     public SpeedoClass getSpeedoClassFromContext(String JavaDoc className) {
349         return jdoPackage.jdoXMLDescriptor.smi.getSpeedoClass(className, jdoPackage);
350     }
351
352     public String JavaDoc getJDOFileName() {
353         return jdoPackage.jdoXMLDescriptor.xmlFile;
354     }
355
356     public SpeedoClass getSuper() {
357         if (superClassName == null) {
358             return null;
359         } else {
360             return getSpeedoClassFromContext(superClassName);
361         }
362     }
363
364     public SpeedoClass getAncestor() {
365         SpeedoClass tmp = getSuper();
366         SpeedoClass ancestor = null;
367         while(tmp != null && ancestor != tmp) {
368             ancestor = tmp;
369             tmp = tmp.getSuper();
370         }
371         return ancestor;
372     }
373 }
374
Popular Tags