KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.SimpleDateFormat JavaDoc;
24 import java.util.Date JavaDoc;
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 /**
32  * @author Andrej Zachar, Matthew L. Wilson
33  * @version $Revision: 1.2 $ $Date: 2005/11/03 18:08:51 $
34  */

35 public class DateSetter extends AbstractSetter
36 {
37    /**
38     * Logger for this class
39     */

40    private static final Log LOGGER = LogFactory.getLog(DateSetter.class);
41
42    public static final String JavaDoc DEFAULT_FORMAT = "MM/dd/yyyy";
43
44    private SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc(DEFAULT_FORMAT);
45
46    /**
47     * <ol>
48     * <li>If is value instance of the String, it try to parse value using
49     * SimpleDateFormat with specified format.</li>
50     * <li>If is value instance of the Date, it will set it directly to query .
51     * </li>
52     * <li>Otherwise it will set null to query for key.</li>
53     * </ol>
54     *
55     * @see net.mlw.vlh.adapter.hibernate3.util.Setter#set(org.hibernate.Query,
56     * java.lang.String, java.lang.Object)
57     * @see #setFormat(String)
58     */

59    public void set(Query query, String JavaDoc key, Object JavaDoc value) throws HibernateException, ParseException JavaDoc
60    {
61       Date JavaDoc date = null;
62       if (value instanceof String JavaDoc)
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 JavaDoc) value);
69       }
70       else if (value instanceof Date JavaDoc)
71       {
72          if (LOGGER.isInfoEnabled())
73          {
74             LOGGER.info("The key='" + key + "' is instance of a Date.");
75          }
76          date = (Date JavaDoc) 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 JavaDoc("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    /**
103     * Set the format for parsing <code>Date</code> from string values of
104     * keys. Default is "MM/dd/yyyy"
105     *
106     * @param format The format to set.
107     */

108    public void setFormat(String JavaDoc format)
109    {
110       formatter = new SimpleDateFormat JavaDoc(format);
111    }
112 }
Popular Tags