KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > mlw > vlh > adapter > hibernate3 > util > setter > CalendarSetter


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.hibernate3.util.setter;
21
22 import java.text.ParseException JavaDoc;
23 import java.util.Calendar JavaDoc;
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 /**
31  * @author Stepan Marek
32  * @version $Revision: 1.2 $ $Date: 2005/11/03 18:08:51 $
33  */

34 public class CalendarSetter extends AbstractSetter
35 {
36    /**
37     * Logger for this class
38     */

39    private static final Log LOGGER = LogFactory.getLog(CalendarSetter.class);
40
41    /**
42     * <ol>
43     * <li>If is filter value instance of the Calendar, it will set it directly
44     * to query. </li>
45     * <li>If is filter value instance of String, it try to convert it it to long and set to Calendar instance</li>
46     * <li>If is filter value instance of Long, it try to convert it it to long and set to Calendar instance</li>
47     * <li>Otherwise it will set null to query for key.</li>
48     * </ol>
49     *
50     * @see net.mlw.vlh.adapter.hibernate3.util.Setter#set(org.hibernate.Query,
51     * java.lang.String, java.lang.Object)
52     */

53    public void set(Query query, String JavaDoc key, Object JavaDoc value) throws HibernateException, ParseException JavaDoc
54    {
55
56       Calendar JavaDoc calendar = null;
57
58       if (value instanceof String JavaDoc)
59       {
60          calendar = Calendar.getInstance();
61          try
62          {
63             calendar.setTimeInMillis(Long.valueOf((String JavaDoc) 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 JavaDoc 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 JavaDoc)
80       {
81          calendar = Calendar.getInstance();
82          calendar.setTimeInMillis(((Long JavaDoc) 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 JavaDoc)
89       {
90          calendar = (Calendar JavaDoc) 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 JavaDoc("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