Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
OjbCharBooleanConversion |
|
| 11.5;11.5 |
1 | /* | |
2 | * Copyright 2005-2007 The Kuali Foundation | |
3 | * | |
4 | * Licensed under the Educational Community License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.opensource.org/licenses/ecl2.php | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | package org.kuali.rice.core.framework.persistence.ojb.conversion; | |
17 | ||
18 | import org.apache.ojb.broker.accesslayer.conversions.FieldConversion; | |
19 | ||
20 | /** | |
21 | * Performs conversion of java boolean fields to and from the database | |
22 | */ | |
23 | 0 | public class OjbCharBooleanConversion implements FieldConversion { |
24 | public static final String DATABASE_BOOLEAN_TRUE_STRING_REPRESENTATION = "Y"; | |
25 | public static final String DATABASE_BOOLEAN_FALSE_STRING_REPRESENTATION = "N"; | |
26 | ||
27 | /** | |
28 | * @see FieldConversion#javaToSql(Object) | |
29 | */ | |
30 | public Object javaToSql(Object source) { | |
31 | 0 | if (source instanceof Boolean) { |
32 | 0 | if (source != null) { |
33 | 0 | Boolean b = (Boolean) source; |
34 | 0 | return b.booleanValue() ? DATABASE_BOOLEAN_TRUE_STRING_REPRESENTATION : DATABASE_BOOLEAN_FALSE_STRING_REPRESENTATION; |
35 | } | |
36 | else { | |
37 | 0 | return null; |
38 | } | |
39 | } | |
40 | 0 | else if (source instanceof String) { |
41 | 0 | if ("true".equalsIgnoreCase((String)source) || "yes".equalsIgnoreCase((String)source) || "y".equalsIgnoreCase((String)source)) { |
42 | 0 | return DATABASE_BOOLEAN_TRUE_STRING_REPRESENTATION; |
43 | } | |
44 | 0 | else if ("false".equalsIgnoreCase((String)source) || "no".equalsIgnoreCase((String)source) || "n".equalsIgnoreCase((String)source)) { |
45 | 0 | return DATABASE_BOOLEAN_FALSE_STRING_REPRESENTATION; |
46 | } | |
47 | } | |
48 | 0 | return source; |
49 | } | |
50 | ||
51 | /** | |
52 | * @see FieldConversion#sqlToJava(Object) | |
53 | */ | |
54 | public Object sqlToJava(Object source) { | |
55 | try { | |
56 | 0 | if (source instanceof String) { |
57 | 0 | if (source != null) { |
58 | 0 | String s = (String) source; |
59 | 0 | String trueValues = DATABASE_BOOLEAN_TRUE_STRING_REPRESENTATION + "T1"; |
60 | 0 | return trueValues.contains(s); |
61 | } | |
62 | else { | |
63 | 0 | return null; |
64 | } | |
65 | } | |
66 | 0 | return source; |
67 | } | |
68 | 0 | catch (Throwable t) { |
69 | 0 | t.printStackTrace(); |
70 | 0 | throw new RuntimeException("I have exploded converting types", t); |
71 | } | |
72 | } | |
73 | ||
74 | } |