View Javadoc

1   /***
2    * Copyright (c) 2003 held jointly by the individual authors.
3    * 
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    * 
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
12   * for more details.
13   * 
14   * You should have received a copy of the GNU Lesser General Public License
15   * along with this library; if not, write to the Free Software Foundation, Inc.,
16   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. >
17   * http://www.gnu.org/copyleft/lesser.html >
18   * http://www.opensource.org/licenses/lgpl-license.php
19   */
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  /***
32   * @author Andrej Zachar
33   * @version $Revision: 1.5 $ $Date: 2005/09/26 09:25:09 $
34   */
35  public class CalendarSetter extends AbstractSetter
36  {
37     /***
38      * Logger for this class
39      */
40     private static final Log LOGGER = LogFactory.getLog(CalendarSetter.class);
41  
42     /***
43      * <ol>
44      * <li>If is filter value instance of the Calendar, it will set it directly
45      * to query. </li>
46      * <li>If is filter value instance of String, it try to convert it it to long and set to Calendar instance</li>
47      * <li>If is filter value instance of Long, it try to convert it it to long and set to Calendar instance</li>
48      * <li>Otherwise it will set null to query for key.</li>
49      * </ol>
50      * 
51      * @see net.mlw.vlh.adapter.hibernate.util.Setter#set(net.sf.hibernate.Query,
52      *      java.lang.String, java.lang.Object)
53      */
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 }