package gastosServlet;

import java.io.IOException;

import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;

import m05bBD.m05bGastos;
import m05bClasses.m05bFileLog;
import m05bGastosEJB.m05Gastos;
import m05bGastosEJB.m05GastosHome;
import m05bGastosEJB.m05GastosPK;


public class m05bGenerarGastosServlet extends javax.servlet.http.HttpServlet{
	m05bFileLog miLog = new m05bFileLog();
/**
 * Process incoming HTTP GET requests
 *
 * @param request Object that encapsulates the request to the servlet
 * @param response Object that encapsulates the response from the servlet
 */
public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {

	performTask(request, response);

}
/**
 * Returns the servlet info string.
 */
public String getServletInfo() {

	return super.getServletInfo();

}
/**
 * Initializes the servlet.
 */
public void init() {
	// insert code to initialize the servlet here

}
/**
 * Process incoming requests for information
 *
 * @param request Object that encapsulates the request to the servlet
 * @param response Object that encapsulates the response from the servlet
 */
public void performTask(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) {

	try

	{

		String codigo;
		String matricula;		
		String sURL = "";
		
		HttpSession session = request.getSession(true);
        codigo = new String(request.getParameter("Codigo"));
        matricula = new String(request.getParameter("Matricula"));

        /*String CodAsig = session.getAttribute("GastosAsig");*/


		try{   
			m05GastosPK primaryKey = new m05GastosPK();
			primaryKey.gas_codigo = codigo;
			InitialContext ctx2 = new InitialContext();
			m05GastosHome gastosEBHome = (m05GastosHome) ctx2.lookup("m05Gastos");
			m05Gastos gastosEB = gastosEBHome.findByPrimaryKey(primaryKey);
			m05bGastos gastos = gastosEB.getGastos();

			session.setAttribute("m05bGastos", gastos);

	    	sURL = response.encodeRedirectURL("m05bGastosJsp.jsp?Codigo=" + codigo +  "&Matricula=" + matricula + "&Cabecera=true");


		    RequestDispatcher rd;
		    rd = request.getRequestDispatcher(sURL);

		    try{

		      rd.forward(request, response);
	    	}
		    catch (ServletException e){
		    	response.sendRedirect("m05bError.jsp");
		    }
		} catch (Exception SQLException) {
			miLog.addTraza(SQLException);
			response.sendRedirect("m05bError.jsp");
		}

	}
	catch(Throwable theException)
	{
		try {
			response.sendRedirect("m05bError.jsp");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			miLog.addTraza(e);
		}
	}
}
}