<%
/*******************************************************************************
*	File name		: MsSqlSysProcesses.jsp
*	Purpose			: To show the Sql & Sybase sysprocesses along with its details
*	Version			: eG 5.3.
*	Creation Date	: 15/03/2011
*	Modifed			: 15/07/2011	- Support Sybase sysprocess
*	Modifed			: 10/08/2011	- To Provide Support for Oracle Sessions & Oracle User connections
******************************************************************************/
%>
<%@ taglib prefix="egui" uri="../WEB-INF/eg-taglib.tld" %>
<%@ include file = "EgMonitorTracker.jsp" %>
<%@ page errorPage = "EgProbsM.jsp" %>
<%@ page import = "com.egurkha.util.StringUtils"%>
<%@ page import = "com.egurkha.util.EgSorter" %>
<%@ page import = "com.egurkha.ini.IniUI" %>
<%@ page import = "com.eg.EgInstallInfo"%>
<%@ page import = "com.eg.EgURLEncoder"%>
<%@ page import = "com.eg.IniFile"%>
<%@ page import = "com.egurkha.util.DateUtil"%>
<%@ page import = "com.egurkha.util.DateTimeTokenizer" %>
<%@ page import = "java.text.SimpleDateFormat"%>
<%@ page import = "com.eg.EgDbInfo" %>
<%@ page import = "java.util.*" %>
<%@ page session = "true" %>
<%@ page import = "com.eg.EgSpecsInfo" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="net.sf.json.*"%>

<%!
	public ArrayList arrayListSort(ArrayList arrList, int cols, int totalCols , String testName )
	{
		ArrayList result = new ArrayList();
		if (arrList==null || arrList.size()==0)
			return result;

		ArrayList dataTypeList = new ArrayList();
		dataTypeList.add("ZERO");
		if(testName.equals("SybSysProcessTest"))
		{
			dataTypeList.add("Long#asc");		//spid
			for(int k=0;k<5;k++){
				dataTypeList.add("String#asc"); //status , user , db , hostname , program_name
			}
			for(int k=0;k<5;k++){
				dataTypeList.add("Long#asc");	//blocking_spid , time_blocked, cpu(ticks) , physical_io, memusage
			}
			dataTypeList.add("String#asc");		//command
		}
		else if(testName.equals("MsSqlSysProcsTest") || testName.equals("MsSqlConnsTest"))
		{
			dataTypeList.add("Long#asc");		//SPID
			int times = 6;
			if(totalCols == 14) times = 5; 
			for(int k=0;k<times;k++){
				dataTypeList.add("String#asc");	//[User],DB,HOST,Program_Name,Login_Name,Last Execution
			}
			dataTypeList.add("Long#desc");		//Waittime
			dataTypeList.add("String#asc");		//Waittype
			dataTypeList.add("Long#asc");		//SPID Blocking
			for(int k=0;k<4;k++){
				dataTypeList.add("Long#desc");	//Trans, Physical_io, Mem_usage, Sleeptime
			}
			dataTypeList.add("String#asc");		//Last command
		}
		else if(testName.equals("OraSessionTest") || testName.equals("OraConnectionsTest"))
		{
			dataTypeList.add("Long#asc");
			dataTypeList.add("Long#asc");
			int times = 6;
			if(testName.equals("OraConnectionsTest"))
				times = 5;
			for(int k=0;k<times;k++){
				dataTypeList.add("String#asc");
			}
			dataTypeList.add("Long#asc");
			dataTypeList.add("String#asc");
			dataTypeList.add("String#asc");
		}

		if(dataTypeList.size() == 1)
			return result;

		String requiredType = (String) dataTypeList.get(cols);
		String colType = requiredType.substring(0,requiredType.indexOf("#"));
		String order =requiredType.substring(requiredType.indexOf("#")+1);

		EgSorter egSort = new EgSorter(arrList,totalCols);
		ArrayList sortList = (ArrayList)egSort.sort(cols,colType,order);
		return sortList;
	}
%>

<%
	String ua = request.getHeader( "User-Agent" );
	boolean isFirefox = ( ua != null && ua.indexOf( "Firefox/" ) != -1 );
	boolean isFirefox2 = ( ua != null && ua.indexOf( "Firefox/2" ) != -1 );
	boolean isMSIE6 = ( ua != null && ua.indexOf( "MSIE 6.0" ) != -1 );
	boolean isMSIE7 = ( ua != null && ua.indexOf( "MSIE 7.0" ) != -1 );
	boolean isChrome = ( ua != null && ua.indexOf( "Chrome/" ) != -1 );

try
{
	response.setHeader("Cache-Control","no-store");
	response.setHeader("Pragma","no-cache");
	response.setDateHeader("Expires", -1);
	response.setContentType("application/json");

	String isTable="";
	isTable = isFirefox ? "Auto" : "Fixed";
	String s_mes = null;
	String s_st_time = "";
	String s_st_hr = "0";
	String s_en_time = "";
	String s_en_hr = "0";
	String s_st_min = "0";
	String s_end_min = "0";
	String s_info=request.getParameter("info");
	String tmp_s_info=com.egurkha.util.StringUtils.replace(s_info,"\\\\","\\");
	String s_first_time = request.getParameter("firsttime");
	String s_flag = request.getParameter("flag");
	String s_site=request.getParameter("site");
	String s_host=request.getParameter("host");
	String s_port=request.getParameter("port");
	String s_test=request.getParameter("test");
	String s_date=request.getParameter("sdate");
	String s_measure=request.getParameter("s_measure");
	String s_msmt_host=request.getParameter("msmt_host");
	String sortOrder=request.getParameter("sortby");
	String filterdate = request.getParameter("filterdate");
		
	boolean isSqlConnTest = false; 
	boolean isSybaseTest = false;
	boolean isOraSessionTest = false;
	boolean isOraConnectionsTest = false;
	boolean isSqlSysProcessTest = false;
	if(s_test!=null)
	{
		if(s_test.equalsIgnoreCase("MsSqlConnsTest"))
			isSqlConnTest = true;
		else if(s_test.equalsIgnoreCase("SybSysProcessTest"))
			isSybaseTest = true;
		else if(s_test.equalsIgnoreCase("OraSessionTest"))
			isOraSessionTest = true;
		else if(s_test.equalsIgnoreCase("OraConnectionsTest"))
			isOraConnectionsTest = true;
		else
			isSqlSysProcessTest = true;
	}
	JSONObject json = new JSONObject();
	JSONArray jsonArray = new JSONArray();

	if(s_info == null)
		s_info = "NONE";
	
	EgDbInfo dbInfo = new EgDbInfo("manager");
	egurkha.TransactionBean trans = new egurkha.TransactionBean();
	com.egurkha.monitor.EgDiagnosisBean dd = new com.egurkha.monitor.EgDiagnosisBean();
	trans.createFile("eg_format.ini");
	String strValue=trans.getStrValue("DDD_TESTS", s_test);
	String [] colNames = dbInfo.getColumnNamesForTest(s_test+"_DDD");
	ArrayList colNames_ar=new ArrayList(Arrays.asList(colNames));

	UserRegistry registry = UserRegistry.getInstance();
	User user = registry.getUser(session.getId());
	String selectedProcessName  = request.getParameter("selectedProcessName");
	if(s_date!=null && s_date.length() > 0)
	{
		s_st_time=request.getParameter("s_st_time");
		s_en_time=request.getParameter("s_en_time");
		s_st_hr=request.getParameter("s_st_hr");
		s_en_hr=request.getParameter("s_en_hr");
		s_st_min=request.getParameter("s_st_min");
		s_end_min=request.getParameter("s_end_min"); 
	}

	Vector v_measures = new Vector();
	String uiFile = "eg_ui.ini";
	String s_iniFile_dir = EgInstallInfo.getInstallDir()+ "/manager/config/";
	IniFile egUI = new IniFile(s_iniFile_dir + uiFile);
	String currentDateFormat = egUI.getValue("CURRENT_DATE_FORMAT","selectedDateFormat");
	String dateFormatForDatePick = (String)user.getUserDateFormat();
	String comFormat = null;
	String endDateTime ="";
	String startDateTime ="";
	String colNamesForQuery = "";
	String lastMesTime = request.getParameter("time");
	String dummylastMesTime = lastMesTime;
	String firstOptionValue=null;
	String s_st_sec="00";

	// Table and cell Width for Browser Compatability
	String wd = "";
	String wdtd = "";
	String divHeight = "";

	ArrayList a_results = new ArrayList();
	if((s_st_time!=null && s_st_time.equals("")) || (s_en_time!=null && s_en_time.equals("")))
	{
		String tempp = currentDateFormat +" HH:mm";

		SimpleDateFormat sdf = new SimpleDateFormat(tempp);
		java.util.Date tmpday = new java.util.Date();
		endDateTime = sdf.format(tmpday);
		java.util.Date startDate2 = new java.util.Date();
		startDate2.setTime(tmpday.getTime() - (3600 * 1000));
		startDateTime = sdf.format(startDate2);

		s_st_time = startDateTime.substring(0,startDateTime.lastIndexOf(" "));
		s_en_time = endDateTime.substring(0,endDateTime.lastIndexOf(" "));

		s_st_hr = startDateTime.substring(startDateTime.lastIndexOf(" ")+1,startDateTime.indexOf(":"));
		s_en_hr = endDateTime.substring(endDateTime.lastIndexOf(" ")+1,endDateTime.indexOf(":"));

		s_st_min = startDateTime.substring(startDateTime.indexOf(":")+1);
		s_end_min = endDateTime.substring(endDateTime.indexOf(":")+1);
	}

	boolean filterDateFlag = false;
	String fdate="";
	if(filterdate!=null && filterdate.length()>4)
	{
		String tmpDate="";
		if(filterdate.indexOf(":")>-1)
		{
			fdate=filterdate.substring(0, filterdate.lastIndexOf(DateTimeTokenizer.BLANK_SPACE));
		}

		int shr = Integer.parseInt(filterdate.substring(filterdate.lastIndexOf(DateTimeTokenizer.BLANK_SPACE)+1, filterdate.indexOf(":")));
		int st_min=Integer.parseInt(filterdate.substring(filterdate.indexOf(":")+1,filterdate.lastIndexOf(":")));
		int st_sec=Integer.parseInt(filterdate.substring(filterdate.lastIndexOf(":")+1));
		s_st_time=filterdate.substring(0, filterdate.indexOf(DateTimeTokenizer.BLANK_SPACE));
		s_st_hr = String.valueOf(shr);
		s_st_min = String.valueOf(st_min);
		s_en_hr=String.valueOf(shr);
		s_st_sec=String.valueOf(st_sec);

		if(!DateUtil.isDate(fdate,"MM/dd/yyyy"))
		{
			fdate = DateUtil.changeDateFormat(fdate,currentDateFormat,"MM/dd/yyyy");
		}
		filterDateFlag=true;
	}

	EgTimeZoneConverter startConverter = new EgTimeZoneConverter(s_st_time+" "+s_st_hr+":"+s_st_min+":00",user,true);
	String ss_st_time = (String)startConverter.getDate(false);
	String ss_st_hr = startConverter.getHour();
	String ss_st_min = startConverter.getMinute();

	EgTimeZoneConverter endConverter = new EgTimeZoneConverter(s_en_time+" "+s_en_hr+":"+s_end_min+":00",user,true);
	String se_en_time = (String)endConverter.getDate(false);
	String ss_en_hr = endConverter.getHour();
	String ss_end_min = endConverter.getMinute();

	if(DateUtil.isDate(s_st_time,"MM/dd/yyyy"))
		comFormat = "MM/dd/yyyy";
	else if(DateUtil.isDate(s_st_time,dateFormatForDatePick))
		comFormat = dateFormatForDatePick;
	else
		comFormat = currentDateFormat;

	//String ss_st_time = s_st_time;

	if(!DateUtil.isDate(ss_st_time,"MM/dd/yyyy"))
		ss_st_time = DateUtil.changeDateFormat(ss_st_time,comFormat,"MM/dd/yyyy");
	
	if(DateUtil.isDate(se_en_time,"MM/dd/yyyy"))
		comFormat = "MM/dd/yyyy";
	else if(DateUtil.isDate(se_en_time,dateFormatForDatePick))
		comFormat = dateFormatForDatePick;
	else
		comFormat = currentDateFormat;

	//String se_en_time = s_en_time;

	if(!DateUtil.isDate(se_en_time,"MM/dd/yyyy"))
		se_en_time = DateUtil.changeDateFormat(se_en_time,comFormat,"MM/dd/yyyy");

	if (!strValue.equals(""))
	{
		if(colNames != null && colNames.length > 0)
		{
			for(int r=0; r<colNames.length; r++)
			{
				colNamesForQuery +=colNames[r] + ",";
			}
			if(colNamesForQuery.endsWith(","))
			{
				colNamesForQuery = colNamesForQuery.substring(0, (colNamesForQuery.length()-1));
			}
		}
	}

	if(s_first_time != null && !s_first_time.equals("") && s_first_time.equals("true"))
	{
		if(s_test != null && !s_test.equals(""))
			v_measures=dd.populateMeasures(s_test);
	}

	s_mes = (String) v_measures.get(0);
	String[] Desc = dd.getDescription(s_test,s_mes);
	ArrayList Desc_ar=new ArrayList(Arrays.asList(colNames));
	if(s_measure!=null && s_measure.length()>0)
	{
		if(filterDateFlag)
		{
			ArrayList al = dd.getDiagnosisDataForDate(s_host, s_port,s_site, s_test, s_measure, tmp_s_info, ss_st_time, se_en_time, ss_st_hr, ss_en_hr,ss_st_min,ss_end_min,s_st_sec,s_flag,colNamesForQuery,s_msmt_host,fdate);
			if(al != null && al.size() > 0)
				a_results.addAll(al);
		}
		else
		{
			ArrayList al = dd.getDiagnosisData(s_host, s_port,s_site, s_test, s_measure, tmp_s_info, ss_st_time, se_en_time, ss_st_hr, ss_en_hr,ss_st_min+":"+s_st_sec,ss_end_min+":59",s_flag,colNamesForQuery,s_msmt_host);
			if(al != null && al.size() > 0)
				a_results.addAll(al);
		}
	}
	
	Hashtable mappingTable = new Hashtable();
	ArrayList columnList = new ArrayList();

	if(Desc!=null && s_measure!=null)
	{
		String colStr = Desc[0].substring(Desc[0].indexOf(":")+1);
		StringTokenizer st = new StringTokenizer(colStr);
		int count = st.countTokens();
		String[] col = new String[15];
		if(isSqlConnTest)
			col = new String[14];
		else if(isSybaseTest)
			col = new String[12];
		else if(isOraSessionTest)
			col = new String[11];
		else if(isOraConnectionsTest)
			col = new String[10];
		for(int j=0;j<count;j++)
		{
			col[j] = st.nextToken();
			columnList.add(col[j]);
			mappingTable.put(col[j],new Integer(j+1));
		}
	}

	String [] test_array=trans.getVariables("OS_DEPENDENT_DD_TEST");
	ArrayList osDepTestList = new ArrayList();
	if(test_array!=null && test_array.length>0)
		osDepTestList = new ArrayList(Arrays.asList(test_array));

	String s_ct=request.getParameter("comptype");
	String os_name="";
	boolean isOSDepTest=false;
	if(osDepTestList!=null && s_test!=null && osDepTestList.contains(s_test))
	{
		os_name=(String)Server.getOSName(s_ct,s_host);
		if(os_name!=null && os_name.trim().length()>0)
		{
			os_name=os_name.toLowerCase();
			isOSDepTest = true;
		}
	}

	String osDepColumn  = null;
	if(isOSDepTest)
	{
		if(os_name!=null && os_name.startsWith("win"))
			osDepColumn  = 	trans.getStrValue("DDD_EmptyColumns_List",s_test+"_DDD:"+s_measure+":Windows");
		else if(os_name!=null && os_name.startsWith("lin"))
			osDepColumn  = 	trans.getStrValue("DDD_EmptyColumns_List",s_test+"_DDD:"+s_measure+":Linux");
		else if(os_name!=null && os_name.startsWith("sol"))
			osDepColumn  = 	trans.getStrValue("DDD_EmptyColumns_List",s_test+"_DDD:"+s_measure+":Solaris");
	}

	String emptyColumn = trans.getStrValue("DDD_EmptyColumns_List",s_test+"_DDD:"+s_measure);

	if(osDepColumn!=null && osDepColumn.trim().length()>0)
	{
		emptyColumn=(emptyColumn==null)?"":emptyColumn;
		if(emptyColumn!=null && emptyColumn.trim().length()>0)
			emptyColumn=emptyColumn+","+osDepColumn;
		else
			emptyColumn=osDepColumn;
	}

	ArrayList emptyColumnsList = new ArrayList();
	if(emptyColumn!=null && emptyColumn.indexOf(",") > -1)
	{
		emptyColumnsList = com.egurkha.util.StringUtils.egTokenizer(emptyColumn,",",true,false,true);
	}
	ArrayList valList = new ArrayList();
	ArrayList measureTimeList = new ArrayList();
	Hashtable measure_db_time = new Hashtable();

	if(a_results.size()>0)
	{
		if(StringUtils.isNull(lastMesTime))
		{
			if(s_date!=null && s_date.length()>0)
			{
				lastMesTime= s_date;
			}
			else
				lastMesTime=(String) a_results.get(0);
		}

		for(int k=0;k<a_results.size();k=k+2)
		{
			String mesTime = (String) a_results.get(k);
			String t_mesTime = mesTime;
			EgTimeZoneConverter timeZoneConverter = new EgTimeZoneConverter(mesTime, user);
			mesTime = timeZoneConverter.getDateTime();
			measureTimeList.add(mesTime);
			measure_db_time.put(mesTime,t_mesTime);
			if(lastMesTime.equals(mesTime))
			{
				ArrayList val = (ArrayList) a_results.get(k+1);
				Set valSet = new HashSet(val);
				List listVal = new ArrayList(valSet);
				for(int j=0;j<listVal.size();j++)
				{
					ArrayList tmpArrList = (ArrayList) listVal.get(j);
					if(tmpArrList!=null && tmpArrList.size() > 0){
						valList.add(tmpArrList);
					}
				}
			}
		}
	}
	int totCols = 0;

	if(isSybaseTest)	// For Sybase System process
	{
		if(StringUtils.isNull(sortOrder)) sortOrder = "1";
		if(s_measure!=null)
		{
			totCols = 12;
		}
	}
	else if(isOraSessionTest)
	{
		if(StringUtils.isNull(sortOrder)) sortOrder = "1";
		totCols = 11;
	}
	else if(isOraConnectionsTest)
	{
		if(StringUtils.isNull(sortOrder)) sortOrder = "1";
		totCols = 10;
	}
	else	// For Ms-SQL System process
	{
		totCols = isSqlConnTest ? 14:15 ;
		if(s_measure!=null && StringUtils.isNull(sortOrder))
		{
			if(s_measure.equalsIgnoreCase("Sleeping_processes"))
				sortOrder = isSqlConnTest ? "13" : "14" ;
			else 
				sortOrder = isSqlConnTest ? "11" : "12" ;
		}
	}
	int sortColumn = Integer.parseInt(sortOrder);
	ArrayList tmpList = arrayListSort(valList,sortColumn,totCols ,s_test);

	ArrayList spidList = new ArrayList();
	Hashtable resultTable = new Hashtable();

	for(int k=0;k<tmpList.size();k++)
	{
		ArrayList arrList =  (ArrayList) tmpList.get(k);
		if(arrList!=null && arrList.size() > 0)
		{
			String spid = (String)arrList.get(0);
			spidList.add(spid);
			StringBuffer sb = new StringBuffer();
			int size = arrList.size();
			for(int y=0;y<size;y++)
			{
				String line = (String) arrList.get(y);
				if(y==(size-1))
				{
					while(line.indexOf(">")>=0)
						line = line.replaceAll(">","&gt;");
					while(line.indexOf("<")>=0)
						line = line.replaceAll("<","&lt;");
					while(line.indexOf("\"")>=0)
						line = line.replaceAll("\"","&quot");
				}
				while(line.indexOf("\\")>=0)
				{
					String before = line.substring(0,line.indexOf("\\"));
					String after = line.substring(line.indexOf("\\")+1);
					line = before +"&#092;" + after;
				}
				sb.append(line + "#~#");
			}
			String res  = (sb.toString()).substring(0,sb.length()-3);
			resultTable.put(spid,res);
		}
	}

	if(spidList.size()==0)
	{
		spidList.add("None*:*None");
		//measureTimeList.add(dummylastMesTime);
		resultTable.put("None*:*None","None");
	}

	//To avoid duplicate measurement time
	TreeSet ts = new TreeSet(measureTimeList);
	measureTimeList = new ArrayList(ts);

	if (isFirefox)
	{
		wd = "672px";
		wdtd = "683px";
		divHeight = "354px";
	}	
	else if (isChrome)
	{
		wd = "672px";
		wdtd = "683px";
		divHeight = "380px";
	}
	else
	{
		wd = "672px";
		wdtd = "683px";
		divHeight = "358px";
	}

	if(StringUtils.isNull(lastMesTime))
		lastMesTime = "Not Available";

	if(!lastMesTime.equals("Not Available")||(s_date!=null) && spidList.size()>=0 && a_results.size()>0)
	{

		String Url="";
		if(s_date==null || s_date.length()==0)
		{
			Url = "../monitor/MsSqlSysProcesses.jsp?site=" + EgURLEncoder.encode("NULL","UTF-8") + "&host=" +com.eg.EgURLEncoder.encode(s_host,"UTF-8") +"&port=" + s_port + "&test="+s_test+"&msmt_host="+com.eg.EgURLEncoder.encode(s_msmt_host,"UTF-8")+ "&from=0&flag=1&firsttime=true"+"&info="+com.eg.EgURLEncoder.encode(s_info,"UTF-8");
		}
		else
		{
			Url = "../monitor/MsSqlSysProcesses.jsp?site=" + EgURLEncoder.encode("NULL","UTF-8") + "&host=" +com.eg.EgURLEncoder.encode(s_host,"UTF-8") +"&port=" + s_port + "&test="+s_test+"&msmt_host="+com.eg.EgURLEncoder.encode(s_msmt_host,"UTF-8")+ "&from=0&flag=1&firsttime=true&sdate="+com.eg.EgURLEncoder.encode(s_date,"UTF-8")+"&s_st_time="+com.eg.EgURLEncoder.encode(s_st_time,"UTF-8")+"&s_en_time="+com.eg.EgURLEncoder.encode(s_en_time,"UTF-8")+"&s_st_hr="+com.eg.EgURLEncoder.encode(s_st_hr,"UTF-8")+"&s_en_hr="+com.eg.EgURLEncoder.encode(s_en_hr,"UTF-8")+"&s_st_min="+com.eg.EgURLEncoder.encode(s_st_min,"UTF-8")+"&s_end_min="+com.eg.EgURLEncoder.encode(s_end_min,"UTF-8")+"&info="+com.eg.EgURLEncoder.encode(s_info,"UTF-8");
		}
		JSONObject measurementTimeCombo = new JSONObject();
			measurementTimeCombo.put("type","combo");
			measurementTimeCombo.put("name","measurementtime");
			measurementTimeCombo.put("url",Url);
			
		JSONArray measureTimeArray = new JSONArray();
		for(int i=measureTimeList.size()-1;i>=0;i--)
		{
			JSONObject measureTimeObject = new JSONObject();
			String tmp = (String)measureTimeList.get(i);
			EgTimeZoneConverter egTimeZoneConverter = new EgTimeZoneConverter(tmp,user);
			String displayDateTime = egTimeZoneConverter.getDateTime();

			String db_time = (String)measure_db_time.get(tmp);
			if(db_time == null)
				db_time = tmp;
			String selected="";
			if (tmp.equals(lastMesTime))
			{
				measureTimeObject.put("name",tmp);
				measureTimeObject.put("value",db_time);
				measurementTimeCombo.put("selectedvalue",db_time);
				selected ="selected";
			}
			else
			{
				measureTimeObject.put("name",tmp);
				measureTimeObject.put("value",db_time);
				selected="";
			}
			measureTimeArray.add(measureTimeObject);
		}
		measurementTimeCombo.put("measuremtime",measureTimeArray);
		jsonArray.add(measurementTimeCombo);

		JSONObject measureCombo = new JSONObject();
		measureCombo.put("type","combo");
		measureCombo.put("name","measurement");
		measureCombo.put("url",Url);
		JSONArray measureArray = new JSONArray();
		
		for(int k=0;k<v_measures.size();k++)
		{
			JSONObject measureObject = new JSONObject();
			String tmp = (String) v_measures.get(k);
			String tmp_measure =(String) mappingInfo.getMeasureDescription(s_test,"monitor.layermodel."+tmp);
			String selected="";
			String displayValue = mappingInfo.getMeasureDescription(s_test,tmp);
			if(tmp.equals(s_measure))
			{
				measureCombo.put("selectedvalue",tmp_measure);
				measureObject.put("name",displayValue);
				measureObject.put("value",tmp_measure);
				selected="selected";
			}
			else
			{
				measureObject.put("name",displayValue);
				measureObject.put("value",tmp_measure);
				selected="";
			}
			measureArray.add(measureObject);

		}
		measureCombo.put("measurement",measureArray);
		jsonArray.add(measureCombo);

		JSONObject sortByCombo = new JSONObject();
		sortByCombo.put("type","combo");
		sortByCombo.put("name","sortby");
		sortByCombo.put("url",Url);
		if(columnList!=null && columnList.size() > 0)
		{
			JSONArray  sortByArray = new JSONArray();
			int col_Num=1;
			for(int k=0;k<columnList.size();k++)
			{
				JSONObject sortByObject = new JSONObject();
				String tmp = (String) columnList.get(k);
				String tmp_measure =(String) mappingInfo.getMeasureDescription(s_test,"monitor.layermodel."+tmp);
				if(emptyColumnsList.contains(tmp))
					continue;

				Integer indexObj = (Integer)mappingTable.get(tmp);
				String selected="";
				if(sortOrder.equals(indexObj.toString()))
				{
					//sortByCombo.put("selectedValue",tmp_measure);
					sortByCombo.put("selectedValue",""+col_Num);
					sortByObject.put("name",tmp_measure);
					sortByObject.put("value",""+col_Num);
					selected="selected";
				}
				else
				{
					sortByObject.put("name",tmp_measure);
					sortByObject.put("value",""+col_Num);
					selected="";
				}
				
				sortByArray.add(sortByObject);
				col_Num++;
			}
			sortByCombo.put("sortByOrder",sortByArray);
			sortByCombo.put("test",s_test);
			jsonArray.add(sortByCombo);
		}
		JSONObject threadName = new JSONObject();
		threadName.put("type","combo");
		threadName.put("name","processname");
		JSONArray threadNameArray = new JSONArray();

		int count =spidList.size();
		if(count==1) 
		{
			if(spidList.contains("None*:*None"))
				count=0;
		}
		
		for(int k=0;k<spidList.size();k++)
		{
			JSONObject threadNameObject = new JSONObject();
			String spid = (String) spidList.get(k);
			String optionValue = (String) resultTable.get(spid);
			if(spid.indexOf("*:*") > 0)
			{
				spid = spid.substring(0,spid.indexOf("*:*"));
			}
			String selected="";
			selected = (k==0)?"selected":"";

			if(selectedProcessName==null && selected!=null && selected.trim().length()>0)
			{
				threadName.put("selectedValue",optionValue.toString());
			}
			else if(selectedProcessName!=null && selectedProcessName.equals(optionValue.toString()))
			{
				threadName.put("selectedValue",optionValue.toString());
			}
			threadNameObject.put("title","No of Threads "+count);
			threadNameObject.put("name",spid);
			threadNameObject.put("value",optionValue.toString());
			
			threadNameArray.add(threadNameObject);
		}
		threadName.put("processname",threadNameArray);
		jsonArray.add(threadName);
		JSONObject hiddenFields = new JSONObject();
		hiddenFields.put("fdate",filterdate);
		json.put("hiddenFields",hiddenFields);
		json.put("data",jsonArray);

		if(spidList.size() >0 && spidList != null)
		{
			String firstKey = (String)spidList.get(0);
			firstOptionValue = (String)resultTable.get(firstKey);
		}
		json.put("firstOptionValue",firstOptionValue);
	}
	else
	{
		String msg = "monitor.layermodel.StackTrace not available";
		JSONObject errorMsg = new JSONObject();
		errorMsg.put("name",msg);
		errorMsg.put("value","<img src=\"eg_images/"+monitor_Skin+"/exclamation-icon.gif\" align=\"center\" border=\"0\"/>");
		json.put("errorMsg",errorMsg);
	}
	if(json!=null)
	{
		
		response.getWriter().write(json.toString());
	}
}
catch(Exception ex)
{
	System.out.println("Exception in MsSqlSysProcesses.jsp");
	ex.printStackTrace();
}
%>
