1 56 package org.objectstyle.cayenne.access.types; 57 58 import java.sql.CallableStatement ; 59 import java.sql.PreparedStatement ; 60 import java.sql.ResultSet ; 61 import java.sql.Types ; 62 import java.util.Date ; 63 64 import org.objectstyle.cayenne.CayenneRuntimeException; 65 import org.objectstyle.cayenne.dba.TypesMapping; 66 import org.objectstyle.cayenne.map.DbAttribute; 67 import org.objectstyle.cayenne.validation.ValidationResult; 68 69 75 public class UtilDateType extends AbstractType { 76 77 80 public String getClassName() { 81 return Date .class.getName(); 82 } 83 84 90 public boolean validateProperty( 91 Object source, 92 String property, 93 Object value, 94 DbAttribute dbAttribute, 95 ValidationResult validationResult) { 96 return true; 97 } 98 99 protected Object convertToJdbcObject(Object val, int type) throws Exception { 100 if (type == Types.DATE) 101 return new java.sql.Date (((Date ) val).getTime()); 102 else if (type == Types.TIME) 103 return new java.sql.Time (((Date ) val).getTime()); 104 else if (type == Types.TIMESTAMP) 105 return new java.sql.Timestamp (((Date ) val).getTime()); 106 else 107 throw new IllegalArgumentException ( 108 "Only date/time types can be used for '" + getClassName() + "'."); 109 } 110 111 public Object materializeObject(ResultSet rs, int index, int type) throws Exception { 112 Date val = null; 113 114 switch (type) { 115 case Types.TIMESTAMP: 116 val = rs.getTimestamp(index); 117 break; 118 case Types.DATE: 119 val = rs.getDate(index); 120 break; 121 case Types.TIME: 122 val = rs.getTime(index); 123 break; 124 default: 125 Object object = rs.getObject(index); 128 129 if (object != null && !(object instanceof Date )) { 130 throw new CayenneRuntimeException( 131 "Expected an instance of java.util.Date, instead got " 132 + object.getClass().getName() 133 + ", column index: " 134 + index); 135 } 136 137 val = (Date ) object; 138 break; 139 } 140 141 return (rs.wasNull()) ? null : new Date (val.getTime()); 142 } 143 144 public Object materializeObject(CallableStatement cs, int index, int type) 145 throws Exception { 146 Object val = null; 147 148 switch (type) { 149 case Types.TIMESTAMP : 150 val = cs.getTimestamp(index); 151 break; 152 case Types.DATE : 153 val = cs.getDate(index); 154 break; 155 case Types.TIME : 156 val = cs.getTime(index); 157 break; 158 default : 159 val = cs.getObject(index); 160 if (val != null && !(val instanceof java.util.Date )) { 162 String typeName = TypesMapping.getSqlNameByType(type); 163 throw new ClassCastException ( 164 "Expected a java.util.Date or subclass, instead fetched '" 165 + val.getClass().getName() 166 + "' for JDBC type " 167 + typeName); 168 } 169 break; 170 } 171 172 return (cs.wasNull()) 177 ? null 178 : new java.util.Date (((java.util.Date ) val).getTime()); 179 } 180 181 public void setJdbcObject( 182 PreparedStatement st, 183 Object val, 184 int pos, 185 int type, 186 int precision) 187 throws Exception { 188 super.setJdbcObject(st, convertToJdbcObject(val, type), pos, type, precision); 189 } 190 } | Popular Tags |