KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > type > EnumType


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free Software Foundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Rodrigo Westrupp
27  */

28
29 package com.caucho.amber.type;
30
31 import com.caucho.amber.manager.AmberPersistenceUnit;
32 import com.caucho.bytecode.JClass;
33 import com.caucho.java.JavaWriter;
34 import com.caucho.util.L10N;
35 import com.caucho.util.Log;
36
37 import java.io.IOException JavaDoc;
38 import java.lang.reflect.Method JavaDoc;
39 import java.sql.PreparedStatement JavaDoc;
40 import java.sql.ResultSet JavaDoc;
41 import java.sql.SQLException JavaDoc;
42 import java.sql.Types JavaDoc;
43 import java.util.logging.Level JavaDoc;
44 import java.util.logging.Logger JavaDoc;
45
46 /**
47  * The enum type.
48  */

49 public class EnumType extends Type {
50   private static final Logger JavaDoc log = Log.open(EnumType.class);
51   private static final L10N L = new L10N(EnumType.class);
52
53   private JClass _beanClass;
54
55   private String JavaDoc _name;
56
57   private boolean _isOrdinal = true;
58
59
60   public EnumType()
61   {
62   }
63
64   /**
65    * Gets the bean class.
66    */

67   public JClass getBeanClass()
68   {
69     return _beanClass;
70   }
71
72   /**
73    * Sets the bean class.
74    */

75   public void setBeanClass(JClass beanClass)
76   {
77     _beanClass = beanClass;
78   }
79
80   /**
81    * Returns the type name.
82    */

83   public String JavaDoc getName()
84   {
85     return _name;
86   }
87
88   /**
89    * Sets the name.
90    */

91   public void setName(String JavaDoc name)
92   {
93     _name = name;
94   }
95
96   /**
97    * Returns true for a numeric type.
98    */

99   public boolean isNumeric()
100   {
101     return isOrdinal();
102   }
103
104   /**
105    * Returns true for ordinal
106    */

107   public boolean isOrdinal()
108   {
109     return _isOrdinal;
110   }
111
112   /**
113    * Sets true for ordinal
114    */

115   public void setOrdinal(boolean isOrdinal)
116   {
117     _isOrdinal = isOrdinal;
118   }
119
120   /**
121    * Returns the type as a foreign key.
122    */

123   public Type getForeignType()
124   {
125     return IntegerType.create();
126   }
127
128   /**
129    * Generates the type for the table.
130    */

131   public String JavaDoc generateCreateColumnSQL(AmberPersistenceUnit manager, int length, int precision, int scale)
132   {
133     if (_isOrdinal)
134       return manager.getCreateColumnSQL(Types.INTEGER, length, precision, scale);
135     else {
136       if (length == 0)
137         length = 255;
138
139       return "varchar(" + length + ")";
140     }
141   }
142
143   /**
144    * Generates a string to load the property.
145    */

146   public int generateLoad(JavaWriter out, String JavaDoc rs,
147                           String JavaDoc indexVar, int index)
148     throws IOException JavaDoc
149   {
150     if (_isOrdinal) {
151       out.print("(" + getName() + ") com.caucho.amber.type.EnumType.toEnum(" +
152                 rs + ".getInt(" + indexVar + " + " + index + "), " +
153                 rs + ".wasNull(), "+
154                 getName() + ".values())");
155     }
156     else {
157       out.print("(" + getName() + ") com.caucho.amber.type.EnumType.toEnum(" +
158                 rs + ".getString(" + indexVar + " + " + index + "), " +
159                 rs + ".wasNull(), "+
160                 getName() + ".class)");
161     }
162
163     return index + 1;
164   }
165
166   /**
167    * Generates a string to load the property.
168    */

169   public int generateLoadForeign(JavaWriter out, String JavaDoc rs,
170                                  String JavaDoc indexVar, int index)
171     throws IOException JavaDoc
172   {
173     if (_isOrdinal) {
174       out.print("(" + getName() + ") com.caucho.amber.type.EnumType.toEnum(" +
175                 rs + ".getInt(" + indexVar + " + " + index + "), " +
176                 rs + ".wasNull(), "+
177                 getName() + ".values())");
178     }
179     else {
180       out.print("(" + getName() + ") com.caucho.amber.type.EnumType.toEnum(" +
181                 rs + ".getString(" + indexVar + " + " + index + "), " +
182                 rs + ".wasNull(), "+
183                 getName() + ".class)");
184     }
185
186     return index + 1;
187   }
188
189   /**
190    * Generates a string to set the property.
191    */

192   public void generateSet(JavaWriter out, String JavaDoc pstmt,
193                           String JavaDoc index, String JavaDoc value)
194     throws IOException JavaDoc
195   {
196     if (_isOrdinal) {
197       out.println("if (" + value + " == null)");
198       out.println(" " + pstmt + ".setNull(" + index + "++, java.sql.Types.INTEGER);");
199       out.println("else");
200       out.println(" " + pstmt + ".setInt(" + index + "++, " + value + ".ordinal());");
201     }
202     else {
203       if (pstmt.startsWith("query"))
204         out.println(pstmt + ".setString(" + index + "++, " + value + ");");
205       else
206         out.println("__caucho_setInternalString(" + pstmt + ", " + index + "++, " + value + " == null ? null : " + value + ".toString());");
207     }
208   }
209
210   /**
211    * Sets the value.
212    */

213   public void setParameter(PreparedStatement JavaDoc pstmt, int index, Object JavaDoc value)
214     throws SQLException JavaDoc
215   {
216     if (_isOrdinal) {
217       if (value == null)
218         pstmt.setNull(index, Types.INTEGER);
219       else
220         pstmt.setInt(index, ((Enum JavaDoc) value).ordinal());
221     }
222     else {
223       if (value == null)
224         pstmt.setNull(index, java.sql.Types.OTHER);
225       else
226         pstmt.setString(index, value.toString());
227     }
228   }
229
230   /**
231    * Converts to an object.
232    */

233   public String JavaDoc toObject(String JavaDoc value)
234   {
235     return value;
236   }
237
238   /**
239    * Converts the value.
240    */

241   public String JavaDoc generateCastFromObject(String JavaDoc value)
242   {
243     return value + ".ordinal()";
244   }
245
246   /**
247    * Converts a value to a enum.
248    */

249   public static Object JavaDoc toEnum(int ordinal,
250                               boolean wasNull,
251                               Object JavaDoc values[])
252   {
253     if (wasNull)
254       return null;
255     else
256       return values[ordinal];
257   }
258
259   /**
260    * Converts a value to a enum.
261    */

262   public static Object JavaDoc toEnum(String JavaDoc name,
263                               boolean wasNull,
264                               Class JavaDoc cl)
265   {
266     if (wasNull)
267       return null;
268     else
269       return Enum.valueOf(cl, name);
270   }
271
272   /**
273    * Gets the value.
274    */

275   public Object JavaDoc getObject(ResultSet JavaDoc rs, int index)
276     throws SQLException JavaDoc
277   {
278     if (_isOrdinal) {
279       Object JavaDoc[] values = getValues();
280
281       if (values == null)
282         return null;
283
284       int v = rs.getInt(index);
285
286       return rs.wasNull() ? null : values[v];
287     }
288     else {
289       Class JavaDoc cl = getBeanClass().getJavaClass();
290
291       String JavaDoc name = rs.getString(index);
292
293       return rs.wasNull() ? null : Enum.valueOf(cl, name);
294     }
295   }
296
297   /**
298    * Converts to an object.
299    */

300   public Object JavaDoc toObject(long value)
301   {
302     if (_isOrdinal) {
303       Object JavaDoc[] values = getValues();
304
305       if (values == null)
306         return null;
307
308       return values[(int) value];
309     }
310
311     return null;
312   }
313
314   private Object JavaDoc[] getValues()
315   {
316     try {
317       Class JavaDoc cl = getBeanClass().getJavaClass();
318
319       Method JavaDoc method = cl.getDeclaredMethod("values", null);
320
321       Object JavaDoc object = method.invoke(cl, null);
322
323       return (Object JavaDoc []) object;
324     } catch (Exception JavaDoc e) {
325       log.log(Level.FINE, e.toString(), e);
326       return null;
327     }
328   }
329 }
330
Popular Tags