View Javadoc
1   /*
2    * Redistribution and use of this software and associated documentation ("Software"), with or
3    * without modification, are permitted provided that the following conditions are met:
4    *
5    * 1. Redistributions of source code must retain copyright statements and notices. Redistributions
6    * must also contain a copy of this document.
7    *
8    * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
9    * conditions and the following disclaimer in the documentation and/or other materials provided with
10   * the distribution.
11   *
12   * 3. The name "Exolab" must not be used to endorse or promote products derived from this Software
13   * without prior written permission of Intalio, Inc. For written permission, please contact
14   * info@exolab.org.
15   *
16   * 4. Products derived from this Software may not be called "Exolab" nor may "Exolab" appear in
17   * their names without prior written permission of Intalio, Inc. Exolab is a registered trademark of
18   * Intalio, Inc.
19   *
20   * 5. Due credit should be given to the Exolab Project (http://www.exolab.org/).
21   *
22   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR
23   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
24   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTALIO, INC. OR ITS
25   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
29   * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   * Copyright 2004 (C) Intalio, Inc. All Rights Reserved.
32   *
33   * $Id$
34   */
35  package org.exolab.castor.xml.handlers;
36  
37  import java.sql.Time;
38  import java.util.Date;
39  
40  import org.exolab.castor.mapping.GeneralizedFieldHandler;
41  
42  /**
43   * An implementation of GeneralizedFieldHandler for java.sql.Time.
44   *
45   * @author <a href="kvisco@intalio.com">Keith Visco</a>
46   * @version $Revision$ $Date: 2006-04-14 04:14:43 -0600 (Fri, 14 Apr 2006) $
47   * @see org.exolab.castor.mapping.FieldDescriptor
48   * @see org.exolab.castor.mapping.FieldHandler
49   */
50  public class SQLTimeFieldHandler extends GeneralizedFieldHandler {
51  
52    /**
53     * Creates a new SQLTimeFieldHandler.
54     */
55    public SQLTimeFieldHandler() {
56      super();
57    }
58  
59    /**
60     * This method is used to convert the value when the getValue method is called. The getValue
61     * method will obtain the actual field value from given 'parent' object. This convert method is
62     * then invoked with the field's value. The value returned from this method will be the actual
63     * value returned by getValue method.
64     *
65     * @param value the object value to convert after performing a get operation
66     * @return the converted value.
67     */
68    public Object convertUponGet(final Object value) {
69      // -- no conversion necessary for marshaling
70      return value;
71    } // -- convertUponGet
72  
73    /**
74     * This method is used to convert the value when the setValue method is called. The setValue
75     * method will call this method to obtain the converted value. The converted value will then be
76     * used as the value to set for the field.
77     *
78     * @param value the object value to convert before performing a set operation
79     * @return the converted value.
80     */
81    public Object convertUponSet(final Object value) {
82      if (value == null) {
83        return null;
84      }
85  
86      String str = value.toString();
87  
88      Time time = null;
89      // if ':' exists at index 2, then we probably have a valid time format: HH:MM:SS
90      if (str.indexOf(':') == 2) {
91        time = Time.valueOf(str);
92      } else {
93        // -- Try a full date YYYY-MM-DDTHH:MM:SS
94        try {
95          Date date = DateFieldHandler.parse(str);
96          time = new Time(date.getTime());
97        } catch (java.text.ParseException px) {
98          throw new IllegalStateException(px.getMessage());
99        }
100     }
101     return time;
102   } // -- convertUponSet;
103 
104   /**
105    * Returns the class type for the field that this GeneralizedFieldHandler converts to and from.
106    * This should be the type that is used in the object model.
107    *
108    * @return the class type of of the field
109    */
110   public Class getFieldType() {
111     return java.sql.Time.class;
112   } // -- getFieldType
113 
114   /**
115    * Creates a new instance of the object described by this field.
116    *
117    * @param parent The object for which the field is created
118    * @return A new instance of the field's value
119    * @throws IllegalStateException This field is a simple type and cannot be instantiated
120    */
121   public Object newInstance(final Object parent) throws IllegalStateException {
122     return new java.sql.Time(0);
123   }
124 
125 } // -- SQLTimeFieldHandler