View Javadoc
1   /**
2    * Copyright 2005-2015 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.math.BigDecimal;
19  
20  import javax.persistence.AttributeConverter;
21  import javax.persistence.Converter;
22  
23  import org.kuali.rice.core.api.util.type.KualiPercent;
24  
25  /**
26   * Converts the custom {@link KualiPercent} objects for OJB by converting them to/from {@link BigDecimal}.
27   */
28  @Converter(
29  		autoApply = true)
30  public class KualiPercentConverter implements AttributeConverter<KualiPercent, BigDecimal> {
31  
32      /**
33       * {@inheritDoc}
34       *
35       * This implementation will convert from a {@link KualiPercent} to a {@link BigDecimal}.
36       */
37  	@Override
38  	public BigDecimal convertToDatabaseColumn(KualiPercent objectValue) {
39  		if (objectValue == null) {
40  			return null;
41  		}
42  		return objectValue.bigDecimalValue();
43  	}
44  
45      /**
46       * {@inheritDoc}
47       *
48       * This implementation will convert from a {@link BigDecimal} to a {@link KualiPercent}.
49       */
50  	@Override
51  	public KualiPercent convertToEntityAttribute(BigDecimal dataValue) {
52  		if (dataValue == null) {
53  			return null;
54  		}
55  		return new KualiPercent(dataValue);
56  	}
57  }