package personalServlet;

import java.io.IOException;

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

import m05bBD.m05bPersonal;
import m05bClasses.m05bConstantes;
import m05bClasses.m05bFileLog;
import m05bPersonalEJB.m05Personal;
import m05bPersonalEJB.m05PersonalHome;
import m05bPersonalEJB.m05PersonalPK;

/**
 * Inserte aquí la descripción del tipo.
 * Fecha de creación: (04/03/04)
 */
public class m05bGenerarPersonalServlet 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

	{
		// Insert user code from here.
	/*	javax.sql.DataSource ds = null;
		java.sql.Connection conn;*/

		String codigo, nombre, orden, codigo1,nombre1,orden1;
		//String nombre = "";
		String sURL = "", msg;

		m05bFileLog miLog = new m05bFileLog();
		HttpSession session = request.getSession(true);
		String persona = (String) session.getAttribute("persona");
		
		codigo = new String(request.getParameter("Codigo"));
		
		
		codigo1 = request.getParameter("Codigo1")==null?"":new String(request.getParameter("Codigo1"));		
		nombre1 = request.getParameter("Nombre1")==null?"":new String(request.getParameter("Nombre1"));		
		orden1 = request.getParameter("Orden1")==null?"":new String(request.getParameter("Orden1"));		

		try{
			m05PersonalPK primaryKey = new m05PersonalPK();
			primaryKey.per_dni = codigo;
			InitialContext ctx2 = new InitialContext();
			m05PersonalHome personalEBHome = (m05PersonalHome) ctx2.lookup("m05Personal");
			m05Personal personalEB = personalEBHome.findByPrimaryKey(primaryKey);
			m05bPersonal personal = personalEB.getPersonal();

			session.setAttribute("m05bPersonal", personal);
	    	         sURL = response.encodeRedirectURL("m05bPersonalJsp.jsp?Codigo=" + codigo +"&Codigo1="+codigo1+"&Nombre1="+nombre1+"&Orden1="+orden1+"&Mensaje=false" );
	    	 		miLog.addTraza(persona, "Generar Personal Servlet", "sURL: " + sURL, m05bConstantes.LOG_NIVEL_BAJO);	    	         
			
		    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 {
			// uncomment the following line when unexpected exceptions
			// are occuring to aid in debugging the problem.
			//miLog.addTraza(theException);
			response.sendRedirect("m05bError.jsp");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			miLog.addTraza(e);
		}
	}
}
}
