001/**
002 * Copyright 2005-2018 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 * This class originates from a similar class in the Kuali Financial System and has been adapted from 
022 * that original state which was originally authored by the Kuali Nervous System team.
023 * @author Kuali Rice Team (rice.collab@kuali.org)
024 */
025public class OjbCharBooleanConversion2 implements FieldConversion {
026    /**
027     * This handles checking the boolean value coming in and converts it to 
028     * the appropriate Y or N value.
029     * @see FieldConversion#javaToSql(Object)
030     */
031    public Object javaToSql(Object source) {
032        if (source instanceof Boolean) {
033            if (source != null) {
034                Boolean b = (Boolean) source;
035                return b.booleanValue() ? "Y" : "N";
036            }
037            else {
038                return null;
039            }
040        }
041        else if (source instanceof String) {
042            if ("true".equals(source)) {
043                return "Y";
044            }
045            else if ("false".equals(source)) {
046                return "N";
047            }
048        }
049        return source;
050    }
051
052    /**
053     * This handles checking the sql coming back from the database and converting 
054     * it to the appropriate boolean true or false value.
055     * @see FieldConversion#sqlToJava(Object)
056     */
057    public Object sqlToJava(Object source) {
058        try {
059            if (source instanceof String) {
060                if (source != null) {
061                    String s = (String) source;
062                    return Boolean.valueOf("YT1".contains(s));
063                }
064                else {
065                    return null;
066                }
067            }
068            return source;
069        }
070        catch (Throwable t) {
071            t.printStackTrace();
072            throw new RuntimeException("I have exploded converting types", t);
073        }
074    }
075}