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