1 |
36864
|
antonis.le
|
package eu.dnetlib.goldoa.service;
|
2 |
|
|
|
3 |
36876
|
antonis.le
|
import eu.dnetlib.goldoa.domain.Currency;
|
4 |
45678
|
panagiotis
|
import org.springframework.stereotype.Service;
|
5 |
36864
|
antonis.le
|
import org.w3c.dom.Document;
|
6 |
|
|
|
7 |
37000
|
antonis.le
|
import javax.annotation.PostConstruct;
|
8 |
|
|
import javax.annotation.PreDestroy;
|
9 |
36864
|
antonis.le
|
import javax.xml.parsers.DocumentBuilder;
|
10 |
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
11 |
|
|
import javax.xml.parsers.ParserConfigurationException;
|
12 |
|
|
import javax.xml.xpath.XPath;
|
13 |
|
|
import javax.xml.xpath.XPathExpression;
|
14 |
|
|
import javax.xml.xpath.XPathExpressionException;
|
15 |
|
|
import javax.xml.xpath.XPathFactory;
|
16 |
|
|
import java.util.HashMap;
|
17 |
|
|
import java.util.Map;
|
18 |
|
|
import java.util.concurrent.Executors;
|
19 |
|
|
import java.util.concurrent.ScheduledExecutorService;
|
20 |
|
|
import java.util.concurrent.TimeUnit;
|
21 |
|
|
|
22 |
45595
|
panagiotis
|
/*
|
23 |
|
|
* Created by antleb on 5/4/15.
|
24 |
45403
|
panagiotis
|
*/
|
25 |
46139
|
panagiotis
|
@Service("currencyConverter")
|
26 |
36864
|
antonis.le
|
public class CurrencyConverter {
|
27 |
|
|
|
28 |
39076
|
antonis.le
|
private ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
|
29 |
|
|
private Map<Currency, Float> rateMap = new HashMap<Currency, Float>();
|
30 |
36864
|
antonis.le
|
|
31 |
39076
|
antonis.le
|
@PostConstruct
|
32 |
|
|
public void init() throws XPathExpressionException, ParserConfigurationException {
|
33 |
|
|
executorService.scheduleAtFixedRate(new RateUpdater(), 0, 1, TimeUnit.HOURS);
|
34 |
|
|
}
|
35 |
36864
|
antonis.le
|
|
36 |
39076
|
antonis.le
|
@PreDestroy
|
37 |
|
|
public void destroy() {
|
38 |
|
|
executorService.shutdownNow();
|
39 |
|
|
}
|
40 |
36864
|
antonis.le
|
|
41 |
39076
|
antonis.le
|
public float convert(Currency from, Currency to, float amount) {
|
42 |
|
|
float fromRate = rateMap.get(from);
|
43 |
|
|
float toRate = rateMap.get(to);
|
44 |
36864
|
antonis.le
|
|
45 |
39076
|
antonis.le
|
return (toRate / fromRate) * amount;
|
46 |
|
|
}
|
47 |
36864
|
antonis.le
|
|
48 |
39076
|
antonis.le
|
class RateUpdater implements Runnable {
|
49 |
36864
|
antonis.le
|
|
50 |
39076
|
antonis.le
|
private Map<Currency, XPathExpression> xpaths = new HashMap<Currency, XPathExpression>();
|
51 |
|
|
DocumentBuilder builder;
|
52 |
36864
|
antonis.le
|
|
53 |
39076
|
antonis.le
|
public RateUpdater() throws ParserConfigurationException, XPathExpressionException {
|
54 |
|
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
55 |
|
|
builder = factory.newDocumentBuilder();
|
56 |
36864
|
antonis.le
|
|
57 |
39076
|
antonis.le
|
XPathFactory xPathfactory = XPathFactory.newInstance();
|
58 |
|
|
XPath xpath = xPathfactory.newXPath();
|
59 |
36864
|
antonis.le
|
|
60 |
39076
|
antonis.le
|
for (Currency currency : Currency.values())
|
61 |
|
|
if (!currency.equals(Currency.EUR))
|
62 |
45688
|
panagiotis
|
xpaths.put(currency, xpath.compile("//*[local-name()='Cube' and @*[local-name()='currency']='" + currency.name() + "']/@rate"));
|
63 |
39076
|
antonis.le
|
}
|
64 |
36864
|
antonis.le
|
|
65 |
39076
|
antonis.le
|
@Override
|
66 |
|
|
public void run() {
|
67 |
|
|
try {
|
68 |
|
|
Document doc = builder.parse("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");
|
69 |
36864
|
antonis.le
|
|
70 |
39076
|
antonis.le
|
rateMap.put(Currency.EUR, 1.0f);
|
71 |
36864
|
antonis.le
|
|
72 |
39076
|
antonis.le
|
for (Currency currency : xpaths.keySet()) {
|
73 |
|
|
String rate = xpaths.get(currency).evaluate(doc);
|
74 |
36864
|
antonis.le
|
|
75 |
39076
|
antonis.le
|
if (rate != null)
|
76 |
|
|
rateMap.put(currency, Float.parseFloat(rate));
|
77 |
|
|
}
|
78 |
|
|
} catch (Exception e) {
|
79 |
|
|
e.printStackTrace();
|
80 |
|
|
}
|
81 |
|
|
}
|
82 |
|
|
}
|
83 |
36864
|
antonis.le
|
}
|