<%@ page import = "java.sql.*" %>
<%@ page import = "java.util.*" %>
<%@ page import ="com.eg.EgDbServlet"%>
<%@ page import = "com.eg.EgUtils"%>
<%@ page import="com.eg.EgConMonHelper"%>
<%@ page import ="egurkhaConnectionPooling.ConnectionPool"%>
<%@ page import ="egObjectPooling.*"%>
<%@ page import ="com.egurkha.util.*"%>

<%!
	//Null or Empty Checking method
	public boolean isNullOrEmpty(Object object)
	{
		try
		{
			if(object == null)
			{
				return true;
			}
			else if(object instanceof java.lang.String)
			{
				String string = (String) object;
				if (string.equals("") || string.equalsIgnoreCase("null"))
				{
					return true;
				}
				return false;
			}
			else if(object instanceof java.lang.StringBuffer)
			{
				String stringBuffer = (String)object.toString();
				if (stringBuffer.equals("") || stringBuffer.equals("null"))
				{
					return true;
				}
				return false;
			}
			else if(object instanceof java.util.StringTokenizer)
			{
				StringTokenizer st = (StringTokenizer) object;
				if (!st.hasMoreTokens())
				{
					return true;
				}
				return false;
			}
			else if(object instanceof java.util.List)
			{
				List list =(List)object;
				if (list.isEmpty())
				{
					return true;
				}
				return false;
			}
			else if(object instanceof java.util.Map)
			{
				Map map =(Map)object;
				if (map.isEmpty())
				{
					return true;
				}
				return false;
			}
			else if(object instanceof java.util.Set)
			{
				Set set =(Set)object;
				if (set.isEmpty())
				{
					return true;
				}
				return false;
			}
			else
			{
				return false;
			}
		}
		catch (Exception e)
		{
			System.out.println("Execption in the method isNullOrEmpty @ /final/admin/EgTrackerA.jsp :"+e);
		}
		return false;
	}

	public boolean isNull(Object object)
	{
		if(object == null)
		{
			return true;
		}
		return false;
	}
	String manager_label = com.eg.EgInstallInfo.isSuperManager ? "SuperManager" : "Manager";
%>

<html>
<head>
<title>eG Manager Database Connection Details</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="refresh" content="15;">
<link rel="stylesheet" href="eg_styles/eg_adminstyle.css" />
</head>
<body bgcolor="#FFFFFF" topmargin="0" leftmargin="0" marginheight="0" marginwidth="0" >
<%
	String det = request.getParameter("details");
	String trace = request.getParameter("trace");
	String reqFrom = "";
	if (trace != null && trace.equals("true"))
	{
		reqFrom = "ConTrace";
	}
	else if (det != null && det.equals("true"))
	{
		reqFrom = "ConDetails";
	}
	else
	{
		reqFrom = "ConGlance";
	}
	if(reqFrom.equalsIgnoreCase("ConTrace"))
	{
%>
		<br><br>
		<table align="center" width="80%" border="0">
		<th id="subhead" align="left"><b>eG <%=manager_label%> Database Connection Details</b></th>
		<th id="subhead" align="right"><b><%=new java.util.Date()%></b></th>
		</table>
		<br>
<%

	}
	else
	{
%>
	<br><br>
	<table align="center" width="80%" border="0">
		<th id="subhead" align="left"><b>eG <%=manager_label%> Database Connection Details</b></th>
		<th id="subhead" align="right"><b><%=new java.util.Date()%></b></th>
	</table>
	<br>
	<table align="center" width="80%" border="01">
		<tr>
			<td align="left" id="subhead"><b> Database connections waiting to be closed</b></td>
			<td width="20%" class="errortext" nowrap><b><%=com.eg.CleanupThread.threadCount%></b></td>
		</tr>
	</table>
	<br><br>

<%
	}
	String sessionInUse = request.getParameter("sessionInUse");
	if (sessionInUse != null && sessionInUse.equalsIgnoreCase("true"))
	{
		session.setAttribute("authenticated","true");
	}

	String comeFrom = request.getParameter("comeFrom");
	if(!StringUtils.isNull(comeFrom) && comeFrom.equals("ConnTrace"))
	{
		session.setAttribute("treeViewSrc","ConTrace");
	}
	else if (!StringUtils.isNull(comeFrom) && comeFrom.equals("ConnMon"))
	{
		session.setAttribute("treeViewSrc","ConGlance");
	}
	else if(!StringUtils.isNull(comeFrom) && comeFrom.equals("ConnDet"))
	{
		session.setAttribute("treeViewSrc","ConDetails");
	}

	String authenticated = (String)session.getAttribute("authenticated");
	String pwd = request.getParameter("pwd");
	if (authenticated == null)
	{
		// Direct access from the browser address bar or from inside the toolkit via the tool
		if (pwd == null)
		{
			response.sendRedirect(request.getContextPath()+"/toolkitlogin?comeFrom=ConGlance");
		}
		else
		{
			// Direct access from the browser address bar
			if (!pwd.equals(EgConMonHelper.executePwd))
			{
				response.sendRedirect(request.getContextPath()+"/toolkitlogin?comeFrom=ConGlance");
			}
			else
			{
				session.setAttribute("reqFromForTk",reqFrom);
				String redirectURL = request.getContextPath()+"/toolkit";
				response.sendRedirect(redirectURL);
			}
		}
	}
	else
	{ 
		// Direct access from the browser address bar and session in use
		if (sessionInUse == null)
		{
			response.sendRedirect(request.getContextPath()+"/toolkit");
		}
	}

	session.setAttribute("reqFromForTk",reqFrom);

	//Taking the Connection Pool Objects
	ArrayList listForSorting = new ArrayList();
	//ArrayList classLevelSorting = new ArrayList();
	boolean details = (!isNull(det) && det.equalsIgnoreCase("true"));
	if (!isNull(EgDbServlet.poolMgr))
	{
		Hashtable connectionPoolDetails = EgDbServlet.poolMgr.getDetails();
		if(!isNullOrEmpty(connectionPoolDetails))
		{
			Enumeration connectionPoolKeys = connectionPoolDetails.keys();
			while(connectionPoolKeys.hasMoreElements())
			{
				String poolType = "Connection Pool";
				String poolName = (String)connectionPoolKeys.nextElement();
				ConnectionPool connectionPool = (ConnectionPool)connectionPoolDetails.get(poolName);
				Enumeration connectionsGot = connectionPool.connGet.keys();
				if(reqFrom.equalsIgnoreCase("ConTrace"))
				{
					Enumeration conEnum = connectionPool.usedConnections.keys();
%>
					<table align="center" width="80%" border="01">
						<tr>
							<td align="left" id="subhead"><b>Connections currently in use</b></td>
							<td width="20%" class="errortext" nowrap><b><%=ConnectionPool.usedConnections.size()%></b></td>
						</tr>
					</table>
					<br><br>
<%
					while (conEnum.hasMoreElements())
					{
						Connection key = (Connection) conEnum.nextElement();
						ArrayList conList = (ArrayList) ConnectionPool.usedConnections.get(key);
						String[] stackTraces = EgUtils.getTokens((String) conList.get(1),";");
%>
						<table align="center" width="80%" border="01">
						<tr>
							<td>
<%
						for(int i=0; i<stackTraces.length; i++)
						{
%>
								<%=stackTraces[i]%><br>
<%
						}
%>
							</td>
						</tr>
						</table>
<%
					}
				}
				while (connectionsGot.hasMoreElements())
				{
					String className = (String) connectionsGot.nextElement();
					long connGot = 0;
					long connFreed = 0;
					Long l1 = (Long) connectionPool.connGet.get(className);
					Long l2 = (Long) connectionPool.connFree.get(className);
					if (l1 == null || l2 == null)
					{
						continue;
					}
					connGot = l1.longValue();
					connFreed = l2.longValue();
					/*classLevelSorting.add(className);
					classLevelSorting.add(new Long((connGot-connFreed)));
					classLevelSorting.add(new Long(connGot));
					classLevelSorting.add(new Long(connFreed));*/
				}
				ArrayList poolDetails = connectionPool.getConnectionDetails();
				listForSorting.add(poolType);
				listForSorting.add(poolName);
				for(int i=0;i<poolDetails.size();i++)
				{
					listForSorting.add(poolDetails.get(i));
				}
			}
		}
	}
	//Taking the Object Pool objects
	EgObjectPoolManager objectPoolManager = EgObjectPoolManager.getInstance();
	if (!isNullOrEmpty(objectPoolManager))
	{
		Hashtable objectPoolDetails = objectPoolManager.getDetails();
		if(!isNullOrEmpty(objectPoolDetails))
		{
			Enumeration objectPoolKeys = objectPoolDetails.keys();
			while(objectPoolKeys.hasMoreElements())
			{
				String poolType = "Object Pool";
				String poolName = (String)objectPoolKeys.nextElement();
				EgObjectPool objectPool = (EgObjectPool)objectPoolDetails.get(poolName);
				ArrayList poolDetails = objectPool.getObjectDetails();
				listForSorting.add(poolType);
				listForSorting.add(poolName);
				for(int i=0;i<poolDetails.size();i++)
				{
					listForSorting.add(poolDetails.get(i));
				}
			}
		}
	}
	//Sort the Object Pool Objects\
	//ArrayList sortedClassList = new ArrayList();
	ArrayList sortedPoolList = new ArrayList();

	if(!isNullOrEmpty(listForSorting))
	{
		EgSorter sorter = new EgSorter(listForSorting, 7);
		sortedPoolList = (ArrayList)sorter.sort(6, "long", "desc");
	}

	/*if(!isNullOrEmpty(classLevelSorting))
	{
		EgSorter classLevelSorter = new EgSorter(classLevelSorting, 4);
		sortedClassList = (ArrayList)classLevelSorter.sort(2, "long", "desc");
	}*/

	if(reqFrom.equalsIgnoreCase("ConTrace"))
	{
		sortedPoolList = null;
		//sortedClassList = null;
	}

	if(!isNullOrEmpty(sortedPoolList))
	{
%>
	<!-- Connection Pool and Object Pool Information table -->
	<table align="center" width="80%" border="01">
		<tr>
			<td align ="left" width="20%" class="errortext" id="subhead" nowrap ><b>Pool Type</b></td>
			<td align="left" width="20%" class="errortext" id="subhead" nowrap><b> Pool Name</b></td>
			<td align="left" width="12%"  class="errortext" id="subhead" nowrap><b>Used </b></td>
			<td align ="left" width="12%" class="errortext" nowrap><b>Free </b></td>
<%
		if(details)
		{
%>
			<td align="left" width="12%" class="errortext" id="subhead" nowrap><b>Initial</b></td>
			<td align="left" width="12%" id="subhead" ><b>Maximum </b></td>
			<td align="left" width="12%" class="errortext" id="subhead" nowrap><b>Timeout </b></td>
<%
		}
%>
		</tr>
<%
		for (int i=0;i<sortedPoolList.size();i=i+7)
		{
			try
			{
%>
		<tr>
			<td><%=sortedPoolList.get(i)%></td>
			<td><%=sortedPoolList.get(i+1)%></td>
			<td><%=sortedPoolList.get(i+5)%></td>
			<td><%=sortedPoolList.get(i+6)%></td>
<%
				if(details)
				{
%>
			<td><%=sortedPoolList.get(i+3)%></td>
			<td><%=sortedPoolList.get(i+4)%></td>
			<td><%=sortedPoolList.get(i+2)%></td>
<%
				}
%>
		</tr>
<%
			}
			catch (IndexOutOfBoundsException iob)
			{
				//iob.printStackTrace();
			}
		}
%>
	</table>
	<br><br>
<%
	}
	/*if(!isNullOrEmpty(sortedClassList))
	{
		//Class level Database Connectons
%>
	<table align="center" width="80%" border="01">
		<tr>
			<td width="30%" class="errortext"  id="subhead" nowrap><b>Classes using database connection</b></td>
			<td width="30%" class="errortext" id="subhead" nowrap><b>Connections currently in use</b></td>
<%
		if(details)
		{
%>
			<td width="20%" class="errortext" id="subhead" nowrap><b>Connections Got</b></td>
			<td width="20%" class="errortext" id="subhead" nowrap><b>Connections Freed</b></td>
<%
		}
%>
		</tr>
<%
		for (int i=0;i<sortedClassList.size();i=i+4)
		{
			Long difference = (Long) sortedClassList.get(i+1);
			long connDiff = 0;
			if(difference != null)
			{
				connDiff = difference.longValue();
			}
			if(!details && connDiff <= 0)
			{
				continue;
			}
			if(connDiff>= 0)
			{
%>
		</tr>
			<td width="30%" class="errortext"><%=sortedClassList.get(i)%></td>
			<td width="20%" class="errortext"><%=sortedClassList.get(i+1)%></td>
<%
				if(details)
				{
%>
			<td width="20%" class="errortext"><%=sortedClassList.get(i+2)%></td>
			<td width="20%" class="errortext"><%=sortedClassList.get(i+3)%></td>
<%
				}
%>
		</tr>
<%
			}
			else
			{
%>
		</tr>
			<td width="30%" class="errortext"><%=sortedClassList.get(i)%></td>
			<td width="20%" class="errortext"><%=sortedClassList.get(i+1)%></td>
<%
				if(details)
				{
%>
			<td width="20%" class="errortext"><%=sortedClassList.get(i+2)%></td>
			<td width="20%" class="errortext"><%=sortedClassList.get(i+3)%></td>
<%
				}
			}
		}
	}*/

	//StateManager processing details table
	if(!reqFrom.equalsIgnoreCase("ConTrace"))
	{
%>
	</table>
	<br><br>
	<table align ="center"width="80%" border="0">
		<th id="subhead" align="left"><b>StateManager processing details are:</b></th>
	</table>
	<table align="center" width="80%" border="01">
		<tr>
			<td align="left" width="20%" class="errortext" id="subhead" nowrap><b>StateManager Process Queue</b></td>
			<td><%=com.eg.EgManagerServlet.mgrQueue.size()%></td>
		</tr>
		<tr>
			<td align="left" width="20%" class="errortext" id="subhead" nowrap><b>StateManager Queue History</b></td>
			<td><%=com.eg.EgManagerServlet.processQ%></td>
		</tr>
		<tr>
			<td align="left" width="20%" class="errortext" id="subhead" nowrap><b>StateManager Busy History</b></td>
			<td><%=com.eg.EgManagerServlet.pctBusy%></td>
		</tr>
	</table>
	<br><br>
<%
	}
%>
</body>
</html>