001/** 002 * Copyright 2005-2016 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.core.framework.persistence.ojb.conversion; 017 018import org.apache.ojb.broker.accesslayer.conversions.FieldConversion; 019 020/** 021 * Performs conversion of java boolean fields to and from the database 022 */ 023public class OjbCharBooleanConversion implements FieldConversion { 024 public static final String DATABASE_BOOLEAN_TRUE_STRING_REPRESENTATION = "Y"; 025 public static final String DATABASE_BOOLEAN_FALSE_STRING_REPRESENTATION = "N"; 026 027 /** 028 * @see FieldConversion#javaToSql(Object) 029 */ 030 public Object javaToSql(Object source) { 031 if (source instanceof Boolean) { 032 if (source != null) { 033 Boolean b = (Boolean) source; 034 return b.booleanValue() ? DATABASE_BOOLEAN_TRUE_STRING_REPRESENTATION : DATABASE_BOOLEAN_FALSE_STRING_REPRESENTATION; 035 } 036 else { 037 return null; 038 } 039 } 040 else if (source instanceof String) { 041 if ("true".equalsIgnoreCase((String)source) || "yes".equalsIgnoreCase((String)source) || "y".equalsIgnoreCase((String)source)) { 042 return DATABASE_BOOLEAN_TRUE_STRING_REPRESENTATION; 043 } 044 else if ("false".equalsIgnoreCase((String)source) || "no".equalsIgnoreCase((String)source) || "n".equalsIgnoreCase((String)source)) { 045 return DATABASE_BOOLEAN_FALSE_STRING_REPRESENTATION; 046 } 047 } 048 return source; 049 } 050 051 /** 052 * @see FieldConversion#sqlToJava(Object) 053 */ 054 public Object sqlToJava(Object source) { 055 try { 056 if (source instanceof String) { 057 if (source != null) { 058 String s = (String) source; 059 String trueValues = DATABASE_BOOLEAN_TRUE_STRING_REPRESENTATION + "T1"; 060 return trueValues.contains(s); 061 } 062 else { 063 return null; 064 } 065 } 066 return source; 067 } 068 catch (Throwable t) { 069 t.printStackTrace(); 070 throw new RuntimeException("I have exploded converting types", t); 071 } 072 } 073 074}