package org.ibai.app.webui.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.commons.lang.StringUtils;
import org.dspace.app.webui.util.JSPManager;
import org.dspace.authorize.AuthorizeException;
import org.dspace.core.Context;
//***************************************************************//
//Dos disenos a la vez
import java.sql.*;
import org.dspace.core.ConfigurationManager;
//***************************************************************//

public class PDSugerenciaCompraServlet extends org.dspace.app.webui.servlet.DSpaceServlet
{
    private static Logger log = Logger.getLogger(PDSugerenciaCompraServlet.class);

    protected void doDSGet(Context context, HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException,
            SQLException, AuthorizeException
    {
    		
    		//***************************************************************//
    		//Dos disenos a la vez
    		
    		String ui = request.getParameter("ui");
    		String carpeta_ui = "prestamo-digital";
    		//Parametro para el diseno en BD
    		Connection conexion = null;
			 	ResultSet resultado = null;
			 	try {
			 			conexion = conecta();				
						PreparedStatement  preparedStatement=conexion.prepareStatement("SELECT VALOR FROM PD_CONFIGURACION WHERE NOMBRE='config_ui'");
						resultado = preparedStatement.executeQuery();
					  if (resultado.next()) {
					  	String ui_bd = resultado.getString("VALOR");
					  	if (ui_bd.equals("responsive")) carpeta_ui = "prestamo-digital-responsive";
					  }
				}
				catch(SQLException e) { }
				finally {
					if (resultado != null) resultado.close();
					if (conexion != null)  conexion.close();
				}
    		
    		//Parametro para el diseno desde parametro url
    		if (ui != null) {
    				if (ui.equals("responsive")) carpeta_ui = "prestamo-digital-responsive";
    		}
    		//***************************************************************//
    		
	  		
	  		String titulo = request.getParameter("title1");
    		if (titulo != null) request.setAttribute("title1", titulo);
    		String autor = request.getParameter("author1");
    		if (autor != null) request.setAttribute("author1", autor);
    		String editorial = request.getParameter("publisher1");
    		if (editorial != null) request.setAttribute("publisher1", editorial);
    		
    		String titulo2 = request.getParameter("title2");
    		if (titulo2 != null) request.setAttribute("title2", titulo2);
    		String autor2 = request.getParameter("author2");
    		if (autor2 != null) request.setAttribute("author2", autor2);
    		String editorial2 = request.getParameter("publisher2");
    		if (editorial2 != null) request.setAttribute("publisher2", editorial2);
    		
    		String titulo3 = request.getParameter("title3");
    		if (titulo3 != null) request.setAttribute("title3", titulo3);
    		String autor3 = request.getParameter("author3");
    		if (autor3 != null) request.setAttribute("author3", autor3);
    		String editorial3 = request.getParameter("publisher3");
    		if (editorial3 != null) request.setAttribute("publisher3", editorial3);
    		
    		String name = request.getParameter("name");
    		if (name != null) request.setAttribute("name", name);
    		String email = request.getParameter("email");
    		if (email != null) request.setAttribute("email", email);
    		
    		//TODO Se pueden mandar hasta 3 libros.
	  		
    		JSPManager.showJSP(request, response, "/" + carpeta_ui + "/sugerencia_compra.jsp");
    }
    
    protected void doDSPost(Context context, HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException,
            SQLException, AuthorizeException
    {
        // Treat as a GET
        doDSGet(context, request, response);
    }
    
    protected static Connection conecta() { 
				try {
					String bd      = ConfigurationManager.getProperty("db.url");
					String usuario = ConfigurationManager.getProperty("db.username");
					String pass    = ConfigurationManager.getProperty("db.password");
					return DriverManager.getConnection(bd, usuario, pass); 
				}
				catch (Exception er) { return null; } 
		}

}
