package org.yacos.web.author.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ModelAndViewDefiningException;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.yacos.core.article.Article;
import org.yacos.core.article.IArticleManager;

public class DetailArticleController extends SimpleFormController {

	protected final Log logger = LogFactory.getLog(getClass());
	private IArticleManager articleManager;

	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {

		logger.info("Returning details view");

		String articleId = request.getParameter("id");
		
		try {
			Integer id = new Integer(articleId);

			Article article = articleManager.getArticle(id);
			
			if (article == null)
				throw new ModelAndViewDefiningException(new ModelAndView("404error"));

			Map <String, Object> model = new HashMap<String, Object>();
			
			model.put("whereIcome", request.getHeader("REFERER"));
			model.put("article", article);
			
			return new ModelAndView("detailArticle", model);
		}
		catch (NumberFormatException e) {
			throw new ModelAndViewDefiningException(new ModelAndView("404error"));
		}
	}

	public IArticleManager getArticleManager() {
		return articleManager;
	}

	public void setArticleManager(IArticleManager articleManager) {
		this.articleManager = articleManager;
	}


}