View Javadoc

1   /*
2    * Copyright 2005-2008 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.util;
17  
18  import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
19  
20  public abstract class OjbCharBooleanFieldConversionBase implements FieldConversion {
21  
22      protected abstract String getTrueValue();
23  
24      protected abstract String getFalseValue();
25  
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() : getFalseValue();
36              }
37          }
38          return result;
39      }
40  
41      /**
42       * @see FieldConversion#sqlToJava(Object)
43       */
44      public Object sqlToJava(Object source) {
45          Object result = source;
46          if (source instanceof String) {
47              if (source != null) {
48                  String s = (String) source;
49                  result = getTrueValue().equals(s) ? Boolean.TRUE : getFalseValue().equals(s) ? Boolean.FALSE : null;
50                  if (result == null) {
51                      throw new RuntimeException("Expected '" + getTrueValue() + "' or '" + getFalseValue() + "' but saw '" + source + "'");
52                  }
53              }
54          }
55          return result;
56      }
57  
58  }