001/*
002 * Copyright (c) 2012, 2023, Werner Keil and others by the @author tag.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy of
006 * the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016package org.javamoney.moneta.convert.ecb;
017
018import org.javamoney.moneta.spi.loader.LoadDataInformation;
019import org.javamoney.moneta.spi.loader.LoadDataInformationBuilder;
020import org.javamoney.moneta.spi.loader.LoaderService;
021
022import javax.money.convert.ConversionContext;
023import javax.money.convert.ProviderContext;
024import javax.money.convert.ProviderContextBuilder;
025import javax.money.convert.RateType;
026import java.net.URI;
027import java.util.HashMap;
028import java.util.Map;
029
030import static org.javamoney.moneta.convert.ecb.defaults.Defaults.ECB_HIST_URL;
031
032/**
033 * <p>
034 * This class implements an {@link javax.money.convert.ExchangeRateProvider}
035 * that loads data from the European Central Bank data feed (XML). It loads the
036 * current exchange rates, as well as historic rates for the past 1500 days. The
037 * provider loads all data up to 1999 into its historic data cache.
038 * </p>
039 * <p>The default date is yesterday or the most recent day of week. To uses exchange rate from a specific date, you can use this way:</p>
040 * <p><code>CurrencyUnit termCurrency = ...;</code></p>
041 * <p><code>LocalDate localDate = ...;</code></p>
042 * <p><code>ConversionQuery conversionQuery = ConversionQueryBuilder.of().setTermCurrency(euro).set(localDate).build();</code>
043 * <p><code>CurrencyConversion currencyConversion = provider.getCurrencyConversion(conversionQuery);</code></p>
044 * <p><code>MonetaryAmount money = ...;</code></p>
045 * <p><code>MonetaryAmount result = currencyConversion.apply(money);</code></p>
046 *
047 * @author Anatole Tresch
048 * @author Werner Keil
049 * @author otaviojava
050 */
051public class ECBHistoricRateProvider extends ECBAbstractRateProvider {
052
053    /**
054     * The data id used for the LoaderService.
055     */
056    private static final String DATA_ID = ECBHistoricRateProvider.class.getSimpleName();
057
058    /**
059     * The {@link ConversionContext} of this provider.
060     */
061    private static final ProviderContext CONTEXT =
062            ProviderContextBuilder.of("ECB-HIST", RateType.HISTORIC, RateType.DEFERRED)
063                    .set("providerDescription", "European Central Bank").set("days", 1500).build();
064
065    public ECBHistoricRateProvider() {
066        super(CONTEXT, ECB_HIST_URL);
067    }
068
069    @Override
070    public String getDataId() {
071        return DATA_ID;
072    }
073    @Override
074    protected LoadDataInformation getDefaultLoadData() {
075        final Map<String, String> props = new HashMap<>();
076        props.put("period", "24:00");
077        props.put("delay", "01:00");
078        props.put("at", "07:00");
079
080        return new LoadDataInformationBuilder()
081            .withResourceId(getDataId())
082            .withUpdatePolicy(LoaderService.UpdatePolicy.SCHEDULED)
083            .withProperties(props)
084            .withBackupResource(URI.create("org/javamoney/moneta/convert/ecb/defaults/eurofxref-hist.xml"))
085            .withResourceLocations(URI.create(ECB_HIST_URL))
086            .withStartRemote(false)
087            .build();
088    }
089
090//    @Override TODO a Java 9+ version for a MRJ
091//    protected LoadDataInformation getDefaultLoadData() {
092//        return new LoadDataInformationBuilder()
093//            .withResourceId(getDataId())
094//            .withUpdatePolicy(LoaderService.UpdatePolicy.SCHEDULED)
095//            .withProperties(Map.of("period", "24:00",
096//                "delay", "01:00",
097//                "at", "07:00"))
098//            .withBackupResource(URI.create("org/javamoney/moneta/convert/ecb/defaults/eurofxref-hist.xml"))
099//            .withResourceLocations(URI.create("https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml"))
100//            .withStartRemote(true)
101//            .build();
102//    }
103
104}