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    
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.hibernate.util.setter;
22  
23  import java.text.ParseException;
24  import java.text.SimpleDateFormat;
25  import java.util.Date;
26  
27  import net.sf.hibernate.HibernateException;
28  import net.sf.hibernate.Query;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  
33  /***
34   * @author Matthew L. Wilson
35   * @version $Revision: 1.5 $ $Date: 2005/12/19 12:22:30 $
36   */
37  public class TimestampSetter extends AbstractSetter
38  {
39     /***
40      * Logger for this class
41      */
42     private static final Log LOGGER = LogFactory.getLog(TimestampSetter.class);
43  
44     public static final String DEFAULT_FORMAT = "MM/dd/yyyy HH:mm:ss";
45  
46     private SimpleDateFormat formatter = new SimpleDateFormat(DEFAULT_FORMAT);
47  
48     /***
49      * @see net.mlw.vlh.adapter.hibernate.util.Setter#set(Query, String, Object)
50      */
51     public void set(Query query, String key, Object value) throws HibernateException, ParseException
52     {
53        Date date = null;
54        if (value instanceof String)
55        {
56           if (LOGGER.isInfoEnabled())
57           {
58              LOGGER.info("The key='" + key + "'s value is instance of a String, now is parsing to date.");
59           }
60           date = formatter.parse((String) value);
61        }
62        else if (value instanceof Date)
63        {
64           date = (Date) value;
65        }
66        else if (value == null)
67        {
68           if (LOGGER.isInfoEnabled())
69           {
70              LOGGER.info("The key='" + key + "'s value is null.");
71           }
72        }
73        else
74        {
75           if (LOGGER.isWarnEnabled())
76           {
77              LOGGER.warn("The key's='" + key + "' value='" + value + "' was expected as Date or String parseable to Date.");
78           }
79           throw new IllegalArgumentException("Cannot convert value of class " + value.getClass().getName() + " to timestamp (key=" + key
80                 + ")");
81        }
82  
83        if (LOGGER.isInfoEnabled())
84        {
85           LOGGER.info("The key='" + key + "' was set to the query as Timestamp with the value date='" + date + "'.");
86        }
87  
88        query.setTimestamp(key, date);
89     }
90  
91     /***
92      * @param format The format to set.
93      */
94     public void setFormat(String format)
95     {
96        formatter = new SimpleDateFormat(format);
97     }
98  }