Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
OjbDecimalPercentageFieldConversion |
|
| 4.0;4 |
1 | /* | |
2 | * Copyright 2005-2007 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 | * | |
24 | * | |
25 | */ | |
26 | 0 | public class OjbDecimalPercentageFieldConversion extends OjbKualiDecimalFieldConversion implements FieldConversion { |
27 | ||
28 | 0 | private static BigDecimal oneHundred = new BigDecimal(100.0000); |
29 | ||
30 | /** | |
31 | * Convert percentages to decimals for proper storage. | |
32 | * | |
33 | * @see FieldConversion#javaToSql(Object) | |
34 | */ | |
35 | public Object javaToSql(Object source) { | |
36 | ||
37 | // Convert to BigDecimal using existing conversion. | |
38 | 0 | source = super.javaToSql(source); |
39 | ||
40 | // Check for null, and verify object type. | |
41 | // Do conversion if our type is correct (BigDecimal). | |
42 | 0 | if (source != null && source instanceof BigDecimal) { |
43 | 0 | BigDecimal converted = (BigDecimal) source; |
44 | 0 | return converted.divide(oneHundred, 4, KualiDecimal.ROUND_BEHAVIOR); |
45 | } | |
46 | else { | |
47 | 0 | return null; |
48 | } | |
49 | } | |
50 | ||
51 | /** | |
52 | * Convert database decimals to 'visual' percentages for use in our business objects. | |
53 | * | |
54 | * @see FieldConversion#sqlToJava(Object) | |
55 | */ | |
56 | public Object sqlToJava(Object source) { | |
57 | ||
58 | // Check for null, and verify object type. | |
59 | // Do conversion if our type is correct (BigDecimal). | |
60 | 0 | if (source != null && source instanceof BigDecimal) { |
61 | 0 | BigDecimal converted = (BigDecimal) source; |
62 | ||
63 | // Once we have converted, we need to do the super conversion to KualiDecimal. | |
64 | 0 | return super.sqlToJava(converted.multiply(oneHundred)); |
65 | } | |
66 | else { | |
67 | 0 | return null; |
68 | } | |
69 | } | |
70 | } |