1 19 package org.apache.cayenne.access.types; 20 21 import java.sql.CallableStatement ; 22 import java.sql.PreparedStatement ; 23 import java.sql.ResultSet ; 24 import java.sql.Types ; 25 import java.util.Calendar ; 26 import java.util.Date ; 27 import java.util.GregorianCalendar ; 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 40 public class CalendarType implements ExtendedType { 41 42 protected Class calendarClass; 43 44 public CalendarType(Class calendarClass) { 45 if (calendarClass == null) { 46 throw new IllegalArgumentException ("Null calendar class"); 47 } 48 49 if (!Calendar .class.isAssignableFrom(calendarClass)) { 50 throw new IllegalArgumentException ( 51 "Must be a java.util.Calendar or a subclass: " + calendarClass); 52 } 53 54 this.calendarClass = calendarClass; 55 } 56 57 public String getClassName() { 58 return calendarClass.getName(); 59 } 60 61 public Object materializeObject(ResultSet rs, int index, int type) throws Exception { 62 63 Date 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 Object object = rs.getObject(index); 79 80 if (object != null && !(object instanceof Date )) { 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 ) object; 89 break; 90 } 91 92 if (rs.wasNull()) { 93 return null; 94 } 95 96 GregorianCalendar calendar = new GregorianCalendar (); 97 calendar.setTime(val); 98 return calendar; 99 } 100 101 public Object materializeObject(CallableStatement rs, int index, int type) 102 throws Exception { 103 Date 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 Object object = rs.getObject(index); 119 120 if (object != null && !(object instanceof Date )) { 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 ) object; 129 break; 130 } 131 132 if (rs.wasNull()) { 133 return null; 134 } 135 136 GregorianCalendar calendar = new GregorianCalendar (); 137 calendar.setTime(val); 138 return calendar; 139 } 140 141 public void setJdbcObject( 142 PreparedStatement statement, 143 Object value, 144 int pos, 145 int type, 146 int precision) throws Exception { 147 148 if (value == null) { 149 statement.setNull(pos, type); 150 } 151 else if (value instanceof Calendar ) { 152 153 Calendar calendar = (Calendar ) value; 154 statement.setObject(pos, convertToJdbcObject(calendar, type)); 155 } 156 else { 157 throw new IllegalArgumentException ("Expected java.util.Calendar, got " 158 + value.getClass().getName()); 159 } 160 } 161 162 protected Object convertToJdbcObject(Calendar value, int type) throws Exception { 163 Calendar calendar = (Calendar ) value; 164 165 if (type == Types.DATE) 166 return new java.sql.Date (calendar.getTimeInMillis()); 167 else if (type == Types.TIME) 168 return new java.sql.Time (calendar.getTimeInMillis()); 169 else if (type == Types.TIMESTAMP) 170 return new java.sql.Timestamp (calendar.getTimeInMillis()); 171 else 172 throw new IllegalArgumentException ( 173 "Only DATE, TIME or TIMESTAMP can be mapped as '" 174 + getClassName() 175 + "', got " 176 + TypesMapping.getSqlNameByType(type)); 177 } 178 179 182 public boolean validateProperty( 183 Object source, 184 String property, 185 Object value, 186 DbAttribute dbAttribute, 187 ValidationResult validationResult) { 188 return true; 189 } 190 } 191 | Popular Tags |