1 19 20 package org.apache.cayenne.access.types; 21 22 import java.sql.CallableStatement ; 23 import java.sql.PreparedStatement ; 24 import java.sql.ResultSet ; 25 import java.sql.Types ; 26 import java.util.Date ; 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 39 public class UtilDateType extends AbstractType { 40 41 44 public String getClassName() { 45 return Date .class.getName(); 46 } 47 48 55 public boolean validateProperty( 56 Object source, 57 String property, 58 Object value, 59 DbAttribute dbAttribute, 60 ValidationResult validationResult) { 61 return true; 62 } 63 64 protected Object convertToJdbcObject(Object val, int type) throws Exception { 65 if (type == Types.DATE) 66 return new java.sql.Date (((Date ) val).getTime()); 67 else if (type == Types.TIME) 68 return new java.sql.Time (((Date ) val).getTime()); 69 else if (type == Types.TIMESTAMP) 70 return new java.sql.Timestamp (((Date ) val).getTime()); 71 else 72 throw new IllegalArgumentException ( 73 "Only DATE, TIME or TIMESTAMP can be mapped as '" 74 + getClassName() 75 + "', got " 76 + TypesMapping.getSqlNameByType(type)); 77 } 78 79 public Object materializeObject(ResultSet rs, int index, int type) throws Exception { 80 Date 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 Object object = rs.getObject(index); 96 97 if (object != null && !(object instanceof Date )) { 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 ) object; 106 break; 107 } 108 109 return (rs.wasNull()) ? null : new Date (val.getTime()); 110 } 111 112 public Object materializeObject(CallableStatement cs, int index, int type) 113 throws Exception { 114 Object 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 if (val != null && !(val instanceof java.util.Date )) { 130 String typeName = TypesMapping.getSqlNameByType(type); 131 throw new ClassCastException ( 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 return (cs.wasNull()) ? null : new java.util.Date (((java.util.Date ) val) 145 .getTime()); 146 } 147 148 public void setJdbcObject( 149 PreparedStatement st, 150 Object val, 151 int pos, 152 int type, 153 int precision) throws Exception { 154 super.setJdbcObject(st, convertToJdbcObject(val, type), pos, type, precision); 155 } 156 } 157 | Popular Tags |