View Javadoc
1   /*
2    * Copyright 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.ole.sys.dataaccess;
17  
18  import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
19  
20  /**
21   * This class is intended to be used for inverting all the boolean values stored in the database before loading them into the
22   * business object and vice versa. This functionality is necessary for situations where a database table stores the opposite of the
23   * intented boolean attribute. An example is where a given business object has a pre-defined attribute, such as "inactive", while
24   * the user wishes to display the value as an 'active' indicator rather than an 'inactive indicator. Ideally, it would be better to
25   * replace the field in the database with the appropriate representation of the data so we do not have to perform these confusing
26   * conversions on data. Unfortunately, this is not always an option.
27   */
28  public class OjbCharBooleanFieldInverseConversion implements FieldConversion {
29  
30      private static final String TRUE = "Y";
31      private static final String FALSE = "N";
32  
33      /**
34       * This method takes the value intended to be passed to the SQL statement and replaces that value with its inverse. Thus TRUE
35       * becomes FALSE and vice versa.
36       * 
37       * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#javaToSql(java.lang.Object)
38       */
39      public Object javaToSql(Object source) {
40          if (source instanceof Boolean) {
41              if (source.equals(Boolean.TRUE)) {
42                  return FALSE;
43              }
44              else {
45                  return TRUE;
46              }
47          }
48          else if (source instanceof String) {
49              if ("Y".equalsIgnoreCase((String) source)) {
50                  return FALSE;
51              }
52              else if ("N".equalsIgnoreCase((String) source)) {
53                  return TRUE;
54              }
55          }
56          return source;
57      }
58  
59      /**
60       * This method takes the value returned from the database and replaces it with its inverse, thus FALSE becomes TRUE and vice
61       * versa.
62       * 
63       * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#sqlToJava(java.lang.Object)
64       */
65      public Object sqlToJava(Object source) {
66          if (source instanceof String) {
67              if (TRUE.equals(source)) {
68                  return Boolean.FALSE;
69              }
70              else {
71                  return Boolean.TRUE;
72              }
73          }
74          else {
75              return source;
76          }
77      }
78  
79  }