KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > mlw > vlh > adapter > hibernate > 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.hibernate.util.setter;
21
22 import java.text.ParseException JavaDoc;
23 import java.text.SimpleDateFormat JavaDoc;
24 import java.util.Date JavaDoc;
25
26 import net.sf.hibernate.HibernateException;
27 import net.sf.hibernate.Query;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 /**
33  * @author Andrej Zachar, Matthew L. Wilson
34  * @version $Revision: 1.3 $ $Date: 2005/09/26 09:25:31 $
35  */

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

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

60    public void set(Query query, String JavaDoc key, Object JavaDoc value) throws HibernateException, ParseException JavaDoc
61    {
62       Date JavaDoc date = null;
63       if (value instanceof String JavaDoc)
64       {
65          if (LOGGER.isInfoEnabled())
66          {
67             LOGGER.info("The key='" + key + "'s value is instance of a String, now is parsing to date.");
68          }
69          date = formatter.parse((String JavaDoc) value);
70       }
71       else if (value instanceof Date JavaDoc)
72       {
73          if (LOGGER.isInfoEnabled())
74          {
75             LOGGER.info("The key='" + key + "' is instance of a Date.");
76          }
77          date = (Date JavaDoc) value;
78       }
79       else if (value == null)
80       {
81          if (LOGGER.isInfoEnabled())
82          {
83             LOGGER.info("The key='" + key + "'s value is null.");
84          }
85       }
86       else
87       {
88          if (LOGGER.isWarnEnabled())
89          {
90             LOGGER.warn("The key's='" + key + "' value='" + value + "' was expected as Date or String parseable to Date.");
91          }
92          throw new IllegalArgumentException JavaDoc("Cannot convert value of class " + value.getClass().getName() + " to date (key=" + key + ")");
93       }
94
95       if (LOGGER.isInfoEnabled())
96       {
97          LOGGER.info("The key='" + key + "' was set to the query as Date with the date='" + date + "'.");
98       }
99
100       query.setDate(key, date);
101    }
102
103    /**
104     * Set the format for parsing <code>Date</code> from string values of
105     * keys. Default is "MM/dd/yyyy"
106     *
107     * @param format The format to set.
108     */

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