View Javadoc

1   /*
2    * Copyright 2006-2008 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.kns.util;
17  
18  import java.math.BigDecimal;
19  
20  import org.apache.ojb.broker.accesslayer.conversions.FieldConversion;
21  
22  /**
23   * This class...
24   * 
25   * 
26   */
27  public class OjbDecimalKualiPercentFieldConversion extends OjbKualiDecimalFieldConversion implements FieldConversion {
28  
29      private static BigDecimal oneHundred = new BigDecimal(100.0000);
30  
31      /**
32       * Convert percentages to decimals for proper storage.
33       * 
34       * @see FieldConversion#javaToSql(Object)
35       */
36      public Object javaToSql(Object source) {
37  
38          // Convert to BigDecimal using existing conversion.
39          source = super.javaToSql(source);
40  
41          // Check for null, and verify object type.
42          // Do conversion if our type is correct (BigDecimal).
43          if (source != null && source instanceof BigDecimal) {
44              BigDecimal converted = (BigDecimal) source;
45              return converted;
46          }
47          else {
48              return null;
49          }
50      }
51  
52      /**
53       * Convert database decimals to 'visual' percentages for use in our business objects.
54       * 
55       * @see FieldConversion#sqlToJava(Object)
56       */
57      public Object sqlToJava(Object source) {
58  
59          // Check for null, and verify object type.
60          // Do conversion if our type is correct (BigDecimal).
61          if (source != null && source instanceof BigDecimal) {
62              BigDecimal converted = (BigDecimal) source;
63  
64              // Once we have converted, we need to convert again to KualiPercent.
65              KualiPercent percentConverted = new KualiPercent((BigDecimal) converted.multiply(oneHundred));
66  
67              return percentConverted;
68  
69          }
70          else {
71              return null;
72          }
73      }
74  }