01: /*
02: * Copyright 2005-2006 The Kuali Foundation.
03: *
04: * Licensed under the Educational Community License, Version 1.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.opensource.org/licenses/ecl1.php
09: *
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.core.util;
17:
18: import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
19:
20: public abstract class OjbCharBooleanFieldConversionBase implements
21: FieldConversion {
22:
23: protected abstract String getTrueValue();
24:
25: protected abstract String getFalseValue();
26:
27: /**
28: * @see FieldConversion#javaToSql(Object)
29: */
30: public Object javaToSql(Object source) {
31: Object result = source;
32: if (source instanceof Boolean) {
33: if (source != null) {
34: Boolean b = (Boolean) source;
35: result = b.booleanValue() ? getTrueValue()
36: : getFalseValue();
37: }
38: }
39: return result;
40: }
41:
42: /**
43: * @see FieldConversion#sqlToJava(Object)
44: */
45: public Object sqlToJava(Object source) {
46: Object result = source;
47: if (source instanceof String) {
48: if (source != null) {
49: String s = (String) source;
50: result = getTrueValue().equals(s) ? Boolean.TRUE
51: : getFalseValue().equals(s) ? Boolean.FALSE
52: : null;
53: if (result == null) {
54: throw new RuntimeException("Expected '"
55: + getTrueValue() + "' or '"
56: + getFalseValue() + "' but saw '" + source
57: + "'");
58: }
59: }
60: }
61: return result;
62: }
63:
64: }
|