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