KickJava   Java API By Example, From Geeks To Geeks.

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


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 Scott Ferguson
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
36 import javax.persistence.TemporalType;
37 import java.io.IOException JavaDoc;
38 import java.sql.PreparedStatement JavaDoc;
39 import java.sql.ResultSet JavaDoc;
40 import java.sql.SQLException JavaDoc;
41 import java.sql.Types JavaDoc;
42
43 /**
44  * The type of a property.
45  */

46 public class UtilDateType extends Type {
47   private static final L10N L = new L10N(UtilDateType.class);
48
49   public static final UtilDateType
50     TEMPORAL_DATE_TYPE = new UtilDateType(TemporalType.DATE);
51   public static final UtilDateType
52     TEMPORAL_TIME_TYPE = new UtilDateType(TemporalType.TIME);
53   public static final UtilDateType
54     TEMPORAL_TIMESTAMP_TYPE = new UtilDateType(TemporalType.TIMESTAMP);
55
56   private TemporalType _temporalType;
57
58   private UtilDateType(TemporalType temporalType)
59   {
60     _temporalType = temporalType;
61   }
62
63   /**
64    * Returns the singleton UtilDate type.
65    */

66   public static UtilDateType create()
67   {
68     return TEMPORAL_TIMESTAMP_TYPE;
69   }
70
71   /**
72    * Returns the type name.
73    */

74   public String JavaDoc getName()
75   {
76     return "java.util.Date";
77   }
78
79   /**
80    * Returns true if the value is assignable to the Java type.
81    */

82   @Override JavaDoc
83   public boolean isAssignableTo(JClass javaType)
84   {
85     return javaType.isAssignableFrom(java.util.Date JavaDoc.class);
86   }
87
88   /**
89    * Generates the type for the table.
90    */

91   public String JavaDoc generateCreateColumnSQL(AmberPersistenceUnit manager,
92                                         int length,
93                                         int precision,
94                                         int scale)
95   {
96     return manager.getCreateColumnSQL(Types.TIMESTAMP, length, precision, scale);
97   }
98
99   /**
100    * Generates a string to load the property.
101    */

102   public int generateLoad(JavaWriter out,
103                           String JavaDoc rs,
104                           String JavaDoc indexVar,
105                           int index)
106     throws IOException JavaDoc
107   {
108     out.print("com.caucho.amber.type.UtilDateType.toDate(" + rs + ".getTimestamp(" + indexVar + " + " + index + "))");
109
110     return index + 1;
111   }
112
113   /**
114    * Generates a string to set the property.
115    */

116   public void generateSet(JavaWriter out,
117                           String JavaDoc pstmt,
118                           String JavaDoc index,
119                           String JavaDoc value)
120     throws IOException JavaDoc
121   {
122     out.println("if (" + value + " == null)");
123     out.println(" " + pstmt + ".setNull(" + index + "++, java.sql.Types.TIMESTAMP);");
124     out.println("else");
125     out.println(" " + pstmt + ".setTimestamp(" + index + "++, new java.sql.Timestamp(" + value + ".getTime()));");
126   }
127
128   /**
129    * Gets the value.
130    */

131   public static java.util.Date JavaDoc toDate(java.sql.Timestamp JavaDoc time)
132     throws SQLException JavaDoc
133   {
134     return time;
135   }
136
137   /**
138    * Gets the value.
139    */

140   public Object JavaDoc getObject(ResultSet JavaDoc rs, int index)
141     throws SQLException JavaDoc
142   {
143     java.sql.Timestamp JavaDoc date = rs.getTimestamp(index);
144
145     if (date == null)
146       return null;
147     else
148       return new java.util.Date JavaDoc(date.getTime());
149   }
150
151   /**
152    * Sets the value.
153    */

154   public void setParameter(PreparedStatement JavaDoc pstmt,
155                            int index,
156                            Object JavaDoc value)
157     throws SQLException JavaDoc
158   {
159     switch (_temporalType) {
160     case DATE:
161       pstmt.setObject(index, value, Types.DATE);
162       break;
163
164     case TIME:
165       pstmt.setObject(index, value, Types.TIME);
166       break;
167
168     default:
169       pstmt.setObject(index, value, Types.TIMESTAMP);
170       break;
171     }
172   }
173 }
174
Popular Tags