001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: VcField.java 7536 2005-10-19 22:08:18Z rhs $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_ejb.genic;
025:
026: import java.lang.reflect.Field;
027:
028: import org.objectweb.jonas_ejb.deployment.api.FieldDesc;
029: import org.objectweb.jonas_ejb.lib.JavaType;
030: import org.objectweb.jonas_ejb.lib.JormType;
031:
032: /**
033: * This class is the "Velocity context" for a container managed persistence bean's field
034: * used in the Velocity Template.
035: * @author Helene Joanin : Initial developer
036: */
037:
038: public class VcField {
039:
040: /**
041: * Name of the cmp field
042: */
043: private String mName = null;
044: /**
045: * Type's name of the cmp field
046: */
047: private String mTypeName = null;
048: /**
049: * Is the field composes the primary key
050: */
051: private boolean isPrimaryKey = false;
052: /**
053: * Is a cmp field version 1 (or version 2)
054: */
055: private boolean mIsCmp1 = false;
056: /*
057: * For Cmp1 only
058: */
059: /**
060: * String representation of the java default value (ie 0 for int, null for object, ...)
061: */
062: private String mDefaultValue = null;
063: /**
064: * SQL type's name of the cmp field
065: */
066: private String mSqlTypeName = null;
067: /**
068: * SQL getter method's name
069: */
070: private String mSqlGetMethod = null;
071: /**
072: * SQL setter method's name
073: */
074: private String mSqlSetMethod = null;
075: /**
076: * Is the type of the cmp field is primitive
077: */
078: private boolean hasNotPrimitiveType = false;
079: /**
080: * Is the type of the cmp field is java.math.BigInteger
081: */
082: private boolean hasBigIntegerType = false;
083: /**
084: * Is the type of the cmp field is Serializable
085: */
086: private boolean hasSerializableType = false;
087: /**
088: * Is the type of the cmp field is a java.lang type except java.lang.String
089: */
090: private boolean hasJavaLangTypeExceptString = false;
091: /*
092: * For CMP2 only
093: */
094: /**
095: * JORM type name of the cmp field
096: */
097: private String jormTypeName = null;
098: /**
099: * Is the cmp field must be converted between the memory representation and the storage representation
100: */
101: private boolean mustBeConvert = false;
102: /**
103: * Convertion class name
104: */
105: private String convertClassName = null;
106:
107: /**
108: * VcField constructor
109: * @param mName name of the cmp field
110: * @param fType type of the cmp field
111: * @param fd field descriptor of the cmp field
112: * @param isCmp1 true if it's a cmp version 1 field, false if it's a cmp version 2 field.
113: */
114: public VcField(String mName, Class fType, FieldDesc fd,
115: boolean isCmp1) {
116: mIsCmp1 = isCmp1;
117: this .mName = mName;
118: mTypeName = JavaType.getName(fType);
119: isPrimaryKey = fd.isPrimaryKey();
120: mDefaultValue = JavaType.getDefaultValue(fType);
121: if (mIsCmp1) {
122: // CMP 1
123: mSqlTypeName = JavaType.getSQLType(fType);
124: mSqlGetMethod = JavaType.getSQLGetMethod(fType);
125: mSqlSetMethod = JavaType.getSQLSetMethod(fType);
126: hasNotPrimitiveType = !fType.isPrimitive();
127: hasBigIntegerType = fType
128: .equals(java.math.BigInteger.class);
129: hasSerializableType = "setSerializable"
130: .equals(mSqlSetMethod)
131: && "getSerializable".equals(mSqlGetMethod);
132: hasJavaLangTypeExceptString = false;
133: if (fType.getPackage() != null) {
134: if ("java.lang".equals(fType.getPackage().getName())
135: && !java.lang.String.class.equals(fType)) {
136: hasJavaLangTypeExceptString = true;
137: }
138: }
139: } else {
140: // CMP 2
141: jormTypeName = JormType.getPType(fType, isPrimaryKey)
142: .getJavaName();
143: if (fType.equals(java.sql.Date.class)) {
144: mustBeConvert = true;
145: convertClassName = "org.objectweb.jonas_ejb.lib.SqlDateFieldMapping";
146: } else if (fType.equals(java.sql.Time.class)) {
147: mustBeConvert = true;
148: convertClassName = "org.objectweb.jonas_ejb.lib.SqlTimeFieldMapping";
149: } else if (fType.equals(java.sql.Timestamp.class)) {
150: mustBeConvert = true;
151: convertClassName = "org.objectweb.jonas_ejb.lib.SqlTimestampFieldMapping";
152: } else if (isPrimaryKey && fType.equals(Float.class)) {
153: // JORM does not support Float for the primary key, we must so convert it
154: mustBeConvert = true;
155: convertClassName = "org.objectweb.jonas_ejb.lib.FloatPkFieldMapping";
156: }
157: }
158: }
159:
160: /**
161: * VcField contructor for cmp field version 1
162: * @param field java field descriptor of the cmp field
163: * @param fd field descriptor of the cmp field
164: */
165: VcField(Field field, FieldDesc fd) {
166: this (field.getName(), field.getType(), fd, true);
167: }
168:
169: /**
170: * VcField onstructor for cmp field version 2
171: * @param fd field descriptor of the cmp field
172: */
173: VcField(FieldDesc fd) {
174: this (fd.getName(), fd.getFieldType(), fd, false);
175: }
176:
177: /**
178: * @return the name of the cmp field
179: */
180: public String getName() {
181: return mName;
182: }
183:
184: /**
185: * @return the name, with the first letter capitalized, of the cmp field
186: */
187: public String getNameUpperFirst() {
188: return Character.toUpperCase(mName.charAt(0))
189: + mName.substring(1);
190: }
191:
192: public String jormGetter() {
193: return "paGet" + getNameUpperFirst();
194: }
195:
196: public String jormSetter() {
197: return "paSet" + getNameUpperFirst();
198: }
199:
200: /**
201: * @return the name of the getter method of the cmp field
202: */
203: public String getGetterName() {
204: return "get" + Character.toUpperCase(mName.charAt(0))
205: + mName.substring(1);
206: }
207:
208: /**
209: * @return the name of the setter method of the cmp field
210: */
211: public String getSetterName() {
212: return "set" + Character.toUpperCase(mName.charAt(0))
213: + mName.substring(1);
214: }
215:
216: /**
217: * @return the type name of the cmp field
218: */
219: public String getTypeName() {
220: return mTypeName;
221: }
222:
223: /**
224: * @return true if the cmp field composes the primary key
225: */
226: public boolean isPrimaryKey() {
227: return isPrimaryKey;
228: }
229:
230: /*
231: * CMP version 1 only
232: */
233: /**
234: * @return the string representation of the java default value for the cmp field (ie "0 "for int, "null" for object ...)
235: */
236: public String getDefaultValue() {
237: return mDefaultValue;
238: }
239:
240: /**
241: * @return the SQL type name of the cmp field
242: */
243: public String getSqlTypeName() {
244: return mSqlTypeName;
245: }
246:
247: /**
248: * @return the SQL getter method name for the cmp field
249: */
250: public String getSqlGetMethod() {
251: return mSqlGetMethod;
252: }
253:
254: /**
255: * @return the SQL setter method name for the cmp field
256: */
257: public String getSqlSetMethod() {
258: return mSqlSetMethod;
259: }
260:
261: /**
262: * @return true if the type of the cmp field is not a java primitive type
263: */
264: public boolean hasNotPrimitiveType() {
265: return hasNotPrimitiveType;
266: }
267:
268: /**
269: * @return true if the type of the cmp field is java.math.BigInteger
270: */
271: public boolean hasBigIntegerType() {
272: return hasBigIntegerType;
273: }
274:
275: /**
276: * @return true of the type of the cmp field is Serializable
277: */
278: public boolean hasSerializableType() {
279: return hasSerializableType;
280: }
281:
282: /**
283: * @return true if the type of the cmp type is a java.lag type except java.lang.String
284: */
285: public boolean hasJavaLangTypeExceptString() {
286: return hasJavaLangTypeExceptString;
287: }
288:
289: /*
290: * CMP version 2 only
291: */
292: /**
293: * @return the JORM type name of the cmp field
294: */
295: public String getJormTypeName() {
296: return jormTypeName;
297: }
298:
299: /**
300: * @return true if a conversion must be dome between the memory representation and the storage representation for the cmp field
301: */
302: public boolean isMustBeConvert() {
303: return mustBeConvert;
304: }
305:
306: /**
307: * @return the class name of for the convertion between the memory representation and the storage representation for the cmp field
308: */
309: public String getConvertClassName() {
310: return convertClassName;
311: }
312:
313: /**
314: * @return a string representation of the VcField for debug use
315: */
316: public String toString() {
317: StringBuffer ret = new StringBuffer();
318: ret.append("\n Name = " + getName());
319: ret.append("\n isPrimaryKey = " + isPrimaryKey());
320: ret.append("\n NameUpperFirst = "
321: + getNameUpperFirst());
322: ret.append("\n GetterName = " + getGetterName());
323: ret.append("\n SetterName = " + getSetterName());
324: ret.append("\n TypeName = " + getTypeName());
325: ret.append("\n DefaultValue = " + getDefaultValue());
326: if (mIsCmp1) {
327: ret.append("\n SqlTypeName = "
328: + getSqlTypeName());
329: ret.append("\n SqlGetMethod = "
330: + getSqlGetMethod());
331: ret.append("\n SqlSetMethod = "
332: + getSqlSetMethod());
333: ret.append("\n hasNotPrimitiveType = "
334: + hasNotPrimitiveType());
335: ret.append("\n hasBigIntegerType = "
336: + hasBigIntegerType());
337: ret.append("\n hasSerializableType = "
338: + hasSerializableType());
339: ret.append("\n hasJavaLangTypeExceptString = "
340: + hasJavaLangTypeExceptString());
341: } else {
342: ret.append("\n JormTypeName = "
343: + getJormTypeName());
344: ret.append("\n MustBeConvert = "
345: + isMustBeConvert());
346: ret.append("\n ConvertClassName = "
347: + getConvertClassName());
348: }
349: return (ret.toString());
350: }
351:
352: }
|