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.krad.data.jpa.converters;
017
018import javax.persistence.AttributeConverter;
019import javax.persistence.Converter;
020
021/**
022 * Converts active/inactive represented by the characters "A" and "I" to and from true and false.
023 *
024 * <p>The conversion treats the values as follows: "A" is true and "I" is false.</p>
025 *
026 * @author Kuali Rice Team (rice.collab@kuali.org)
027 */
028@Converter
029public class BooleanAIConverter implements AttributeConverter<Boolean, String> {
030
031    /**
032     * {@inheritDoc}
033     *
034     * This implementation will convert from a false or true value to an "I" or "A" value.
035     */
036        @Override
037        public String convertToDatabaseColumn(Boolean objectValue) {
038                if (objectValue == null) {
039                        return "I";
040                }
041                return objectValue ? "A" : "I";
042        }
043
044    /**
045     * {@inheritDoc}
046     *
047     * This implementation will convert from an "I" or "A" value to a false or true value.
048     */
049        @Override
050        public Boolean convertToEntityAttribute(String dataValue) {
051                if (dataValue == null) {
052                        return false;
053                }
054                return dataValue.equals("A");
055        }
056}