KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.cayenne.access.types;
20
21 import java.sql.CallableStatement JavaDoc;
22 import java.sql.PreparedStatement JavaDoc;
23 import java.sql.ResultSet JavaDoc;
24 import java.sql.Types JavaDoc;
25 import java.util.Calendar JavaDoc;
26 import java.util.Date JavaDoc;
27 import java.util.GregorianCalendar JavaDoc;
28
29 import org.apache.cayenne.CayenneRuntimeException;
30 import org.apache.cayenne.dba.TypesMapping;
31 import org.apache.cayenne.map.DbAttribute;
32 import org.apache.cayenne.validation.ValidationResult;
33
34 /**
35  * ExtendedType that handles {@link java.util.Calendar} fields.
36  *
37  * @since 3.0
38  * @author Andrus Adamchik
39  */

40 public class CalendarType implements ExtendedType {
41
42     protected Class JavaDoc calendarClass;
43
44     public CalendarType(Class JavaDoc calendarClass) {
45         if (calendarClass == null) {
46             throw new IllegalArgumentException JavaDoc("Null calendar class");
47         }
48
49         if (!Calendar JavaDoc.class.isAssignableFrom(calendarClass)) {
50             throw new IllegalArgumentException JavaDoc(
51                     "Must be a java.util.Calendar or a subclass: " + calendarClass);
52         }
53
54         this.calendarClass = calendarClass;
55     }
56
57     public String JavaDoc getClassName() {
58         return calendarClass.getName();
59     }
60
61     public Object JavaDoc materializeObject(ResultSet JavaDoc rs, int index, int type) throws Exception JavaDoc {
62
63         Date JavaDoc val = null;
64
65         switch (type) {
66             case Types.TIMESTAMP:
67                 val = rs.getTimestamp(index);
68                 break;
69             case Types.DATE:
70                 val = rs.getDate(index);
71                 break;
72             case Types.TIME:
73                 val = rs.getTime(index);
74                 break;
75             default:
76                 // here the driver can "surpirse" us
77
// check the type of returned value...
78
Object JavaDoc object = rs.getObject(index);
79
80                 if (object != null && !(object instanceof Date JavaDoc)) {
81                     throw new CayenneRuntimeException(
82                             "Expected an instance of java.util.Date, instead got "
83                                     + object.getClass().getName()
84                                     + ", column index: "
85                                     + index);
86                 }
87
88                 val = (Date JavaDoc) object;
89                 break;
90         }
91
92         if (rs.wasNull()) {
93             return null;
94         }
95
96         GregorianCalendar JavaDoc calendar = new GregorianCalendar JavaDoc();
97         calendar.setTime(val);
98         return calendar;
99     }
100
101     public Object JavaDoc materializeObject(CallableStatement JavaDoc rs, int index, int type)
102             throws Exception JavaDoc {
103         Date JavaDoc val = null;
104
105         switch (type) {
106             case Types.TIMESTAMP:
107                 val = rs.getTimestamp(index);
108                 break;
109             case Types.DATE:
110                 val = rs.getDate(index);
111                 break;
112             case Types.TIME:
113                 val = rs.getTime(index);
114                 break;
115             default:
116                 // here the driver can "surpirse" us
117
// check the type of returned value...
118
Object JavaDoc object = rs.getObject(index);
119
120                 if (object != null && !(object instanceof Date JavaDoc)) {
121                     throw new CayenneRuntimeException(
122                             "Expected an instance of java.util.Date, instead got "
123                                     + object.getClass().getName()
124                                     + ", column index: "
125                                     + index);
126                 }
127
128                 val = (Date JavaDoc) object;
129                 break;
130         }
131
132         if (rs.wasNull()) {
133             return null;
134         }
135
136         GregorianCalendar JavaDoc calendar = new GregorianCalendar JavaDoc();
137         calendar.setTime(val);
138         return calendar;
139     }
140
141     public void setJdbcObject(
142             PreparedStatement JavaDoc statement,
143             Object JavaDoc value,
144             int pos,
145             int type,
146             int precision) throws Exception JavaDoc {
147
148         if (value == null) {
149             statement.setNull(pos, type);
150         }
151         else if (value instanceof Calendar JavaDoc) {
152
153             Calendar JavaDoc calendar = (Calendar JavaDoc) value;
154             statement.setObject(pos, convertToJdbcObject(calendar, type));
155         }
156         else {
157             throw new IllegalArgumentException JavaDoc("Expected java.util.Calendar, got "
158                     + value.getClass().getName());
159         }
160     }
161
162     protected Object JavaDoc convertToJdbcObject(Calendar JavaDoc value, int type) throws Exception JavaDoc {
163         Calendar JavaDoc calendar = (Calendar JavaDoc) value;
164
165         if (type == Types.DATE)
166             return new java.sql.Date JavaDoc(calendar.getTimeInMillis());
167         else if (type == Types.TIME)
168             return new java.sql.Time JavaDoc(calendar.getTimeInMillis());
169         else if (type == Types.TIMESTAMP)
170             return new java.sql.Timestamp JavaDoc(calendar.getTimeInMillis());
171         else
172             throw new IllegalArgumentException JavaDoc(
173                     "Only DATE, TIME or TIMESTAMP can be mapped as '"
174                             + getClassName()
175                             + "', got "
176                             + TypesMapping.getSqlNameByType(type));
177     }
178
179     /**
180      * @deprecated since 3.0 as validation should not be done at the DataNode level.
181      */

182     public boolean validateProperty(
183             Object JavaDoc source,
184             String JavaDoc property,
185             Object JavaDoc value,
186             DbAttribute dbAttribute,
187             ValidationResult validationResult) {
188         return true;
189     }
190 }
191
Popular Tags