001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.amber.type;
030:
031: import com.caucho.amber.manager.AmberPersistenceUnit;
032: import com.caucho.bytecode.JClass;
033: import com.caucho.java.JavaWriter;
034: import com.caucho.util.L10N;
035:
036: import javax.persistence.TemporalType;
037: import java.io.IOException;
038: import java.sql.PreparedStatement;
039: import java.sql.ResultSet;
040: import java.sql.SQLException;
041: import java.sql.Types;
042:
043: /**
044: * The type of a property.
045: */
046: public class UtilDateType extends Type {
047: private static final L10N L = new L10N(UtilDateType.class);
048:
049: public static final UtilDateType TEMPORAL_DATE_TYPE = new UtilDateType(
050: TemporalType.DATE);
051: public static final UtilDateType TEMPORAL_TIME_TYPE = new UtilDateType(
052: TemporalType.TIME);
053: public static final UtilDateType TEMPORAL_TIMESTAMP_TYPE = new UtilDateType(
054: TemporalType.TIMESTAMP);
055:
056: private TemporalType _temporalType;
057:
058: private UtilDateType(TemporalType temporalType) {
059: _temporalType = temporalType;
060: }
061:
062: /**
063: * Returns the singleton UtilDate type.
064: */
065: public static UtilDateType create() {
066: return TEMPORAL_TIMESTAMP_TYPE;
067: }
068:
069: /**
070: * Returns the type name.
071: */
072: public String getName() {
073: return "java.util.Date";
074: }
075:
076: /**
077: * Returns true if the value is assignable to the Java type.
078: */
079: @Override
080: public boolean isAssignableTo(JClass javaType) {
081: return javaType.isAssignableFrom(java.util.Date.class);
082: }
083:
084: /**
085: * Generates the type for the table.
086: */
087: public String generateCreateColumnSQL(AmberPersistenceUnit manager,
088: int length, int precision, int scale) {
089: return manager.getCreateColumnSQL(Types.TIMESTAMP, length,
090: precision, scale);
091: }
092:
093: /**
094: * Generates a string to load the property.
095: */
096: public int generateLoad(JavaWriter out, String rs, String indexVar,
097: int index) throws IOException {
098: out.print("com.caucho.amber.type.UtilDateType.toDate(" + rs
099: + ".getTimestamp(" + indexVar + " + " + index + "))");
100:
101: return index + 1;
102: }
103:
104: /**
105: * Generates a string to set the property.
106: */
107: public void generateSet(JavaWriter out, String pstmt, String index,
108: String value) throws IOException {
109: out.println("if (" + value + " == null)");
110: out.println(" " + pstmt + ".setNull(" + index
111: + "++, java.sql.Types.TIMESTAMP);");
112: out.println("else");
113: out.println(" " + pstmt + ".setTimestamp(" + index
114: + "++, new java.sql.Timestamp(" + value
115: + ".getTime()));");
116: }
117:
118: /**
119: * Gets the value.
120: */
121: public static java.util.Date toDate(java.sql.Timestamp time)
122: throws SQLException {
123: return time;
124: }
125:
126: /**
127: * Gets the value.
128: */
129: public Object getObject(ResultSet rs, int index)
130: throws SQLException {
131: java.sql.Timestamp date = rs.getTimestamp(index);
132:
133: if (date == null)
134: return null;
135: else
136: return new java.util.Date(date.getTime());
137: }
138:
139: /**
140: * Sets the value.
141: */
142: public void setParameter(PreparedStatement pstmt, int index,
143: Object value) throws SQLException {
144: switch (_temporalType) {
145: case DATE:
146: pstmt.setObject(index, value, Types.DATE);
147: break;
148:
149: case TIME:
150: pstmt.setObject(index, value, Types.TIME);
151: break;
152:
153: default:
154: pstmt.setObject(index, value, Types.TIMESTAMP);
155: break;
156: }
157: }
158: }
|