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_CURRENT_FALLBACK_PATH;
031import static org.javamoney.moneta.convert.ecb.defaults.Defaults.ECB_CURRENT_URL;
032
033/**
034 * This class implements an {@link javax.money.convert.ExchangeRateProvider} that loads data from
035 * the European Central Bank data feed (XML). It loads the current exchange
036 * rates. The provider loads all data up to 1999 into its
037 * historic data cache.
038 *
039 * @author Anatole Tresch
040 * @author Werner Keil
041 * @author otaviojava
042 */
043public class ECBCurrentRateProvider extends ECBAbstractRateProvider {
044
045    /**
046     * The data id used for the LoaderService.
047     */
048    private static final String DATA_ID = ECBCurrentRateProvider.class.getSimpleName();
049    /**
050     * The {@link ConversionContext} of this provider.
051     */
052    private static final ProviderContext CONTEXT =
053            ProviderContextBuilder.of("ECB", RateType.DEFERRED).set("providerDescription", "European Central Bank")
054                    .set("days", 1).build();
055
056    public ECBCurrentRateProvider() {
057        super(CONTEXT, ECB_CURRENT_URL);
058    }
059
060    @Override
061    public String getDataId() {
062        return DATA_ID;
063    }
064
065    @Override
066    protected LoadDataInformation getDefaultLoadData() {
067        final Map<String, String> props = new HashMap<>();
068        props.put("period", "03:00");
069
070        return new LoadDataInformationBuilder()
071                .withResourceId(getDataId())
072                .withUpdatePolicy(LoaderService.UpdatePolicy.SCHEDULED)
073                .withProperties(props)
074                .withBackupResource(URI.create(ECB_CURRENT_FALLBACK_PATH))
075                .withResourceLocations(URI.create(ECB_CURRENT_URL))
076                .withStartRemote(true)
077                .build();
078    }
079
080//    @Override TODO a Java 9+ version for a MRJ
081//    protected LoadDataInformation getDefaultLoadData() {
082//        return new LoadDataInformationBuilder()
083//            .withResourceId(getDataId())
084//            .withUpdatePolicy(LoaderService.UpdatePolicy.SCHEDULED)
085//            .withProperties(Map.of("period", "03:00"))
086//            .withBackupResource(URI.create("org/javamoney/moneta/convert/ecb/defaults/eurofxref-daily.xml"))
087//            .withResourceLocations(URI.create("https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"))
088//            .withStartRemote(true)
089//            .build();
090//    }
091}