package eu.socialsensor.framework.retrievers.newsfeed.rss;

import java.io.IOException;
import java.net.URL;
import java.util.List;


import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;

import eu.socialsensor.framework.abstractions.newsfeed.rss.RSSItem;
import eu.socialsensor.framework.common.domain.Feed;
import eu.socialsensor.framework.common.domain.feeds.URLFeed;
import eu.socialsensor.framework.retrievers.Retriever;
import eu.socialsensor.framework.streams.newsfeed.rss.RSSStream;


public class RSSRetriever implements Retriever{
	
	RSSStream rssStream;
	
	long oneMonthPeriod = 2592000000L;
	
	public RSSRetriever(RSSStream rssStream){
		
		this.rssStream = rssStream;
	}
	
	@Override
	public Integer retrieve(Feed feed){
		URLFeed ufeed = (URLFeed) feed;
		Integer totalRetrievedItems = 0;
		try {
			
			URL url  = new URL(ufeed.getURL());
			if(ufeed.getURL().equals(""))
				return totalRetrievedItems;
			
		    XmlReader reader = new XmlReader(url);

	        SyndFeed rssData = new SyndFeedInput().build(reader);
			
			@SuppressWarnings("unchecked")
			List<SyndEntry> rssEntries = rssData.getEntries();
			
			for (SyndEntry rss:rssEntries){
				
				if(rss.getLink() != null){
					
					if(rss.getPublishedDate() != null && rss.getPublishedDate().getTime()>0 
							&& Math.abs(System.currentTimeMillis() - rss.getPublishedDate().getTime())<oneMonthPeriod){
						
						RSSItem rssItem = new RSSItem(rss);
						
						rssStream.store(rssItem);
						
						totalRetrievedItems++;
					}
				
				}
				
				
			}
		 
	      
		} catch (IOException e) {
			// TODO Auto-generated catch block
			return 0;
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			return 0;
		} catch (FeedException e) {
			// TODO Auto-generated catch block
			return 0;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return 0;
		}
		return totalRetrievedItems;
	}

	
	@Override
	public void stop(){
	
	}
	

	
}

@@ -1180,19 +1180,35 @@
 );%0D%0A
+%09%09    
 %0D%0A
+%09
 %09    
+try%7B%0D%0A%09%09
     
+%09
 Synd
@@ -1260,21 +1260,20 @@
 er);%0D%0A%09%09
-%09
-%0D%0A%09%09
+    
 %09@Suppre
@@ -1297,16 +1297,17 @@
 cked%22)%0D%0A
+%09
 %09%09%09List%3C
@@ -1356,18 +1356,20 @@
 ();%0D%0A%09%09%09
+%09
 %0D%0A
+%09
 %09%09%09for (
@@ -1396,26 +1396,28 @@
 ries)%7B%0D%0A%09%09%09%09
+%09
 %0D%0A
+%09
 %09%09%09%09if(rss.g
@@ -1433,34 +1433,36 @@
 != null)%7B%0D%0A%09%09%09%09%09
+%09
 %0D%0A
+%09
 %09%09%09%09%09if(rss.getP
@@ -1524,16 +1524,17 @@
 e()%3E0 %0D%0A
+%09
 %09%09%09%09%09%09%09&
@@ -1631,24 +1631,25 @@
 %0D%0A%09%09%09%09%09%09
+%09
 %0D%0A%09%09%09%09%09%09
 RSSItem 
@@ -1640,16 +1640,17 @@
 %0D%0A%09%09%09%09%09%09
+%09
 RSSItem 
@@ -1680,26 +1680,28 @@
 ss);%0D%0A%09%09%09%09%09%09
+%09
 %0D%0A
+%09
 %09%09%09%09%09%09rssStr
@@ -1723,26 +1723,28 @@
 em);%0D%0A%09%09%09%09%09%09
+%09
 %0D%0A
+%09
 %09%09%09%09%09%09totalR
@@ -1770,50 +1770,147 @@
 %09%09%09%09
+%09
 %7D%0D%0A%09%09%09%09
+%09
 %0D%0A%09%09%09%09
+%09
 %7D%0D%0A%09%09%09%09
+%09
 %0D%0A%09%09%09%09
+%09
 %0D%0A%09%09%09
+%09
 %7D%0D%0A%09%09
- %0D%0A%09  
+%09%09%0D%0A%09%09%09%09%0D%0A%09%09    %7Dcatch(NullPointerException e2)%7B%0D%0A%09%09    %09return totalRetrievedItems;%0D%0A%09%09    %7D%0D%0A%09%09
     

add try-catch exception in rss retriever