KickJava   Java API By Example, From Geeks To Geeks.

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


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.java.JavaWriter;
32 import com.caucho.util.L10N;
33
34 import javax.persistence.TemporalType;
35 import java.io.IOException JavaDoc;
36 import java.sql.PreparedStatement JavaDoc;
37 import java.sql.ResultSet JavaDoc;
38 import java.sql.SQLException JavaDoc;
39 import java.sql.Timestamp JavaDoc;
40 import java.sql.Types JavaDoc;
41 import java.util.Calendar JavaDoc;
42
43 /**
44  * The type of a property.
45  */

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

66   public static CalendarType 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.Calendar";
77   }
78
79   /**
80    * Generates a string to load the property.
81    */

82   public int generateLoad(JavaWriter out, String JavaDoc rs,
83         String JavaDoc indexVar, int index)
84     throws IOException JavaDoc
85   {
86     out.print("com.caucho.amber.type.CalendarType.toCalendar(" + rs + ".getTimestamp(" + indexVar + " + " + index + "))");
87
88     return index + 1;
89   }
90
91   /**
92    * Generates a string to set the property.
93    */

94   public void generateSet(JavaWriter out, String JavaDoc pstmt,
95         String JavaDoc index, String JavaDoc value)
96     throws IOException JavaDoc
97   {
98     out.println("if (" + value + " == null)");
99     out.println(" " + pstmt + ".setNull(" + index + "++, java.sql.Types.TIMESTAMP);");
100     out.println("else");
101     out.println(" " + pstmt + ".setTimestamp(" + index + "++, new java.sql.Timestamp(" + value + ".getTimeInMillis()));");
102   }
103
104   /**
105    * Gets the value.
106    */

107   public static Calendar JavaDoc toCalendar(java.util.Date JavaDoc time)
108     throws SQLException JavaDoc
109   {
110     if (time == null)
111       return null;
112     else {
113       Calendar JavaDoc cal = Calendar.getInstance();
114       cal.setTime(time);
115
116       return cal;
117     }
118   }
119
120   /**
121    * Gets the value.
122    */

123   public Object JavaDoc getObject(ResultSet JavaDoc rs, int index)
124     throws SQLException JavaDoc
125   {
126     java.sql.Timestamp JavaDoc time = rs.getTimestamp(index);
127
128     if (time == null)
129       return null;
130     else {
131       Calendar JavaDoc cal = Calendar.getInstance();
132       cal.setTime(time);
133
134       return cal;
135     }
136   }
137
138   /**
139    * Sets the value.
140    */

141   public void setParameter(PreparedStatement JavaDoc pstmt,
142                            int index,
143                            Object JavaDoc value)
144     throws SQLException JavaDoc
145   {
146     Timestamp JavaDoc timestamp
147       = new Timestamp JavaDoc(((Calendar JavaDoc) value).getTimeInMillis());
148
149     switch (_temporalType) {
150     case DATE:
151       pstmt.setObject(index, timestamp, Types.DATE);
152       break;
153
154     case TIME:
155       pstmt.setObject(index, timestamp, Types.TIME);
156       break;
157
158     default:
159       pstmt.setObject(index, timestamp, Types.TIMESTAMP);
160       break;
161     }
162   }
163 }
164
Popular Tags