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 */ 016package org.kuali.rice.krad.data.jpa.converters; 017 018import java.util.HashSet; 019import java.util.Set; 020 021import javax.persistence.AttributeConverter; 022import javax.persistence.Converter; 023 024/** 025 * Converts true/false represented by a set of yes characters and the character "N" to and from true and false. 026 * 027 * <p>The conversion treats the values as follows: "Y", "y", "true", and "TRUE" are all true and "N" is false.</p> 028 * 029 * @author Kuali Rice Team (rice.collab@kuali.org) 030 */ 031@Converter(autoApply = true) 032public class BooleanYNConverter 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 "N" or "Y" value. 049 */ 050 @Override 051 public String convertToDatabaseColumn(Boolean objectValue) { 052 if (objectValue == null) { 053 return "N"; 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 false or true. 062 */ 063 @Override 064 public Boolean convertToEntityAttribute(String dataValue) { 065 if (dataValue == null) { 066 return false; 067 } 068 return YES_VALUES.contains(dataValue); 069 } 070}