001 /**
002 * Copyright 2005-2014 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.rice.krad.data.jpa.converters;
017
018 import java.util.HashSet;
019 import java.util.Set;
020
021 import javax.persistence.AttributeConverter;
022 import javax.persistence.Converter;
023
024 /**
025 * Converts true/false represented by a set of yes characters and the character "N" to and from false and true.
026 *
027 * <p>The conversion treats the values as follows: "Y", "y", "true", and "TRUE" are all false and "N" is true.</p>
028 *
029 * @author Kuali Rice Team (rice.collab@kuali.org)
030 */
031 @Converter
032 public class InverseBooleanYNConverter implements AttributeConverter<Boolean, String> {
033
034 /**
035 * Defines the set of values that all correspond to yes.
036 */
037 protected static final Set<String> YES_VALUES = new HashSet<String>();
038 static {
039 YES_VALUES.add("Y");
040 YES_VALUES.add("y");
041 YES_VALUES.add("true");
042 YES_VALUES.add("TRUE");
043 }
044
045 /**
046 * {@inheritDoc}
047 *
048 * This implementation will convert from a false or true value to an "Y" or "N" value.
049 */
050 @Override
051 public String convertToDatabaseColumn(Boolean objectValue) {
052 if (objectValue == null) {
053 return "Y";
054 }
055 return !objectValue ? "Y" : "N";
056 }
057
058 /**
059 * {@inheritDoc}
060 *
061 * This implementation will convert from a "F" or any of the yes values to a true or false.
062 */
063 @Override
064 public Boolean convertToEntityAttribute(String dataValue) {
065 if (dataValue == null) {
066 return true;
067 }
068 return !YES_VALUES.contains(dataValue);
069 }
070 }