1 20 package net.mlw.vlh.adapter.hibernate3.util.setter; 21 22 import java.text.ParseException ; 23 import java.util.Calendar ; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 import org.hibernate.HibernateException; 28 import org.hibernate.Query; 29 30 34 public class CalendarSetter extends AbstractSetter 35 { 36 39 private static final Log LOGGER = LogFactory.getLog(CalendarSetter.class); 40 41 53 public void set(Query query, String key, Object value) throws HibernateException, ParseException 54 { 55 56 Calendar calendar = null; 57 58 if (value instanceof String ) 59 { 60 calendar = Calendar.getInstance(); 61 try 62 { 63 calendar.setTimeInMillis(Long.valueOf((String ) value).longValue()); 64 if (LOGGER.isDebugEnabled()) 65 { 66 LOGGER.debug("The key's='" + key + "' String value='" + value + "' was converted to Calendar."); 67 } 68 } 69 catch (NumberFormatException e) 70 { 71 if (LOGGER.isWarnEnabled()) 72 { 73 LOGGER.warn("The key's='" + key + "' String value='" + value + "' was not converted to Calendar, error was:" 74 + e.getMessage()); 75 } 76 throw e; 77 } 78 } 79 else if (value instanceof Long ) 80 { 81 calendar = Calendar.getInstance(); 82 calendar.setTimeInMillis(((Long ) value).longValue()); 83 if (LOGGER.isDebugEnabled()) 84 { 85 LOGGER.debug("The key's='" + key + "' Long value='" + value + "' was converted to Calendar."); 86 } 87 } 88 else if (value instanceof Calendar ) 89 { 90 calendar = (Calendar ) value; 91 } 92 else if (value == null) 93 { 94 if (LOGGER.isInfoEnabled()) 95 { 96 LOGGER.info("The key='" + key + "'s value is null."); 97 } 98 } 99 else 100 { 101 if (LOGGER.isWarnEnabled()) 102 { 103 LOGGER.warn("The key's='" + key + "' value='" + value + "' was expected as Calendar."); 104 } 105 throw new IllegalArgumentException ("Cannot convert value of class " + value.getClass().getName() + " to calendar (key=" + key 106 + ")"); 107 } 108 109 query.setCalendar(key, calendar); 110 111 if (LOGGER.isInfoEnabled()) 112 { 113 LOGGER.info("The key='" + key + "' was set to the query as Calendar='" + calendar + "'."); 114 } 115 } 116 } | Popular Tags |