KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > webdocwf > util > loader > transformation > CurrencyConverter


1 /*
2     Copyright (C) 2003 Together
3
4     This library is free software; you can redistribute it and/or
5     modify it under the terms of the GNU Lesser General Public
6     License as published by the Free Software Foundation; either
7     version 2.1 of the License, or (at your option) any later version.
8
9     This library is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12     Lesser General Public License for more details.
13
14     You should have received a copy of the GNU Lesser General Public
15     License along with this library; if not, write to the Free Software
16     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */

18 package org.webdocwf.util.loader.transformation;
19
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.math.BigDecimal JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Properties JavaDoc;
27 import java.util.Vector JavaDoc;
28
29
30
31 /**
32  * This class implements Transformer interface. It can be used to convert
33  * currency from one to another type
34  * Date: 23.07.2004.
35  * @version 1.0
36  * @author Zeljko Kovacevic
37  *
38  */

39 public class CurrencyConverter implements Transformer {
40
41     private String JavaDoc filePath = "";
42     private List JavaDoc retValue = new Vector JavaDoc();
43     private String JavaDoc multiplyValue = "";
44     private Properties JavaDoc properties = new Properties JavaDoc();
45     private String JavaDoc multiplyAll = "";
46     private String JavaDoc scale = "";
47     /**
48      * This method reads path to property file where currency is placed.
49      * @param filePath Path to property file
50      */

51     public void configure(String JavaDoc filePath) throws Exception JavaDoc {
52
53         try {
54             this.filePath = filePath;
55             File JavaDoc propertyFile = new File JavaDoc(this.filePath);
56             this.properties.load(new FileInputStream JavaDoc(propertyFile));
57             this.multiplyValue = this.properties.getProperty("multiplyValue");
58             this.multiplyAll = this.properties.getProperty("multiplyAll");
59                         this.scale = this.properties.getProperty("scale");
60         } catch (FileNotFoundException JavaDoc e) {
61             throw new Exception JavaDoc("File not found!");
62         } catch (IOException JavaDoc e) {
63             throw new Exception JavaDoc("Error while reading property file!");
64         }
65
66     }
67
68     /**
69      * This method release resources
70      */

71     public void release() throws Exception JavaDoc {
72
73     }
74
75     /**
76      * This method will multiply currency from source for given value from
77      * property file
78      * @param valueToTransform List for transformation
79      * @return List with transformed values
80      */

81     public List JavaDoc transformValue(List JavaDoc valueToTransform) throws Exception JavaDoc,ArithmeticException JavaDoc {
82         retValue.clear();
83         String JavaDoc oldValue = valueToTransform.get(0).toString();
84                 BigDecimal JavaDoc bdMultiplyValue;
85                 BigDecimal JavaDoc finalValue;
86         String JavaDoc key = "";
87         try {
88             if (this.multiplyAll.equalsIgnoreCase("true")) {
89                                 bdMultiplyValue = new BigDecimal JavaDoc(this.multiplyValue);
90                     BigDecimal JavaDoc bigdec = new BigDecimal JavaDoc(oldValue);
91                                 finalValue = bigdec.multiply(bdMultiplyValue);
92                                 String JavaDoc multyValue ="";
93                                 try {
94                         if (this.scale != ""){
95                             multyValue = finalValue.setScale(new Integer JavaDoc(this.scale).intValue()).toString();
96                         }else{
97                         multyValue = finalValue.setScale(2).toString();
98                                 }
99                 } catch (ArithmeticException JavaDoc e) {
100                    throw new Exception JavaDoc("Error. Check values in property file.Probably, value for scale is too small, because of multiplyValue!");
101                 }
102                 retValue.add(multyValue);
103             } else {
104                 boolean containsKey = this.properties.containsKey(oldValue);
105                 if (containsKey == true) {
106                     String JavaDoc specificMultiplyValue = this.properties.getProperty(oldValue);
107                                         BigDecimal JavaDoc bigdec = new BigDecimal JavaDoc(oldValue);
108                                         bdMultiplyValue = new BigDecimal JavaDoc(specificMultiplyValue);
109                                         finalValue = bigdec.multiply(bdMultiplyValue);
110                                         String JavaDoc multyValue;
111                                         if (this.scale != ""){
112                                             multyValue = finalValue.setScale(new Integer JavaDoc(this.scale).intValue()).toString();
113                                         }else{
114                                             multyValue = finalValue.setScale(2).toString();
115                                         }
116                                             retValue.add(multyValue);
117                     
118                 } else {
119                     retValue.add(oldValue);
120                 }
121             }
122         } catch (Exception JavaDoc e) {
123             
124             throw new Exception JavaDoc("Error while converting currency using class CurrencyConverter",e);
125         }
126
127         return retValue;
128     }
129
130 }
131
Popular Tags