<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ page import="java.util.*"%>
<%@ page import="com.eg.*"%>
<%@ page import=" java.io.*"%>

<%@ page import = "com.egurkha.util.DateUtil" %>
<%@ page import = "egurkha.reporter.*" %>
<%@ page import = "com.egurkha.ini.*" %>
<%@ page import = "com.egurkha.*"%>
<%@ page import = "com.eg.IniFile" %>
<%@ page import = "java.util.regex.*"%>


<%@ taglib prefix = "egui" uri = "../WEB-INF/eg-taglib.tld"%>
<html>
<head>
<style>

@media print {body {-webkit-print-color-adjust: exact;}}

@font-face{font-family:'LucidaGrandeRegular';src:url('../../fonts/6216/6216.woff') format('woff')}
@font-face{font-family:'LucidaGrandeBold';src:url('../../fonts/6215/6215.woff') format('woff')}

body{font-size:11px;line-height:1.5em;color:#2a292a;}
header{font-size:15px;}
.formLabel {font-size:12px;}
.formValue {font-weight:500;font-size:12px;}
b{font-weight:500;font-size:11px;}
table tr td{font-size:11px;padding:4px;}

.printTable{border: 1px solid #c3c3c3; border-collapse: collapse;}
.printTable tr td{border: 1px solid #c3c3c3;}
.graphHeading{padding:7px;width:100%;height:20px;}
.printTableHeader{font-weight:500;border: 1px solid #c3c3c3;text-align:center;background-color:#cdcdcd;padding:4px;}
.printTableHeader2{font-weight:500;border: 1px solid #c3c3c3;text-align:left;background-color:#cdcdcd;padding:4px;}

body{font-family:Verdana,Tahoma,Calibri,Arial,Helvetica,sans-serif;font-size:8pt;}

.eg_darkbutton {font-family:Verdana,Arial; font-size: 10px; color: #FFFFFF; font-weight: bold; background-color: #777777; text-align: center;}

.eg_medium{background-color:#a7a7a7}

.eg_lightlight{background-color:#f2f2f2}

.table_border{border-left-width:1px;border-right-width:1px;border-top-width:1px;border-bottom-width:1px;border-color:#555555;border-style:solid;}

.eg_8bold{font-family:Verdana,Tahoma,Calibri,Arial,Helvetica,sans-serif; font-size:8.5pt; font-weight:bold;color: #000000;}

.table_header{padding:1px;margin-left:1px; text-align:center;background-color:#d1d1d1; border-right: 1px solid #777777;border-bottom: 1px solid #777777; border-left:1px solid #f2f2f2; border-top:1px solid #f2f2f2;font-family:Verdana,Tahoma,Calibri,Arial,Helvetica,sans-serif;;font-size:8pt;font-weight:bold;color:#000000}
.cell_border1{border-left-width:0px; border-right-width:1px;border-top-width:0px;border-bottom-width:1px;border-color:#d1d1d1;border-style:solid;background-color:#e7e7e7;}

.cell_border2{text-align: center;}
.iconExclamation {background: url('/final/images/Light/eG_Icons32.png') no-repeat ;}
.iconExclamation{background-position:0 -200px !important; width:32px !important; height:32px !important;}
.commonErrorMsgText{font-size:13px !important;color:#000;text-align:center;
font-family:Calibri,Candara,"Segoe UI",Arial,sans-serif;}

</style>

<%
	response.setHeader("Cache-Control","no-store");
	response.setHeader("Pragma","no-cache");
	response.setDateHeader("Expires", -1);
	
	ArrayList list=new ArrayList();
	ArrayList zoneList=new ArrayList();
	String peakUsgDetails = "";
	IniUI iniUI = IniUI.getInstance();
	String mgrMiscPath = EgInstallInfo.getInstallDir() + "/manager/config/eg_mgrmisc.ini";
	IniFile mgrMiscObj = new IniFile(mgrMiscPath);
	String zoneUsageValue = mgrMiscObj.getValue("EgCert_Usage","ZoneUsageMail");
	String licenseUsageValue = mgrMiscObj.getValue("EgCert_Usage","CertUsageMail");
	String DetailTypeValue = mgrMiscObj.getValue("EgCert_Usage","DetailType");
	EgCertUsage certUsage = new EgCertUsage();
	EgDateInfo egDate =new EgDateInfo();
	String[] zoneName = certUsage.getAllZoneNames();
	int zoneLen = zoneName.length;
	String uiFile = "eg_ui.ini";
	String s_iniFile_dir = EgInstallInfo.getInstallDir()+ "/manager/config/";
	IniFile egUI_01 = new IniFile(s_iniFile_dir + uiFile);
	String currentDateFormat = egUI_01.getValue("CURRENT_DATE_FORMAT","selectedDateFormat");
	
	EgUsersInfo usersInfo = new EgUsersInfo();
	boolean usrWiseCertEnabled = usersInfo.userWiseUsageFlag;
	boolean zonWiseCertEnabled = usersInfo.zoneWiseUsageFlag;
	UserRegistry reg = UserRegistry.getInstance();
	User user = reg.getUser(session.getId());

	boolean isZoneUsageEnabled = (zoneUsageValue != null && (zoneUsageValue.equalsIgnoreCase("yes") || zoneUsageValue.equalsIgnoreCase("true")));
	boolean isConcUserBased = EgInstallInfo.isconcurrentUserLicense;
	boolean isNamedUserBased = EgInstallInfo.isnamedUserLicense;
	String userBasedValue = "";
	if (isConcUserBased)
	{
		userBasedValue = "Concurrent";
	}
	else if (isNamedUserBased)
	{
		userBasedValue = "Named";
	}

	String timezoneOffsetValue ="";
	EgDateInfo tracker_tdateInfo = new EgDateInfo();
	TimeZone tracker_tz = null;
	Calendar cal = Calendar.getInstance();
	java.util.Date local_date = new java.util.Date(tracker_tdateInfo.egetUTCDate());
	cal.setTime(local_date);
	cal.setTimeZone(tracker_tdateInfo.getRealTimeZone());
	
	int currYear = (cal.get(cal.YEAR));
	int prevMonth = (cal.get(cal.MONTH));
	int end_date = 0;
	String previousMonth = cal.getDisplayName(cal.MONTH, cal.LONG, Locale.getDefault());
	if(prevMonth == 2)
	{
		end_date = 29;
	}
	else
	{
		end_date = 31;
	}
	String temp1 = (cal.get(cal.MONTH)) + "/" + "01" + "/" +cal.get(cal.YEAR);
	String trac_currentDateFormat = iniUI.getValue("CURRENT_DATE_FORMAT","selectedDateFormat");
	String startDate = DateUtil.changeDateFormat(temp1,"MM/dd/yyyy",trac_currentDateFormat);

	String temp2 = (cal.get(cal.MONTH)) + "/" + end_date + "/" +cal.get(cal.YEAR);
	String endDate = DateUtil.changeDateFormat(temp2,"MM/dd/yyyy",trac_currentDateFormat);

	cal.add(Calendar.MONTH ,-1);
	String currMonth = cal.getDisplayName(cal.MONTH, cal.LONG, Locale.getDefault());
	String pattern = "(^[\"]?\\d+[\"]?$)|(^[\"]?\\d+(\\.\\d*)[\"]?$)"; 
	String currentdate = "";
	
	String da=egDate.egetDate();
	int index1=da.indexOf(" ");
	if(index1>0)
	{
		currentdate = da.substring(0,index1);
		currentdate = DateUtil.changeDateFormat(currentdate,"dd/MM/yyyy",trac_currentDateFormat);
	}
	String report = "Report generated on "+currentdate ;
	String licValue =""; 
	File file=null;
	com.eg.EgDecryptLicense license=new com.eg.EgDecryptLicense();
	com.eg.EgCrypt enlicense=new com.eg.EgCrypt();
	EgTestInfo tokInfo = new EgTestInfo("manager");
	ArrayList arrlist=new ArrayList();
	arrlist = readFile(EgInstallInfo.getInstallDir()+"/manager/logs/license.txt");
	if(arrlist != null && arrlist.size() > 0)
	{
		for(int i =0; i<arrlist.size(); i++)
		{
			licValue += (String)arrlist.get(i);
		}
	}
	StringTokenizer line = new StringTokenizer(licValue, "\n");
	while(line.hasMoreTokens())
	{
		String st=((String)line.nextToken()).trim();
		if(st.equals(""))
		{
			out.println("Remove extra empty space");  
			return;
		}

		String output=license.decrypt(st);
		if (output.startsWith("peakUsgMsg#"))
		{
			peakUsgDetails = output;
		}
		else if(output.startsWith("<tr>"))
		{
			zoneList.add(output);
		}
		else
		{
			list.add(output);
		}
	}
	if(list == null || list.size() == 0)
	{
		%>
			<table width="600" align="center" height="600">
					<tr>
						<td width="100%" height="24">
							<table width="72%" border="0" cellspacing="1" cellpadding="0" align="center">
								<tr>
									<td align="center" valign="top"><div class="iconExclamation"></div></td>
								</tr>
								<tr>
									<td align="center" valign="bottom" nowrap class="commonErrorMsgText"><b>No data found.</b></td>
								</tr>
							</table>
						</td>
					</tr>
				</table>
		<%
		return;
	}

%>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<!-- <LINK href="/final/monitor/eg_styles/monstyle_gray.css" type="text/css" rel="stylesheet"> -->
</head>

<body  style="padding:10px;background-color:#ffffff">
<form action='EgLicensePdf.jsp' style="margin:0px">

<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
	<tr>
		<td>
			<table cellpadding="0" cellspacing="0" border="0" width="100%" align="center">
				<tr>
					<td style="padding:5px 10px 10px 10px"><img src='<%=iniUI.getMonitorHomeLogo()%>' border="0" /></td>
				</tr>
			</table>
			<%
			//if(licenseUsageValue.equalsIgnoreCase("yes"))
			//{
			%>
			<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center"  style="border-bottom:0px solid #999;">
				<tr>
					<td style="padding:2px" align="center">
						<div style="float:center"><header><egui:intnl text="LICENSE USAGE REPORT"/></header></div><br/>
						<div style="float:center"><header><%=report%></header></div><br/>
					</td>
				</tr>
			</table>
			<%
			//}
			%>
		</td>
	</tr>
</table>
	<table align="center" border="0" width="100%" class="printTable" cellpadding="0" cellspacing="0"><col width="50%"><col width="50%">
	<tr>
		<td align="center"  class="eg_8bold table_header" colspan="2"><egui:intnl text="eG Manager License Information"/></td>
	</tr>
	<%
			String data="";
			int locmark=0;
			for(int i=0;i<=30;i++)
			{
				data=((String)list.get(i)).trim();
				if(data.indexOf(":")!=-1 && !data.startsWith("<tr>"))
				{
					int index = data.indexOf(":");
					if(index > 0)
					{
						String info=(data.substring(0,index)).trim();
						String value=data.substring(index+1);
						if(info.equals("Report generated on"))
						{
							continue;
						}
					else
						{

%>
						<tr>
							<td width="50%" class="cell_border1"><%=info%></td>
							<td width="50%" class="cell_border1"><%=value%></td>
						</tr>
<% 
						}
					continue;
					}
				}
				if(data.startsWith("-----"))
				{
					locmark=i;
					break;
				}
			}
		
	%>
</table>
<br/>
<table align="center" border="0" width="100%" class="printTable" cellpadding="0" cellspacing="0">

			<tr>
				<td align="center"  class="eg_8bold table_header" colspan="8"><egui:intnl text="eG Manager License Usage"/></td>
			</tr>
<%
			data=((String)list.get(locmark+1)).trim(); 
			StringTokenizer st_first = new StringTokenizer(data, " ");
			StringTokenizer st = new StringTokenizer(data, " ");

			if(st_first.countTokens()>5)
			{
%>
				<tr>
<% 
				while (st.hasMoreTokens())
				{
					String key1 = ((String)st.nextToken()).trim();
					if(key1.equals("Not"))
					{
						String key2 = st.nextToken();
%>
						<td  class="table_header">
						<%=key1+" "+key2%>
						</td>
<%
					}
					else
					{
%>
						<td class="table_header">
						<%=key1 %>
						</td>
<%
					}
				}
%>	
				</tr>
				
<%				
				for(int i=locmark+3;i<list.size();i++)
				{
					data=((String)list.get(i)).trim();
					
				
					if(!data.startsWith("------"))
					{
						ArrayList emptyColumnsList = tokInfo.egTokenizer(data, "  ");
						%>
						<tr>
						<%
						for(int j=0;j<emptyColumnsList.size();j++)
						{
							if(((String)emptyColumnsList.get(j)).equals(""))
								continue;
							if(((String)emptyColumnsList.get(j)).indexOf("Agentless")!=-1 ||((String)emptyColumnsList.get(j)).indexOf("Agents")!=-1)
							{
%>
							<td class="cell_border1 cell_border2"><%=emptyColumnsList.get(j)%></td>
<%			
								continue;
							}
							String egUI=EgInstallInfo.getInstallDir() + "/manager/config/eg_ui.ini";
							IniFile iniFile = new IniFile(egUI);
							String subject="";
							Vector compList=new Vector();
							compList.add("Citrix Users");
							compList.add("Terminal Users");
							compList.add("VDI Users");
							String temp=(String)emptyColumnsList.get(j);
							if(compList!=null && compList.contains(temp))
							{
%>
								<td class="cell_border1 cell_border2" style="padding: 4px 0px 4px 40px;" valign="top"><p1><egui:intnl type = "<%=temp%>" /> </p1></td>
<%
							}
							else
							{
								String val1 = (String)emptyColumnsList.get(j);
								String val ="";
								if(val1!= null&& val1.indexOf("$")>-1)
								{
									val=val1.substring(0,val1.indexOf("$"));
									Pattern r = Pattern.compile(pattern);
									Matcher m = r.matcher(val);
									String strDecimal="";				
									if(val!=null && val.trim().indexOf(".")>-1)
									{
										//strDecimal = val.substring(val.indexOf("."));
										//val = val.substring(0,val.indexOf("."));
										%>
										 <td align='left' class='cell_border1 cell_border2'><%=val%><%=strDecimal%></td>
										 <%
									}
									else if(!val.trim().equalsIgnoreCase("-"))
									{
										String dec = ".0";
										%>
										<td align='left' class='cell_border1 cell_border2'><%=val%><%=dec%></td>
										<%
									}
									else
									{
										%>
										<td align='left' class='cell_border1 cell_border2'><%=val%></td>
										<%
									}
								}
								else
								{
%>
								<td align="left" class="cell_border1 cell_border2"><p1><%=emptyColumnsList.get(j)%></p1></td>
<%
								}
							}
						}
%>
				</tr>
<%
				}
			}
		}
			
			else if(st_first.countTokens()==4)
			{
 %>
				<tr>
					<td  width="12%" class="eg_8bold table_header"> Details </td>
					<td  width="12%" class="eg_8bold table_header"> Allowed </td>
					<td  width="12%" class="eg_8bold table_header"> Used </td>
					<td  width="12%" class="eg_8bold table_header"> Usage(%) </td>
				</tr>
<% 
				for(int i=locmark+3;i<list.size()-1;i++)
				{
 					data=((String)list.get(i)).trim();   
 					if(!data.startsWith("------"))
					{
 						//String[] third_tokens=data.split("\\s+");
						ArrayList emptyColumnsList = tokInfo.egTokenizer(data, "\t");
						for(int j=0;j<emptyColumnsList.size();j++)
						{
							if(((String)emptyColumnsList.get(j)).equals(""))
								continue;
%>
							<td width="12%"class="cell_border1 cell_border2"><p1><%=emptyColumnsList.get(j)%></p1></td>
<%
						}
%>
				</tr>
<%
					}
				}
			}
%>
			</table>
			
<%
		int zoneHeight = 30;
		if (usersInfo.isUserBasedLicense())
		{
			String userBasedStartDate = DateUtil.changeDateFormat(startDate,currentDateFormat,"dd-MM-yyyy");
			String userBasedEndDate = DateUtil.changeDateFormat(endDate,currentDateFormat,"dd-MM-yyyy");
			Map aListData3 = usersInfo.getTotalUser("Total",userBasedStartDate,userBasedEndDate);
			aListData3 = new HashMap();
			if (aListData3 != null && aListData3.size()>0)
			{
%>
			<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center"><tr><td height="30px"></td></tr></table><br/>
				<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center"  style="border-bottom:0px solid #999;">
				<tr valign="top">
<%
					zoneHeight = 300;
%>
					<td align="center">
						<h3  style="line-height:1.45px; padding:2px; margin:2;" >eG Manager <%=userBasedValue%> Users report for the month of <%=currMonth%>, <%=currYear%></h3>
						<div id="namedUserGraph" style="height:297px; width:970px;"><img src="/final/admin/EgUserLicenseInfoPdfGraph.jsp?er_timeline=Any&datePickFrom=<%=java.net.URLEncoder.encode(startDate)%>&datePickTo=<%=java.net.URLEncoder.encode(endDate)%>&fwdPage=licensePdf"></div>
					</td>
				</tr>
				</table>
<%
			}
		}
%>

	<%
	if(isZoneUsageEnabled)
	{
		if (zoneLen == 0)
		{
			zoneHeight = 30;
		}
		if(zoneLen>0)
		{
	%>
		<br/>
			<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
			<tr>
				<td align="center">
					<h3  style="line-height:1.45px; padding:2px; margin:2;" >License Usage for Zones</h3>
				</td>
				</tr>
			</table>
							<table align="center" border="0" width="100%" class="printTable" cellpadding="0" cellspacing="0">
							<col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%">
							<col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%"><col width="4.3%">
							<tr>
								<%
									String data1="";
									for(int i=0;i<=(zoneLen+2);i++)
									{
										data1 =((String)zoneList.get(i)).trim();
										if (data1.startsWith("<tr>") && data1.indexOf("-----") == -1)
										{
								%>
											<%=data1%>
								<% 
										}
									}
								%>
							</tr>
							<table align="center" border="0" width="100%" class="printTable" cellpadding="0" cellspacing="0">
								<tr> 
									<td align='left'>Note:'%' are based on total allowed monitors</td>
								</tr>
							</table>
							</table>
						</td>
					</tr>
				</table>
	<%
		}
	}
	%>
</body>
</html>

<%!
private ArrayList readFile(String filename)
{
  ArrayList records = new ArrayList();
  try
  {
	BufferedReader reader = new BufferedReader(new FileReader(filename));
	String line;
	while ((line = reader.readLine()) != null)
	{
	  records.add(line+"\n");
	}
	reader.close();
	return records;
  }
  catch (Exception e)
  {
	System.err.format("Exception occurred trying to read '%s'.", filename);
	e.printStackTrace();
	return null;
  }
}
%>
