KickJava   Java API By Example, From Geeks To Geeks.

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


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
5  * under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; either version 2.1 of the License, or
7  * (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; with out even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License 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,
16  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  *
18  * > http://www.gnu.org/copyleft/lesser.html
19  * > http://www.opensource.org/licenses/lgpl-license.php
20  */

21 package net.mlw.vlh.adapter.hibernate3.util.setter;
22
23 import java.text.ParseException JavaDoc;
24 import java.text.SimpleDateFormat JavaDoc;
25 import java.util.Date JavaDoc;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.hibernate.HibernateException;
30 import org.hibernate.Query;
31
32 /**
33  * @author Matthew L. Wilson
34  * @version $Revision: 1.3 $ $Date: 2005/12/19 12:22:26 $
35  */

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

41    private static final Log LOGGER = LogFactory.getLog(TimestampSetter.class);
42
43    public static final String JavaDoc DEFAULT_FORMAT = "MM/dd/yyyy HH:mm:ss";
44
45    private SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc(DEFAULT_FORMAT);
46
47    /**
48     * @see net.mlw.vlh.adapter.hibernate3.util.Setter#set(Query, String, Object)
49     */

50    public void set(Query query, String JavaDoc key, Object JavaDoc value) throws HibernateException, ParseException JavaDoc
51    {
52       Date JavaDoc date = null;
53       if (value instanceof String JavaDoc)
54       {
55          if (LOGGER.isInfoEnabled())
56          {
57             LOGGER.info("The key='" + key + "'s value is instance of a String, now is parsing to date.");
58          }
59          date = formatter.parse((String JavaDoc) value);
60       }
61       else if (value instanceof Date JavaDoc)
62       {
63          date = (Date JavaDoc) value;
64       }
65       else if (value == null)
66       {
67          if (LOGGER.isInfoEnabled())
68          {
69             LOGGER.info("The key='" + key + "'s value is null.");
70          }
71       }
72       else
73       {
74          if (LOGGER.isWarnEnabled())
75          {
76             LOGGER.warn("The key's='" + key + "' value='" + value + "' was expected as Date or String parseable to Date.");
77          }
78          throw new IllegalArgumentException JavaDoc("Cannot convert value of class " + value.getClass().getName() + " to timestamp (key=" + key
79                + ")");
80       }
81
82       if (LOGGER.isInfoEnabled())
83       {
84          LOGGER.info("The key='" + key + "' was set to the query as Timestamp with the value date='" + date + "'.");
85       }
86
87       query.setTimestamp(key, date);
88    }
89
90    /**
91     * @param format The format to set.
92     */

93    public void setFormat(String JavaDoc format)
94    {
95       formatter = new SimpleDateFormat JavaDoc(format);
96    }
97 }
Popular Tags