01: /*
02: * Copyright 2005-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: *
24: *
25: */
26: public class OjbDecimalPercentageFieldConversion extends
27: 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.divide(oneHundred, 4,
46: KualiDecimal.ROUND_BEHAVIOR);
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 do the super conversion to KualiDecimal.
65: return super.sqlToJava(converted.multiply(oneHundred));
66: } else {
67: return null;
68: }
69: }
70: }
|