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 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.jdbc.util.setter;
21  
22  import java.sql.PreparedStatement;
23  import java.sql.SQLException;
24  import java.sql.Timestamp;
25  import java.text.ParseException;
26  import java.util.Calendar;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  
31  /***
32   * Sets a <code>Calendar</code> using a <code>java.sql.Timestamp</code> on a
33   * <code>Query</code>. This could be usefull when you are using hibernate
34   * mapping and accesing directly via jdbc.
35   * 
36   * @author Andrej Zachar
37   * @version $Revision: 1.3 $ $Date: 2005/12/19 10:56:41 $
38   */
39  public class Hibernate20CalendarSetter extends AbstractSetter
40  {
41     /***
42      * Logger for this class
43      */
44     private static final Log LOGGER = LogFactory.getLog(Hibernate20CalendarSetter.class);
45  
46     /***
47      * <ol>
48      * <li>If filter value is instance of the Calendar, it will set it directly
49      * to <code>PreparedStatement</code> query. </li>
50      * <li>Otherwise it will set new Timestamp(with actual time) to query for
51      * key.</li>
52      * </ol>
53      * 
54      * @see net.mlw.util.sql.StatementBuilder.Setter#set(java.sql.PreparedStatement,
55      *      int, java.lang.Object)
56      */
57     public int set(PreparedStatement query, int index, Object value) throws SQLException, ParseException
58     {
59        long timeInMillis = 0;
60        if (value instanceof Calendar)
61        {
62           timeInMillis = ((Calendar) value).getTimeInMillis();
63        }
64        else
65        {
66           if (LOGGER.isWarnEnabled())
67           {
68              LOGGER.warn("The key's index's='" + index + "' value='" + value + "' was expected as Calendar. Using actual time.");
69           }
70           timeInMillis = Calendar.getInstance().getTimeInMillis();
71        }
72  
73        query.setTimestamp(index++, new Timestamp(timeInMillis));
74        return index;
75     }
76  }