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 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 PDServlet extends org.dspace.app.webui.servlet.DSpaceServlet
{
    private static Logger log = Logger.getLogger(PDServlet.class);

    protected void doDSGet(Context context, HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException,
            SQLException, AuthorizeException
    {
    	
    		String libro_id     = request.getParameter("libro");
    		String categoria_id = request.getParameter("categoria");
    		String termino      = request.getParameter("termino");
    		String ui           = request.getParameter("ui");
    		
    		
    		//***************************************************************//
    		//Dos disenos a la vez
    		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";
    		}
    		//***************************************************************//
    		
    		
    		
    		
    		
    		
    		if ((libro_id == null) && (categoria_id == null)) {
    				request.setAttribute("novedades", "true");
    				JSPManager.showJSP(request, response, "/" + carpeta_ui + "/buscar.jsp");
    		}
    		if (categoria_id != null) {
    			  request.setAttribute("categoria", categoria_id);
    				JSPManager.showJSP(request, response, "/" + carpeta_ui + "/categoria.jsp");
    		}
    		if (libro_id != null) {
    				if (StringUtils.isNumeric(libro_id)) {
	    			  request.setAttribute("libro", libro_id);
	    			  request.setAttribute("termino", termino);
	    				JSPManager.showJSP(request, response, "/" + carpeta_ui + "/libro.jsp");
	    			}
	    			else JSPManager.showJSP(request, response, "/error");
    		}
    }
    
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException
    {
        doGet(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; } 
		}

}
