package restriccionServlet;

import java.io.IOException;

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

import m05bBD.m05bRestriccion;
import m05bClasses.m05bConstantes;
import m05bClasses.m05bFileLog;
import m05bRestriccionEJB.m05Restriccion;
import m05bRestriccionEJB.m05RestriccionHome;
import m05bRestriccionEJB.m05RestriccionPK;

/**
 * Inserte aquí la descripción del tipo.
 * Fecha de creación: (03/03/06)
 */
public class m05bGenerarRestriccionServlet 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 dni, descripcion = "";
		String sURL = "", msg;
		
		m05bFileLog miLog = new m05bFileLog();
		HttpSession session = request.getSession(true);
		String persona = (String) session.getAttribute("persona");
		miLog.addTraza(persona, "Generar restriccion", "INICIO" , m05bConstantes.LOG_NIVEL_BAJO);
		dni = new String(request.getParameter("Codigo"));
		miLog.addTraza("", "", "DNI: " + dni, m05bConstantes.LOG_NIVEL_BAJO);

		try{
			m05RestriccionPK primaryKey = new m05RestriccionPK();
			primaryKey.res_dni = dni;
			InitialContext ctx2 = new InitialContext();
			m05RestriccionHome restriccionEBHome = (m05RestriccionHome) ctx2.lookup("m05Restriccion");
			m05Restriccion restriccionEB = restriccionEBHome.findByPrimaryKey(primaryKey);
			m05bRestriccion restriccion = restriccionEB.getRestriccion();
			session.setAttribute("m05bRestriccion", restriccion);
	    	sURL = response.encodeRedirectURL("m05bRestriccionesJsp.jsp?Dni=" + dni + "&Mensaje=false");

		    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);
		}
	}
}
}