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