package poblacionServlet;

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

import javax.servlet.http.HttpSession;

import m05bBD.m05bPoblacion;
import m05bClasses.m05bConexion;
import m05bClasses.m05bConstantes;
import m05bClasses.m05bFileLog;

/**
 * Inserte aquí la descripción del tipo.
 * Fecha de creación: (04/12/03)
 */
public class m05bGrabarPoblacionServlet extends javax.servlet.http.HttpServlet {m05bFileLog miLog = new m05bFileLog();
/**
 * Process incoming HTTP POST 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) {

       	String sCodigo;
       	String sDescripcion;
       	String accion;
       	String sql = "";
		java.sql.Connection conn=null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		boolean finalizado = false;
       try

		{

		HttpSession session = request.getSession(true);


        m05bPoblacion poblacion = (m05bPoblacion)session.getAttribute("m05bPoblacion");
		sCodigo = request.getParameter("Codigo");
		poblacion.setPob_codigo(sCodigo);
		sDescripcion=request.getParameter("Descripcion").toUpperCase();
		poblacion.setPob_descripcion(sDescripcion);
		////System.out.println("Entra en el servlet");
		
		accion=request.getParameter("Accion");

		try{
			m05bConexion miConex = new m05bConexion();
			conn = miConex.getConnection();
			if (accion.equals(m05bConstantes.ACC_ACTUALIZAR))  {
				pstmt = conn.prepareStatement(
					"update	M0524T00 " + 
					"set	DESCRIP_24 = ? " + 
					"where	COD_POB_24 = ?");
				pstmt.setString(1, sDescripcion);
				pstmt.setString(2, sCodigo);
			}
			else if (accion.equals(m05bConstantes.ACC_INSERTAR))  {
			// Chequear si ya existe una poblacion con ese codigo
			   sql = "select 1  from M0524T00 where COD_POB_24 = '" + sCodigo + "'";
		       pstmt = conn.prepareStatement(sql);
			   rs = pstmt.executeQuery();
			   if (rs.next() == true){ 		
       	    		response.sendRedirect("m05bPoblacionesJsp.jsp?Codigo=" + sCodigo + "&Mensaje=Existe");
       	    		finalizado = true;	
			   } 
			   else {
					pstmt = conn.prepareStatement(
						"insert into	M0524T00 " + 
						"(COD_POB_24, DESCRIP_24) " + 
						" values " + 
						"(? ,?)" );
					pstmt.setString(1, sCodigo);
					pstmt.setString(2, sDescripcion);
				}
			}
			else   {
				pstmt = conn.prepareStatement(
					"delete from 	M0524T00 " + 
					" where COD_POB_24 =  ? " );
				pstmt.setString(1, sCodigo);
			}
			if(!finalizado){
				pstmt.executeUpdate();
				response.sendRedirect("m05bRelPoblacionesJsp.jsp");
			}
		} 	catch (Exception SQLException) {
				miLog.addTraza(SQLException);
				response.sendRedirect("m05bError.jsp");
		} 	finally{
				if (rs != null)
					rs.close();
				if (pstmt != null)
					pstmt.close();
				if (conn != null)
					conn.close();
		}
	}
	catch(Throwable theException)
	{
		// uncomment the following line when unexpected exceptions
		// are occuring to aid in debugging the problem.
		miLog.addTraza(theException);
		try {
			response.sendRedirect("m05bError.jsp");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			miLog.addTraza(e);
		}
	}
}
}
