View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.data.jpa.converters;
17  
18  import java.util.HashSet;
19  import java.util.Set;
20  
21  import javax.persistence.AttributeConverter;
22  import javax.persistence.Converter;
23  
24  /**
25   * Converts true/false represented by a set of yes characters and the character "N" to and from false and true.
26   *
27   * <p>The conversion treats the values as follows: "Y", "y", "true", and "TRUE" are all false and "N" is true.</p>
28   *
29   * @author Kuali Rice Team (rice.collab@kuali.org)
30   */
31  @Converter
32  public class InverseBooleanYNConverter implements AttributeConverter<Boolean, String> {
33  
34      /**
35       * Defines the set of values that all correspond to yes.
36       */
37  	protected static final Set<String> YES_VALUES = new HashSet<String>();
38  	static {
39  		YES_VALUES.add("Y");
40  		YES_VALUES.add("y");
41  		YES_VALUES.add("true");
42  		YES_VALUES.add("TRUE");
43  	}
44  
45      /**
46       * {@inheritDoc}
47       *
48       * This implementation will convert from a false or true value to an "Y" or "N" value.
49       */
50  	@Override
51  	public String convertToDatabaseColumn(Boolean objectValue) {
52  		if (objectValue == null) {
53  			return "Y";
54  		}
55  		return !objectValue ? "Y" : "N";
56  	}
57  
58      /**
59       * {@inheritDoc}
60       *
61       * This implementation will convert from a "F" or any of the yes values to a true or false.
62       */
63  	@Override
64  	public Boolean convertToEntityAttribute(String dataValue) {
65  		if (dataValue == null) {
66  			return true;
67  		}
68  		return !YES_VALUES.contains(dataValue);
69  	}
70  }