package be.ugent.zeus.resto.client.data.services;

import java.io.StringReader;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import be.ugent.zeus.resto.client.data.NewsItem;
import be.ugent.zeus.resto.client.util.NewsXmlParser;
import android.content.Intent;
import android.os.Bundle;
import android.os.ResultReceiver;
import android.util.Log;

public class NewsIntentService extends HTTPIntentService {

	public NewsIntentService() {
		super("NewsIntentService");
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onHandleIntent(Intent intent) {
    Log.i("blub", "handle intent");
		final ResultReceiver receiver = intent.getParcelableExtra(RESULT_RECEIVER_EXTRA);
		
    final Bundle bundle = new Bundle();		
		try {
			String xml = fetch("http://golive.myverso.com/ugent/versions.xml");
			
			ArrayList<NewsItem> list = new ArrayList<NewsItem>();
		
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			Document doc = dbf.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
			NodeList nodeList = doc.getFirstChild().getChildNodes();
			NewsXmlParser parser = new NewsXmlParser();
			
			for(int i = 0; i < nodeList.getLength(); i++) {
				Node node = nodeList.item(i);
				
				String path = node.getAttributes().getNamedItem("path").getTextContent();
				if(path.startsWith("News")) {
          try {
            Log.i("[NewsIntentService]", "Downloading " + path);
            String clubXML = fetch("http://golive.myverso.com/ugent/" + path);
            // TODO: filter based on user preferences
            list.addAll(parser.parse(clubXML));
          } catch (Exception e) {
            Log.e("[NewIntentService]", e.getMessage());
          }
				}
			}
			
			bundle.putSerializable("newsItemList", list);
			
		} catch (Exception e) {
      Log.e("[NewsIntentService]", e.getMessage());
		}		
    receiver.send(STATUS_FINISHED, bundle);
	}
}

@@ -44,16 +44,235 @@
 vices;%0A%0A
+import android.content.Intent;%0Aimport android.os.Bundle;%0Aimport android.os.ResultReceiver;%0Aimport android.util.Log;%0Aimport be.ugent.zeus.resto.client.data.NewsItem;%0Aimport be.ugent.zeus.resto.client.util.NewsXmlParser;%0A
 import j
@@ -288,24 +288,24 @@
 ringReader;%0A
-
 import java.
@@ -320,17 +320,16 @@
 ayList;%0A
-%0A
 import j
@@ -369,17 +369,16 @@
 actory;%0A
-%0A
 import o
@@ -489,228 +489,8 @@
 e;%0A%0A
-import be.ugent.zeus.resto.client.data.NewsItem;%0Aimport be.ugent.zeus.resto.client.util.NewsXmlParser;%0Aimport android.content.Intent;%0Aimport android.os.Bundle;%0Aimport android.os.ResultReceiver;%0Aimport android.util.Log;%0A%0A
 publ
@@ -609,50 +609,8 @@
 %22);%0A
-%09%09// TODO Auto-generated constructor stub%0A
 %09%7D%0A%0A
@@ -867,41 +867,26 @@
 tch(
-%22http://golive.myverso.com/ugent/
+HYDRA_BASE_URL + %22
 vers
@@ -1554,42 +1554,22 @@
 tch(
-%22http://golive.myverso.com/ugent/%22
+HYDRA_BASE_URL
  + p

Use HYDRA_BASE_URL constant, instead of mulitple hardcoded paths