<!DOCTYPE html>

<%
/************************************************************************************************
*	File			: ErApplicationBillingReportPrint.jsp
*	Version			:	6.0.1
*	Author			:	Karthikeyan R
*	Creation Date	:	12-02-2016
*
*************************************************************************************************/
%>


<%@ include file="ErTracker.jsp"%>
<%@ page import="com.eg.EgDbInfo"%>
<%@ page import="com.eg.EgMappingInfo"%>
<%@ page import="com.eg.EgReportInfo"%>
<%@ page import="com.eg.EgTablesInfo"%>
<%@ page import = "com.eg.EgConfigInfo"%>
<%@ page import = "com.egurkha.util.DateUtil" %>
<%@ page import="egurkha.reporter.*"%>
<%@ page import="com.eg.EgDateInfo"%>
<%@ page import="com.eg.* "%>
<%@ page import="java.net.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.util.regex.*"%>
<%@ page import="com.egurkha.User"%>
<%@ page import="com.egurkha.util.DateTimeTokenizer"%>
<%@ page import="com.egurkha.util.DateUtil"%>

<%@ taglib prefix="egui" uri="../WEB-INF/eg-taglib.tld"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="com.egurkha.util.StringUtils"%>

<%@ page import="com.eg.EgReportInfo" %>

<%!
	private static final String BLANK_SPACE = " ";
	private static final String COMA = ",";
	private static final String COLON = ":";
	private static final String PIE = "|";
	private static final String BSLASH = "/";
	private static final String UTF_8 = "UTF-8";
%>
<%

	EgReportInfo reportInfo = new EgReportInfo("manager");
	String pdfPrint = request.getParameter("pdfPrint");
	String titleToDisplay=request.getParameter("er_RmDispName");
	String s_st_date = request.getParameter("startdate");
	String s_en_date = request.getParameter("enddate");
	String s_st_hr = request.getParameter("starthr");
	String s_en_hr = request.getParameter("endhr");
	String strmin = request.getParameter("startmin");
	String enmin = request.getParameter("endmin");
	String rtype =request.getParameter("er_type");
	String er_mode = request.getParameter("er_mode");
	String timefrom2=request.getParameter("timefrom");
	String minfrom2=request.getParameter("minfrom");
	String timeto2=request.getParameter("timeto");
	String minto2=request.getParameter("minto");
	String fontStyleDblSingl = "";
	String s_comptype = request.getParameter("er_compType");
	String er_component[] = request.getParameterValues("er_component");
	String sample = request.getParameter("sample");
	String er_componentType = request.getParameter("er_compType");
	String er_option =request.getParameter("er_option");
	String er_site = request.getParameter("er_site");
	String headerKeyName="";

	if(er_option!=null && er_option.equalsIgnoreCase("Segment"))
	{
		headerKeyName=er_option+" : "+er_site;
	}
	else if(er_option!=null && er_option.equalsIgnoreCase("Service"))
	{
		headerKeyName=er_option+" : "+er_site;
	}
	else if(er_option!=null && er_option.equalsIgnoreCase("Group"))
	{
		headerKeyName=er_option+" : "+er_site;
	}
	else if(er_option!=null && er_option.equalsIgnoreCase("Component"))
	{
		headerKeyName=er_option;
	}
	else
	{
		headerKeyName="Zone"+" : "+request.getParameter("select_farm");
	}

	try 
	{
		String isVmCompSt = request.getParameter("isVmComp");
		boolean isVmComp=false;
		if(isVmCompSt!=null && isVmCompSt.equalsIgnoreCase("true"))
		{
			isVmComp=true;
		}

		//For Time Zone Conversion
		EgTimeZoneConverter timeZoneConverterStart = null;
		EgTimeZoneConverter timeZoneConverterEnd = null;
		String startDateForDisplay = null;
		String startHourForDisplay = null;
		String endDateForDisplay = null;
		String endHourForDisplay = null;
		String startMinForDisplay = null;
		String endMinForDisplay = null;

		User userName = UserRegistry.getInstance().getUser(session.getId());

		String mngrIP= new IniFile(new EgConfigInfo("manager").getDbFile()).getValue("DB_PROPERTIES","ManagerUrl");
		String strManagerURL = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();
		EgDbInfo dbInfo = new EgDbInfo("manager");
		EgMappingInfo intlmap = null;
		if(thisuser != null) 
		{
			intlmap = new EgMappingInfo(thisuser.getLanguage());
		}
		if((dbInfo.doubleByteEnabled || !thisuser.getLanguage().equals("en_US")) && !(pdfPrint != null && pdfPrint.equalsIgnoreCase("yes") ||  (sample != null && !sample.equalsIgnoreCase("yes")))) 
		{
			fontStyleDblSingl = "multi_font";
		} 
		else 
		{
			fontStyleDblSingl = "";
		}
		EgTablesInfo tablesInfo = new EgTablesInfo("manager");
		ErUtils erUtils = new ErUtils();


		List lst = new LinkedList();
		Vector v_measure_csv = new Vector();
		List colList = new ArrayList();
		List lst_dat1 = new LinkedList();
%>

<!Doctype html>
<head>
<%
	if(pdfPrint !=null && pdfPrint.equalsIgnoreCase("yes"))
	{
%>
		<title>&nbsp; <egui:intnl text="reporter.common.Reporter" />
		&nbsp; &nbsp; <egui:intnl text="reporter.common.Print" /> &nbsp;</title>
<%
	}
	else
	{
%>
		<title>&nbsp; <egui:intnl text="reporter.common.Reporter" />
		&nbsp; &nbsp;</title>
<%
	}
%>

<meta name="Author" content="">
<meta  charset="UTF-8">
	<link rel="stylesheet" type="text/css" href="../../final/styles/eGLightThemeIcons.css" />
	<link rel="stylesheet" type="text/css" href="../../final/reporter/styles/printpagestyle.css" />

</head>
<body marginwidth="20" marginheight="20" bgcolor="#FFFFFF">
<%
	String strLogCls = "printHeaderBoxLogo";
	if(pdfPrint !=null && pdfPrint.equalsIgnoreCase("yes"))
	{
		strLogCls = "printHeaderBoxLogo";
	}
	else
	{
		strLogCls = "printPdfHeaderBoxLogo";
	}

	if(pdfPrint !=null && pdfPrint.equalsIgnoreCase("yes"))
	{
%>
		<div class="<%=strLogCls%>" style="padding:10px"><img src="<%=imageName%>" border="0" /></div>
<%
	}
	else
	{
%>
		<div class="<%=strLogCls%>"><img src="<%=imageName%>" border="0" /></div>
<%
	}
%>
	<!--<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center"  style="border-bottom:1px solid #999;">
		<tr>
			<td style="padding:2px">-->

	<div class="printHeaderBox">
<%
	if (!StringUtils.isNull(titleToDisplay))
	{
%>
		<!-- 	<div style="float:left" > <header><%=titleToDisplay%> for &nbsp;<%=headerKeyName%>&nbsp;</header></div>	 -->
			<div class="printHeaderBoxTitle"><%=titleToDisplay%> for &nbsp;<%=headerKeyName%></div>
<%
	}
	else
	{
%>
		<!-- <div style="float:left" > <header><egui:intnl text="reporter.common.Application Billing " />&nbsp;<%=headerKeyName%>&nbsp;</header></div>	   -->
		<div class="printHeaderBoxTitle"><egui:intnl text="reporter.common.Application Billing " />&nbsp;<%=headerKeyName%></div>
<%
	}
	if (pdfPrint != null && pdfPrint.equalsIgnoreCase("yes")) 
	{
%>
		<div class="printHeaderBoxPrintIcon" style="float:left"><img class="iconPrinter mouse"  id="prin tbt" src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" border="0" onClick="javascript:print()" alt='<egui:intnl text="reporter.common.PRINT"/>' title='<egui:intnl text="reporter.common.PRINT"/>'/></div>
<%
	}
%>
	</div>
	
	<div class="printOuterBoxNoBorder">
				<!--</td>
			</tr>
		</table>-->

		<!--<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center" style="border-bottom:1px solid #ccc;">
			<tr>
				<td align="left" valign="top" width="50%">-->
					<!--<div style="float:left;padding:4px;">
						<div style="float:left" class="formLabel"><%= intlmap.getTextDescription("reporter.thin.Component Type") %></div>
						<div style="float:left;width:10px" align="center" >:</div>
						<div style="float:left;" class="formValue"><%= intlmap.getComponentTypeDescription(s_comptype) %></div>
					</div>	-->
					<div class="printSelectionFields"><%= intlmap.getTextDescription("reporter.thin.Component Type") %> - <span class="bold"><%= intlmap.getComponentTypeDescription(s_comptype) %></span></div>

			<!-- 	</td>
				<td align="right" valign="top">	  -->
<%
		String dispStart = "";
		String dispEnd = "";
		boolean needLogonDetails = false;
		//dispStartDate=start_date.trim()+" "+erd.s_sthr+":"+erd.s_startmin;
		dispStart = s_st_date + " " + s_st_hr + COLON + strmin ;
		dispEnd = s_en_date + " " + s_en_hr + COLON + enmin ;
		//Time zone Conversion
		
		timeZoneConverterStart = new EgTimeZoneConverter(dispStart ,userName);
		startDateForDisplay = (String) timeZoneConverterStart.getDate(false);
		startHourForDisplay = timeZoneConverterStart.getHour();
		startMinForDisplay = timeZoneConverterStart.getMinute();

		timeZoneConverterEnd = new EgTimeZoneConverter(dispEnd ,userName);
		endDateForDisplay = (String) timeZoneConverterEnd.getDate(false);
		endHourForDisplay = timeZoneConverterEnd.getHour();
		endMinForDisplay = timeZoneConverterEnd.getMinute();

		String pdfStartDate=startDateForDisplay+" "+startHourForDisplay+":"+startMinForDisplay;
		String pdfEndDate = endDateForDisplay+" "+endHourForDisplay+":"+endMinForDisplay;

		lst.add(intlmap.getTextDescription("reporter.common.Application Billing")+" for "+headerKeyName);
		lst.add(intlmap.getTextDescription("reporter.thin.Component Type")+" : "+intlmap.getComponentTypeDescription(s_comptype));

		String Days = reportInfo.getExcludeWeekend("APPLICATION_BILLING");
		String strLogonDetailsValue= reportInfo.getValue("APPLICATION_BILLING","NeedLogonDetails");
		if(strLogonDetailsValue!=null && (strLogonDetailsValue.equalsIgnoreCase("True") || strLogonDetailsValue.equalsIgnoreCase("Yes")))
			needLogonDetails = true;

		String excludeweekend=request.getParameter("er_excwe");
		if (excludeweekend==null ||	excludeweekend.equals(""))
			excludeweekend="No";

		if(excludeweekend.equalsIgnoreCase("Yes") )
		{
			lst.add('"'+intlmap.getTextDescription("reporter.common.Time period")+":"+intlmap.getTextDescription("reporter.common.From")+" "+request.getParameter("timefrom")+":"+request.getParameter("minfrom")+intlmap.getTextDescription("reporter.common.Hrs")+" "+intlmap.getTextDescription("reporter.common.To")+" "+request.getParameter("timeto")+":"+request.getParameter("minto")+intlmap.getTextDescription("reporter.common.Hrs")+'"'+","+'"'+
			
			intlmap.getTextDescription("reporter.common.Exclude weekends")+":"+Days+'"');
		}
		else
		{
			lst.add(intlmap.getTextDescription("reporter.common.Time period")+":"+intlmap.getTextDescription("reporter.common.From")+" "+request.getParameter("timefrom")+":"+request.getParameter("minfrom")+intlmap.getTextDescription("reporter.common.Hrs")+" "+intlmap.getTextDescription("reporter.common.To")+" "+request.getParameter("timeto")+":"+request.getParameter("minto")+intlmap.getTextDescription("reporter.common.Hrs"));
		}

		Vector dvec = new Vector();
		String stimefmt =	pdfStartDate+ " hrs";
		String etimefmt =	pdfEndDate+ " hrs";

		String ssd="Start Date : "+stimefmt+"  "+"End Date : "+etimefmt;
		lst.add('"'+ssd+'"');

%>

	<div class="printSelectionFields">Timeline - <span class="bold"><%=stimefmt%> to <%=etimefmt%></span></div>

</div>

<div class="printOuterBox">
	<div class="printOuterBoxHeader">Application Billing</div>
<div class="newTable">
<%
	ArrayList resultList = (ArrayList)session.getAttribute("APPLICATION_BILLING_RESULT_MAP");
	String isAddColumnFnd = (String)session.getAttribute("APPLICATION_BILLING_COLUMNFOUND");

	if(resultList!=null && resultList.size()>0)
	{
		String showSessionStartAndEnd = reportInfo.getValue("APPLICATION_BILLING","ShowSessionStartAndEnd");
		boolean isShowSessionStEnd=false;
		if(showSessionStartAndEnd!=null&&showSessionStartAndEnd.equalsIgnoreCase("yes"))
			isShowSessionStEnd=true;

		boolean isAddCmFnd=false;
		if(isAddColumnFnd!=null && isAddColumnFnd=="0")
			isAddCmFnd=true;
	%>

	<table align="center" width="100%" cellspacing="0" cellpadding="0" border="0">
		<thead>
		<tr>
			<th> User</th>
			<th> Published<br>Application</th>
			<th> Component</th>
	<%
		if(isVmComp)
		{
	%>
			<th> Desktop</th>
	<%
		}
	 %>
			<th> Application Launch Time</th>
			<th> Application End Time</th>
			<th> Duration<br>(Mins)</th>
	<%
		if(isShowSessionStEnd)
		{
	 %>
			<th> Session Start</th>
			<th> Session End</th>
	<%
		}
	%>

	<%
		if(isAddCmFnd)
		{
	%>
			<th> Client Address</th>
			<th> Client Name</th>
			<th> Client Version</th>
	<%
		}
		v_measure_csv.add("User");
		v_measure_csv.add("Published Application");
		v_measure_csv.add("Component");
		colList.add("User");
		colList.add("Published Application");
		colList.add("Component");

		if(isVmComp)
		{
			v_measure_csv.add("Desktop");
			colList.add("Desktop");
		}

		v_measure_csv.add("Application Launch Time");
		v_measure_csv.add("Application End Time");
		v_measure_csv.add("Duration (Mins)");

		colList.add("Application Launch Time");
		colList.add("Application End Time");
		colList.add("Duration (Mins)");

		if(isShowSessionStEnd)
		{
			v_measure_csv.add("Session Start");
			v_measure_csv.add("Session End");

			colList.add("Session Start");
			colList.add("Session End");
		}
		if(isAddCmFnd)
		{
			v_measure_csv.add("Client Address");
			v_measure_csv.add("Client Name");
			v_measure_csv.add("Client Version");

			colList.add("Client Address");
			colList.add("Client Name");
			colList.add("Client Version");
		}
		if(needLogonDetails)
		{
			v_measure_csv.add("Logon Duration(Sec)");
			colList.add("Logon Duration(Sec)");
		}
		if(needLogonDetails)
		{
	%>
		<th>Logon Duration<br>(Sec)</th>
	<%
		}

		String resourceinfo = reportInfo.getValue("APPLICATION_BILLING",s_comptype+"_resouce");
		List resourceColumnNames=new ArrayList();
		String testName = reportInfo.getValue("APPLICATION_BILLING",s_comptype+"_app");
		String measureUnit = reportInfo.getValue("APPLICATION_BILLING",s_comptype+"_measure_unit");

		Map measueUnitMap=new HashMap();
		List measueUnitLst=new ArrayList();
		StringTokenizer mToken = new StringTokenizer(measureUnit,",");
		while (mToken.hasMoreTokens())
		{
			String measure = mToken.nextToken();
			String unitmeasure=tablesInfo.getUnitForMeasure(testName.trim(),measure.trim());

			if(unitmeasure==null || unitmeasure.equals(""))
			{
				String str_measureName = dbInfo.getMeasureForColumn(testName,measure);
				unitmeasure = tablesInfo.getUnitForMeasure(testName,str_measureName);
			}

			if(unitmeasure==null || unitmeasure.equals(""))
				unitmeasure = "-";
			measueUnitLst.add(unitmeasure);
		}

		if(resourceinfo!=null && resourceinfo.length()>0)
		{
			StringTokenizer stToken = new StringTokenizer(resourceinfo,":");
			int t=0;
			while (stToken.hasMoreTokens())
			{
				String unit="";
				if(measueUnitLst.size()>t)
					unit=(String)measueUnitLst.get(t);
				String strTemp = stToken.nextToken();
				String measure=strTemp.substring(0,strTemp.indexOf("["));
				String colms=strTemp.substring(strTemp.indexOf("[")+1,strTemp.indexOf("]"));
				StringTokenizer cToken = new StringTokenizer(colms,",");
				int f=1;
				List tmp=new ArrayList();
				while (cToken.hasMoreTokens())
				{
					String cms = "";
					String tok = cToken.nextToken();
					cms = measure+"_"+tok;
					resourceColumnNames.add(cms);
					measueUnitMap.put(cms,unit);
				}
				t++;
			}
		}

		for(int u=0;u<resourceColumnNames.size();u++)
		{
			String str=(String)resourceColumnNames.get(u);
			String unit=(String)measueUnitMap.get(str);
			if(str!=null&& str.equalsIgnoreCase("CPU_UTIL_avg"))
			{
				str="Cpu Util<br>Avg ("+unit+")";
				v_measure_csv.add("Cpu Util-Avg ("+unit+")");
				colList.add("Cpu Util-Avg ("+unit+")");
			%>
					<th><%=str%></th>
			<%
			}
			else if(str!=null&& str.equalsIgnoreCase("CPU_UTIL_min"))
			{

				str="Cpu Util<br>Min ("+unit+")";
				v_measure_csv.add("Cpu Util-Min ("+unit+")");
				colList.add("Cpu Util-Min ("+unit+")");
			%>
					<th><%=str%></th>
			<%
			}
			else if(str!=null&& str.equalsIgnoreCase("CPU_UTIL_max"))
			{

				str="Cpu Util<br>Max ("+unit+")";
				v_measure_csv.add("Cpu Util-Max ("+unit+")");
				colList.add("Cpu Util-Max ("+unit+")");
			%>
					<th><%=str%></th>
			<%
			}

			else if(str!=null&& str.equalsIgnoreCase("MEMORY_UTIL_avg"))
			{
				str="Memory Util<br>Avg ("+unit+")";
				v_measure_csv.add("Memory Util-Avg ("+unit+")");
				colList.add("Memory Util-Avg ("+unit+")");
			%>
				<th><%=str%></th>
			<%
			}
			else if(str!=null&& str.equalsIgnoreCase("MEMORY_UTIL_min"))
			{
				str="Memory Util<br>Min ("+unit+")";
				v_measure_csv.add("Memory Util-Min ("+unit+")");
				colList.add("Memory Util-Min ("+unit+")");
			%>
				<th><%=str%></th>
			<%
			}
			else if(str!=null&& str.equalsIgnoreCase("MEMORY_UTIL_max"))
			{
				str="Memory Util<br>Max ("+unit+")";
				v_measure_csv.add("Memory Util-Max ("+unit+")");
				colList.add("Memory Util-Max ("+unit+")");
			%>
				<th><%=str%></th>
			<%
			}
			else
			{
				str=customReplaceAll(str,"_"," ");
				str=str+" ("+unit+")";
				v_measure_csv.add(str);
				colList.add(str);
			%>
				<th><%=str%></th>
			<%
			}
		}

	%>
		</tr>
		</thead>
			<tbody>
	<%
		int sSt=6;
		int eSt=7;
		if(isVmComp)
		{
			sSt++;
			eSt++;
		}

		EgTimeZoneConverter time_converter = new EgTimeZoneConverter();
		for(int x=0;x<resultList.size();x++)
		{
	%>
		<tr>
	<%
			List valueList = (ArrayList) resultList.get(x);
			int loopCount = 0;
			for (int i=0; i<valueList.size(); i++)
			{
				if(!isShowSessionStEnd&&(i==sSt || i==eSt))
					continue;
				Object obj = (Object)valueList.get(i);
				String objvalue = (String)obj;

				String colStr = (String)colList.get(loopCount);
				if(colStr != null && (colStr.equalsIgnoreCase("Application Launch Time") || colStr.equalsIgnoreCase("Application End Time")))
				{
					if(objvalue!=null && !objvalue.equals("-"))
					{
						time_converter.convertProcess(objvalue,userName,false);
						objvalue = time_converter.getDateTime();
					}
					obj = (Object)objvalue;
				}
				loopCount++;
				if(obj!=null && obj instanceof String)
				{
					String tempStr=(String)obj;
					if (i == 2 && tempStr.indexOf("+") > -1)
						tempStr = tempStr.substring(tempStr.indexOf("+")+1);
					try {
						tempStr = erUtils.round(Double.parseDouble(tempStr),2);
					} catch(Exception e) {
						tempStr = tempStr;
					}
					lst_dat1.add("\""+tempStr+"\"");
				}
				else
					lst_dat1.add("\""+obj+"\"");

				if(i==1)
				{
	%>
					<td><div style="width: 80px; word-wrap: break-word; overflow-x:auto;"><%= obj %></div></td>
	<%
				}
				else
				{
					String valueStr = obj.toString();
					String pattern = "(^[\"]?\\d+[\"]?$)|(^[\"]?\\d+(\\.\\d*)[\"]?$)";
					Pattern r = Pattern.compile(pattern);
					Matcher m = r.matcher(valueStr);
					String textAlign = "left";
					if (m.find() || valueStr.equals("-")) 
					{
						textAlign = "center";
					}
	%>
					<td align="<%=textAlign%>" ><%= obj %> </td>
	<%
				}
			}
	%>
		</tr>
	<%
		}
		
	%>
		</tbody>
	</table>
	<%
	}
	else
	{
	lst.add(intlmap.getTextDescription("reporter.common.No measures available for the current selection"));
	%>
		<table align="center" width="97%" cellspacing="0" cellpadding="4"   border="0">
			<tr>
				<table width='100%' height='100%'><tr>	<td align='center' valign='middle'><img src='/images/Light/nomeasures_custom.png' /></td></tr></table>
			</tr>
		</table>
	<%
	}
%>
	<!-- </table>   -->
	</div>
	</div>
	</body>
	</html>
<%
		Hashtable sen1 = new Hashtable();
		String s_tit1= "";
		sen1.put(v_measure_csv,lst_dat1);
		Hashtable ht1=new Hashtable();
		ht1.put(s_tit1,sen1);
		lst.add(ht1);
		sen1 = new Hashtable();
		ht1 = new Hashtable();
		session.setAttribute("ScheduleCSVData",lst);

		if(pdfPrint!=null && pdfPrint.equalsIgnoreCase("yes"))
		{
		%>
			<div class="printFooterText"><%=strPrintFooterText%></div>
		<%
		}
	} catch(Exception e) {
		e.printStackTrace();
	}
%>

<%@ include file = "ErRemoveUser.jsp" %>

<%
	if(titleToDisplay!=null && !titleToDisplay.isEmpty())
	{
		//For Setting the PDf File Name
		String reportFileName=customReplaceAll(titleToDisplay," ","_");
		HashMap paramVal =new HashMap();
		paramVal.put("fileName",reportFileName);
		session.setAttribute("parameters", paramVal);
	}

%>
<%!
	// karthikeyan 
	public  String customReplaceAll(String source,String toReplace,String replacement) 
	{
		int idx = source.lastIndexOf(toReplace);
		if (idx != -1) 
		{
			StringBuffer ret = new StringBuffer(source);
			ret.replace(idx, idx + toReplace.length(), replacement);
			while ((idx = source.lastIndexOf(toReplace, idx - 1)) != -1) {
				ret.replace(idx, idx + toReplace.length(), replacement);
			}
			source = ret.toString();
		}
		return source;
	}
%>



















