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;
21 import java.io.FileInputStream;
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.math.BigDecimal;
25 import java.util.List;
26 import java.util.Properties;
27 import java.util.Vector;
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 filePath = "";
42 private List retValue = new Vector();
43 private String multiplyValue = "";
44 private Properties properties = new Properties();
45 private String multiplyAll = "";
46 private String 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 filePath) throws Exception {
52
53 try {
54 this.filePath = filePath;
55 File propertyFile = new File(this.filePath);
56 this.properties.load(new FileInputStream(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 e) {
61 throw new Exception("File not found!");
62 } catch (IOException e) {
63 throw new Exception("Error while reading property file!");
64 }
65
66 }
67
68 /***
69 * This method release resources
70 */
71 public void release() throws Exception {
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 transformValue(List valueToTransform) throws Exception,ArithmeticException {
82 retValue.clear();
83 String oldValue = valueToTransform.get(0).toString();
84 BigDecimal bdMultiplyValue;
85 BigDecimal finalValue;
86 String key = "";
87 try {
88 if (this.multiplyAll.equalsIgnoreCase("true")) {
89 bdMultiplyValue = new BigDecimal(this.multiplyValue);
90 BigDecimal bigdec = new BigDecimal(oldValue);
91 finalValue = bigdec.multiply(bdMultiplyValue);
92 String multyValue ="";
93 try {
94 if (this.scale != ""){
95 multyValue = finalValue.setScale(new Integer(this.scale).intValue()).toString();
96 }else{
97 multyValue = finalValue.setScale(2).toString();
98 }
99 } catch (ArithmeticException e) {
100 throw new Exception("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 specificMultiplyValue = this.properties.getProperty(oldValue);
107 BigDecimal bigdec = new BigDecimal(oldValue);
108 bdMultiplyValue = new BigDecimal(specificMultiplyValue);
109 finalValue = bigdec.multiply(bdMultiplyValue);
110 String multyValue;
111 if (this.scale != ""){
112 multyValue = finalValue.setScale(new Integer(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 e) {
123
124 throw new Exception("Error while converting currency using class CurrencyConverter",e);
125 }
126
127 return retValue;
128 }
129
130 }
This page was automatically generated by Maven