<%@	include	file = "EgMonitorTracker.jsp" %>
<%@ page import="com.egurkha.UserRegistry"%>
<%@ page import="com.egurkha.EgUserRole"%>
<%@  page import= "java.util.Vector"%>
<%@  page import= "java.util.Hashtable"%>
<%@	page import	= "java.util.HashMap"%>
<%@	page import	= "java.util.StringTokenizer"%>
<%@  page import= "com.eg.IniFile"%>
<%@ page import = "egurkha.reporter.ErUtils"%>
<%@  page import= "com.eg.EgInstallInfo" %>
<%@  page import= "java.net.URLEncoder" %>
<%@  page import="com.egurkha.monitor.EgCtxUserBean" %>
<%@ page import = "com.egurkha.monitor.VMDesktopBean"%>
<%@  page import="com.egurkha.monitor.EgDashBoardDAO" %>
<%@ page import = "com.eg.EgMappingInfo" %>
<%@  page import = "com.egurkha.User" %>
<%@  page import = "com.egurkha.UserRegistry" %>
<%@  page import = "com.egurkha.EgUserRole" %>
<%@  page import = "com.egurkha.ini.IniUI" %>
<%@  page import= "com.eg.EgDateInfo" %>
<%@  page import= "com.eg.EgTablesInfo" %>
<%@  page import= "com.eg.EgDbInfo" %>
<%@  page import= "com.eg.EgExternalInfo" %>
<%@  page import= "com.eg.EgSpecsInfo" %>
<%@  page import= "com.eg.EgAgentInfo" %>
<%@  page import= "com.eg.EgTesterInfo" %>
<%@ page import = "com.eg.EgMemInfo" %>
<%@page import = "com.eg.EgURLEncoder"%>
<%@  page import= "com.egurkha.util.DateUtil" %>
<%@ page import = "com.egurkha.monitor.EgServerDashBoardDAO"%>
<%@ page import = "com.egurkha.monitor.MonitorDAOFactory"%>
<%@ page import="net.sf.json.*"%>
<%@ page import="java.text.SimpleDateFormat"%> 
<%@  page import= "com.eg.EgFormatInfo" %>
<%@ page import = "com.egurkha.util.DateTimeTokenizer" %>
<%@ taglib prefix="egui" uri="../WEB-INF/eg-taglib.tld" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<jsp:useBean id="dd" scope="page" class="com.egurkha.monitor.EgDiagnosisBean" />
<jsp:useBean id="trans" scope="page" class="egurkha.TransactionBean"/>
<%!
	//Don't add this method in 7.0v since it already exists in EgCommonDashboardBean.java
	public List getTimelineParams(String timeline){
		List timeParams = new ArrayList();
		EgDateInfo edi = new EgDateInfo();
		String startDateTime = "",endDateTime = "";

		int blankSpaceIndex = timeline.indexOf(DateTimeTokenizer.BLANK_SPACE);
		if (blankSpaceIndex > 0)
		{
			SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
			java.util.Date tmpday = edi.egetRealDate();
			endDateTime = sdf.format(tmpday);
			int value = Integer.parseInt(timeline.substring(0, blankSpaceIndex));
			String unit = timeline.substring(blankSpaceIndex+1).toLowerCase();
			if (unit.startsWith("min"))
			{//minutes
				value *= 60;
				endDateTime = null;
			}
			else if (unit.startsWith("h"))
			{//hours
				value *= 3600;
			}
			else if (unit.startsWith("d"))
			{//days
				value *= (24 * 3600);
			}
			else if (unit.startsWith("w"))
			{//weeks
				value *= (7 * 24 * 3600);
			}
			else if (unit.startsWith("mo"))
			{//months
				value *=(ErUtils.getMaxDaysInMonth()) * 24 * 3600;
			}
			long value2=value;
			java.util.Date start = edi.egetRealDate();
			start.setTime((tmpday.getTime() - (value2 * 1000)));
			startDateTime = sdf.format(start);
			timeParams.add(startDateTime);
			timeParams.add(endDateTime);
		}
		return timeParams;
	}
%>
<%
	EgDbInfo MeasureDisplay=new EgDbInfo("manager");
	TreeMap linksMap = new TreeMap();
	String uiFile = "eg_ui.ini";
	String s_iniFile_dir = EgInstallInfo.getInstallDir()+ "/manager/config/";
	IniFile egUI = new IniFile(s_iniFile_dir + uiFile);
	String[] options = {"Any","Recent","1 hour", "3 hours", "6 hours", "9 hours", "12 hours","15 hours", "18 hours","21 hours","24 hours", "2 days", "3 days", "4 days", "5 days", "6 days","1 week","2 weeks","3 weeks","4 weeks","1 month","2 months"};           
	JSONObject dataJson = new JSONObject();
	JSONArray parentDataArr = new JSONArray(); 
	JSONArray columnDetails = new JSONArray(); 
	JSONObject rootJson = new JSONObject();
	JSONObject screenLabels = new JSONObject();
	JSONArray  stateGenOpts = new JSONArray();
	JSONObject dateTime = new JSONObject();
	JSONArray dataArr = new JSONArray();
	JSONObject parentData = new JSONObject();
	JSONObject linkObj = new JSONObject();
	JSONObject rowData = new JSONObject();
	List timelineParams = null;
	boolean flage_header=true;
	String  defaulttimelinevalue="";
	String s_timeline=request.getParameter("eg_timeline"); 
	String eg_tbound=request.getParameter("eg_tbound");  
	eg_tbound = (eg_tbound==null||eg_tbound.equalsIgnoreCase("null")||eg_tbound.trim().length()==0)?"":eg_tbound;
	String eg_stdate = request.getParameter("eg_stdate");
	//System.out.println("eg_stdate-----VMMig---->"+eg_stdate);
	String eg_enddate = request.getParameter("eg_enddate");
	//System.out.println("eg_enddate-----VMMig---->"+eg_enddate);
	String start_hr = request.getParameter("starthr");
	String end_hr = request.getParameter("endhr");
	String startmin_h = request.getParameter("startmin_h");
	startmin_h=(startmin_h==null || startmin_h.trim().length()==0 || startmin_h.equals("null"))?"00":startmin_h;
	String endmin_h = request.getParameter("endmin_h");
	endmin_h=(endmin_h==null || endmin_h.trim().length()==0|| endmin_h.equals("null"))?"59":endmin_h;
	String isFromLayers = request.getParameter("isFromLayers");
	boolean isFromLayersFlag = false;
	isFromLayersFlag = (isFromLayers==null||isFromLayers.equalsIgnoreCase("null")||isFromLayers.trim().length()==0)?false:true;
	String currentDateFormat = egUI.getValue("CURRENT_DATE_FORMAT","selectedDateFormat");
	if(isFromLayersFlag)
	{
		dataJson.put("isFromLayers",isFromLayersFlag);
		String comFormat = "";
		String userFormat = u_ser.getUserDateFormat();
		if(eg_stdate!=null && !eg_stdate.equalsIgnoreCase("null") && eg_stdate.trim().length()>0)
		{
			if(DateUtil.isDate(eg_stdate,"MM/dd/yyyy"))
			{
				comFormat = "MM/dd/yyyy";
			}
			else
			{
				comFormat = currentDateFormat;
			}
			eg_stdate = DateUtil.changeDateFormat(eg_stdate,comFormat,userFormat);
			
			dateTime.put("eg_stdate",eg_stdate);
			dateTime.put("start_hr",start_hr);
		}
		if(eg_enddate!=null && !eg_enddate.equalsIgnoreCase("null") && eg_enddate.trim().length()>0)
		{
			if(DateUtil.isDate(eg_enddate,"MM/dd/yyyy"))
			{
				comFormat = "MM/dd/yyyy";
			}
			else
			{
				comFormat = currentDateFormat;
			}
			eg_enddate = DateUtil.changeDateFormat(eg_enddate,comFormat,userFormat);

			dateTime.put("eg_enddate",eg_enddate);
			dateTime.put("end_hr",end_hr);
		}
	}
	if(s_timeline==null)
	{
		defaulttimelinevalue=egUI.getValue("SEARCH_DISPLAY","Timeline");
		s_timeline=defaulttimelinevalue;
		dataJson.put("defaulttimelinevalue",defaulttimelinevalue);
	}
	else
	{
		dataJson.put("defaulttimelinevalue",s_timeline);
	}

	EgAgentInfo agentInfo = new EgAgentInfo(EgConstants.MANAGER);
	EgTesterInfo tt = new EgTesterInfo(EgConstants.MANAGER);
	EgSpecsInfo specsInfo = new EgSpecsInfo("manager");
	boolean ddEnabled = EgMemInfo.ddEnabled;
	EgFormatInfo formatInfo = new EgFormatInfo ("manager");
	ArrayList excludeDDTestList = formatInfo.getHideDDTestList();

	trans.createFile("eg_format.ini");
	String strVarArr[] = trans.getVariables("FORMAT");
	String dummyTestArr[] = trans.getVariables("COMMON_DD_MAPPING");
  
	String state=request.getParameter("state");
	state = (state==null||state.equalsIgnoreCase("null")||state.trim().length()==0)?"":state;
	ArrayList timlinelist = new ArrayList(Arrays.asList(options));
	JSONArray historyTmeLineList = new JSONArray();
	ListIterator itr = timlinelist.listIterator();
	String tempStr = "";
	while(itr.hasNext())
	{
		tempStr = (String)itr.next();
		JSONObject temp = new JSONObject();
		temp.put("displayField",mappingInfo.getTextDescription("monitor.common."+tempStr));
		temp.put("valueField",tempStr);
		historyTmeLineList.add(temp);
	}

	String timeLineLbl = mappingInfo.getTextDescription("monitor.common.Timeline");
	String fromLbl = mappingInfo.getTextDescription("monitor.common.From");
	String toLbl = mappingInfo.getTextDescription("monitor.common.To");
	screenLabels.put("monitor.common.State",mappingInfo.getTextDescription("monitor.common.State"));
	screenLabels.put("monitor.common.Timeline",timeLineLbl);
	screenLabels.put("monitor.common.From",fromLbl);
	screenLabels.put("monitor.common.To",toLbl);
	screenLabels.put("monitor.common.Hr",mappingInfo.getTextDescription("monitor.common.Hr"));
	JSONObject poweredOpt = new JSONObject();
	poweredOpt.put("displayField",mappingInfo.getTextDescription("monitor.vmsearch.Powered on")); 
	poweredOpt.put("valueField","on"); 
	stateGenOpts.add(poweredOpt);
	JSONObject anyOpt = new JSONObject();
	anyOpt.put("displayField",mappingInfo.getTextDescription("monitor.vmsearch.Any")); 
	anyOpt.put("valueField","any"); 
	stateGenOpts.add(anyOpt);
	if(state!=null && state.equalsIgnoreCase("any"))
	{
		dataJson.put("defaultstateopt","any");
	}

	if(state!=null&&state.equals("on"))  
	{
		dataJson.put("defaultstateopt","on");
	}
	if(flage_header)
	{
		columnDetails.add(mappingInfo.getTextDescription("monitor.vmsearch.VMNAME"));
		columnDetails.add(mappingInfo.getTextDescription("monitor.vmsearch.VMCOMPNAME"));
		columnDetails.add(mappingInfo.getTextDescription("monitor.vmsearch.VMCOMPTYPE"));
		if(state!=null&&state.equals("any"))
		{
			columnDetails.add(mappingInfo.getTextDescription("monitor.vmsearch.POWEREDON"));
		}

		columnDetails.add(mappingInfo.getTextDescription("monitor.vmsearch.TIME"));
		columnDetails.add(mappingInfo.getTextDescription("monitor.vmsearch.VMGRAPH"));
		columnDetails.add(mappingInfo.getTextDescription("monitor.common.VMDD"));
		flage_header=false;
	}
	try
	{
		StringBuffer s_title = new StringBuffer();
		String ftitle="";		
		s_title.append(mappingInfo.getTextDescription("monitor.search.Search results for")+" - "+mappingInfo.getTextDescription("monitor.search.VM")+" ");
		EgTablesInfo tables=new EgTablesInfo("manager");
		String usersdetailsperpage = egUI.getValue("SEARCH_DISPLAY","UserDetails_PerPage");
		EgCtxUserBean egCtxBean  =  new EgCtxUserBean();
		com.eg.IniFile egEx = new com.eg.IniFile(EgInstallInfo.getInstallDir()+ "/manager/config/eg_external.ini");
		String dispDate = null;
		String mode ="2D";
		com.egurkha.ini.IniUI ui = (com.egurkha.ini.IniUI)com.egurkha.ini.IniUI.getInstance();
		ErUtils erUtils = new ErUtils();
		EgDateInfo egDt=new EgDateInfo();
		java.util.Date local_date = new java.util.Date(egDt.egetUTCDate());
		Calendar cal = Calendar.getInstance();
		cal.setTime(local_date);
		cal.setTimeZone(egDt.getRealTimeZone());
		String start_date="";
		String end_date="";
		ArrayList v_data=null;
		StringBuffer printQS=new StringBuffer();
		String title="";
		Vector v_columns=new Vector();
		String searchType = request.getParameter("searchtype");
		String s_user=request.getParameter("searchvalue");
		String from = request.getParameter("from");
		String currentPageNo = request.getParameter("currentPageNo");
		s_user=com.egurkha.util.StringUtils.replace(s_user,"|quots|","'");
		s_user=com.egurkha.util.StringUtils.replace(s_user,"|hash|","#");
		if(s_user!=null)
		{ 
			String temp_s_user = com.egurkha.util.StringUtils.decodeSecurityChar(s_user);
			s_title.append(temp_s_user);
			s_title.append(" ");
		}
		if (s_user==null)
			s_user="";
		boolean showTreeView = false;
		Hashtable ht_server = new Hashtable();
		if(from == null || from.trim().length() == 0 || !from.equalsIgnoreCase("pagination")){
			ht_server=(Hashtable)request.getAttribute("CitrixUser");
			session.setAttribute("CitrixUser",ht_server);
		}
		else
			ht_server=(Hashtable)session.getAttribute("CitrixUser");

		int totalRec = ht_server.size();
		if(ht_server.containsKey("keyList"))
			totalRec = totalRec-2;
		int recPerPage = Integer.parseInt(usersdetailsperpage);
		int start = 0;
		int end = (totalRec<recPerPage)?totalRec:recPerPage;
		String startIdx = request.getParameter("startIdx");
		if(startIdx != null && startIdx.trim().length() > 0)
			start = Integer.parseInt(startIdx);
		String endIdx = request.getParameter("endIdx");
		if(endIdx != null && endIdx.trim().length() > 0)
			end = Integer.parseInt(endIdx);
		if(currentPageNo != null && currentPageNo.trim().length() > 0){
			end = Integer.parseInt(currentPageNo)*recPerPage;
			start = end-recPerPage;
		}
		int pageSize = 0;
		String totalPages = request.getParameter("totalPages");
		if(totalPages == null || totalPages.trim().length() == 0){
			pageSize = Math.round(totalRec/recPerPage);
			//System.out.println("ht_server--pageSize---VMMig---->"+pageSize+" mod opr -> "+totalRec%recPerPage);
			if(totalRec%recPerPage > 0)
				pageSize = pageSize+1;
		}
		else
			pageSize = Integer.parseInt(totalPages);

		//System.out.println("pageSize-----VMMig---->"+totalRec+" "+pageSize+" "+recPerPage);
		//System.out.println("pageSize-----VMMig---->"+start+" "+end);
		StringBuffer sb = new StringBuffer();
		Hashtable state_hash=new Hashtable();
		if(ht_server != null && ht_server.size() > 0)
		{
			String searchStartTime = (String)ht_server.get("startTime");
			String vmEndTime = "", vmEndHr = "";
			ArrayList keysList = (ArrayList)ht_server.get("keyList");
			for (int k=start;k<end;k++)
			{
				sb = new StringBuffer();
				showTreeView = false;
				String info = (String)keysList.get(k);
				if(info!=null && info.equals("keyList"))
					continue;

				String userinfo = info;
				userinfo=com.egurkha.util.StringUtils.replace(userinfo, "\\", "\\\\");
				userinfo=com.egurkha.util.StringUtils.replace(userinfo,"#","|hash|");
				userinfo=com.egurkha.util.StringUtils.replace(userinfo,"%","|percent|");
				userinfo=com.egurkha.util.StringUtils.replace(userinfo,"&","|amp|");

				dataArr = new JSONArray();
				parentData = new JSONObject();
				linkObj = new JSONObject();

				v_data=(ArrayList)ht_server.get(info);
				String test="";
				//Collections.sort(v_data,Collections.reverseOrder());
				int count_powerOff=0;
				int noofrows = v_data.size();
				if(noofrows > 6)
					showTreeView = true;

				for(int i=0;i<noofrows;i=i+6)
				{
					String timeLineValue = (String)v_data.get(i);
					String strdateValue = "";
					if (timeLineValue!=null && !timeLineValue.equals("Recent"))
					{
						//strdateValue=st.nextToken();
					}
					String server = (String)v_data.get(i+1);
					String componentName = server;
					if(showTreeView)
						sb.append(","+componentName);
					if (componentName.endsWith("NULL"))
						componentName=componentName.substring(0,componentName.lastIndexOf(":"));

					String comptype = (String)v_data.get(i+2);
					String poweredOn = (String)v_data.get(i+3);
					poweredOn = EgDbInfo.formatValue(poweredOn);
					if(poweredOn!=null && poweredOn.equals("1"))
						poweredOn ="yes";
					else
						poweredOn ="no";
					if(state!=null&&!state.equals("any"))
					{
						if(poweredOn!=null&&poweredOn.equals("no"))
						{
							count_powerOff++;
							continue;
						}
					}
					String enddate = (String)v_data.get(i+4);// max of msmt_time
					String measureList="";
					String ddMeasure="";
					String ddTest = null;
					String layer_for_url=null;
					// for getting Measure from eg_external.ini
					if(comptype!=null && comptype.trim().length()>0)
					{
						String test_measure=egEx.getValue("VMSearch_Measure",comptype);
						if(test_measure!=null && test_measure.trim().length()>0)
						{
							StringTokenizer test_measure_token=new StringTokenizer(test_measure,":");
							if(test_measure_token.countTokens()==2)
							{
								test=test_measure_token.nextToken();
								measureList=test_measure_token.nextToken();
							}
						}

						String dd_test_measure=egEx.getValue("VMSearch_Measure",comptype+":DD");
						if(dd_test_measure!=null && dd_test_measure.trim().length()>0)
						{
							StringTokenizer test_measure_token=new StringTokenizer(dd_test_measure,":");
							if(test_measure_token.countTokens()==2)
							{
								ddTest=test_measure_token.nextToken();
								ddMeasure=test_measure_token.nextToken();
							}
						}
						layer_for_url=egEx.getValue("VMSearch_InsideviewLayer",comptype);
					}
					//--end---

					String layer=tables.getLayerForTest(test);
					if(layer!=null && layer.indexOf(",")>-1)
					{
						layer=layer.substring(0,layer.indexOf(","));
					}
					layer_for_url=(layer_for_url==null)?layer:layer_for_url;

					String starttime = "",sthr="";
					Date dateObj=null;
					if(!showTreeView){
						Date startdateObj = new Date(searchStartTime);
						starttime = searchStartTime;
						Calendar cal_temp = new GregorianCalendar();
						cal_temp.setTime(startdateObj);
						//cal_temp.add(Calendar.HOUR_OF_DAY,-1);

						dateObj = cal_temp.getTime();
						long l_starttime = dateObj.getTime();
						String startDate = egDt.egetDateUS(l_starttime);
						sthr=startDate.substring(startDate.indexOf(" ")+1,startDate.indexOf(":"));
						starttime=startDate.substring(0,startDate.indexOf(" "));
					}
					else{
						String strdate = "";
						if(i+10 < noofrows)
							strdate = (String)v_data.get(i+10);
						else
							strdate = searchStartTime;
						Date startdateObj = new Date(strdate);
						starttime =strdate;
						Calendar cal_temp = new GregorianCalendar();
						cal_temp.setTime(startdateObj);
						cal_temp.add(Calendar.HOUR_OF_DAY,-1);

						dateObj = cal_temp.getTime();
						long l_starttime = dateObj.getTime();
						String startDate = egDt.egetDateUS(l_starttime);
						sthr=startDate.substring(startDate.indexOf(" ")+1,startDate.indexOf(":"));
						starttime=startDate.substring(0,startDate.indexOf(" "));
					}

					if(eg_stdate==null || eg_stdate.equals("null"))
					{
						eg_stdate=starttime;
						//System.out.println("eg_stdate-----VMMig--2-->"+eg_stdate);
					}

					//if(start_hr==null || start_hr.equals("null"))
					{
						start_hr=sthr;
					}

					Date enddateObj = new Date(enddate);
					Calendar cal_temp = new GregorianCalendar();
					cal_temp.setTime(enddateObj);
					cal_temp.add(Calendar.HOUR_OF_DAY,1);

					dateObj = cal_temp.getTime();
					long l_endtime = dateObj.getTime();
					String endtime = egDt.egetDateUS(l_endtime);
					String endhr=endtime.substring(endtime.indexOf(" ")+1,endtime.indexOf(":"));
					endtime=endtime.substring(0,endtime.indexOf(" "));
					timeLineValue="Any";
					if(vmEndTime.trim().length() == 0){
						vmEndTime = endtime;
						vmEndHr = endhr;
					}

					if(eg_enddate==null || eg_enddate.equals("null"))
					{
						eg_enddate=endtime;
					}

					//if(end_hr==null || end_hr.equals("null"))
					{
						end_hr=endhr;
					}

					String layerQs="../servlet/com.eg.EgDashboardController?from=list&site=NULL&qctr=0&host="+EgURLEncoder.encode(server,"UTF-8")+"&comptype="+comptype+"&tab=LayerModel&toDashBoardLayer=true";
					String ddQs="../monitor/EgDiagnosis.jsp?site=NULL&host="+EgURLEncoder.encode(server.substring(0,server.lastIndexOf(":")),"UTF-8")+"&port="+server.substring(server.lastIndexOf(":")+1)+"&test="+ddTest+"&info=NONE&msmt_host=&from=0&newWindow=true&measure="+ddMeasure+"&firsttime=true&flag=0 &starttime="+starttime+"&endtime="+endtime+"&starthr="+sthr+"&endhr="+endhr+"&timeline="+timeLineValue;

					String graphQs="";
					dispDate = DateUtil.changeDateFormat(enddate,"MM/dd/yyyy HH:mm:ss",currentDateFormat +" HH:mm:ss");
					EgTimeZoneConverter timeZoneConverter = new EgTimeZoneConverter(dispDate , u_ser);
					dispDate = timeZoneConverter.getDateTime();
					String compNameWithType = server + ":" + comptype;

					Hashtable Valid_info=new Hashtable();
					ArrayList insideview_vm_list=new ArrayList();
					String key_for_hash=comptype+"^eg^"+server+"^eg^"+layer_for_url;
					if(state_hash!=null && state_hash.size()>0 && state_hash.containsKey(key_for_hash))
					{
						insideview_vm_list=(ArrayList)state_hash.get(key_for_hash);
					}
					else
					{
						VMDesktopBean vmdeskBean=new VMDesktopBean(comptype,server,layer_for_url);
						Valid_info=vmdeskBean.getInfoForTest();
						if(Valid_info!=null && Valid_info.size()>0)
						{
							Enumeration vm_keys=Valid_info.keys();
							while(vm_keys.hasMoreElements())
							{
								String key = (String)vm_keys.nextElement();
								insideview_vm_list.add(key.toLowerCase());
							}
						}

						if(insideview_vm_list!=null)
						{
							state_hash.put(key_for_hash,(ArrayList)insideview_vm_list);
						}
					}
					String isUser = (String)v_data.get(i+5);
					String mHost = "";
					List agents=new LinkedList();
					String nick_name = server.substring(0,server.lastIndexOf(":"));
					EgExternalInfo egExternalInfo=new EgExternalInfo("manager");
					agents=egExternalInfo.getAgentsFor(test,comptype,nick_name);
					if (agents!=null && agents.size()>0)
					{
						mHost = (String) agents.get(0);
					}
					else
					{
						mHost=nick_name;
					}
					agents=egExternalInfo.getAgentsFor(test,comptype,nick_name);

					String s_qryString="/final/monitor/eGVMDetails.jsp?comp="+EgURLEncoder.encode(server,"UTF-8")+"&comptype="+comptype+"&info="+EgURLEncoder.encode("*"+userinfo, "UTF-8")+"&compName="+EgURLEncoder.encode(server+":"+comptype,"UTF-8")+"&componentName="+componentName+"&username="+EgURLEncoder.encode(s_user,"UTF-8")+"&searchval="+EgURLEncoder.encode(s_user,"UTF-8")+"&searchtype="+EgURLEncoder.encode(searchType, "UTF-8")+"&msmt_host="+EgURLEncoder.encode(mHost,"UTF-8")+"&searchuser="+EgURLEncoder.encode(searchType+" "+s_user,"UTF-8");

					if(isUser != null && isUser.trim().length() > 0)
						s_qryString += "&isUser="+isUser;

					if((s_timeline!=null && !s_timeline.equalsIgnoreCase("null") && s_timeline.trim().length()>0) && (eg_stdate!=null && !eg_stdate.equalsIgnoreCase("null") && eg_stdate.trim().length()>0))
					{
						s_qryString = s_qryString + "&eg_timeline="+EgURLEncoder.encode(s_timeline,"UTF-8")+"&eg_stdate="+EgURLEncoder.encode(starttime,"UTF-8")+"&eg_enddate="+EgURLEncoder.encode(endtime,"UTF-8")+"&starthr="+EgURLEncoder.encode(start_hr,"UTF-8")+"&endhr="+EgURLEncoder.encode(end_hr,"UTF-8")+"&startmin_h="+EgURLEncoder.encode(startmin_h,"UTF-8")+"&endmin_h="+EgURLEncoder.encode(endmin_h,"UTF-8");

						if(!s_timeline.equals("Recent"))
							s_qryString = s_qryString + "&eg_tbound="+EgURLEncoder.encode(eg_tbound,"UTF-8");
						else if(state!=null && !state.equalsIgnoreCase("null") && state.trim().length()>0)
							s_qryString = s_qryString +"&state="+state;
					}

					String tmp_s_timeline=s_timeline;
					if(tmp_s_timeline!=null && tmp_s_timeline.equals("Recent"))
					{
						tmp_s_timeline = "24 hours";
					}
					if(showTreeView)
						tmp_s_timeline="Any";
					//componentMeasureTabset
					String hostname=EgURLEncoder.encode(server.substring(0,server.lastIndexOf(":")),"UTF-8");
					String port = server.substring(server.lastIndexOf(":")+1);

					String dLink = "/final/monitor/EgDiagnosis.jsp?site=NULL&host="+hostname+"&port="+port+"&test="+ddTest+"&isPopUp=true";
					
					String compddLink = dLink + "&info=NONE&msmt_host="+EgURLEncoder.encode(mHost,"UTF-8")+"&from=0&measure="+ddMeasure+"&flag=1&firsttime=true&comptype="+EgURLEncoder.encode(comptype,"UTF-8")+"&timeline="+tmp_s_timeline;

					String histLink="../monitor/dispComponentHistory.jsp?site=NULL&info=" + EgURLEncoder.encode(info,"UTF-8") + "&test=" + test + "&component=" +EgURLEncoder.encode(server+":"+comptype,"UTF-8");

					String ddLink = response.encodeURL("../monitor/componentMeasureTabSet.jsp?")+"type="+EgURLEncoder.encode(comptype,"UTF-8")+"&site=NULL&host="+hostname+"&port="+port+"&info="+EgURLEncoder.encode(info,"UTF-8")+"&state="+state+"&messy="+measureList+"&test="+test+"&msmt_host="+EgURLEncoder.encode(mHost,"UTF-8")+"&clicked=dd&from=measurespage";
					ddLink+="&starttime="+EgURLEncoder.encode(starttime,"UTF-8")+"&starthr="+EgURLEncoder.encode(start_hr,"UTF-8")+"&startmin="+EgURLEncoder.encode(startmin_h,"UTF-8")+"&endtime="+EgURLEncoder.encode(endtime,"UTF-8")+"&endhr="+EgURLEncoder.encode(end_hr,"UTF-8")+"&endmin="+EgURLEncoder.encode(endmin_h,"UTF-8")+"&appendHostinKey=true";
					
					String compgraphLink = "/final/monitor/dispComponentGraphs.jsp?site=NULL&host=" +hostname+"&port="+port+"&layer="+layer+"&test="+test+"&comptype="+EgURLEncoder.encode(comptype,"UTF-8");


					String  graphLink = compgraphLink + "&info=*"+EgURLEncoder.encode(info,"UTF-8")+ "&gtype=1&measure="+measureList+ "&msmt_host="+	EgURLEncoder.encode(mHost,"UTF-8")+"&state="+state+"&timeLine="+tmp_s_timeline;

					String compsummarygraphLink ="../monitor/dispComponentSummaryGraphs.jsp?site=NULL&host=" +EgURLEncoder.encode(hostname,"UTF-8")+ "&port=" + port + "&layer=" +EgURLEncoder.encode(layer,"UTF-8")+ "&test=" + test + "&comptype="+EgURLEncoder.encode(comptype,"UTF-8");

					String summarygraphLink = compsummarygraphLink + "&info=*"+EgURLEncoder.encode(info,"UTF-8")+ "&gtype=1&measure="+measureList+ "&msmt_host="+	EgURLEncoder.encode(mHost,"UTF-8")+"&state="+state;

					String comptrendgraphLink ="../monitor/dispComponentTrendGraphs.jsp?site=NULL&host=" +EgURLEncoder.encode(hostname,"UTF-8")+ "&port=" + port + "&layer=" +EgURLEncoder.encode(layer,"UTF-8")+ "&test=" + test + "&comptype="+EgURLEncoder.encode(comptype,"UTF-8");

					String trendgraphLink = comptrendgraphLink + "&info=*"+EgURLEncoder.encode(info,"UTF-8")+ "&gtype=1&measure="+measureList+ "&msmt_host="+	EgURLEncoder.encode(mHost,"UTF-8")+"&state="+state;

					String  aggLink="../monitor/dispAggComponentGraphs.jsp?site=NULL&host=" +EgURLEncoder.encode(hostname,"UTF-8")+ "&port=" + port + "&layer=" +EgURLEncoder.encode(layer,"UTF-8")+ "&test=" + test + "&comptype="+EgURLEncoder.encode(comptype,"UTF-8");

					String historyLink = histLink+"&measure="+measureList;

					String feedLink="/final/monitor/dispComponentFeedback.jsp?site=NULL&trgthost="+hostname+"&port="+port+"&test="+test+"&comptype="+EgURLEncoder.encode(comptype,"UTF-8");

					MonitorDAOFactory	monitorDaoFactory = MonitorDAOFactory.getInstance();
					EgServerDashBoardDAO dashboardDAO=monitorDaoFactory.getServerDashBoardDAO();
					String portForMsmtTime="NULL";
					String key=componentName+":"+portForMsmtTime+":"+test+":"+mHost;
					String msmtTimeStr=dashboardDAO.getMeasureLastMsmtTime(key,info);
					String msmtTimeStr2="";
					try
					{
						SimpleDateFormat df=new SimpleDateFormat("MM/dd/yyyy*HH:mm:ss");
						SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
						java.util.Date msd=sdf2.parse(msmtTimeStr);
						msmtTimeStr2 =df.format(msd);
					}
					catch(Exception eexx)
					{
						System.out.println("Exception--->"+eexx.getMessage());
						eexx.printStackTrace();
					}

					String feedbackLink =  feedLink+"&info=*"+EgURLEncoder.encode(info,"UTF-8")+ "&measure="+measureList+ "&msmttime="+msmtTimeStr2 +"&msmt_host="+EgURLEncoder.encode(mHost,"UTF-8");

					String aggregateLink=aggLink + "&info=*"+EgURLEncoder.encode(info,"UTF-8")+ "&gtype=1&measure="+measureList+ "&msmt_host="+	EgURLEncoder.encode(mHost,"UTF-8")+"&state="+state;

					String measureLink = response.encodeURL("../monitor/componentMeasureTabSet.jsp?")+"type="+EgURLEncoder.encode(comptype,"UTF-8")+"&site=NULL&host="+hostname+"&port="+port+"&info="+EgURLEncoder.encode(info,"UTF-8")+"&state="+state+"&messy="+measureList+"&from=measurespage&dd_disp=yes&test="+test+"&msmt_host="+EgURLEncoder.encode(mHost,"UTF-8");
					measureLink+="&starttime="+EgURLEncoder.encode(starttime,"UTF-8")+"&starthr="+EgURLEncoder.encode(start_hr,"UTF-8")+"&startmin="+EgURLEncoder.encode(startmin_h,"UTF-8")+"&endtime="+EgURLEncoder.encode(endtime,"UTF-8")+"&endhr="+EgURLEncoder.encode(end_hr,"UTF-8")+"&endmin="+EgURLEncoder.encode(endmin_h,"UTF-8")+"&appendHostinKey=true";
					graphQs = measureLink;
					ddQs = ddLink;
					//componentMeasureTabset
					if((insideview_vm_list!=null && insideview_vm_list.contains(info.toLowerCase())) && (poweredOn!=null && poweredOn.equals("yes")))
					{
						String strShownInsideview=egEx.getValue("User_VM_Search","shownInsideview");
						if(strShownInsideview!=null && strShownInsideview.trim().length()>0 && (strShownInsideview.equalsIgnoreCase("true") || strShownInsideview.equalsIgnoreCase("yes")))
						{
							s_qryString ="../monitor/egInsideviewDashboard.jsp?getFor=measure&comp="+EgURLEncoder.encode(server,"UTF-8")+"&comptype="+comptype+"&Layer="+layer_for_url+"&info="+EgURLEncoder.encode(info,"UTF-8")+"&compName="+EgURLEncoder.encode(compNameWithType,"UTF-8");
						}
					}
					String dec_info="";
					if(info!=null && info.trim().length()>0)
						dec_info = com.egurkha.util.StringUtils.encodeSecurityChar(info);
					String tmpLayerQs="site|eq|NULL^qctr|eq|2^fromalarm|eq|1^fromPage|eq|vm^newWindow|eq|false^comptype|eq|"+comptype+"^host|eq|"+server+"^layer|eq|"+layer+"^test|eq|"+test+"-"+dec_info+"^msmt_host|eq|^isFromVmHost|eq|true^gotoLayerModel|eq|true^tab|eq|LayerModel^toDashBoardLayer|eq|true^isTotalVm|eq|null^poweredOnVMs|eq|null^tstate|eq|GOOD^priority|eq|10^searchValueandType|eq|"+searchType+" "+s_user+"^searchString|eq|"+searchType+" "+s_user;
					if(state!=null && !state.equalsIgnoreCase("null") && state.trim().length()>0)
						tmpLayerQs = tmpLayerQs +"^state|eq|"+state;

					rowData = new JSONObject();
					if(showTreeView){
						rowData.put("VMNAME","");
					}
					else{
						linkObj = new JSONObject();
						linkObj.put("VMNAME_LINK_TEXT",info);
						linkObj.put("VMNAME_LINK_URL",s_qryString);
						rowData.put("VMNAME",linkObj);
					}

					linkObj = new JSONObject();
					linkObj.put("VMCOMPNAME_LINK_TEXT",componentName);
					if(showTreeView)
						linkObj.put("VMNAME_LINK_URL",s_qryString);
					else
						linkObj.put("VMCOMPNAME_LINK_URL",EgURLEncoder.encode(tmpLayerQs,"UTF-8"));
					rowData.put("VMCOMPNAME",linkObj);
					rowData.put("VMCOMPTYPE",mappingInfo.getComponentTypeDescription(comptype));

					boolean showDDIcon = false;
					String msmtHost = "";
					String hostName = server.substring(0,server.lastIndexOf(":"));

					if(ddEnabled)
					{
						Vector measures = new Vector();
						if(strVarArr!=null && strVarArr.length>0)
						{
							for(int sva=0; sva<strVarArr.length; sva++)
							{
								if(strVarArr[sva].startsWith(ddTest))
								{
									String meas = strVarArr[sva].substring(strVarArr[sva].indexOf(":")+1,strVarArr[sva].length());
									if(meas!=null && !measures.contains(meas))
									{
										measures.add(meas);
									}
								}
							}
						}
						if(dummyTestArr!=null && dummyTestArr.length>0)
						{
							for(int dta=0; dta<dummyTestArr.length; dta++)
							{
								if(dummyTestArr[dta].startsWith(ddTest))
								{
									String meas = dummyTestArr[dta].substring(dummyTestArr[dta].indexOf(":")+1,dummyTestArr[dta].length());
									if(meas!=null && !measures.contains(meas))
									{
										measures.add(meas);
									}
								}
							}
						}
						if(measures.contains(ddMeasure))
						{
							ArrayList DDEnabledList=tt.DefaultDDEnabledTypes();
							boolean isRemoteDetailed = false;
							if(DDEnabledList!=null && DDEnabledList.size()>0 && DDEnabledList.contains(comptype))
							{
								isRemoteDetailed=true;
							}
							else
							{
								isRemoteDetailed = tt.getRemoteDetailed();
							}

							boolean isAgentlessServer = false;
							if (!isRemoteDetailed)
								isAgentlessServer = agentInfo.isAgentlessServer(hostName, comptype);

							//Fix to hide DD icon based on Ini entry
							if(excludeDDTestList != null && excludeDDTestList.size() > 0 && excludeDDTestList.contains(ddTest))
							{
								showDDIcon = false;
							}
							else if (ui.getVirtualServerTypes().contains(comptype)|| !isAgentlessServer || specsInfo.isTestExternal(ddTest))
							{
								showDDIcon = true; //dd.ShowDDData("NULL",hostName,ddTest,ddMeasure,"+",msmtHost);
							}
						}
					}

					if(state!=null&&state.equals("any"))
					{       
						if(poweredOn!=null&&poweredOn.equals("yes"))
						{
							linkObj = new JSONObject();
							linkObj.put("POWERED_LINK_TEXT","Powered On");
							linkObj.put("POWERED_LINK_TITLE",mappingInfo.getTextDescription("monitor.common.Powered On"));
							rowData.put("POWEREDON",linkObj);
						}
						else
						{
							linkObj = new JSONObject();
							linkObj.put("POWERED_LINK_TEXT","Powered Off");
							linkObj.put("POWERED_LINK_TITLE",mappingInfo.getTextDescription("monitor.common.Powered Off"));
							rowData.put("POWEREDON",linkObj);
						}
					}
					rowData.put("TIME",dispDate);
					linkObj = new JSONObject();
					linkObj.put("VMGRAPH_LINK_IMAGE","vmgraphicon");
					linkObj.put("VMGRAPH_LINK_URL",graphQs);
					linkObj.put("VMGRAPH_ALT",mappingInfo.getTextDescription("monitor.common.GRAPH")); 
					rowData.put("VMGRAPH",linkObj);
					if(showDDIcon)
					{
						linksMap.put(test+"#"+measureList+"#"+info+"#"+mHost+"#"+hostname+"compddlink", compddLink);
						linkObj = new JSONObject();
						linkObj.put("VMDD_LINK_IMAGE","vmddicon");
						linkObj.put("VMDD_LINK_URL",ddQs);
						linkObj.put("VMDD_ALT",mappingInfo.getTextDescription("monitor.vmsearch.DIAGNOSIS"));
						rowData.put("VMDD",linkObj);
					}
					// Rights Symbol replace
					info = com.egurkha.util.StringUtils.replace(info,"\u00AE","|rights|");

					linksMap.put(test+"#"+measureList+"#"+info+"#"+mHost+"compfeedbacklink", aggregateLink);

					linksMap.put(test+"#"+measureList+"#"+info+"#"+mHost+"#"+hostname+"compgraphlink", graphLink);

					linksMap.put(test+"#"+measureList+"#"+info+"#"+mHost+"compsummarygraphlink", summarygraphLink);

					linksMap.put(test+"#"+measureList+"#"+info+"#"+mHost+"comptrendgraphlink", trendgraphLink);

					linksMap.put(test+"#"+measureList+"#"+info+"#"+mHost+"comphistorylink", historyLink);

					linksMap.put(test+"#"+measureList+"#"+info+"#"+mHost+"compfeedbacklink", feedbackLink);

					rowData.put("leaf",true);
					if(showTreeView){
						dataArr.add(rowData);
					}
				}
				session.setAttribute("Tablinks", linksMap);
				if(((count_powerOff)==v_data.size()) && flage_header)
				{
					//No powered on VMs found for the current selection
					dataJson.put("searchmessage",mappingInfo.getTextDescription("monitor.vmsearch.No powered on VMs found for the current selection"));
				}
				if(showTreeView){
					String qryString="/final/monitor/eGVMDetails.jsp?comptype="+(String)v_data.get(2)+"&info="+EgURLEncoder.encode("*"+userinfo, "UTF-8")+"&username="+EgURLEncoder.encode(s_user,"UTF-8")+"&searchval="+EgURLEncoder.encode(s_user,"UTF-8")+"&searchtype="+EgURLEncoder.encode(searchType, "UTF-8")+"&searchuser="+EgURLEncoder.encode(searchType+" "+s_user,"UTF-8");

					if((s_timeline!=null && !s_timeline.equalsIgnoreCase("null") && s_timeline.trim().length()>0) && (eg_stdate!=null && !eg_stdate.equalsIgnoreCase("null") && eg_stdate.trim().length()>0))
					{
						timelineParams = getTimelineParams(s_timeline);
						qryString = qryString + "&eg_timeline="+EgURLEncoder.encode(s_timeline,"UTF-8")+"&eg_stdate="+EgURLEncoder.encode((String)timelineParams.get(0),"UTF-8")+"&eg_enddate="+EgURLEncoder.encode(vmEndTime,"UTF-8")+"&starthr="+EgURLEncoder.encode(start_hr,"UTF-8")+"&endhr="+EgURLEncoder.encode(vmEndHr,"UTF-8")+"&startmin_h="+EgURLEncoder.encode(startmin_h,"UTF-8")+"&endmin_h="+EgURLEncoder.encode(endmin_h,"UTF-8");

						if(!s_timeline.equals("Recent"))
							qryString = qryString + "&eg_tbound="+EgURLEncoder.encode(eg_tbound,"UTF-8");
						else if(state!=null && !state.equalsIgnoreCase("null") && state.trim().length()>0)
							qryString = qryString +"&state="+state;
					}
					if(sb != null)
						qryString += "&componentList="+sb.toString().substring(1); 

					linkObj = new JSONObject();
					linkObj.put("VMNAME_LINK_TEXT",info);
					linkObj.put("VMNAME_LINK_URL",qryString);
					parentData.put("VMNAME",linkObj);
					parentData.put("VMCOMPNAME","");
					parentData.put("VMCOMPTYPE","");
					parentData.put("POWEREDON","");
					parentData.put("TIME","");
					parentData.put("VMGRAPH","");
					parentData.put("VMDD","");
					parentData.put("children",dataArr);
					parentDataArr.add(parentData);
				}
				else
					parentDataArr.add(rowData);
			}//endofwhile
		  }//endofif
		ftitle=s_title.toString();
		JSONObject treeJson = new JSONObject();
		treeJson.put("expanded",true);
		treeJson.put("text",".");
		treeJson.put("children",parentDataArr);
		String mngrIProc = new IniFile(new com.eg.EgConfigInfo("manager").getDbFile()).getValue("DB_PROPERTIES","ManagerUrl");
		String htmlFileName = mngrIProc + "/final/monitor/eGVMigrationPrint.jsp;jsessionid="+session.getId()+"?";
		String csvFilePath = "/final/monitor/eGVMigrationPrint.jsp;jsessionid="+session.getId()+"?";
		session.setAttribute("USERSEARCH_PDF_URL",htmlFileName);
		session.setAttribute("REPORT_NAME","userSearch");
		session.setAttribute("title","User Search Details");
		session.setAttribute("frmPage","userSearch");

		dataJson.put("searchmessage",mappingInfo.getTextDescription("monitor.vmsearch.No  data available for the current search"));
		dataJson.put("searchtitle",ftitle);
		dataJson.put("searchtype","vmsearch");
		dataJson.put("screenLabels",screenLabels);
		dataJson.put("usersdetailsperpage",usersdetailsperpage);
		dataJson.put("stateGenOpts",stateGenOpts);
		dataJson.put("timeLineList",historyTmeLineList);
		if(dateTime!=null && dateTime.size()>0)
			dataJson.put("dateTime",dateTime);
		dataJson.put("totalPages",pageSize);
		dataJson.put("recPerPage",recPerPage);
		dataJson.put("totalRecCount",totalRec);
		int increment = Integer.parseInt(usersdetailsperpage);
		int optionCount = 5;
		String rowsPerPage[] = new String[optionCount];
		for(int i=0;i<optionCount;i++)
		{
			int j=i+1;
			rowsPerPage[i] = "" + j* increment;
		}
		JSONArray eventsPerPageJson = new JSONArray();
		String rowCount = request.getParameter("eventsPerPage");
		if(rowCount == null || rowCount.equals("null"))
			rowCount = rowsPerPage[0];
		for(int i=0;i<rowsPerPage.length;i++)
		{
			JSONObject eventsPerPageJsonObj = new JSONObject();
			eventsPerPageJsonObj.put("displayField",rowsPerPage[i]);
			eventsPerPageJsonObj.put("valueField",rowsPerPage[i]);
			eventsPerPageJson.add(eventsPerPageJsonObj);
		}
		dataJson.put("eventsPerPage",eventsPerPageJson);
		dataJson.put("eventsPerPageValue",rowCount);
		dataJson.put("columns",columnDetails);
		rootJson.put("SEARCH_DATA",dataJson);
		rootJson.put("searchtree",treeJson);
		rootJson.put("csvFilePath",csvFilePath);

		out.println(rootJson.toString());
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}
%>
