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