KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > types > UtilDateType


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.access.types;
21
22 import java.sql.CallableStatement JavaDoc;
23 import java.sql.PreparedStatement JavaDoc;
24 import java.sql.ResultSet JavaDoc;
25 import java.sql.Types JavaDoc;
26 import java.util.Date JavaDoc;
27
28 import org.apache.cayenne.CayenneRuntimeException;
29 import org.apache.cayenne.dba.TypesMapping;
30 import org.apache.cayenne.map.DbAttribute;
31 import org.apache.cayenne.validation.ValidationResult;
32
33 /**
34  * Maps <code>java.util.Date</code> to any of the three database date/time types: TIME,
35  * DATE, TIMESTAMP.
36  *
37  * @author Andrus Adamchik
38  */

39 public class UtilDateType extends AbstractType {
40
41     /**
42      * Returns "java.util.Date".
43      */

44     public String JavaDoc getClassName() {
45         return Date JavaDoc.class.getName();
46     }
47
48     /**
49      * Always returns true indicating no validation failures. There is no date-specific
50      * validations at the moment.
51      *
52      * @since 1.1
53      * @deprecated since 3.0 as validation should not be done at the DataNode level.
54      */

55     public boolean validateProperty(
56             Object JavaDoc source,
57             String JavaDoc property,
58             Object JavaDoc value,
59             DbAttribute dbAttribute,
60             ValidationResult validationResult) {
61         return true;
62     }
63
64     protected Object JavaDoc convertToJdbcObject(Object JavaDoc val, int type) throws Exception JavaDoc {
65         if (type == Types.DATE)
66             return new java.sql.Date JavaDoc(((Date JavaDoc) val).getTime());
67         else if (type == Types.TIME)
68             return new java.sql.Time JavaDoc(((Date JavaDoc) val).getTime());
69         else if (type == Types.TIMESTAMP)
70             return new java.sql.Timestamp JavaDoc(((Date JavaDoc) val).getTime());
71         else
72             throw new IllegalArgumentException JavaDoc(
73                     "Only DATE, TIME or TIMESTAMP can be mapped as '"
74                             + getClassName()
75                             + "', got "
76                             + TypesMapping.getSqlNameByType(type));
77     }
78
79     public Object JavaDoc materializeObject(ResultSet JavaDoc rs, int index, int type) throws Exception JavaDoc {
80         Date JavaDoc val = null;
81
82         switch (type) {
83             case Types.TIMESTAMP:
84                 val = rs.getTimestamp(index);
85                 break;
86             case Types.DATE:
87                 val = rs.getDate(index);
88                 break;
89             case Types.TIME:
90                 val = rs.getTime(index);
91                 break;
92             default:
93                 // here the driver can "surpirse" us
94
// check the type of returned value...
95
Object JavaDoc object = rs.getObject(index);
96
97                 if (object != null && !(object instanceof Date JavaDoc)) {
98                     throw new CayenneRuntimeException(
99                             "Expected an instance of java.util.Date, instead got "
100                                     + object.getClass().getName()
101                                     + ", column index: "
102                                     + index);
103                 }
104
105                 val = (Date JavaDoc) object;
106                 break;
107         }
108
109         return (rs.wasNull()) ? null : new Date JavaDoc(val.getTime());
110     }
111
112     public Object JavaDoc materializeObject(CallableStatement JavaDoc cs, int index, int type)
113             throws Exception JavaDoc {
114         Object JavaDoc val = null;
115
116         switch (type) {
117             case Types.TIMESTAMP:
118                 val = cs.getTimestamp(index);
119                 break;
120             case Types.DATE:
121                 val = cs.getDate(index);
122                 break;
123             case Types.TIME:
124                 val = cs.getTime(index);
125                 break;
126             default:
127                 val = cs.getObject(index);
128                 // check if value was properly converted by the driver
129
if (val != null && !(val instanceof java.util.Date JavaDoc)) {
130                     String JavaDoc typeName = TypesMapping.getSqlNameByType(type);
131                     throw new ClassCastException JavaDoc(
132                             "Expected a java.util.Date or subclass, instead fetched '"
133                                     + val.getClass().getName()
134                                     + "' for JDBC type "
135                                     + typeName);
136                 }
137                 break;
138         }
139
140         // all sql time/date classes are subclasses of java.util.Date,
141
// so lets cast it to Date,
142
// if it is not date, ClassCastException will be thrown,
143
// which is what we want
144
return (cs.wasNull()) ? null : new java.util.Date JavaDoc(((java.util.Date JavaDoc) val)
145                 .getTime());
146     }
147
148     public void setJdbcObject(
149             PreparedStatement JavaDoc st,
150             Object JavaDoc val,
151             int pos,
152             int type,
153             int precision) throws Exception JavaDoc {
154         super.setJdbcObject(st, convertToJdbcObject(val, type), pos, type, precision);
155     }
156 }
157
Popular Tags