<%@ page errorPage="EgProbs.jsp"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

<%@ page import="com.eg.*"%>
<%@ page import="com.egurkha.util.*"%>
<%@ page import="com.egurkha.EgTimeZoneConverter"%>
<%@ page import = "com.egurkha.User"%>
<%@ page import = "com.egurkha.UserRegistry"%>
<%@ page import="com.eg.supermanager.EgSmSuperInfo"%>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.net.InetAddress"%>

<%
	String sessionInUse = request.getParameter("sessionInUse");
	String pwd = request.getParameter("pwd");
	String authenticated = (String)session.getAttribute("authenticated");

	if (authenticated == null)
	{
		// Direct access from the browser address bar or from inside the toolkit via the tool
		if (pwd == null)
		{
			if (sessionInUse == null)
			{
				response.sendRedirect(request.getContextPath()+"/toolkitlogin?comeFrom=Usage");
			}
			else
			{
%>
				<script>
					window.parent.location.href ="/final/toolkitlogin?comeFrom=Usage";
				</script>
<%
			}
		}
		else
		{
			// Direct access from the browser address bar
			if (!pwd.equals(EgConMonHelper.executePwd))
			{
				response.sendRedirect(request.getContextPath()+"/toolkitlogin?comeFrom=Usage");
			}
			else
			{
				session.setAttribute("reqFromForTk","Usage");
				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","Usage");
	String comeFrom = request.getParameter("comeFrom");
	if (!StringUtils.isNull(comeFrom) && comeFrom.equals("License"))
	{
		session.setAttribute("treeViewSrc","Usage");
	}
%>

<%!
	public String checkDate(int dateValue)
	{
		String finalDate = "";
		if (dateValue < 10)
		{
			finalDate = "0"+dateValue;
		}
		else
		{
			finalDate = ""+dateValue;
		}
		return finalDate;
	}
%>

<%

	InetAddress inetAddress = InetAddress.getLocalHost();
	String hostAddress = inetAddress.getHostAddress();
	String hostName = inetAddress.getHostName();

	IniFile egUI = new IniFile(EgInstallInfo.getInstallDir()+ "/manager/config/eg_ui.ini");
	String s_iniFileDir = EgInstallInfo.getInstallDir();
	String strCommand = s_iniFileDir + "/bin/viewCert" + " " + s_iniFileDir + "/bin/license";
	EgUtilities eG_Utils = EgUtilities.createInstance();

	ArrayList allEnteries = eG_Utils.getExecOutputLines(strCommand);
	if (allEnteries == null || allEnteries.size() < 2)
	{
		return;
	}
	ArrayList licenseInfoLines = (ArrayList) allEnteries.get(0);
	if (licenseInfoLines == null || licenseInfoLines.size() == 0)
	{
		return;
	}

	int eGMgrsCount = 0;
	int licenseUsed = EgSmSuperInfo.getInstance().getManagedManagersCount();
	int licenseAvailable = 0;

	ArrayList aListLicenseEntries = new ArrayList();
	for (int i = 0; i<licenseInfoLines.size(); i++)
	{
		String line = (String) licenseInfoLines.get(i);
		if(line.length()==0)
		{
			continue;
		}
		aListLicenseEntries.add(line);
	}

	HashMap licenseInfoMap = new HashMap();
	for(int i=0; i<aListLicenseEntries.size(); i++)
	{
		String eleme = (String)aListLicenseEntries.get(i);
		int index = eleme.indexOf(":");
		if(index > 0)
		{
			String info=(eleme.substring(0,index)).trim();
			String value=eleme.substring(index+1);
			if(value.indexOf("Not Applicable") >= 0||(value.trim().startsWith("-")))
			{
				value = "-";
			}
			licenseInfoMap.put(info,value.trim());
		}
	}

	String currentDateTime = (String) licenseInfoMap.get("Expiry Date");
	String destDate = "";
	if (currentDateTime != null && !currentDateTime.equals("No expiry date set"))
	{
		//Convert expiry date to user timezone - Start
		SimpleDateFormat sourceformatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy");
		ParsePosition pos = new ParsePosition(0);
		Date parsedDate = sourceformatter.parse(currentDateTime, pos);

		Calendar calObj = Calendar.getInstance();
		calObj.setTime(parsedDate);
		String s_date = checkDate(calObj.get(Calendar.DATE));
		String s_month = checkDate(calObj.get(Calendar.MONTH)+1);
		String s_hourofday = checkDate(calObj.get(Calendar.HOUR_OF_DAY));
		String s_min = checkDate(calObj.get(Calendar.MINUTE));
		String s_sec = checkDate(calObj.get(Calendar.SECOND));
		String s_year = ""+calObj.get(Calendar.YEAR);

		destDate = s_month+"-"+s_date+"-"+s_year;
		String destTime = s_hourofday+":"+s_min+":"+s_sec;

		String currentDateFormat = egUI.getValue("CURRENT_DATE_FORMAT","selectedDateFormat");
		if(DateUtil.isDate(destDate,"MM-dd-yyyy"))
		{
			destDate = DateUtil.changeDateFormat(destDate,"MM-dd-yyyy",currentDateFormat);
			UserRegistry registry = UserRegistry.getInstance();
			User user = registry.getUser(session.getId());
			EgTimeZoneConverter egtc = new EgTimeZoneConverter(destDate+" "+destTime,user);
			destDate = egtc.getDateTime();
		}
	}
	else
	{
		destDate = currentDateTime;
	}

	Map licenseDetilsMap = new LinkedHashMap();
	licenseDetilsMap.put("Product",licenseInfoMap.get("Product"));
	licenseDetilsMap.put("Version",licenseInfoMap.get("Version"));
	licenseDetilsMap.put("IP Address",licenseInfoMap.get("IP Address"));
	licenseDetilsMap.put("Host ID",licenseInfoMap.get("Host ID"));
	licenseDetilsMap.put("Expiry Date",destDate);
	licenseDetilsMap.put("Mail ID",licenseInfoMap.get("Email Address"));
	licenseDetilsMap.put("Company Name",licenseInfoMap.get("Company Name"));

	String mgrsCount = (String)licenseInfoMap.get("No of eGManagers");
	if(mgrsCount != null && mgrsCount.trim().length() >0)
	{
		eGMgrsCount = Integer.parseInt(mgrsCount);
		licenseDetilsMap.put("No of eG Managers",String.valueOf(eGMgrsCount));
		if(licenseUsed <= eGMgrsCount)
		{
			licenseAvailable = eGMgrsCount - licenseUsed;
			licenseDetilsMap.put("License Currently Used",String.valueOf(licenseUsed));
			licenseDetilsMap.put("License Currently Available",String.valueOf(licenseAvailable));
		}
	}
%>

<html>
<head>
<title>License Info</title>
<META HTTP-EQUIV="expires" CONTENT="0">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="eg_styles/adminstyle_blue.css" />
<script language="javascript" src="/final/admin/eg_scripts/timeout.js"></script>
</head>
<body style="overflow:auto" id="space6">
<table align="center" cellspacing="0" cellpadding="0" width="100%" border="0">
	<tr>
		<td height="20" align="left"  class="eg_cellbglits" colspan="2"> 
			<table align="center" cellspacing="0" cellpadding="4" border="0" >
				<tr>
					<td height="24" nowrap align="right"><img src="/final/admin/eg_images/info2.gif"> </td>
					<td height="24" nowrap>This page enables the user to view the license details.</td>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td background="/final/admin/eg_images/in_titleshadowbg1.gif" height="7" id="eg_blankspace" width="100%" colspan="2">&nbsp;</td>
	</tr>
	<tr>
		<td>
			<table align="center" width="90%" cellspacing="0" cellpadding="0" border="0">
				<tr>
					<td>
						<table cellspacing="0" cellpadding="0" width="100%" border="0" height="22">
							<tr>
								<td id="subhead_left" width="7">
								<img src="/final/admin/eg_images/spacer.gif" border="0" width="7">
								</td>
								<td id="eg_subheadbg" style="border-left-width: 0px; border-right-width: 0px;" align="center" width="100%">
								<strong>License Details for eG SuperManager Installed on&nbsp;<%=hostName%>&nbsp; (with IP Address&nbsp;<%=hostAddress%>)</strong> 
								</td>
								<td id="subhead_right" width="7">
								<img src="/final/admin/eg_images/spacer.gif" border="0" width="7">
								</td>
							</tr>
						</table>
					</td>
				</tr>
				<tr>
				<td align="center" valign="top" class="eg_subcontentbg">
					<table align="center" cellspacing="0" cellpadding="0" width="100%" border="0">
						<col width="3"><col width="140"><col width="10"><col width="25%"><col width="140"><col width="10"><col width="25%"><col width="3">
<%
	ArrayList infoList = new ArrayList(licenseDetilsMap.keySet());
	for(int i=0;i<infoList.size();i=i+2)
	{
		String head_1 = (String)infoList.get(i);
		String head_2 = (String)infoList.get(i+1);
%>
						<tr >
							<td height="6" width="7" id="form_border" > <img src="/final/admin/eg_images/spacer.gif" width="7" height="6" border="0"></td>
							<th valign="top" id="form_border" class="eg_subcontent"><%=head_1%></th>
								<td valign="top" id="form_border" class="eg_subcontent"> : </td>
								<td valign="top" id="form_border" class="eg_subcontent eg_darktextbold"><%=licenseDetilsMap.get(head_1)%></td>
								<th id="form_border" class="eg_subcontent" style="border-left-width:2px;"  valign="top"><%=head_2%></th>
								<td valign="top" id="form_border" class="eg_subcontent"> : </td>
								<td id="form_border" class="eg_subcontent eg_darktextbold" valign="top"><%=licenseDetilsMap.get(head_2)%></td>
								<td height="6" width="7" id="form_border"> <img src="/final/admin/eg_images/spacer.gif" width="7" height="6" border="0"></td>
						</tr>
<%
	}
%>
					</table>
				</td>
				</tr>
			</table>
		</td>
		</tr>
	</table>
</body>
</html>


