001    /*
002     * Copyright 2011 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     */
016    package org.kuali.kfs.sys.dataaccess;
017    
018    import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
019    
020    /**
021     * This class is intended to be used for inverting all the boolean values stored in the database before loading them into the
022     * business object and vice versa. This functionality is necessary for situations where a database table stores the opposite of the
023     * intented boolean attribute. An example is where a given business object has a pre-defined attribute, such as "inactive", while
024     * the user wishes to display the value as an 'active' indicator rather than an 'inactive indicator. Ideally, it would be better to
025     * replace the field in the database with the appropriate representation of the data so we do not have to perform these confusing
026     * conversions on data. Unfortunately, this is not always an option.
027     */
028    public class OjbCharBooleanFieldInverseConversion implements FieldConversion {
029    
030        private static final String TRUE = "Y";
031        private static final String FALSE = "N";
032    
033        /**
034         * This method takes the value intended to be passed to the SQL statement and replaces that value with its inverse. Thus TRUE
035         * becomes FALSE and vice versa.
036         * 
037         * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#javaToSql(java.lang.Object)
038         */
039        public Object javaToSql(Object source) {
040            if (source instanceof Boolean) {
041                if (source.equals(Boolean.TRUE)) {
042                    return FALSE;
043                }
044                else {
045                    return TRUE;
046                }
047            }
048            else if (source instanceof String) {
049                if ("Y".equalsIgnoreCase((String) source)) {
050                    return FALSE;
051                }
052                else if ("N".equalsIgnoreCase((String) source)) {
053                    return TRUE;
054                }
055            }
056            return source;
057        }
058    
059        /**
060         * This method takes the value returned from the database and replaces it with its inverse, thus FALSE becomes TRUE and vice
061         * versa.
062         * 
063         * @see org.apache.ojb.broker.accesslayer.conversions.FieldConversion#sqlToJava(java.lang.Object)
064         */
065        public Object sqlToJava(Object source) {
066            if (source instanceof String) {
067                if (TRUE.equals(source)) {
068                    return Boolean.FALSE;
069                }
070                else {
071                    return Boolean.TRUE;
072                }
073            }
074            else {
075                return source;
076            }
077        }
078    
079    }