<%@ include file = "ErReporterTracker.jsp" %>
<%@ page import="egurkha.reporter.*" %>
<%@ page import="egurkha.reporter.uptime.*" %>
<%@ page import="com.egurkha.ini.*" %>
<%@ page import="com.egurkha.*" %>
<%@ page import="com.eg.*" %>
<%@ page import = "java.net.URLEncoder" %>
<%@ page import = "java.net.URLDecoder" %>
<%@ page import = "com.egurkha.util.DateTimeTokenizer" %>
<%@ page import = "com.egurkha.util.DateUtil" %>
<%@ page import = "java.text.*" %>
<%@ page import = "com.egurkha.pdfmaker.*" %>
<%@ page import = "java.io.*" %>
<%@ page import = "java.io.OutputStream" %>
<%@ page import = "java.net.URL" %>
<%@ page import = "net.sf.json.*" %>
<%@ page language = "java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>


<%
try{
	String myQueryString = null;
	String select_server = request.getParameter("select_server");
	Map myMap = new HashMap(request.getParameterMap());
	//myMap.remove("select_server");
	String drillDprocess = (String)request.getParameter("drillSes");
	
	if(drillDprocess !=null && drillDprocess.indexOf("?")>-1)
	{
		drillDprocess = drillDprocess.substring(0,drillDprocess.indexOf("?"));
	}
	String satusPage = "ErUptimeSave1.jsp";
	String strTemp = (String)request.getParameter("StatusPage");
	if(strTemp!=null && !strTemp.equals("") && !strTemp.equalsIgnoreCase("NULL"))
		satusPage = strTemp;

	String targType = "";
	String qryTrgt = "";
	try
	{
		targType = (String)myMap.get("er_trgttype");
	}
	catch (ClassCastException e)
	{
		targType = ((String[])myMap.get("er_trgttype"))[0];
		
	}
	if(targType !=null)
	{
		qryTrgt = "&er_trgttype"+ "=" +com.eg.EgURLEncoder.encode(targType, "UTF-8")+ "&";
	}
	else
	{
		qryTrgt ="";
	}
	String queryString = getEncodedString(myMap,targType);//ErUtils.prepareQueryString(myMap);
	if(myQueryString != null) {
		queryString += "&"+myQueryString;
	}
	String queryString_2 = (String) session.getAttribute("PDF_URL");
	Map mapReqParams = ErUtils.parseQueryString(queryString_2);
	Object pageWidthProces=mapReqParams.get("pageWidthProces");
	if(pageWidthProces != null)
	{
		queryString += "&pageWidthProces="+(String)pageWidthProces;
	}
	session.setAttribute("TEMP_QUERY_STRING_FOR_PDF",queryString);
	//..
	String isBuilderTimeline = request.getParameter("isBuilderTimeline");
	String graphIndex = request.getParameter("graphIndex");
	String widgetIdInfo = request.getParameter("widgetIdInfo");
	String pdfUrl = "";
	
	if(isBuilderTimeline != null && isBuilderTimeline.equalsIgnoreCase("Yes"))
	{
		pdfUrl = (String)session.getAttribute("DRILLDOWN_PDF_URL");
		pdfUrl=pdfUrl+"&printBy=drillDown&graphIndex="+graphIndex+"&widgetIdInfo="+widgetIdInfo+"&er_RmDispName=ReportBuilder";
		session.removeAttribute("DRILLDOWN_PDF_URL");
		session.setAttribute("DRILLDOWN_PDF_URL",pdfUrl);
		System.out.println("pdfurl=======>>==  "+pdfUrl);
	}
	JSONObject json =  new JSONObject();
	if(drillDprocess !=null && drillDprocess.equalsIgnoreCase("yes")){
		json.put("drillDnSess",drillDprocess);
	}
	json.put("status","success");
	response.getWriter().write(json.toString());
}
catch(Exception e)
{
	e.printStackTrace();
}
	//..
%>

<%!
	public String getEncodedString(Map hastable , String targType)
	{
		String queryString =(String)ErUtils.prepareQueryString(hastable);
		String Qstring = "";
		String qstringData ="";
		String valueP = "";
		try{
		if(queryString!=null)
		{
			StringTokenizer strtok = new StringTokenizer(queryString,"&");
			while(strtok.hasMoreTokens())
			{
				String param = strtok.nextToken();
				int ind = param.indexOf("=");
				if (ind < 0)
					continue;
				String reqparam = param.substring(0,ind);
				String reqvalue = param.substring(ind+1);
				if(reqparam != null)
				{
					if (reqparam.equals("stdate") || reqparam.equals("endate") || reqparam.equals("er_userid") || reqparam.equals("eruserid") || reqparam.equals("userID") || reqparam.equals("us_id") || reqparam.equals("uid") || reqparam.equals("appForAgent") || reqparam.equals("er_applicationX") || reqparam.equals("selectedApp") || reqparam.equals("limitforbg") || reqparam.equals("er_selection") || reqparam.equals("er_service") || reqparam.equals("er_userOption") || reqparam.equals("subevent")|| reqparam.equals("select_server") || reqparam.equals("select_groupName")|| reqparam.equals("er_site") || reqparam.equals("er_server")|| reqparam.equals("selectedCompType") || reqparam.equals("prior_type") || reqparam.equals("er_eventType") ||reqparam.equals("er_infos") || reqparam.equals("currentDateFormatForJS") || reqparam.equals("er_vms")|| reqparam.equals("currentformat") || reqparam.equals("currentDateFormat") || reqparam.equals("timelineval") || reqparam.equals("t_user") || reqparam.equals("er_template") || reqparam.equals("sel_mes") || reqparam.equals("er_transaction")|| reqparam.equals("dispstdate")|| reqparam.equals("dispendate") || reqparam.equals("er_titlle") || reqparam.equals("er_eventSource") || reqparam.equals("s_en_time")|| reqparam.equals("s_st_time") || reqparam.equals("er_application") || reqparam.equals("er_include") || reqparam.equals("er_showby") || reqparam.equals("userTimeline") || reqparam.equals("er_sortBy") || (targType != null && targType.equalsIgnoreCase("Prediction") && reqparam.equals("er_comptype")) || reqparam.equals("er_RmDispName") ||   reqparam.equals("er_top") ||reqparam.equals("er_gtype")|| 
					reqparam.equals("tests")||reqparam.equals("toScrpText")||reqparam.equals("hiddens")||reqparam.equals("measures")||reqparam.equals("infos") ||reqparam.equals("dsktp_searchTxt")|| reqparam.equals("er_Selection1") || reqparam.equals("er_Selection2")|| reqparam.equals("selectionName1") || reqparam.equals("selectionName2") || reqparam.equals("er_select_comp") || reqparam.equals("er_select_server")|| reqparam.equals("searchByUser")|| reqparam.equals("newBookletID")|| reqparam.equals("bookletDesId")|| reqparam.equals("rNameID") ||  reqparam.equals("er_timeInterval")||reqparam.equals("titleDisp") || reqparam.equals("er_bgreportfrom")||reqparam.equals("er_hostNames")||reqparam.equals("showFrom")||reqparam.equals("deskNameData") || reqparam.equals("chartInfo") || reqparam.equals("er_measure") || reqparam.equals("appStackUser") || reqparam.equals("reportType") || reqparam.equals("sess_service"))//reqparam.equals("component") - note when we uncomment pls check uptime Graph drilldn save

					{
						// below comment is because of 2nd index of string Array values not added
						// issues in drilldown pdf page
						/*Object obj = hastable.get(reqparam);
						if(obj instanceof String[])
						{
								reqvalue = EgURLEncoder.encode(((String[])hastable.get(reqparam))[0], "UTF-8");
						} 
						else
						{
							reqvalue = EgURLEncoder.encode((String)hastable.get(reqparam), "UTF-8");
						}*/
						reqvalue = EgURLEncoder.encode(reqvalue, "UTF-8");
					}
					else if(reqparam.equals("glduration"))
					{
						Object obj = hastable.get(reqparam);
						String data = "";
						if(obj instanceof String[])
						{
							data = ((String[])hastable.get(reqparam))[0];
						} 
						else
						{
							data = (String)hastable.get(reqparam);
						}
						if(data!=null && (data.equals(">")||data.equals("<")))
								reqvalue = EgURLEncoder.encode(data, "UTF-8");
					}
					else if(reqparam.equals("er_bgreportfrom")||reqparam.equals("compNameList"))
					{
						Object obj = hastable.get(reqparam);
						String data = "";
						if(obj instanceof String[])
						{
							data = ((String[])hastable.get(reqparam))[0];
						}
						else
						{
							data = (String)hastable.get(reqparam);
						}
						if(data!=null && data.indexOf(" ")>-1)
						{
							reqvalue = EgURLEncoder.encode(data, "UTF-8");
						}
					}
					/*if(reqparam.equals("startdate") || reqparam.equals("enddate")
						|| reqparam.equals("inidate"))
					{
						reqvalue = EgURLEncoder.encode(reqvalue, "UTF-8");
					}*/
				}
				Qstring = Qstring + reqparam + "=" + reqvalue + "&";

			}
		}
			Map mapReqParamsData = ErUtils.parseQueryString(Qstring);
			Object obj = mapReqParamsData.get("er_component");

			if(obj instanceof String[])
			{
				String[] comps = (String[]) obj;
				for (int j = 0;j < comps.length; j++)
				{
					if(comps[j] != null && !comps[j].equals(""))
					{
						try
						{
							mapReqParamsData.remove("er_component");
							//param.put("er_component",EgURLEncoder.encode(comps[j],"UTF-8"));
							valueP +="er_component="+ EgURLEncoder.encode(comps[j].toString(),"UTF-8")+ "&";
						}catch(Exception t1){}
					}
				}
			}
			else
			{
				try
				{
					mapReqParamsData.remove("er_component");
					valueP +="er_component="+ EgURLEncoder.encode(obj.toString(),"UTF-8")+ "&";
				}catch(Exception t1){}
			}
			qstringData =(String)ErUtils.prepareQueryString(mapReqParamsData);
		}catch(Exception e){
			e.printStackTrace();
		}
		return qstringData+"&"+valueP;
	}
%>


