package org.yacos.web.author.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ModelAndViewDefiningException;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.view.RedirectView;
import org.yacos.core.article.Article;
import org.yacos.core.article.IArticleManager;

public class ArticleDeleteController implements Controller {
	private IArticleManager articleManager;

	public IArticleManager getArticleManager() {
		return articleManager;
	}

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

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

		try {
			Integer articleId = Integer.parseInt(request.getParameter("articleId"));
			Article article = articleManager.getArticle(articleId);

			// FIXME : vérifier si le gars a le droit de suppr ou pas

			if(article == null){
				return new ModelAndView(new RedirectView("404error.htm"));
			}

			articleManager.removeArticle(articleId);


		} 
		catch(NumberFormatException e) {
			throw new ModelAndViewDefiningException(new ModelAndView("404error"));
		}

		return new ModelAndView(new RedirectView("manageArticle.htm"));
	}

}