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