package org.yacos.web.PCmember.controller;

import java.lang.reflect.UndeclaredThrowableException;

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.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;
import org.yacos.core.article.Article;
import org.yacos.core.article.IArticleManager;
import org.yacos.core.article.Article.State;

public class ValidateOrRejectController extends SimpleFormController {

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

	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException{
		
        logger.info("Returning ValidateOrReject view");
        String id=(String)request.getParameter("id");
        Article art=articleManager.getArticle(Integer.parseInt(id));
        String value=request.getParameter("value");
        if (value.equals("valide"))
        {
        	
				art.setState(State.ACCEPTED);
		 }
        else if (value.equals("reject"))
        {
        	
				art.setState(State.REJECTED);
			
        }
        
        try {
			articleManager.updateArticle(art);
		} catch (UndeclaredThrowableException e) {
			e.printStackTrace();
			System.out.println(e.getUndeclaredThrowable().toString());
		}
		return new ModelAndView(new RedirectView("validateArticle.htm"));
    }
	
	public IArticleManager getArticleManager() {
		return articleManager;
	}
	
	public void setArticleManager(IArticleManager articleManager) {
		this.articleManager = articleManager;
	}
}