Updater.java 1.34 KB
package pl.labno.bernard.nbp.exchangerate;

import org.apache.commons.httpclient.HttpURL;
import pl.labno.bernard.nbp.exchangerate.rest.ExchangeRateTableUnmarshaller;
import pl.labno.bernard.nbp.exchangerate.rest.generated.ExchangeRateTable;
import yarfraw.core.datamodel.ChannelFeed;
import yarfraw.core.datamodel.ItemEntry;
import yarfraw.core.datamodel.YarfrawException;
import yarfraw.utils.reader.FeedReaderUtils;

import javax.xml.bind.JAXBException;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.Executors;

public class Updater {

    private String feedURL = "http://rss.nbp.pl/kursy/TabelaA.xml";

    public String getFeedURL() {
        return feedURL;
    }

    public void setFeedURL(String feedURL) {
        this.feedURL = feedURL;
    }

    public ExchangeRateTable getRecentExchangeTable() throws IOException, YarfrawException, JAXBException {
        final List<ChannelFeed> feedList = FeedReaderUtils.readAll(Executors.newCachedThreadPool(), new HttpURL(feedURL));
        if(feedList.size() <  1) {
            throw new IllegalArgumentException("Read 0 feeds.");
        }
        final List<ItemEntry> entries = feedList.get(0).getItems();
        if(entries.size() < 1) {
            return null;
        }
        return ExchangeRateTableUnmarshaller.unmarshal(entries.get(0).getEnclosure().getUrl());
    }
}