01: /*
02: * Copyright 2006 The Kuali Foundation.
03: *
04: * Licensed under the Educational Community License, Version 1.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.opensource.org/licenses/ecl1.php
09: *
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.core.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
28: OjbKualiDecimalFieldConversion implements FieldConversion {
29:
30: private static BigDecimal oneHundred = new BigDecimal(100.0000);
31:
32: /**
33: * Convert percentages to decimals for proper storage.
34: *
35: * @see FieldConversion#javaToSql(Object)
36: */
37: public Object javaToSql(Object source) {
38:
39: // Convert to BigDecimal using existing conversion.
40: source = super .javaToSql(source);
41:
42: // Check for null, and verify object type.
43: // Do conversion if our type is correct (BigDecimal).
44: if (source != null && source instanceof BigDecimal) {
45: BigDecimal converted = (BigDecimal) source;
46: return converted;
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(
66: (BigDecimal) converted.multiply(oneHundred));
67:
68: return percentConverted;
69:
70: } else {
71: return null;
72: }
73: }
74: }
|