package gr.iti.mklab.framework.abstractions.socialmedia.items;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;

import com.sun.syndication.feed.module.slash.Slash;
import com.sun.syndication.feed.module.mediarss.MediaEntryModule;
import com.sun.syndication.feed.module.mediarss.types.MediaContent;
import com.sun.syndication.feed.module.mediarss.types.Metadata;
import com.sun.syndication.feed.synd.SyndCategory;
import com.sun.syndication.feed.synd.SyndEntry;

import gr.iti.mklab.framework.common.domain.Item;
import gr.iti.mklab.framework.common.domain.MediaItem;

/**
 * Class that holds the information of an RSS feed
 * 
 * @author Manos Schinas
 * 
 * @author manosetro@iti.gr
 */
public class RSSItem extends Item {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1413164596016357110L;

	// URIs
	private static String mrss = "http://search.yahoo.com/mrss/";
	private static String slash = "http://purl.org/rss/1.0/modules/slash/";
			
	public RSSItem(SyndEntry syndEntry) {
		
		if(syndEntry == null || syndEntry.getLink() == null)
			return;
		
		//Id
		id = syndEntry.getLink();
		
		//Document's title
		title = syndEntry.getTitle();
		
		//description = rssEntry.getDescription().getValue();
		//Document's content - Extract text content from html structure
		if(syndEntry.getDescription() != null) {
			description = extractText(syndEntry.getDescription().getValue());
		}
		
		//Document's time of publication
		publicationTime = syndEntry.getPublishedDate().getTime();
		
		//The URL where the document can be found
		url = syndEntry.getLink();
		
		uid = syndEntry.getAuthor();
		
		@SuppressWarnings("unchecked")
		List<SyndCategory> syndCategories = syndEntry.getCategories();
		if(syndCategories != null) {
			List<String> categories = new ArrayList<String>();
			for(SyndCategory category : syndCategories) {
				categories.add(category.getName());
			}
			tags = categories.toArray(new String[categories.size()]);
		}
		
		mediaItems = getMediaItems(syndEntry);
		
		Slash slashModule = (Slash) syndEntry.getModule(slash);
		if(slashModule != null) {
			comments = (long) slashModule.getComments();
		}
		
	}
	
	private List<MediaItem> getMediaItems(SyndEntry syndEntry) {
		List<MediaItem> mediaItems = new ArrayList<MediaItem>();
		MediaEntryModule module = (MediaEntryModule) syndEntry.getModule(mrss);
		if(module != null) {
			MediaContent[] mediaContents = module.getMediaContents();
			for(MediaContent mediaContent : mediaContents) {
				MediaItem mi = new MediaItem();
				mi.setUrl(mediaContent.getReference().toString());
				mi.setType(mediaContent.getMedium());
				
				Metadata metadata = mediaContent.getMetadata();
				mi.setTitle(metadata.getTitle()); 
				mi.setDescription(metadata.getDescription());
				
				mediaItems.add(mi);
			}
		}
		return mediaItems;
	}
	
	private String extractText(String content) {
		org.jsoup.nodes.Document doc = Jsoup.parse(content);
		String text = doc.body().text();
		return text;
	}
	
}

@@ -139,18 +139,16 @@
 Jsoup;%0D%0A
-%0D%0A
 import c
@@ -445,16 +445,69 @@
 egory;%0D%0A
+import com.sun.syndication.feed.synd.SyndEnclosure;%0D%0A
 import c
@@ -1063,20 +1063,18 @@
 sh/%22;%0D%0A%09
-%09%09
 %0D%0A
+
 %09public 
@@ -2287,20 +2287,18 @@
 ;%0D%0A%09%09%7D%0D%0A
-%09%09
 %0D%0A
+
 %09%7D%0D%0A%09%0D%0A%09
@@ -2419,16 +2419,442 @@
 em%3E();%0D%0A
+%09%09%0D%0A%09%09@SuppressWarnings(%22unchecked%22)%0D%0A%09%09List%3CSyndEnclosure%3E enclosures = syndEntry.getEnclosures();%0D%0A%09%09for(SyndEnclosure encl : enclosures) %7B%0D%0A%09%09%09MediaItem mi = new MediaItem();%0D%0A%09%09%09%0D%0A%09%09%09mi.setUrl(encl.getUrl());%0D%0A%09%09%09%0D%0A%09%09%09String type = encl.getType();%0D%0A%09%09%09if(type.contains(%22image%22)) %7B%0D%0A%09%09%09%09mi.setType(%22image%22);%0D%0A%09%09%09%7D%0D%0A%09%09%09else if(type.contains(%22video%22)) %7B%0D%0A%09%09%09%09mi.setType(%22video%22);%0D%0A%09%09%09%7D%0D%0A%09%09%09%0D%0A%09%09%09mediaItems.add(mi);%0D%0A%09%09%7D%0D%0A%09%09%0D%0A
 %09%09MediaE
@@ -3294,16 +3294,16 @@
 e()); %0D%0A
-
 %09%09%09%09mi.s
@@ -3347,24 +3347,185 @@
 ());%0D%0A%09%09%09%09%0D%0A
+%09%09%09%09if(mediaContent.getWidth() != null && mediaContent.getHeight() != null) %7B%0D%0A%09%09%09%09%09mi.setSize(mediaContent.getWidth(), mediaContent.getHeight());%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%09%0D%0A
 %09%09%09%09mediaIte

Add SyndEnclosure in RSS items