package br.com.senacagil.springmvc.commands;

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 br.com.senacagil.springmvc.business.Professor;
import br.com.senacagil.springmvc.dao.FacadeDAO;
import br.com.senacagil.springmvc.dao.ProfessorDAO;

public class ProfessorAlterar extends AbstractCommand {

	ProfessorDAO professorDao = null;

	public void processarComando(HttpServletRequest request,
			HttpServletResponse response, HttpServlet servlet) {
		String id = request.getParameter("id");
		String nome = request.getParameter("nome");
		String matricula = request.getParameter("matricula");
		String titulacao = request.getParameter("titulacao");
		
		professorDao = FacadeDAO.getInstance(servlet).getProfessorDAO();

		Professor p1 = new Professor();
		p1.setNome(nome);
		p1.setMatricula(matricula);
		p1.setTitulacao(titulacao);
		p1.setId(Integer.parseInt(id));

		String msg = professorDao.atualizaProfessor(p1);

		List<Professor> professores = new ArrayList<Professor>();
		professores.add(p1);
		request.setAttribute("professores", professores);
		RequestDispatcher rd;
		if (msg != null)
			rd = request.getRequestDispatcher("professorAlterar.jsp?mensagem="
					+ msg);
		else
			rd = request
					.getRequestDispatcher("professorAlterar.jsp?mensagem=Professor Alterado com Sucesso!");
		try {
			rd.forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
