KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ejb > genic > VcField


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
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.1 of the License, or 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
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: VcField.java,v 1.17 2004/09/21 14:28:46 joaninh Exp $
23  * --------------------------------------------------------------------------
24  */

25
26
27 package org.objectweb.jonas_ejb.genic;
28
29
30 import java.lang.reflect.Field JavaDoc;
31
32 import org.objectweb.jonas_ejb.deployment.api.FieldDesc;
33 import org.objectweb.jonas_ejb.lib.JavaType;
34 import org.objectweb.jonas_ejb.lib.JormType;
35
36
37 /**
38  * This class is the "Velocity context" for a container managed persistence bean's field
39  * used in the Velocity Template.
40  * @author Helene Joanin : Initial developer
41  */

42
43 public class VcField {
44
45     /**
46      * Name of the cmp field
47      */

48     private String JavaDoc mName = null;
49     /**
50      * Type's name of the cmp field
51      */

52     private String JavaDoc mTypeName = null;
53     /**
54      * Is the field composes the primary key
55      */

56     private boolean isPrimaryKey = false;
57     /**
58      * Is a cmp field version 1 (or version 2)
59      */

60     private boolean mIsCmp1 = false;
61     /*
62      * For Cmp1 only
63      */

64     /**
65      * String representation of the java default value (ie 0 for int, null for object, ...)
66      */

67     private String JavaDoc mDefaultValue = null;
68     /**
69      * SQL type's name of the cmp field
70      */

71     private String JavaDoc mSqlTypeName = null;
72     /**
73      * SQL getter method's name
74      */

75     private String JavaDoc mSqlGetMethod = null;
76     /**
77      * SQL setter method's name
78      */

79     private String JavaDoc mSqlSetMethod = null;
80     /**
81      * Is the type of the cmp field is primitive
82      */

83     private boolean hasNotPrimitiveType = false;
84     /**
85      * Is the type of the cmp field is java.math.BigInteger
86      */

87     private boolean hasBigIntegerType = false;
88     /**
89      * Is the type of the cmp field is Serializable
90      */

91     private boolean hasSerializableType = false;
92     /**
93      * Is the type of the cmp field is a java.lang type except java.lang.String
94      */

95     private boolean hasJavaLangTypeExceptString = false;
96     /*
97      * For CMP2 only
98      */

99     /**
100      * JORM type name of the cmp field
101      */

102     private String JavaDoc jormTypeName = null;
103     /**
104      * Is the cmp field must be converted between the memory representation and the storage representation
105      */

106     private boolean mustBeConvert = false;
107     /**
108      * Convertion class name
109      */

110     private String JavaDoc convertClassName = null;
111
112     /**
113      * VcField constructor
114      * @param mName name of the cmp field
115      * @param fType type of the cmp field
116      * @param fd field descriptor of the cmp field
117      * @param isCmp1 true if it's a cmp version 1 field, false if it's a cmp version 2 field.
118      */

119     public VcField(String JavaDoc mName, Class JavaDoc fType, FieldDesc fd, boolean isCmp1) {
120         mIsCmp1 = isCmp1;
121         this.mName = mName;
122         mTypeName = JavaType.getName(fType);
123         isPrimaryKey = fd.isPrimaryKey();
124         mDefaultValue = JavaType.getDefaultValue(fType);
125         if (mIsCmp1) {
126             // CMP 1
127
mSqlTypeName = JavaType.getSQLType(fType);
128             mSqlGetMethod = JavaType.getSQLGetMethod(fType);
129             mSqlSetMethod = JavaType.getSQLSetMethod(fType);
130             hasNotPrimitiveType = !fType.isPrimitive();
131             hasBigIntegerType = fType.equals(java.math.BigInteger JavaDoc.class);
132             hasSerializableType = "setSerializable".equals(mSqlSetMethod)
133                 && "getSerializable".equals(mSqlGetMethod);
134             hasJavaLangTypeExceptString = false;
135             if (fType.getPackage() != null) {
136                 if ("java.lang".equals(fType.getPackage().getName())
137                     && !java.lang.String JavaDoc.class.equals(fType)) {
138                     hasJavaLangTypeExceptString = true;
139                 }
140             }
141         } else {
142             // CMP 2
143
jormTypeName = JormType.getPType(fType, isPrimaryKey).getJavaName();
144             if (fType.equals(java.sql.Date JavaDoc.class)) {
145                 mustBeConvert = true;
146                 convertClassName = "org.objectweb.jonas_ejb.lib.SqlDateFieldMapping";
147             } else if (fType.equals(java.sql.Time JavaDoc.class)) {
148                 mustBeConvert = true;
149                 convertClassName = "org.objectweb.jonas_ejb.lib.SqlTimeFieldMapping";
150             } else if (fType.equals(java.sql.Timestamp JavaDoc.class)) {
151                 mustBeConvert = true;
152                 convertClassName = "org.objectweb.jonas_ejb.lib.SqlTimestampFieldMapping";
153             } else if (isPrimaryKey && fType.equals(Float JavaDoc.class)) {
154                 // JORM does not support Float for the primary key, we must so convert it
155
mustBeConvert = true;
156                 convertClassName = "org.objectweb.jonas_ejb.lib.FloatPkFieldMapping";
157             }
158         }
159     }
160
161     /**
162      * VcField contructor for cmp field version 1
163      * @param field java field descriptor of the cmp field
164      * @param fd field descriptor of the cmp field
165      */

166     VcField(Field JavaDoc field, FieldDesc fd) {
167         this(field.getName(), field.getType(), fd, true);
168     }
169
170     /**
171      * VcField onstructor for cmp field version 2
172      * @param fd field descriptor of the cmp field
173      */

174     VcField(FieldDesc fd) {
175         this(fd.getName(), fd.getFieldType(), fd, false);
176     }
177
178     /**
179      * @return the name of the cmp field
180      */

181     public String JavaDoc getName() {
182         return mName;
183     }
184
185     /**
186      * @return the name, with the first letter capitalized, of the cmp field
187      */

188     public String JavaDoc getNameUpperFirst() {
189         return Character.toUpperCase(mName.charAt(0)) + mName.substring(1);
190     }
191
192     /**
193      * @return the name of the getter method of the cmp field
194      */

195     public String JavaDoc getGetterName() {
196         return "get" + Character.toUpperCase(mName.charAt(0)) + mName.substring(1);
197     }
198
199     /**
200      * @return the name of the setter method of the cmp field
201      */

202     public String JavaDoc getSetterName() {
203         return "set" + Character.toUpperCase(mName.charAt(0)) + mName.substring(1);
204     }
205
206     /**
207      * @return the type name of the cmp field
208      */

209     public String JavaDoc getTypeName() {
210         return mTypeName;
211     }
212
213     /**
214      * @return true if the cmp field composes the primary key
215      */

216     public boolean isPrimaryKey() {
217         return isPrimaryKey;
218     }
219
220     /*
221      * CMP version 1 only
222      */

223     /**
224      * @return the string representation of the java default value for the cmp field (ie "0 "for int, "null" for object ...)
225      */

226     public String JavaDoc getDefaultValue() {
227         return mDefaultValue;
228     }
229
230     /**
231      * @return the SQL type name of the cmp field
232      */

233     public String JavaDoc getSqlTypeName() {
234         return mSqlTypeName;
235     }
236
237     /**
238      * @return the SQL getter method name for the cmp field
239      */

240     public String JavaDoc getSqlGetMethod() {
241         return mSqlGetMethod;
242     }
243
244     /**
245      * @return the SQL setter method name for the cmp field
246      */

247     public String JavaDoc getSqlSetMethod() {
248         return mSqlSetMethod;
249     }
250
251     /**
252      * @return true if the type of the cmp field is not a java primitive type
253      */

254     public boolean hasNotPrimitiveType() {
255         return hasNotPrimitiveType;
256     }
257
258     /**
259      * @return true if the type of the cmp field is java.math.BigInteger
260      */

261     public boolean hasBigIntegerType() {
262         return hasBigIntegerType;
263     }
264
265     /**
266      * @return true of the type of the cmp field is Serializable
267      */

268     public boolean hasSerializableType() {
269         return hasSerializableType;
270     }
271
272     /**
273      * @return true if the type of the cmp type is a java.lag type except java.lang.String
274      */

275     public boolean hasJavaLangTypeExceptString() {
276         return hasJavaLangTypeExceptString;
277     }
278
279     /*
280      * CMP version 2 only
281      */

282     /**
283      * @return the JORM type name of the cmp field
284      */

285     public String JavaDoc getJormTypeName() {
286         return jormTypeName;
287     }
288
289     /**
290      * @return true if a conversion must be dome between the memory representation and the storage representation for the cmp field
291      */

292     public boolean isMustBeConvert() {
293         return mustBeConvert;
294     }
295
296     /**
297      * @return the class name of for the convertion between the memory representation and the storage representation for the cmp field
298      */

299     public String JavaDoc getConvertClassName() {
300         return convertClassName;
301     }
302
303     /**
304      * @return a string representation of the VcField for debug use
305      */

306     public String JavaDoc toString() {
307         StringBuffer JavaDoc ret = new StringBuffer JavaDoc();
308         ret.append("\n Name = " + getName());
309         ret.append("\n isPrimaryKey = " + isPrimaryKey());
310         ret.append("\n NameUpperFirst = " + getNameUpperFirst());
311         ret.append("\n GetterName = " + getGetterName());
312         ret.append("\n SetterName = " + getSetterName());
313         ret.append("\n TypeName = " + getTypeName());
314         ret.append("\n DefaultValue = " + getDefaultValue());
315         if (mIsCmp1) {
316             ret.append("\n SqlTypeName = " + getSqlTypeName());
317             ret.append("\n SqlGetMethod = " + getSqlGetMethod());
318             ret.append("\n SqlSetMethod = " + getSqlSetMethod());
319             ret.append("\n hasNotPrimitiveType = " + hasNotPrimitiveType());
320             ret.append("\n hasBigIntegerType = " + hasBigIntegerType());
321             ret.append("\n hasSerializableType = " + hasSerializableType());
322             ret.append("\n hasJavaLangTypeExceptString = " + hasJavaLangTypeExceptString());
323         } else {
324             ret.append("\n JormTypeName = " + getJormTypeName());
325             ret.append("\n MustBeConvert = " + isMustBeConvert());
326             ret.append("\n ConvertClassName = " + getConvertClassName());
327         }
328         return (ret.toString());
329     }
330
331 }
332
Popular Tags