package se.kth.infosys.smx.ladok3;

import java.net.URL;
import java.util.Date;
import java.util.List;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.impl.ScheduledPollConsumer;

import com.rometools.rome.feed.synd.SyndEntry;
import com.rometools.rome.feed.synd.SyndFeed;
import com.rometools.rome.io.SyndFeedInput;
import com.rometools.rome.io.XmlReader;

/**
 * The ladok3 consumer.
 */
public class Ladok3Consumer extends ScheduledPollConsumer {
    private final Ladok3Endpoint endpoint;

    public Ladok3Consumer(Ladok3Endpoint endpoint, Processor processor) throws Exception {
        super(endpoint, processor);
        this.endpoint = endpoint;
    }

    @Override
    protected int poll() throws Exception {
        URL feedUrl = new URL(String.format("https://%s/handelser/feed/recent", endpoint.getHost()));

        XmlReader reader = new XmlReader(endpoint.get(feedUrl));

        SyndFeedInput input = new SyndFeedInput();
        SyndFeed feed = input.build(reader);
        List<SyndEntry> entries = feed.getEntries();

        Exchange exchange = endpoint.createExchange();

        // create a message body
        Date now = new Date();
        exchange.getIn().setBody("Hello World! The time is " + now);

        try {
            // send message to next processor in the route
            getProcessor().process(exchange);
            return 1; // number of messages polled
        } finally {
            // log exception if an exception occurred and was not handled
            if (exchange.getException() != null) {
                getExceptionHandler().handleException("Error processing exchange", exchange, exchange.getException());
            }
        }
    }
}

@@ -29,16 +29,83 @@
 adok3;%0A%0A
+import java.io.IOException;%0Aimport java.net.MalformedURLException;%0A
 import j
@@ -326,21 +326,20 @@
 ynd.Synd
-Entry
+Feed
 ;%0Aimport
@@ -364,36 +364,80 @@
 e.feed.synd.Synd
-Feed
+Link;%0Aimport com.rometools.rome.io.FeedException
 ;%0Aimport com.rom
@@ -891,16 +891,27 @@
 edUrl = 
+rewindFeed(
 new URL(
@@ -980,18 +980,18 @@
 Host()))
+)
 ;
-%0A
 %0A       
@@ -995,212 +995,59 @@
     
-XmlReader reader = new XmlReader(endpoint.get(feedUrl));%0A%0A        SyndFeedInput input = new SyndFeedInput();%0A        SyndFeed feed = input.build(reader);%0A        List%3CSyndEntry%3E entries = feed.getEntries(
+log.debug(%22Start fetching events from: %7B%7D%22, feedUrl
 );%0A%0A
@@ -1700,10 +1700,1158 @@
 %7D%0A    %7D%0A
-%7D
 %0A
+    private SyndFeed getFeed(URL feedUrl) throws IOException, FeedException %7B%0A        log.debug(%22fetching feed: %7B%7D%22, feedUrl);%0A        XmlReader reader = new XmlReader(endpoint.get(feedUrl));%0A        SyndFeedInput input = new SyndFeedInput();%0A        return input.build(reader);%0A    %7D%0A%0A    private URL getLink(String rel, List%3CSyndLink%3E links) throws MalformedURLException %7B%0A        for (SyndLink link : links) %7B%0A            if (link.getRel().equals(rel)) %7B%0A                return new URL(link.getHref());%0A            %7D%0A        %7D%0A        return null;%0A    %7D%0A%0A    private int feedId(SyndFeed feed) %7B%0A        return Integer.parseInt(feed.getUri().trim().substring(7)); // %22getUri() -%3E urn:id:123%22%0A    %7D%0A%0A    private URL rewindFeed(URL url) throws IOException, FeedException %7B%0A        SyndFeed feed = getFeed(url);%0A%0A        if (feedId(feed) == endpoint.getFeedId()) %7B%0A            return getLink(%22next-archive%22, feed.getLinks());%0A        %7D%0A%0A        URL prevArchive = getLink(%22prev-archive%22, feed.getLinks());%0A        if (prevArchive == null) %7B%0A            return getLink(%22via%22, feed.getLinks());%0A        %7D%0A%0A        return rewindFeed(prevArchive);%0A    %7D%0A%7D

Implement feed rewind to next feed.