View Javadoc

1   /*
2    * Copyright 2005-2008 The Kuali Foundation
3    * 
4    * 
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * 
9    * http://www.opensource.org/licenses/ecl2.php
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.core.util;
18  
19  import java.sql.Timestamp;
20  
21  import org.apache.commons.beanutils.ConversionException;
22  import org.apache.commons.beanutils.Converter;
23  
24  /**
25   * Converts an incoming object to a timestamp.
26   * 
27   * Adapted from org.apache.commons.beanutils.converters.SqlTimestampConverter
28   */
29  public class SqlTimestampConverter implements Converter {
30  
31      /**
32       * Create a {@link Converter} that will throw a {@link ConversionException}
33       * if a conversion error occurs.
34       */
35      public SqlTimestampConverter() {
36          this.defaultValue = null;
37          this.useDefault = true;
38      }
39  
40      /**
41       * Create a {@link Converter} that will return the specified default value
42       * if a conversion error occurs.
43       *
44       * @param defaultValue The default value to be returned
45       */
46      public SqlTimestampConverter(Object defaultValue) {
47          this.defaultValue = defaultValue;
48          this.useDefault = true;
49      }
50  
51      // ----------------------------------------------------- Instance Variables
52  
53      /**
54       * The default value specified to our Constructor, if any.
55       */
56      private Object defaultValue = null;
57  
58      /**
59       * Should we return the default value on conversion errors?
60       */
61      private boolean useDefault = true;
62  
63      // --------------------------------------------------------- Public Methods
64  
65      /**
66       * Convert the specified input object into an output object of the
67       * specified type.
68       *
69       * @param type Data type to which this value should be converted
70       * @param value The input value to be converted
71       *
72       * @exception ConversionException if conversion cannot be performed
73       *  successfully
74       */
75      public Object convert(Class type, Object value) {
76          if (value == null) {
77              if (useDefault) {
78                  return (defaultValue);
79              } else {
80                  throw new ConversionException("No value specified");
81              }
82          }
83  
84          if (value instanceof Timestamp) {
85              return (value);
86          }
87  
88          try {
89              return (Timestamp.valueOf(value.toString()));
90          } catch (Exception e) {
91              if (useDefault) {
92                  return (defaultValue);
93              } else {
94                  throw new ConversionException(e);
95              }
96          }
97      }
98  
99  }