package piezasrepaServlet;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

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

import m05bBD.m05bPiezasRepa;
import m05bClasses.m05bConstantes;
import m05bClasses.m05bFileLog;



public class m05bGenerarPiezasRepaServlet 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.
			
		String sURL = "", msg;
		String codigo, norden, pieza, empleado;
		String Cantidad="", Descripcion="", Precio="", Total="", Empleado="", Codigo2="";
		
		StringBuffer sql = new StringBuffer();
		HttpSession session = request.getSession(true);
		
		javax.sql.DataSource m05bDS = null;
		java.sql.Connection m05bConn = null;
		PreparedStatement m05bPstmt = null;
		ResultSet m05bRS = null;
		javax.sql.DataSource ds = null;
		
		codigo = request.getParameter("Codigo");
		norden = request.getParameter("NOrden");
		pieza  = request.getParameter("Pieza");
		
		sql.append("SELECT ").append(m05bConstantes.COL_23_PRECIO).append(",");
		sql.append(m05bConstantes.COL_23_DESCRIP).append(",").append(m05bConstantes.COL_23_CANTIDAD);
		sql.append(",").append(m05bConstantes.COL_23_TOTAL);
		sql.append(" FROM ").append(m05bConstantes.TAB_PIEZAREP);
		sql.append(" WHERE ").append(m05bConstantes.COL_23_ORDEN);
		sql.append("='").append(norden).append("' AND ").append(m05bConstantes.COL_23_CODIGO);
		sql.append("='").append(codigo).append("' AND ").append(m05bConstantes.COL_23_PIE);
		sql.append("='").append(pieza).append("'");

		try{
			
					InitialContext ctx2 = new InitialContext();
					ds = (javax.sql.DataSource) ctx2.lookup("m05b.m05bDataSource");
					m05bConn = ds.getConnection();
					m05bPstmt = m05bConn.prepareStatement(sql.toString());
					m05bRS = m05bPstmt.executeQuery();
           			if (m05bRS.next() == true){
               				Cantidad=m05bRS.getString(m05bConstantes.COL_23_CANTIDAD)==null?"":m05bRS.getString(m05bConstantes.COL_23_CANTIDAD);
               				Descripcion=m05bRS.getString(m05bConstantes.COL_23_DESCRIP)==null?"":m05bRS.getString(m05bConstantes.COL_23_DESCRIP);
               				Total=m05bRS.getString(m05bConstantes.COL_23_TOTAL)==null?"":m05bRS.getString(m05bConstantes.COL_23_TOTAL);
               				Precio=m05bRS.getString(m05bConstantes.COL_23_PRECIO)==null?"":m05bRS.getString(m05bConstantes.COL_23_PRECIO);
       		        }
      	  } catch (Exception ex1){
      	  		response.sendRedirect("m05bError.jsp");
          } finally {
  		        try{
					m05bRS.close();
					m05bPstmt.close();
					m05bConn.close();
	 			} catch (Exception ex2) {
					throw new Exception(ex2);
				}
	 			
          		m05bPiezasRepa piezasrepa = new m05bPiezasRepa();
          		piezasrepa.setPir_codigo(codigo);
    			piezasrepa.setPir_descrip(Descripcion);
    			piezasrepa.setPir_orden(norden);
    			piezasrepa.setPir_pieza(pieza);
    			piezasrepa.setPir_cantidad(Cantidad);
    			piezasrepa.setPir_preciou(Precio);
    			piezasrepa.setPir_total(Total);

    			session.setAttribute("m05bPiezasRepa", piezasrepa);
			
    			response.sendRedirect("m05bOrdPiezaJsp.jsp?Codigo=" + codigo + "&NOrden="+norden+"&Pieza="+pieza);
		  }  

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