OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 104 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
104 ilm 1
package org.openconcerto.modules.finance.accounting.exchangerates;
2
 
3
import java.io.BufferedInputStream;
4
import java.io.ByteArrayOutputStream;
5
import java.io.IOException;
6
import java.math.BigDecimal;
7
import java.net.URL;
8
import java.util.ArrayList;
9
import java.util.Arrays;
10
import java.util.Calendar;
11
import java.util.Collections;
12
import java.util.Comparator;
13
import java.util.List;
14
import java.util.TimeZone;
15
 
16
import javax.xml.parsers.DocumentBuilder;
17
import javax.xml.parsers.DocumentBuilderFactory;
18
 
19
import org.openconcerto.utils.StringInputStream;
20
import org.w3c.dom.Document;
21
import org.w3c.dom.Element;
22
import org.w3c.dom.Node;
23
import org.w3c.dom.NodeList;
24
 
25
public class ExchangeRatesDownloader {
26
    public static List<String> supportedCurrencyCodes = Arrays.asList("USD", "JPY", "BGN", "CZK", "DKK", "GBP", "HUF", "PLN", "RON", "SEK", "CHF", "NOK", "HRK", "RUB", "TRY", "AUD", "BRL", "CAD",
27
            "CNY", "HKD", "IDR", "ILS", "INR", "KRW", "MXN", "MYR", "NZD", "PHP", "SGD", "THB", "ZAR");
28
    private final List<Report> reports = new ArrayList<Report>();
29
 
30
    public ExchangeRatesDownloader() {
31
    }
32
 
33
    public static void setSupportedCurrencyCodes(List<String> codes) {
34
        supportedCurrencyCodes = codes;
35
    }
36
 
37
    public static void main(String[] args) throws Exception {
38
        ExchangeRatesDownloader d = new ExchangeRatesDownloader();
39
        d.downloadAndParse();
40
        Report r = d.getLastReport();
41
        System.out.println(r.getDate());
42
        System.out.println(r.getRate("USD"));
43
        System.out.println(r.convert(new BigDecimal(100), "EUR", "USD"));
44
        System.out.println(r.convert(new BigDecimal(100), "USD", "EUR"));
45
        System.out.println(r.convert(new BigDecimal(1), "EUR", "PLN"));
46
        System.out.println(r.convert(new BigDecimal(1), "PLN", "EUR"));
47
        System.out.println(r.convert(new BigDecimal(1), "USD", "PLN"));
48
        System.out.println(r.convert(new BigDecimal(1), "JPY", "PLN"));
49
    }
50
 
51
    private Report getLastReport() {
52
        if (reports.isEmpty()) {
53
            return null;
54
        }
55
        return reports.get(reports.size() - 1);
56
    }
57
 
58
    public void downloadAndParse() throws Exception {
59
        String s = downloadXML();
60
        parse(s);
61
    }
62
 
63
    private void parse(String s) throws Exception {
64
        reports.clear();
65
        final DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
66
        final DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
67
        final Document doc = dBuilder.parse(new StringInputStream(s, "UTF-8"));
68
        doc.getDocumentElement().normalize();
69
        final NodeList children = doc.getFirstChild().getChildNodes();
70
        final int length = children.getLength();
71
        for (int i = 0; i < length; i++) {
72
            final Node n = children.item(i);
73
            if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals("Cube")) {
74
                NodeList cubes = ((Element) n).getChildNodes();
75
                for (int j = 0; j < cubes.getLength(); j++) {
76
                    final Node item = cubes.item(j);
77
                    if (item.getNodeType() == Node.ELEMENT_NODE) {
78
                        reports.add(parseReport((Element) item));
79
                    }
80
                }
81
            }
82
        }
83
        Collections.sort(reports, new Comparator<Report>() {
84
            @Override
85
            public int compare(Report o1, Report o2) {
86
                return o1.getDate().compareTo(o2.getDate());
87
            }
88
        });
89
    }
90
 
91
    private Report parseReport(Element element) {
92
        final String time = element.getAttribute("time");
93
        final int year = Integer.parseInt(time.substring(0, 4));
94
        final int month = Integer.parseInt(time.substring(5, 7));
95
        final int dayOfMonth = Integer.parseInt(time.substring(8, 10));
96
        final Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
97
        c.clear();
98
        c.set(Calendar.YEAR, year);
99
        c.set(Calendar.MONTH, month - 1);
100
        c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
101
        System.out.println(c.getTime());
102
        final Report r = new Report(c.getTime(), "EUR");
103
        final NodeList children = element.getChildNodes();
104
        final int length = children.getLength();
105
        for (int i = 0; i < length; i++) {
106
            final Element n = (Element) children.item(i);
107
            final String currency = n.getAttribute("currency");
108
            final BigDecimal rate = new BigDecimal(n.getAttribute("rate"));
109
            r.addRate(currency, rate);
110
        }
111
        return r;
112
    }
113
 
114
    public List<String> getSupportedCurrencyCodes() {
115
        return supportedCurrencyCodes;
116
    }
117
 
118
    public static String downloadXML() {
181 ilm 119
        final String urlString = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml";
104 ilm 120
        BufferedInputStream in = null;
121
        ByteArrayOutputStream fout = null;
122
        try {
123
            in = new BufferedInputStream(new URL(urlString).openStream());
124
            fout = new ByteArrayOutputStream();
125
 
126
            final byte data[] = new byte[1024];
127
            int count;
128
            while ((count = in.read(data, 0, 1024)) != -1) {
129
                fout.write(data, 0, count);
130
            }
131
        } catch (Exception e) {
132
            return null;
133
        } finally {
134
            if (in != null) {
135
                try {
136
                    in.close();
137
                } catch (IOException e) {
138
                    return null;
139
                }
140
            }
141
            if (fout != null) {
142
                try {
143
                    fout.close();
144
                    byte[] bytes = fout.toByteArray();
145
                    System.out.println(bytes.length);
146
                    return new String(bytes);
147
                } catch (IOException e) {
148
                    return null;
149
                }
150
            }
151
        }
152
        return null;
153
    }
154
 
155
    public List<Report> getReports() {
156
        return reports;
157
    }
158
}