
package jm14.ant.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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

import jm14.ant.model.ArtigosHome;
import jm14.ant.model.ServiceLocator;

public class ListaArtigosServlet extends HttpServlet {
	
	private ArtigosHome artigosHome = null;
	private static final ServiceLocator serviceLocator = ServiceLocator.getInstance();

	public void init() throws ServletException {		
		artigosHome = serviceLocator.getArtigosHome();
	}
	
	protected void doPost(
		HttpServletRequest request,
		HttpServletResponse response)
		throws ServletException, IOException {
			
			
			String edicao = request.getParameter("edicao");
			List artigos = null;
			try {
				artigos = this.artigosHome.listaArtigosPorEdicao( Integer.parseInt(edicao) );
			} catch( Exception exc ) {
				System.out.println("excecao para edicao " + edicao + ": " + exc.getMessage());
				exc.printStackTrace();
				artigos = new ArrayList();
			}
			System.out.println("size: " + artigos.size() + " home: " + this.artigosHome );
			request.setAttribute( "artigos", artigos );
			
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/view/listaArtigos.jsp");
			dispatcher.forward(request,response);


	}
}
