<%
/*****************************************************************************************************
* Name				:	EgMHelpFrame.jsp
* Purpose			:	This jsp displays the help for all monitor pages 
* Creation Date	:	30/01/2001 (ASPlite)
* LastModified	:	07/11/2002 //Sandhya - to change i_maxpages		
*							23/11/2003 //Williams - eG
* Author			:	Balamurugan. V
* Version			:	3.0
******************************************************************************************************/
%>
<%@ page isThreadSafe="false" %>
<%@ page errorPage="EgMerr.html"%>
<%@ page import="java.io.*" %>
<%@ page import="egurkha.EgCrawler" %>
<%@ page import="com.eg.*" %>
<%@ page import="net.sf.json.*"%>
<%@ page import="com.egurkha.*" %>
<%@ page language="java" contentType="application/json;charset=UTF-8;" pageEncoding="UTF-8"%>
<%
	response.setHeader("Cache-Control","no-store");
	response.setHeader("Pragma","no-cache");
	response.setDateHeader ("Expires", -1);
%>
<%
	IniFile uiFile = new IniFile(EgInstallInfo.getInstallDir()+ "/manager/config/eg_ui.ini");
	String flag = uiFile.getValue("DEBUG_MODE", "help");
	boolean debug = (flag != null && !flag.equals("") && flag.equalsIgnoreCase("yes"));
	HelpXMLFileReader egHelp = HelpParserThread.egHelp;
%>

<%!
	//This unit represents the number of maximum help pages + 1
	int i_maxpages = 4184;
	static String logDir = EgConfigInfo.optEgurkha + "/manager/logs/" ;
	public static synchronized void logToErrorFile(String module, String fileName, String id, String user)
	{
		PrintWriter logWriter;
		String log;
		EgDateInfo dateInfo = new EgDateInfo();
		String logDateAndTime = dateInfo.egetDate(false);
		File ff = new File(logDir + "eghelp");
		if (ff.length() >= 1000000)
		{
			EgInstallInfo.fileRename(logDir + "eghelp",logDir + "eghelp"+".1");
		}
		ff = null;
		try
		{
			//open log file for writing
			logWriter = new PrintWriter(new FileWriter(logDir + "eghelp", true));
		}
		catch(Exception e)
		{
			System.err.println("Cannot open log file ");
			return;
		}
		log=new String(logDateAndTime+" ["+module+" : "+fileName+" : "+id+"] " +user);
		logWriter.println(log);
		logWriter.close();
	}
%>
<%
	JSONObject rootJson = new JSONObject();
	int i_prev = 0;
	int i_next = 0;
	String s_id = "";
	String s_page = "";
	String s_test="";
	String from="";
	String fromPopup = "";
	String s_path = "";
	String s_pathP = "";
	String s_module = "";
	String parent_page="";
	s_id = request.getParameter("id");
	s_test=request.getParameter("test");
	from=request.getParameter("from");
	fromPopup=request.getParameter("fromPopup");
	String measurement = request.getParameter("msrmt");
	s_path = (String) session.getValue("Page_IdentifierM");
	s_pathP = (String) session.getValue("Page_IdentifierMP");

	UserRegistry reg = UserRegistry.getInstance();
	User usr = reg.getUser(session.getId());
	boolean isICTest=false;
	if(s_test!=null && s_test.endsWith("_ex"))
	{
		isICTest=true;
	}
System.out.println("killer id " + s_id);
System.out.println("killer from " + from);
	try
	{
		if(!isICTest)
		{
				if(s_id == null)
				{
					egHelp.setModule("MPage");
					String s_from=request.getParameter("from");
					if( s_from== null)
						s_path = (String) session.getValue("Page_IdentifierM");
					else if(s_from.equalsIgnoreCase("repo"))
						s_path = (String) session.getValue("Page_IdentifierR");
					else  if(s_from.equalsIgnoreCase("QINSIGHT"))
						s_path = (String) session.getValue("Page_IdentifierQI");
					else if(s_from.equalsIgnoreCase("LIVEGRAPH"))
						s_path = (String) session.getValue("Page_IdentifierLG");
					else if(s_from.equalsIgnoreCase("ALARMS"))
						s_path = "/final/monitor/EgAlarms.jsp";
System.out.println("s_path is------------- " + s_path);
					if(s_path != null)
					{
						if(s_path.indexOf("?") > -1)
						{
								s_path = s_path.substring(0,s_path.indexOf("?"));
						}
						s_module = s_path.substring(0,s_path.lastIndexOf("/"));
						s_module = s_module.substring(s_module.lastIndexOf("/")+1);
						s_path = s_path.substring(s_path.lastIndexOf("/")+1);

						if(s_pathP != null && s_pathP.trim().length()>0)
						{
							if(s_pathP.indexOf("?") > -1)
							{
								s_pathP = s_pathP.substring(0,s_pathP.indexOf("?"));
							}
							s_pathP = s_pathP.substring(s_pathP.lastIndexOf("/")+1);
						}

						if(fromPopup != null && (fromPopup.equalsIgnoreCase("true") || fromPopup.equalsIgnoreCase("yes")) && s_pathP != null && s_pathP.trim().length()>0 && !s_pathP.equals(s_path))
						{
							s_path=s_pathP;
						}

						if(s_path.equals("ErReporterMenu.jsp") || s_path.equals("ErMacro.jsp") || s_path.equals("ErSnapshot.jsp")
							|| s_path.equals("ErSystemCapacityReport.jsp")|| s_path.equals("ErEsxReport.jsp")|| s_path.equals("ErVirtualCenterReport.jsp") || s_path.equals("ErAlarmHistory.jsp")|| s_path.equals("egAppDashboard.jsp") || s_path.equals("ErThinClientUserReport.jsp") || 
							s_path.equals("ErServiceReport.jsp") ||
							s_path.equals("ErVMCapacityReport.jsp") ||
							s_path.equals("ErVirtualClusterReport.jsp") ||
							s_path.equals("ErThinClientApplication.jsp")||
							s_path.equals("ErRumPGPReport.jsp") ||
							s_path.equals("ErHitCountTopNReport.jsp")
							|| s_path.equals("ErRumReport.jsp") || s_path.equals("ReportBuilder.jsp")|| s_path.equals("ErCtxLogonSimulator.jsp") || s_path.equals("MailExchangeReport.jsp") || s_path.equals("ErExternalMonitorReport.jsp") || s_path.equals("ErLogonFailure.jsp") || s_path.equals("ErVMAppVolumeLicense.jsp") || s_path.equals("ErVMAppVolumeDetails.jsp") || s_path.equals("ErCitrixConnectionDetail.jsp") || s_path.equals("ErCitrixMachineReport.jsp") || s_path.equals("JvmMemoryReport.jsp") || s_path.equals("ErUptimeReportData.jsp"))
						{
							Object val = session.getAttribute("Source");
							if(val != null)
							{
								s_id = egHelp.getId(s_path+"?"+(String)val);
							}
						}
						else if(s_path.equals("componentListData.jsp") || s_path.equals("boxListData.jsp") || s_path.equals("comparisonSystem.jsp") || s_path.equals("comparisonComponent.jsp"))
						{
							Object val = session.getAttribute("Source");
							System.out.println("s_path file val is -------------------" + val);
							if(val != null)
							{
								s_id = egHelp.getId(s_path+"?"+(String)val);
							}
						}
						else
						{
							s_id = egHelp.getId(s_path);
							System.out.println("s_path file id is -------------" + s_id);
						}
					}
					else
					{
						s_page = "EgMerr.html";
						i_next = i_maxpages;
					}
					
					if(debug)
						logToErrorFile(s_module, s_path, s_id, usr.getLoginID());
				}
		}
		if(s_id != null && !(s_id.equals("NOT_FOUND")) && !(s_id.equals("")))
		{
			i_prev = new Integer(s_id).intValue();
			i_prev--;
			i_next = new Integer(s_id).intValue();
			i_next++;
			s_page = "EgM"+ s_id + ".html?test="+s_test+"&from="+from;
		}
		else
		{
			File helpPageObj = new File(EgInstallInfo.getInstallDir() +"/manager/tomcat/webapps/final/eghelp/"+s_page);
			if (!helpPageObj.exists())
			{	
				s_page = "EgAerr1.html";
			}	
			if(isICTest)
			{
				
				String strHelpPagePath = application.getRealPath("/eghelp/"+s_test+"_Monitor.html");
				File file = new File(strHelpPagePath);
				if(!file.exists())
					s_page="EgTemplateHelp.html";
				else
					s_page=s_test+"_Monitor.html";
			}else if(s_test!=null && s_test.endsWith("_ag"))
			{
				//s_page = "EgAggTemplateHelp.html?test="+s_test+"&from="+from;
				s_page = "../../final/monitor/agg.jsp?aggTest="+s_test+"&from="+from;
			}
			else
			{
				s_page = "EgMerr.html";
			}
			i_next = i_maxpages;
		}
	}
	catch(Exception e)
	{
		System.err.println("ERR in Monitor Help" + e.getMessage());
		e.printStackTrace();
		out.println("ERR in Monitor Help" + e.getMessage());
		s_page = "EgMerr.html";
		i_next = i_maxpages;
	}
	if(s_path!=null && s_path.length()>0 && s_path!="" && s_path.contains("/"))
	{
		String path=s_path.substring(0,s_path.lastIndexOf("/"));
		String module=path.substring(path.lastIndexOf("/")+1);
		if(module!=null && module.length()>0)
		{
			parent_page=module;
		}
	}
	rootJson.put("PAGE_ID", s_id);

	if(measurement!=null && measurement.trim().length()>0 && !measurement.trim().equalsIgnoreCase("null"))
	{
		String Qstr=s_page+"#"+measurement;
		rootJson.put("PAGE_URL", "/final/eghelp/"+Qstr);
	}
	else
	{
		rootJson.put("PAGE_URL", "/final/eghelp/"+s_page);
		if(s_page!=null && s_page.contains("EgM419"))
		{
			parent_page= "reporter";
		}
		else
		{
			if(s_page!=null && s_page.contains("EgM6") || s_page.contains("EgM329"))
			{
				parent_page= "monitor";
			}
		}
	}
	out.println(rootJson.toString());
%>