001/**
002 * Copyright 2005-2015 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.BigDecimal;
021
022/**
023 * Converts values of 0 or 1 to and from false or true where the field is stored as a Decimal numeric type.
024 *
025 * @author Kuali Rice Team (rice.collab@kuali.org)
026 */
027@Converter
028public class Boolean01BigDecimalConverter implements AttributeConverter<Boolean, BigDecimal> {
029
030    /**
031     * {@inheritDoc}
032     *
033     * This implementation will convert from a false or true value to a 0 or 1 Decimal numeric type value.
034     */
035    @Override
036    public BigDecimal convertToDatabaseColumn(Boolean objectValue) {
037        if (objectValue == null) {
038            return BigDecimal.ZERO;
039        }
040        return objectValue ? BigDecimal.ONE : BigDecimal.ZERO;
041    }
042
043    /**
044     * {@inheritDoc}
045     *
046     * This implementation will convert from a 0 or 1 Decimal numeric type value to a false or true value.
047     */
048    @Override
049    public Boolean convertToEntityAttribute(BigDecimal dataValue){
050        if(dataValue == null){
051            return false;
052        }
053        return dataValue.compareTo(BigDecimal.ONE) == 0;
054    }
055
056}