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.ProviderContext;
023import javax.money.convert.ProviderContextBuilder;
024import javax.money.convert.RateType;
025import java.net.URI;
026import java.util.HashMap;
027import java.util.Map;
028
029import static org.javamoney.moneta.convert.ecb.defaults.Defaults.ECB_HIST90_FALLBACK_PATH;
030import static org.javamoney.moneta.convert.ecb.defaults.Defaults.ECB_HIST90_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 90 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).setTimestamp(localDate).build();</code>v
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 ECBHistoric90RateProvider extends ECBAbstractRateProvider {
052
053
054    private static final String DATA_ID = ECBHistoric90RateProvider.class.getSimpleName();
055
056    private static final ProviderContext CONTEXT =
057            ProviderContextBuilder.of("ECB-HIST90", RateType.HISTORIC, RateType.DEFERRED)
058                    .set("providerDescription", "European Central Bank (last 90 days)").set("days", 90).build();
059
060    public ECBHistoric90RateProvider() {
061        super(CONTEXT, ECB_HIST90_URL);
062    }
063
064    @Override
065    public String getDataId() {
066        return DATA_ID;
067    }
068
069    @Override
070    protected LoadDataInformation getDefaultLoadData() {
071        final Map<String, String> props = new HashMap<>();
072        props.put("period", "03:00");
073
074        return new LoadDataInformationBuilder()
075                .withResourceId(getDataId())
076                .withUpdatePolicy(LoaderService.UpdatePolicy.SCHEDULED)
077                .withProperties(props)
078                .withBackupResource(URI.create(ECB_HIST90_FALLBACK_PATH))
079                .withResourceLocations(URI.create(ECB_HIST90_URL))
080                .withStartRemote(true)
081                .build();
082    }
083
084//    @Override TODO this is for a MRJ version <=Java 9
085//    protected LoadDataInformation getDefaultLoadData() {
086//        return new LoadDataInformationBuilder()
087//            .withResourceId(getDataId())
088//            .withUpdatePolicy(LoaderService.UpdatePolicy.SCHEDULED)
089//            .withProperties(Map.of("period", "03:00"))
090//            .withBackupResource(URI.create("org/javamoney/moneta/convert/ecb/defaults/eurofxref-hist-90d.xml"))
091//            .withResourceLocations(URI.create("https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml"))
092//            .withStartRemote(true)
093//            .build();
094//    }
095
096}