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;
020import java.math.BigInteger;
021
022/**
023 * Converts values of 0 or 1 to and from false or true.
024 *
025 * <p>The conversion treats the values as follows: 1 is true and 0 is false.</p>
026 *
027 * @author Kuali Rice Team (rice.collab@kuali.org)
028 */
029@Converter
030public class Boolean01Converter implements AttributeConverter<Boolean, BigInteger> {
031
032    /**
033     * {@inheritDoc}
034     *
035     * This implementation will convert from a false or true value to a 0 or 1 value.
036     */
037        @Override
038        public BigInteger convertToDatabaseColumn(Boolean objectValue) {
039                if (objectValue == null) {
040                        return BigInteger.valueOf(0);
041                }
042                return objectValue ? BigInteger.valueOf(1) : BigInteger.valueOf(0);
043        }
044
045    /**
046     * {@inheritDoc}
047     *
048     * This implementation will convert from a false or true value to a 0 or 1 value.
049     */
050        @Override
051        public Boolean convertToEntityAttribute(BigInteger dataValue) {
052                if (dataValue == null) {
053                        return false;
054                }
055                return dataValue.intValue() == 1;
056        }
057
058}