KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ejb > lib > JormType


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: JormType.java,v 1.12 2004/09/17 08:25:02 joaninh Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_ejb.lib;
27
28 import javax.ejb.EJBLocalObject JavaDoc;
29 import org.objectweb.jorm.type.api.PType;
30 import org.objectweb.jorm.type.api.PTypeSpace;
31 import org.objectweb.jorm.naming.api.PNamingContext;
32 import org.objectweb.jorm.naming.api.PExceptionNaming;
33 import org.objectweb.medor.type.lib.PTypeSpaceMedor;
34
35
36 /**
37  * This class contains several methods to manipulate a PType.
38  *
39  * @author Sebastien Chassande-Barrioz : Initial developper
40  * @author Helene Joanin
41  */

42 public class JormType {
43
44     /**
45      * This method converts a java.lang.Class into a jorm PType.
46      * @param cl is a java.lang.Class
47      * @param isPkField true if this is for a field of the primary key, false otherwise
48      * @return The PType which matches the class specified in parameter.
49      */

50     public static PType getPType(java.lang.Class JavaDoc cl, boolean isPkField) {
51         if (cl.equals(Boolean.TYPE)) {
52             return PTypeSpace.BOOLEAN;
53         } else if (cl.equals(Boolean JavaDoc.class)) {
54             return PTypeSpace.OBJBOOLEAN;
55         } else if (cl.equals(Character.TYPE)) {
56             return PTypeSpace.CHAR;
57         } else if (cl.equals(Character JavaDoc.class)) {
58             return PTypeSpace.OBJCHAR;
59         } else if (cl.equals(Byte.TYPE)) {
60             return PTypeSpace.BYTE;
61         } else if (cl.equals(Byte JavaDoc.class)) {
62             return PTypeSpace.OBJBYTE;
63         } else if (cl.equals(Short.TYPE)) {
64             return PTypeSpace.SHORT;
65         } else if (cl.equals(Short JavaDoc.class)) {
66             return PTypeSpace.OBJSHORT;
67         } else if (cl.equals(Integer.TYPE)) {
68             return PTypeSpace.INT;
69         } else if (cl.equals(Integer JavaDoc.class)) {
70             return PTypeSpace.OBJINT;
71         } else if (cl.equals(Long.TYPE)) {
72             return PTypeSpace.LONG;
73         } else if (cl.equals(Long JavaDoc.class)) {
74             return PTypeSpace.OBJLONG;
75         } else if (cl.equals(Float.TYPE)) {
76             return PTypeSpace.FLOAT;
77         } else if (cl.equals(Float JavaDoc.class)) {
78             if (isPkField) {
79                 return (JormType.getPType(FloatPkFieldMapping.getStorageType(), false));
80             } else {
81                 return PTypeSpace.OBJFLOAT;
82             }
83         } else if (cl.equals(Double.TYPE)) {
84             return PTypeSpace.DOUBLE;
85         } else if (cl.equals(Double JavaDoc.class)) {
86             return PTypeSpace.OBJDOUBLE;
87         } else if (cl.equals(String JavaDoc.class)) {
88             return PTypeSpace.STRING;
89         } else if (cl.equals(java.util.Date JavaDoc.class)
90                    || cl.equals(java.sql.Date JavaDoc.class)
91                    || cl.equals(java.sql.Time JavaDoc.class)
92                    || cl.equals(java.sql.Timestamp JavaDoc.class)) {
93             return PTypeSpace.DATE;
94         } else if (cl.equals(java.math.BigDecimal JavaDoc.class)) {
95             return PTypeSpace.BIGDECIMAL;
96         } else if (EJBLocalObject JavaDoc.class.isAssignableFrom(cl)) {
97             return PTypeSpaceMedor.PNAME;
98         } else if (cl.isArray() && java.lang.Byte.TYPE.equals(cl.getComponentType())) {
99             return PTypeSpace.BYTEARRAY;
100         } else if (cl.isArray() && java.lang.Character.TYPE.equals(cl.getComponentType())) {
101             return PTypeSpace.CHARARRAY;
102         }
103         return PTypeSpace.SERIALIZED;
104     }
105
106     /**
107      * This method converts a java.lang.Class into a jorm PType.
108      * @param cl is a java.lang.Class
109      * @param isPkField true if this is for a field of the primary key, false otherwise
110      * @return A String that represents The PType which matches the class.
111      */

112     public static String JavaDoc getPTypeDef(Class JavaDoc cl, boolean isPkField) {
113         if (cl.equals(Boolean.TYPE)) {
114             return "PTypeSpace.BOOLEAN";
115         } else if (cl.equals(Boolean JavaDoc.class)) {
116             return "PTypeSpace.OBJBOOLEAN";
117         } else if (cl.equals(String JavaDoc.class)) {
118             return "PTypeSpace.STRING";
119         } else if (cl.equals(Character.TYPE)) {
120             return "PTypeSpace.CHAR";
121         } else if (cl.equals(Character JavaDoc.class)) {
122             return "PTypeSpace.OBJCHAR";
123         } else if (cl.equals(Byte.TYPE)) {
124             return "PTypeSpace.BYTE";
125         } else if (cl.equals(Byte JavaDoc.class)) {
126             return "PTypeSpace.OBJBYTE";
127         } else if (cl.equals(Short.TYPE)) {
128             return "PTypeSpace.SHORT";
129         } else if (cl.equals(Short JavaDoc.class)) {
130             return "PTypeSpace.OBJSHORT";
131         } else if (cl.equals(Integer.TYPE)) {
132             return "PTypeSpace.INT";
133         } else if (cl.equals(Integer JavaDoc.class)) {
134             return "PTypeSpace.OBJINT";
135         } else if (cl.equals(Long.TYPE)) {
136             return "PTypeSpace.LONG";
137         } else if (cl.equals(Long JavaDoc.class)) {
138             return "PTypeSpace.OBJLONG";
139         } else if (cl.equals(Float.TYPE)) {
140             return "PTypeSpace.FLOAT";
141         } else if (cl.equals(Float JavaDoc.class)) {
142             if (isPkField) {
143                 return (JormType.getPTypeDef(FloatPkFieldMapping.getStorageType(), false));
144             } else {
145                 return "PTypeSpace.OBJFLOAT";
146             }
147         } else if (cl.equals(Double.TYPE)) {
148             return "PTypeSpace.DOUBLE";
149         } else if (cl.equals(Double JavaDoc.class)) {
150             return "PTypeSpace.OBJDOUBLE";
151         } else if (cl.equals(java.util.Date JavaDoc.class)
152                    || cl.equals(java.sql.Date JavaDoc.class)
153                    || cl.equals(java.sql.Time JavaDoc.class)
154                    || cl.equals(java.sql.Timestamp JavaDoc.class)) {
155             return "PTypeSpace.DATE";
156         } else if (cl.equals(java.math.BigDecimal JavaDoc.class)) {
157             return "PTypeSpace.BIGDECIMAL";
158         } else if (EJBLocalObject JavaDoc.class.isAssignableFrom(cl)) {
159             return "PTypeSpaceMedor.PNAME";
160         } else if (cl.isArray() && java.lang.Byte.TYPE.equals(cl.getComponentType())) {
161             return "PTypeSpace.BYTEARRAY";
162         } else if (cl.isArray() && java.lang.Character.TYPE.equals(cl.getComponentType())) {
163             return "PTypeSpace.CHARARRAY";
164         }
165         return "PTypeSpace.SERIALIZED";
166     }
167
168     /**
169      * It retrieves a coding type either a Class
170      * @param the Class
171      * @param isPkField true if this is for a field of the primary key, false otherwise
172      * @return a coding type wich matches to one of CTxxx fields of the
173      * PNamingcontext interface.
174      * @throws PExceptionNaming when the class specified is not support in
175      * the jorm naming.
176      */

177     public static short getCodingType(Class JavaDoc c, boolean isPkField) throws PExceptionNaming {
178         if (c == null) {
179             throw new PExceptionNaming("No CodingType associated to a null Class");
180         }
181         return getCodingType(getPType(c, isPkField).getTypeCode());
182     }
183
184     /**
185      * It converts a PType into a coding type.
186      * @param a PType
187      * @return a coding type wich matches to one of CTxxx fields of the
188      * PNamingcontext interface.
189      * @throws PExceptionNaming when the PType specified is not support in
190      * the jorm naming.
191      */

192     public static short getCodingType(PType pt) throws PExceptionNaming {
193         if (pt == null) {
194             throw new PExceptionNaming("No CodingType associated to a null PType");
195         }
196         return getCodingType(pt.getTypeCode());
197     }
198
199     /**
200      * It converts a type code into a coding type.
201      * @param a type code of a PType
202      * @return a coding type wich matches to one of CTxxx fields of the
203      * PNamingcontext interface.
204      * @throws PExceptionNaming when the type code specified is not support in
205      * the jorm naming.
206      */

207     public static short getCodingType(int typeCode) throws PExceptionNaming {
208         switch(typeCode) {
209         case PType.TYPECODE_CHAR:
210             return PNamingContext.CTCHAR;
211         case PType.TYPECODE_OBJCHAR:
212             return PNamingContext.CTOCHAR;
213
214         case PType.TYPECODE_BYTE:
215             return PNamingContext.CTBYTE;
216         case PType.TYPECODE_OBJBYTE:
217             return PNamingContext.CTOBYTE;
218
219         case PType.TYPECODE_SHORT:
220             return PNamingContext.CTSHORT;
221         case PType.TYPECODE_OBJSHORT:
222             return PNamingContext.CTOSHORT;
223
224         case PType.TYPECODE_INT:
225             return PNamingContext.CTINT;
226         case PType.TYPECODE_OBJINT:
227             return PNamingContext.CTOINT;
228
229         case PType.TYPECODE_LONG:
230             return PNamingContext.CTLONG;
231         case PType.TYPECODE_OBJLONG:
232             return PNamingContext.CTOLONG;
233
234         case PType.TYPECODE_BYTEARRAY:
235             return PNamingContext.CTBYTEARRAY;
236
237         case PType.TYPECODE_CHARARRAY:
238             return PNamingContext.CTCHARARRAY;
239
240         case PType.TYPECODE_STRING:
241             return PNamingContext.CTSTRING;
242
243         case PType.TYPECODE_DATE:
244             return PNamingContext.CTDATE;
245
246         default:
247             throw new PExceptionNaming("This type is not suportted: typeCode=" + typeCode);
248         }
249     }
250
251 }
252
Popular Tags