Project

General

Profile

1
package eu.dnetlib.goldoa.service;
2

    
3
import eu.dnetlib.goldoa.domain.Currency;
4
import org.w3c.dom.Document;
5

    
6
import javax.annotation.PostConstruct;
7
import javax.annotation.PreDestroy;
8
import javax.xml.parsers.DocumentBuilder;
9
import javax.xml.parsers.DocumentBuilderFactory;
10
import javax.xml.parsers.ParserConfigurationException;
11
import javax.xml.xpath.XPath;
12
import javax.xml.xpath.XPathExpression;
13
import javax.xml.xpath.XPathExpressionException;
14
import javax.xml.xpath.XPathFactory;
15
import java.util.HashMap;
16
import java.util.Map;
17
import java.util.concurrent.Executors;
18
import java.util.concurrent.ScheduledExecutorService;
19
import java.util.concurrent.TimeUnit;
20

    
21
/**
22
 * Created by antleb on 5/4/15.
23
 */
24
public class CurrencyConverter {
25

    
26
    private ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
27
    private Map<Currency, Float> rateMap = new HashMap<Currency, Float>();
28

    
29
    @PostConstruct
30
    public void init() throws XPathExpressionException, ParserConfigurationException {
31
        executorService.scheduleAtFixedRate(new RateUpdater(), 0, 1, TimeUnit.HOURS);
32
    }
33

    
34
    @PreDestroy
35
    public void destroy() {
36
        executorService.shutdownNow();
37
    }
38

    
39
    public float convert(Currency from, Currency to, float amount) {
40
        float fromRate = rateMap.get(from);
41
        float toRate = rateMap.get(to);
42

    
43
        return (toRate/fromRate)*amount;
44
    }
45

    
46
    class RateUpdater implements Runnable {
47

    
48
        private Map<Currency, XPathExpression> xpaths = new HashMap<Currency, XPathExpression>();
49
        DocumentBuilder builder;
50

    
51
        public RateUpdater() throws ParserConfigurationException, XPathExpressionException {
52
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
53
            builder = factory.newDocumentBuilder();
54

    
55
            XPathFactory xPathfactory = XPathFactory.newInstance();
56
            XPath xpath = xPathfactory.newXPath();
57

    
58
            for (Currency currency: Currency.values())
59
                if (!currency.equals(Currency.EUR))
60
                    xpaths.put(currency, xpath.compile("//*[local-name()='Cube' and @*[local-name()='currency']='" + currency.name() + "']/@rate"));
61
        }
62

    
63
        @Override
64
        public void run() {
65
            try {
66
                Document doc = builder.parse("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");
67

    
68
                rateMap.put(Currency.EUR, 1.0f);
69

    
70
                for (Currency currency:xpaths.keySet()) {
71
                    String rate = xpaths.get(currency).evaluate(doc);
72

    
73
                    if (rate != null)
74
                        rateMap.put(currency, Float.parseFloat(rate));
75
                }
76
            } catch (Exception e) {
77
                e.printStackTrace();
78
            }
79
        }
80
    }
81
}
(5-5/29)