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 PDAdminEstadisticas3BiblioServlet extends org.dspace.app.webui.servlet.DSpaceServlet
{
    private static Logger log = Logger.getLogger(PDAdminEstadisticas3BiblioServlet.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";
    		}
    		//***************************************************************//
    	
    	  HttpSession session = request.getSession(true);
    	  
    	  
    	  String usuario_conectado = (String)session.getAttribute("usuario");
				String usuario_tipo      = (String)session.getAttribute("usuario_tipo");
				if ((usuario_conectado == null) || (usuario_conectado.equals("")) || (usuario_conectado.equals("null")) || (!usuario_tipo.equals("admin"))) {
					response.sendRedirect(response.encodeRedirectURL(request.getContextPath() + "/login"));
				}
				else {
    	  	JSPManager.showJSP(request, response, "/" + carpeta_ui + "/admin/libros_estadisticas_detalle_3_biblio.jsp");
    		}
    }
    
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException
    {
        doGet(request, response);
    }
    
    private static boolean isNullOrBlank(String param) { 
		    return param == null || param.trim().length() == 0;
		}
		
		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; } 
		}

}
