1 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 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 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 71 public void release() throws Exception { 72 73 } 74 75 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 } 131 | Popular Tags |