01: /*
02: * Copyright 2007 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.kfs.util;
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
29: FieldConversion {
30:
31: private static final String TRUE = "Y";
32: private static final String FALSE = "N";
33:
34: /**
35: * This method takes the value intended to be passed to the SQL statement and replaces that value with its inverse. Thus TRUE
36: * becomes FALSE and vice versa.
37: *
38: * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#javaToSql(java.lang.Object)
39: */
40: public Object javaToSql(Object source) {
41: if (source instanceof Boolean) {
42: if (source.equals(Boolean.TRUE)) {
43: return FALSE;
44: } else {
45: return TRUE;
46: }
47: } else if (source instanceof String) {
48: if ("Y".equalsIgnoreCase((String) source)) {
49: return FALSE;
50: } else if ("N".equalsIgnoreCase((String) source)) {
51: return TRUE;
52: }
53: }
54: return source;
55: }
56:
57: /**
58: * This method takes the value returned from the database and replaces it with its inverse, thus FALSE becomes TRUE and vice
59: * versa.
60: *
61: * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#sqlToJava(java.lang.Object)
62: */
63: public Object sqlToJava(Object source) {
64: if (source instanceof String) {
65: if (TRUE.equals(source)) {
66: return Boolean.FALSE;
67: } else {
68: return Boolean.TRUE;
69: }
70: } else {
71: return source;
72: }
73: }
74:
75: }
|