1 21 package net.mlw.vlh.adapter.hibernate3.util.setter; 22 23 import java.text.ParseException ; 24 import java.text.SimpleDateFormat ; 25 import java.util.Date ; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.hibernate.HibernateException; 30 import org.hibernate.Query; 31 32 36 public class TimestampSetter extends AbstractSetter 37 { 38 41 private static final Log LOGGER = LogFactory.getLog(TimestampSetter.class); 42 43 public static final String DEFAULT_FORMAT = "MM/dd/yyyy HH:mm:ss"; 44 45 private SimpleDateFormat formatter = new SimpleDateFormat (DEFAULT_FORMAT); 46 47 50 public void set(Query query, String key, Object value) throws HibernateException, ParseException 51 { 52 Date date = null; 53 if (value instanceof String ) 54 { 55 if (LOGGER.isInfoEnabled()) 56 { 57 LOGGER.info("The key='" + key + "'s value is instance of a String, now is parsing to date."); 58 } 59 date = formatter.parse((String ) value); 60 } 61 else if (value instanceof Date ) 62 { 63 date = (Date ) value; 64 } 65 else if (value == null) 66 { 67 if (LOGGER.isInfoEnabled()) 68 { 69 LOGGER.info("The key='" + key + "'s value is null."); 70 } 71 } 72 else 73 { 74 if (LOGGER.isWarnEnabled()) 75 { 76 LOGGER.warn("The key's='" + key + "' value='" + value + "' was expected as Date or String parseable to Date."); 77 } 78 throw new IllegalArgumentException ("Cannot convert value of class " + value.getClass().getName() + " to timestamp (key=" + key 79 + ")"); 80 } 81 82 if (LOGGER.isInfoEnabled()) 83 { 84 LOGGER.info("The key='" + key + "' was set to the query as Timestamp with the value date='" + date + "'."); 85 } 86 87 query.setTimestamp(key, date); 88 } 89 90 93 public void setFormat(String format) 94 { 95 formatter = new SimpleDateFormat (format); 96 } 97 } | Popular Tags |