

<%@ include file="../../EgTrackerAJSON.jsp" %>
<%@ page import = "java.util.*" %>
<%@ page import = "java.lang.String" %>
<%@ page import="com.eg.EgRoleInfo"%>
<%@ page import="com.eg.EgMappingInfo"%>
<%@ page import = "com.egurkha.ini.IniUI"%>
<%@ page import="com.eg.EgName"%>
<%@ page import="com.eg.EgInstallInfo"%>
<%@ page import="com.eg.EgSpecsInfo"%>
<%@ page import="com.eg.EgThresholdInfo"%>
<%@ page import="com.eg.EgConfigInfo" %>
<%@ page import="com.eg.IniFile"%>
<%@ page import="com.egurkha.ini.*" %>
<%@ page import="com.egurkha.util.StringUtils" %>
<%@ page import = "com.eg.EgURLEncoder" %>
<%@ page import = "com.eg.EgTablesInfo" %>
<%@ page import = "egurkha.EgDbAdapterUI" %>
<%@ page import ="com.eg.EgDbAdapterTypeUI"%>
<%@ page import ="com.eg.EgServerGroupBean"%>
<%@ page import="java.sql.*" %>
<%@ page import="net.sf.json.*"%>

<%@ page language="java" contentType="application/json; charset=UTF-8" pageEncoding="UTF-8" %>
<jsp:useBean id="prgBean" scope="page" class="egurkha.EgTestParamBean"/>
<%

	JSONObject rootJson = new JSONObject();
	JSONObject screenLabels = new JSONObject();
	JSONObject hiddenObj = new JSONObject();
	JSONObject measureDataObj = new JSONObject();

	IniUI iniUI = IniUI.getInstance();
	//user language
	String userLanguage = iniUI.getDefaultLanguage();
	String ua = request.getHeader("User-Agent");
	boolean isIE6 = (ua != null && ua.indexOf("MSIE 6.0") != -1);

	if (_user != null)
	{
		userLanguage = _user.getLanguage();
	}

	//String loginId = _user.getLoginID();
	String includePage = "listSearch.jsp?title=SPECIFIC THRESHOLDS&backButton=true&subtitle=false";
	EgMappingInfo mappingInfo = new EgMappingInfo(userLanguage);
	EgSpecsInfo specsInfo = new EgSpecsInfo("manager");
	EgThresholdInfo egThresh = new EgThresholdInfo("manager");
	EgTablesInfo tablesInfo = new EgTablesInfo("manager");
	EgDbAdapterUI egDb;
	EgServerGroupBean serverGrpBean = new EgServerGroupBean();

	String[] hostTests = specsInfo.getDefaultTestsForServer("Host_system");
	String[] specDescThreshArr;
	ArrayList hostLevelTests = new ArrayList(Arrays.asList(hostTests));
	ArrayList specificDescThresh = new ArrayList();
	ArrayList tmpDescList = new ArrayList();

	String strComponentType = request.getParameter("comptype");
	String strComponentName = request.getParameter("compname");
	String o_strComponentType = request.getParameter("server");
	String strTestName = request.getParameter("test");
	String from = request.getParameter("from");
	String descriptorName = request.getParameter("descName");
	String isRefine = request.getParameter("isRefine");
	String comeFrom = request.getParameter("comeFrom");
	String isNewMon = request.getParameter("isNewMon");
	if(!StringUtils.isNull(descriptorName))
	{
		descriptorName = StringUtils.decodeSecurityChar(descriptorName);
	}


	if (!StringUtils.isNull(isNewMon))
	{
		if(!StringUtils.isNull(descriptorName) && descriptorName.equals("NONE"))
		{
			descriptorName = "";
		}
	}

	String fromMon = null;
	String strDescNameVal = null;
	String strTestState = null;
	String strLayerName = null;
	boolean isMonBack = false;
	boolean isTestWithThresholds = false;

	if(StringUtils.isNull(comeFrom))
	{
		fromMon = (String) session.getAttribute("fromMon");
		if(!StringUtils.isNull(fromMon) && fromMon.equalsIgnoreCase("yes"))
		{
			if(StringUtils.isNull(strComponentType))
			{
				strComponentType = (String) session.getAttribute("comptype");
			}
			if(StringUtils.isNull(strComponentName))
			{
				strComponentName = (String) session.getAttribute("compname");
			}
			if(StringUtils.isNull(strTestName))
			{
				strTestName = (String) session.getAttribute("testFromMon");
			}
			strTestState = (String) session.getAttribute("testState");
			strLayerName = (String) session.getAttribute("layerName");
			if(StringUtils.isNull(from))
			{
				from = (String) session.getAttribute("from");
			}
			if(StringUtils.isNull(descriptorName))
			{
				descriptorName = (String) session.getAttribute("descName");
			}
			if(!StringUtils.isNull(descriptorName) && descriptorName.equals("NONE"))
			{
				descriptorName = "";
			}

			isMonBack = true;
		}
	}

	if(!StringUtils.isNull(comeFrom))
	{
		session.removeAttribute("fromMon");
		session.removeAttribute("comptype");
		session.removeAttribute("compname");
		session.removeAttribute("testFromMon");
		session.removeAttribute("descName");
		session.removeAttribute("testState");
		session.removeAttribute("layerName");
	}

	if(isMonBack)
	{
		includePage = "listSearch.jsp?title=SPECIFIC THRESHOLDS&backButton=false&subtitle=false";
	}

	boolean isShowSpecificDesc = false;
	isShowSpecificDesc = (!StringUtils.isNull(isRefine) && isRefine.equalsIgnoreCase("true"));
	specDescThreshArr = egThresh.getDescThresholds();

	if(specDescThreshArr !=null && specDescThreshArr.length > 0)
	{
		tmpDescList = new ArrayList(Arrays.asList(specDescThreshArr));
		for(int sd = 0;sd<tmpDescList.size();sd++)
		{
			String nickWithoutPort = "";
			String nickWithNullPort = "";
			if(hostLevelTests.contains(strTestName))
			{
				if(strComponentName.indexOf(":") != -1)
				{
					nickWithoutPort = strComponentName.substring(0,strComponentName.indexOf(":"));
					nickWithNullPort = nickWithoutPort+":NULL";
				}
			}
			else
			{
				nickWithNullPort = strComponentName;
			}

			String strDescData = (String) tmpDescList.get(sd);
			if(strDescData.startsWith(strTestName+":"+nickWithNullPort))
			{
				specificDescThresh.add(strDescData.substring(strDescData.indexOf("#")+1));
			}
		}
	}

	boolean isHasSpecificData = false;
	session.setAttribute("componentType",strComponentType);
	session.setAttribute("componentName",strComponentName);
	session.setAttribute("testName",strTestName);

	IniFile defaultIni = new IniFile(EgInstallInfo.getInstallDir()+"/manager/config/eg_thresholds.ini");
	String defaultThreshold = defaultIni.getValue("DEFAULT_THRESHOLDS",strTestName);
	String specificThreshold = defaultIni.getValue("USER_THRESHOLDS",strTestName);
	String descThreshold = defaultIni.getValue("DESCRIPTOR_THRESHOLDS",strTestName);
	String iniVariable = "";
	String iniValue = "";
	if(from != null && from.equalsIgnoreCase("specificTest"))
	{
		if(descriptorName != null && !descriptorName.equals("null") && descriptorName.length() > 0)
		{
			String nickWithoutPort = "";
			String nickWithNullPort = "";
			if(hostLevelTests.contains(strTestName))
			{
				if(strComponentName.indexOf(":") != -1)
				{
					nickWithoutPort = strComponentName.substring(0,strComponentName.indexOf(":"));
					nickWithNullPort = nickWithoutPort+":NULL";
				}
			}
			else
			{
				nickWithoutPort = strComponentName;
				nickWithNullPort = strComponentName;
			}

			iniVariable = strTestName+":"+nickWithNullPort+"#"+StringUtils.encodeSecurityChar(descriptorName);
			iniValue = defaultIni.getValue("DESCRIPTOR_THRESHOLDS",iniVariable);
			if(iniValue == null || iniValue.equals("null") || iniValue.length() == 0) 
			{
				iniVariable = strTestName+":"+nickWithNullPort+"#"+descriptorName;
				iniValue = defaultIni.getValue("DESCRIPTOR_THRESHOLDS",iniVariable);
			}
			if(iniValue != null && iniValue.length() > 0 && !iniValue.equals("null"))
			{
				isHasSpecificData = true;
			}
			if(iniValue == null || iniValue.equals("null") || iniValue.length() == 0) 
			{
				iniVariable = strTestName+":"+nickWithoutPort;
				iniValue = defaultIni.getValue("USER_THRESHOLDS",iniVariable);
			}
		}
		else
		{
			String tmpCompName = "";
			if(hostLevelTests.contains(strTestName))
			{
				if(strComponentName.indexOf(":") != -1)
				{
					tmpCompName = strComponentName.substring(0,strComponentName.indexOf(":"));
				}
			}
			else
			{
				tmpCompName = strComponentName;
			}
			iniVariable = strTestName+":"+tmpCompName;
			iniValue = defaultIni.getValue("USER_THRESHOLDS",iniVariable);
			if(iniValue != null && iniValue.length() > 0 && !iniValue.equals("null"))
			{
				isHasSpecificData = true;
			}
		}

		if(iniValue == null || iniValue.equals("null") || iniValue.length() == 0) 
		{
			Vector serverGroup = new Vector();
			boolean b_portRelev = specsInfo.isPortRelevantForTest(strTestName);
			if(descriptorName != null && !descriptorName.equals("null") && descriptorName.length() > 0)
			{
				serverGroup = serverGrpBean.getServerGroup(strComponentName,strTestName,b_portRelev,descriptorName);
				if (serverGroup != null && serverGroup.size() > 0)
				{
					String thisGroup = (String) serverGroup.get(0);
					String policyName = serverGrpBean.getPolicyForTest(thisGroup,strTestName,descriptorName);

					if (policyName != null && policyName.length() > 0)
					{
						iniValue = serverGrpBean.getPolicyValue(policyName);
					}
				}

				if(iniValue == null || iniValue.equals("null") || iniValue.length() == 0) 
				{
					serverGroup = serverGrpBean.getServerGroup(strComponentName,strTestName,b_portRelev);
					if (serverGroup != null && serverGroup.size() > 0)
					{
						String thisGroup = (String) serverGroup.get(0);
						String policyName = serverGrpBean.getPolicyForTest(thisGroup,strTestName);
						if (policyName != null && policyName.length() > 0)
						{
							iniValue = serverGrpBean.getPolicyValue(policyName);
						}
					}
				}
			}
			else
			{
				serverGroup = serverGrpBean.getServerGroup(strComponentName,strTestName,b_portRelev);
				if (serverGroup != null && serverGroup.size() > 0)
				{
					String thisGroup = (String) serverGroup.get(0);
					String policyName = serverGrpBean.getPolicyForTest(thisGroup,strTestName);
					if (policyName != null && policyName.length() > 0)
					{
						iniValue = serverGrpBean.getPolicyValue(policyName);
					}
				}
			}
		}
	}
	else if(from != null && from.equalsIgnoreCase("grpRuleTest"))
	{
		Vector serverGroup = new Vector();
		boolean b_portRelev = specsInfo.isPortRelevantForTest(strTestName);
		if(descriptorName != null && !descriptorName.equals("null") && descriptorName.length() > 0)
		{
			serverGroup = serverGrpBean.getServerGroup(strComponentName,strTestName,b_portRelev,descriptorName);
			if (serverGroup != null && serverGroup.size() > 0)
			{
				String thisGroup = (String) serverGroup.get(0);
				String policyName = serverGrpBean.getPolicyForTest(thisGroup,strTestName,descriptorName);

				if (policyName != null && policyName.length() > 0)
				{
					iniValue = serverGrpBean.getPolicyValue(policyName);
				}

				if(iniValue != null && iniValue.length() > 0 && !iniValue.equals("null"))
				{
					isHasSpecificData = true;
				}

				if(iniValue == null || iniValue.equals("null") || iniValue.length() == 0) 
				{
					policyName = serverGrpBean.getPolicyForTest(thisGroup,strTestName);
					if (policyName != null && policyName.length() > 0)
					{
						iniValue = serverGrpBean.getPolicyValue(policyName);
					}
				}
			}
			else
			{
				serverGroup = serverGrpBean.getServerGroup(strComponentName,strTestName,b_portRelev);

				if (serverGroup != null && serverGroup.size() > 0)
				{
					String thisGroup = (String) serverGroup.get(0);
					String policyName = serverGrpBean.getPolicyForTest(thisGroup,strTestName);

					if (policyName != null && policyName.length() > 0)
					{
						iniValue = serverGrpBean.getPolicyValue(policyName);
					}
				}
			}
		}
		else
		{
			serverGroup = serverGrpBean.getServerGroup(strComponentName,strTestName,b_portRelev);
			if (serverGroup != null && serverGroup.size() > 0)
			{
				String thisGroup = (String) serverGroup.get(0);
				String policyName = serverGrpBean.getPolicyForTest(thisGroup,strTestName);

				if (policyName != null && policyName.length() > 0)
				{
					iniValue = serverGrpBean.getPolicyValue(policyName);
				}

				if(iniValue != null && iniValue.length() > 0 && !iniValue.equals("null"))
				{
					isHasSpecificData = true;
				}
			}
		}

		if(iniValue == null || iniValue.equals("null") || iniValue.length() == 0) 
		{
			iniValue = defaultIni.getValue("DEFAULT_THRESHOLDS",strTestName);
		}
	}
	else if(from != null && from.equalsIgnoreCase("defaultDescriptorTest"))
	{
		String strVariable = strTestName+":"+descriptorName;
		iniValue = defaultIni.getValue("DESCRIPTOR_DEFAULT_THRESHOLDS",strVariable);
		if(StringUtils.isNull(iniValue))
		{
			String[] globalVars = defaultIni.getVariables("DESCRIPTOR_DEFAULT_THRESHOLDS");
			List filterKeysList = Arrays.asList(globalVars);
			ArrayList filterKeys = new ArrayList();
			for (int fk = 0; fk < filterKeysList.size(); fk++)
			{
				String key = (String) filterKeysList.get(fk);
				if (!com.egurkha.util.StringUtils.isNull(key) && key.startsWith(strTestName+":") && key.indexOf("*") != -1)
				{
					String wildCardStr = key.substring(key.indexOf(":")+1);
					if (!com.egurkha.util.StringUtils.isNull(wildCardStr) && !filterKeys.contains(wildCardStr))
					{
						filterKeys.add(wildCardStr);
					}
				}
			}

			if (filterKeys != null && filterKeys.size() > 0)
			{
				for (int k=0;k<filterKeys.size() ;k++ )
				{
					String searchString = (String) filterKeys.get(k);
					if (com.egurkha.util.EgUtilities.isMatch(descriptorName, searchString) )
					{
						iniValue = defaultIni.getValue("DESCRIPTOR_DEFAULT_THRESHOLDS",strTestName+":"+searchString);
						break;
					}
				}
			}

			if(StringUtils.isNull(iniValue))
			{
				iniValue = defaultIni.getValue("DEFAULT_THRESHOLDS",strTestName);
			}
		}
	}
	else
	{
		iniValue = defaultIni.getValue("DEFAULT_THRESHOLDS",strTestName);
	}
	if(iniValue == null || iniValue.length() == 0)
	{
		iniValue = defaultIni.getValue("DEFAULT_THRESHOLDS",strTestName);
	}
	hiddenObj.put("componentType",strComponentType);
	hiddenObj.put("componentName",strComponentName);
	hiddenObj.put("testName",strTestName);
	hiddenObj.put("descriptorName",descriptorName);
	hiddenObj.put("from",from);
	hiddenObj.put("isRefine",isRefine);
	hiddenObj.put("old_componentType",strComponentType);
	boolean isHostTest = specsInfo.isTestRelevantForServer("Host_system",strTestName,true);
	if (isHostTest)
	{
		rootJson.put("isHostTest",true);
		screenLabels.put("hTest_msg",_mapping.getTextDescription("admin.threshold.Changes made to specific thresholds will be applied to all components with the same nick name"));
	}
	if(isMonBack)
	{
		rootJson.put("isMonBack",true);
		screenLabels.put("monitor_msg","Click here to go to the layer model");
	String qryTestVal = null;
	if(!StringUtils.isNull(descriptorName) && !descriptorName.equals("NONE"))
	{
		qryTestVal = strTestName+"- "+descriptorName;
	}
	else
	{
		qryTestVal = strTestName+"- ";
	}

		String monURL = "/final/servlet/com.eg.EgDashboardController?from=list&comptype="+EgURLEncoder.encode(strComponentType,"UTF-8")+"&host="+EgURLEncoder.encode(strComponentName,"UTF-8")+"&tab=LayerModel&comesFrom=LayerModel&site=NULL&host="+EgURLEncoder.encode(strComponentName,"UTF-8")+"&layer="+strLayerName+"&qctr=2&comptype="+EgURLEncoder.encode(strComponentType,"UTF-8")+"&fromalarm=null&msmt_host=&test="+EgURLEncoder.encode(qryTestVal,"UTF-8")+"&tstate="+strTestState+"&checkedAll=true&showDashBoardLink=true";
		session.setAttribute("monitorLastSession",monURL);
		session.setAttribute("fromAdmin","Yes");
		rootJson.put("mon_url",monURL);
	}
	if(from != null && (from.equalsIgnoreCase("specificTest") || from.equalsIgnoreCase("grpRuleTest") || from.equalsIgnoreCase("defaultTest") || from.equalsIgnoreCase("defaultDescriptorTest")) && descriptorName != null && !descriptorName.equals("null") && descriptorName.length() > 0 )
	{
			if(from != null && from.equalsIgnoreCase("specificTest"))
			{
				screenLabels.put("disp_msg","Specific thresholds for the '"+descriptorName+"' descriptor of the '"+mappingInfo.getTestDescription(strTestName)+"' test");
			}
			else if(from != null && from.equalsIgnoreCase("grpRuleTest"))
			{
				screenLabels.put("disp_msg","Group thresholds for the '"+descriptorName+"' descriptor of the '"+mappingInfo.getTestDescription(strTestName)+"' test");
			}
			else if(from != null && from.equalsIgnoreCase("defaultDescriptorTest"))
			{
				screenLabels.put("disp_msg","Default descriptor threshold settings for the '"+descriptorName+"' descriptor of the '"+mappingInfo.getTestDescription(strTestName)+"' test");
			}
			else if(from != null && from.equalsIgnoreCase("defaultTest"))
			{
				screenLabels.put("disp_msg","Default thresholds for the '"+descriptorName+"' descriptor of the '"+mappingInfo.getTestDescription(strTestName)+"' test");
			}
		screenLabels.put("disp_msg_tipHide","Click here to hide details");
		screenLabels.put("disp_msg_tipShow","Click here for more details");
		screenLabels.put("admin.threshold.Component type",_mapping.getTextDescription("admin.threshold.Component type"));
		screenLabels.put("admin.threshold.Component name",_mapping.getTextDescription("admin.threshold.Component name"));
		rootJson.put("selectedCompType",strComponentType);
		rootJson.put("selectedCompName",removeNull(strComponentName));
	}
	else
	{
		if(from != null && from.equalsIgnoreCase("specificTest"))
		{
			screenLabels.put("disp_msg","Specific thresholds for the '"+mappingInfo.getTestDescription(strTestName)+"' test of  '"+removeNull(strComponentName)+"' ("+mappingInfo.getComponentTypeDescription(strComponentType)+")");
		}
		else if(from != null && from.equalsIgnoreCase("grpRuleTest"))
		{
			screenLabels.put("disp_msg","Group thresholds for the '"+mappingInfo.getTestDescription(strTestName)+"' test of  '"+removeNull(strComponentName)+"' ("+mappingInfo.getComponentTypeDescription(strComponentType)+")");
		}
		else if(from != null && from.equalsIgnoreCase("defaultDescriptorTest"))
		{
			screenLabels.put("disp_msg","Default descriptor threshold settings for the '"+descriptorName+"' descriptor of the '"+mappingInfo.getTestDescription(strTestName)+"' test");
		}
		else if(from != null && from.equalsIgnoreCase("defaultTest"))
		{
			screenLabels.put("disp_msg","Default thresholds for the '"+mappingInfo.getTestDescription(strTestName)+"' test of  '"+removeNull(strComponentName)+"' ("+mappingInfo.getComponentTypeDescription(strComponentType)+")");
		}
	}
	if (strTestName != null && strTestName.length() > 0)
	{
		boolean heading_flag = false;
		ArrayList aListTestWithNoThresholds = new ArrayList();
		boolean cellFlag = false;
		StringTokenizer st = new StringTokenizer(iniValue,",");
		JSONArray threshValArr = new JSONArray();
		JSONArray measureValArr = new JSONArray();
		JSONObject hiddenDataObj = new JSONObject();
		rootJson.put("displayCompType", mappingInfo.getComponentTypeDescription(strComponentType));
		rootJson.put("displayCompName",removeNull(strComponentName));
		rootJson.put("displayTestName", mappingInfo.getTestDescription(strTestName));
		JSONObject toolTipMsg = new JSONObject();

		while (st.hasMoreTokens())
		{
			JSONObject measHiddenObj = new JSONObject();
			measHiddenObj.put("componentType",strComponentType);
			measHiddenObj.put("componentName",strComponentName);
			measHiddenObj.put("testName",strTestName);
			measHiddenObj.put("strTestName",strTestName);
			measHiddenObj.put("descriptorName",descriptorName);
			measHiddenObj.put("comeFrom",comeFrom);
			measHiddenObj.put("from",from);
			measHiddenObj.put("fromMon",fromMon);
			measHiddenObj.put("isRefine",isRefine);
			measHiddenObj.put("old_componentType",strComponentType);
			measHiddenObj.put("old_testName",strTestName);

			String strTok = st.nextToken();
			StringTokenizer st1 = new StringTokenizer(strTok,":");
			while (st1.hasMoreTokens())
			{
				JSONObject tmpObject = new JSONObject();
				JSONObject threshValObj = new JSONObject();


				String measure = st1.nextToken();
				String policy = st1.nextToken();
				String min = st1.nextToken();
				String max = st1.nextToken();
				String alarmPolicy = st1.nextToken();
				String lastToken = st1.nextToken();
				threshValObj.put("AlarmPolicy",alarmPolicy);
				measHiddenObj.put("alarmPolicy", alarmPolicy);
				measHiddenObj.put("measure", measure);
				
				String criticalMsg = mappingInfo.getThresholdPriorityDescription(strTestName+":"+measure+":Critical");

				if (!StringUtils.isNull(criticalMsg))
				{
					threshValObj.put("isToolTip",true);
					toolTipMsg.put("toolTipCritical",criticalMsg);
					criticalMsg = mappingInfo.getThresholdPriorityDescription(strTestName+":"+measure+":Major");

					toolTipMsg.put("toolTipMajor",criticalMsg);
				}
				else
				{
					threshValObj.put("isToolTip",false);
				}

				String absMinCritical = "-";
				String relMinCritical = "-";

				String absMinMajor = "-";
				String relMinMajor = "-";

				String absMinMinor = "-";
				String relMinMinor = "-";

				String absMaxCritical = "-";
				String relMaxCritical = "-";

				String absMaxMajor = "-";
				String relMaxMajor = "-";

				String absMaxMinor = "-";
				String relMaxMinor = "-";

				if (policy.equalsIgnoreCase("none"))
				{
					policy = "mix";
				}
				else if (policy.equalsIgnoreCase("absolute"))
				{
					if (!min.equals("-1"))
					{
						StringTokenizer stAbsMin = new StringTokenizer(min,"/");
						absMinCritical = stAbsMin.nextToken();
						absMinMajor = stAbsMin.nextToken();
						absMinMinor = stAbsMin.nextToken();
					}

					if (!max.equals("-1"))
					{
						StringTokenizer stAbsMax = new StringTokenizer(max,"/");
						absMaxCritical = stAbsMax.nextToken();
						absMaxMajor = stAbsMax.nextToken();
						absMaxMinor = stAbsMax.nextToken();
					}
				}
				else if (policy.equalsIgnoreCase("relative"))
				{
					if (!min.equals("-1"))
					{
						StringTokenizer stRelMin = new StringTokenizer(min,"/");
						relMinCritical = stRelMin.nextToken();
						relMinMajor = stRelMin.nextToken();
						relMinMinor = stRelMin.nextToken();
					}

					if (!max.equals("-1"))
					{
						StringTokenizer stRelMax = new StringTokenizer(max,"/");
						relMaxCritical = stRelMax.nextToken();
						relMaxMajor = stRelMax.nextToken();
						relMaxMinor = stRelMax.nextToken();
					}
				}
				else if (policy.equalsIgnoreCase("mix"))
				{
					Vector vec = getAbsAndRel(min);
					String absA = (String) vec.get(0);
					String relA = (String) vec.get(1);
					Vector vec1 = getAbsAndRel(max);
					String absB = (String) vec1.get(0);
					String relB = (String) vec1.get(1);

					if (!absA.equals("none"))
					{
						StringTokenizer stmixMin = new StringTokenizer(absA,"/");
						absMinCritical = stmixMin.nextToken();
						absMinMajor = stmixMin.nextToken();
						absMinMinor = stmixMin.nextToken();
					}

					if (!absB.equals("none"))
					{
						StringTokenizer stmixMax = new StringTokenizer(absB,"/");
						absMaxCritical = stmixMax.nextToken();
						absMaxMajor = stmixMax.nextToken();
						absMaxMinor = stmixMax.nextToken();
					}

					if (!relA.equals("none"))
					{
						StringTokenizer stRelMin = new StringTokenizer(relA,"/");
						relMinCritical = stRelMin.nextToken();
						relMinMajor = stRelMin.nextToken();
						relMinMinor = stRelMin.nextToken();
					}

					if (!relB.equals("none"))
					{
						StringTokenizer stmixMax = new StringTokenizer(relB,"/");
						relMaxCritical = stmixMax.nextToken();
						relMaxMajor = stmixMax.nextToken();
						relMaxMinor = stmixMax.nextToken();
					}
				}
				measHiddenObj.put("absMinCritical", absMinCritical);
				measHiddenObj.put("relMinCritical", relMinCritical);
				measHiddenObj.put("absMinMajor", absMinMajor);
				measHiddenObj.put("relMinMajor", relMinMajor);
				measHiddenObj.put("absMinMinor", absMinMinor);
				measHiddenObj.put("relMinMinor", relMinMinor);
				measHiddenObj.put("absMaxCritical", absMaxCritical);
				measHiddenObj.put("relMaxCritical", relMaxCritical);
				measHiddenObj.put("absMaxMajor", absMaxMajor);
				measHiddenObj.put("relMaxMajor", relMaxMajor);
				measHiddenObj.put("absMaxMinor", absMaxMinor);
				measHiddenObj.put("relMaxMinor", relMaxMinor);
				hiddenDataObj.put(measure,measHiddenObj);
				String minCritical = getThresholdValue("min",absMinCritical,relMinCritical);
				String minMajor = getThresholdValue("min",absMinMajor,relMinMajor);
				String minMinor = getThresholdValue("min",absMinMinor,relMinMinor);

				String maxCritical = getThresholdValue("max",absMaxCritical,relMaxCritical);
				String maxMajor = getThresholdValue("max",absMaxMajor,relMaxMajor);
				String maxMinor = getThresholdValue("max",absMaxMinor,relMaxMinor);

				if (minCritical.equals("-")  && minMajor.equals("-") && minMinor.equals("-") && 
				maxCritical.equals("-") && maxMajor.equals("-") && maxMinor.equals("-"))
				{
					aListTestWithNoThresholds.add(measure);
					cellFlag = !cellFlag;
					continue;
				}
				else
				{
					isTestWithThresholds = true;
				}


				if (!heading_flag)
				{
					heading_flag = true;
				}
				String unit = tablesInfo.getUnitForMeasure(strTestName,measure);
				tmpObject.put("measure",mappingInfo.getMeasureDescription(strTestName,measure)+"("+unit+")");

				String tmpMinThresh = "";

				if(!minCritical.equals("-") || !minMajor.equals("-") || !minMinor.equals("-"))
				{
					tmpMinThresh = minCritical+"$"+minMajor+"$"+minMinor;

				}

				tmpObject.put("minThresh",tmpMinThresh);
				String tmpMaxThresh = "";

				if(!maxCritical.equals("-") || !maxMajor.equals("-") || !maxMinor.equals("-"))
				{
										tmpMaxThresh = maxCritical+"$"+maxMajor+"$"+maxMinor;

				}
				tmpObject.put("maxThresh",tmpMaxThresh);
				tmpObject.put("alarmPolicy",alarmPolicy);
				
				String tempThresh = "";
				boolean isMinData = false;
				if(!minCritical.equals("-")  || !minMajor.equals("-") || !minMinor.equals("-"))
				{
					tempThresh += "Min|"+minCritical+"|"+minMajor+"|"+minMinor;
					isMinData = true;
				}

				if(!maxCritical.equals("-") || !maxMajor.equals("-") || !maxMinor.equals("-"))
				{
					if (isMinData)
					{
						tempThresh += "#Max|"+maxCritical+"|"+maxMajor+"|"+maxMinor;
					}
					else
					{
						tempThresh += "Max|"+maxCritical+"|"+maxMajor+"|"+maxMinor;
					}
				}
				measureDataObj.put(mappingInfo.getMeasureDescription(strTestName,measure),measure);
				threshValObj.put("Measure",mappingInfo.getMeasureDescription(strTestName,measure)+"  ("+unit+")");
				threshValObj.put("Threshold",tempThresh);
				measureValArr.add(threshValObj);
				threshValArr.add(tmpObject);
			}

			cellFlag = !cellFlag;
		}

		rootJson.put("toolTipMsg",toolTipMsg);
		rootJson.put("hiddenDataObj",hiddenDataObj);
		rootJson.put("measureValArr",measureValArr);
		rootJson.put("threshValArr",threshValArr);
		if (aListTestWithNoThresholds != null && !aListTestWithNoThresholds.isEmpty())
		{
			screenLabels.put("no_thresh_title","Measures without thresholds");
			JSONArray noThreshArr = new JSONArray();
			JSONObject noThreshObj = new JSONObject();

			JSONArray noMeasureValArr = new JSONArray();
			if(isTestWithThresholds)
			{
			}
			for (int d=0; d<aListTestWithNoThresholds.size(); d=d+3)
			{
				int k = 1;
				for (int e=d; e<d+3; e++)
				{
					if (e<aListTestWithNoThresholds.size())
					{
						String strMeasure = (String)aListTestWithNoThresholds.get(e);
						String tmpStyple="";
						if(k == 1)
						{
							tmpStyple="border-left-width:1px";
							k++;
						}
						//noThreshObj.put("no_threh_meas_"+e,mappingInfo.getMeasureDescription(strTestName,strMeasure));
						noThreshArr.add(mappingInfo.getMeasureDescription(strTestName,strMeasure));


					}
					else
					{
					}
				}
			}
			
			int d=0;
			while(d<aListTestWithNoThresholds.size())
			{
				JSONObject noMeasureThreshValObj = new JSONObject();
				String strMeasure1 = "";
				String strMeasure2 = "";
				String strMeasure = (String)aListTestWithNoThresholds.get(d);
				int count = d;
				if(!StringUtils.isNull(strMeasure))
				{
					measureDataObj.put(mappingInfo.getMeasureDescription(strTestName,strMeasure),strMeasure);
					strMeasure = mappingInfo.getMeasureDescription(strTestName,strMeasure);
				}
				else
					strMeasure = "";
				count = d+1;
				if(count < aListTestWithNoThresholds.size())
				{
					strMeasure1 = (String)aListTestWithNoThresholds.get(count);
					if(!StringUtils.isNull(strMeasure1))
					{
						measureDataObj.put(mappingInfo.getMeasureDescription(strTestName,strMeasure1),strMeasure1);
						strMeasure1 = mappingInfo.getMeasureDescription(strTestName,strMeasure1);
					}
				}
				count = d+2;
				if(count < aListTestWithNoThresholds.size())
				{
					strMeasure2 = (String)aListTestWithNoThresholds.get(count);
					if(!StringUtils.isNull(strMeasure2))
					{
						measureDataObj.put(mappingInfo.getMeasureDescription(strTestName,strMeasure2),strMeasure2);
						strMeasure2 = mappingInfo.getMeasureDescription(strTestName,strMeasure2);
					}
				}

				noMeasureThreshValObj.put("Measure1",strMeasure);
				noMeasureThreshValObj.put("Measure2",strMeasure1);
				noMeasureThreshValObj.put("Measure3",strMeasure2);
				noMeasureValArr.add(noMeasureThreshValObj);
				d = count+1;
			}
			rootJson.put("noMeasureValArr",noMeasureValArr);
			rootJson.put("noThreshArr",noThreshArr);
		}
		if(from != null && from.equals("specificTest") && isHasSpecificData)
		{
			String trgt_host = "";
			String str_info = "";
			String sidVal = "";
			
			EgRoleInfo ri = EgRoleInfo.getInstance(session.getId(),(String)session.getAttribute("sess_userid"));
			ArrayList allCompNameList = new ArrayList();
			List aListExpectedComponents = new ArrayList();

			String[] componentsList = null;
			if (!StringUtils.isNull(strComponentType))
			{
				componentsList = ri.getAllServersOfType(strComponentType);
				Arrays.sort(componentsList);
			}
			
			for(int i=0;i<componentsList.length;i++)
			{
				String strComp = componentsList[i];
				String strCompName = strComp.substring(strComp.indexOf(":")+1,strComp.length());
				if (strCompName.equals(strComponentName)) continue;
				aListExpectedComponents.add(strCompName);
			}
			ArrayList excludeComponents = new ArrayList();
			boolean isPortRelevantForTest = specsInfo.isPortRelevantForTest(strTestName);
			//remove the component from the aListExpectedComponents whose test is excluded - starts
			prgBean.createFile("eg_disable.ini");
			if (isPortRelevantForTest)
			{
				String[] variables = prgBean.getVariables(strComponentType);
				ArrayList aListVariables = new ArrayList(Arrays.asList(variables));
				for (int i=0;i<aListExpectedComponents.size();i++)
				{
					String tempComp = (String)aListExpectedComponents.get(i);
					String pattern = strComponentType+":"+tempComp;
					if (aListVariables.contains(pattern))
					{
						String value = prgBean.getStrValue(strComponentType,pattern);
						if (value.indexOf(strTestName) > -1)
						{
							excludeComponents.add(tempComp);
						}
					}
				}
			}
			else
			{
				String[] variables = prgBean.getVariables("Default_server");
				ArrayList aListVariables = new ArrayList(Arrays.asList(variables));
				for (int i=0;i<aListExpectedComponents.size();i++)
				{
					String tempComp = (String)aListExpectedComponents.get(i);
					String pattern = "Default_server:"+tempComp.substring(0,tempComp.indexOf(":"));
					if (aListVariables.contains(pattern))
					{
						String value = prgBean.getStrValue("Default_server",pattern);
						if (value.indexOf(strTestName) > -1)
						{
							excludeComponents.add(tempComp);
						}
					}
				}
			}
			if(excludeComponents != null && excludeComponents.size() > 0)
			{
				aListExpectedComponents.removeAll(excludeComponents);
			}
			//remove the component from the aListExpectedComponents whose test is excluded - ends

			if(!StringUtils.isNull(descriptorName) && (aListExpectedComponents != null && !aListExpectedComponents.isEmpty()) )
			{
				boolean isAppTestForOracle = false;
				egDb=EgDbAdapterTypeUI.getObject();
				ResultSet rs = null;
				String status = "";
				ArrayList availableCompList = new ArrayList();
				for (int i = 0; i < aListExpectedComponents.size(); i++)
				{
					String cName = (String)aListExpectedComponents.get(i);
					if(!StringUtils.isNull(strComponentType) && strComponentType.equals("Oracle_server"))
					{
						if(!hostLevelTests.contains(strTestName))
						{
							isAppTestForOracle = true;
							String strTrgtHost = cName.substring(0,cName.indexOf(":"));
							String strSID = cName.substring(cName.lastIndexOf(":")+1);
							String strInfo = strSID+"+"+descriptorName;
							strTrgtHost = "'"+strTrgtHost+"'";
							try
							{
								boolean isConnected = false;
								status = egDb.setConnection();
								if(status.equals("CONNECTED"))
								{
									isConnected = true;
								}
								
								if(isConnected)
								{
									String query = egDb.getQueryForSameTypeInfoComponents(strTrgtHost,strTestName,strInfo);
									rs = egDb.SelectExec(query);
									String matchData = "";
									matchData = (String)egDb.getResultSetAsDescSeverString(rs,aListExpectedComponents);
									if(!StringUtils.isNull(matchData))
									{
										availableCompList.add(matchData);
									}
								}
								rs.close();
								egDb.releaseConnection(false);
								EgDbAdapterTypeUI.releaseObject(egDb);
							}
							catch(Exception e)
							{
								if(rs != null)
								{
									rs.close();
								}
								egDb.releaseConnection(true);
								EgDbAdapterTypeUI.releaseObject(egDb);
							}
						}
						else
						{
							cName = cName.substring(0,cName.indexOf(":"));
							trgt_host = trgt_host + "'" + cName + "'" + ",";
						}
					}
					else
					{
						cName = cName.substring(0,cName.indexOf(":"));
						trgt_host = trgt_host + "'" + cName + "'" + ",";
					}
				}

				if(!isAppTestForOracle)
				{
					trgt_host = trgt_host.substring(0, trgt_host.lastIndexOf(","));
					str_info = "+"+descriptorName;
					try
					{
						boolean isConnected = false;
						status = egDb.setConnection();
						if(status.equals("CONNECTED"))
						{
							isConnected = true;
						}
						
						if(isConnected)
						{
							boolean isHostLevel = (hostLevelTests != null && hostLevelTests.contains(strTestName));
							String query = egDb.getQueryForSameTypeInfoComponents(trgt_host,strTestName,str_info);
							rs = egDb.SelectExec(query);
							String matchData = "";
							List result = new LinkedList();
							result = (List)egDb.getResultSetAsDescSeverList(rs,aListExpectedComponents,isHostLevel);
							if(result != null && !result.isEmpty())
							{
								availableCompList.addAll(result);
							}
						}
						rs.close();
						egDb.releaseConnection(false);
						EgDbAdapterTypeUI.releaseObject(egDb);
					}
					catch(Exception e)
					{
						if(rs != null)
						{
							rs.close();
						}
						egDb.releaseConnection(true);
						EgDbAdapterTypeUI.releaseObject(egDb);
					}
				}
				aListExpectedComponents = new ArrayList();
				aListExpectedComponents.addAll(availableCompList);
			}
			
			if(aListExpectedComponents != null && aListExpectedComponents.size() > 0)
			{
				rootJson.put("isApplyToOtherCompBtn",true);
				rootJson.put("isDeleteBtn",true);
				session.setAttribute("aListExpectedComponents",aListExpectedComponents);
				rootJson.put("delete_msg",_mapping.getTextDescription("admin.common.Delete"));
				rootJson.put("apply_other_msg",_mapping.getTextDescription("admin.common.Apply to other components"));
			}
			else
			{
				rootJson.put("delete_msg",_mapping.getTextDescription("admin.common.Delete"));
				rootJson.put("isDeleteBtn",true);
			}
		}
	}

	if(isShowSpecificDesc && from.equalsIgnoreCase("specificTest") && (specificDescThresh != null && specificDescThresh.size() > 0))
	{
		screenLabels.put("specs_desc_msg",_mapping.getTextDescription("admin.threshold.Descriptors with specific threshold settings"));
		rootJson.put("isHasSpecific",true);
		JSONArray specsDescArr = new JSONArray();
		if(specificDescThresh != null && specificDescThresh.size() > 0)
		{
			//if (specificDescThresh.size() > 15)
			//{
			//}
			//else
			//{
			//}

			Collections.sort(specificDescThresh,String.CASE_INSENSITIVE_ORDER);
			for (int i=0;i<specificDescThresh.size();i=i+3)
			{
				for (int j=i;j<i+3;j++)
				{
					if (j < specificDescThresh.size())
					{
						String strTempTest = (String)specificDescThresh.get(j);
						specsDescArr.add(strTempTest);
					}
					else
					{
					}
				}
			}


			int d=0;
			JSONArray specificDescListArr = new JSONArray();
			while(d<specificDescThresh.size())
			{
				JSONObject noMeasureThreshValObj = new JSONObject();
				String strMeasure1 = "";
				String strMeasure2 = "";
				String strMeasure = (String)specificDescThresh.get(d);
				int count = d;
				if(StringUtils.isNull(strMeasure))
					strMeasure = "";

				count = d+1;
				if(count < specificDescThresh.size())
				{
					strMeasure1 = (String)specificDescThresh.get(count);
					if(StringUtils.isNull(strMeasure1))
					{
						strMeasure1 = "";
					}
				}
				count = d+2;
				if(count < specificDescThresh.size())
				{
					strMeasure2 = (String)specificDescThresh.get(count);
					if(StringUtils.isNull(strMeasure2))
					{
						strMeasure2 = "";
					}
				}

				noMeasureThreshValObj.put("Test1",strMeasure);
				noMeasureThreshValObj.put("Test2",strMeasure1);
				noMeasureThreshValObj.put("Test3",strMeasure2);
				specificDescListArr.add(noMeasureThreshValObj);
				d = count+1;
			}
			rootJson.put("specificDescListArr",specificDescListArr);
			rootJson.put("specsDescArr",specsDescArr);
		}
	}

rootJson.put("screenLabels",screenLabels);
rootJson.put("hiddenObj",hiddenObj);
rootJson.put("measureDataObj",measureDataObj);

out.println(rootJson.toString());

%>
<%!
	int alarmPolicyLength = 15;

	private String get1(String H)
	{
		String h;
		if (!(H.equals("-")))
		{
			if (H.indexOf("&") > -1)
			{
				StringTokenizer st = new StringTokenizer(H,"&");
				H = st.nextToken();
				h = st.nextToken();
			}
			else
			{
				if (H.indexOf("sqc") > -1)
				{
					h = H;
					H = "-";
				}
				else
				{
					h = "-";
				}
			}
		}
		else
		{
			h = "-";
		}
		return(H+"_"+h);
	}

	private Vector getAbsAndRel(String format)
	{
		Vector vect = new Vector();
		StringTokenizer stok1 = new StringTokenizer(format,"/");
		if (!format.equals("-1") && stok1.countTokens() == 3)
		{
			String H1 = stok1.nextToken();
			String h2=get1(H1);
			String M = stok1.nextToken();
			String m1=get1(M);
			String L = stok1.nextToken();
			String l1=get1(L);
			StringTokenizer h1 = new StringTokenizer(h2,"_");
			String High= h1.nextToken();
			String high = h1.nextToken();
			StringTokenizer m = new StringTokenizer(m1,"_");
			String Mid = m.nextToken();
			String mid = m.nextToken();
			StringTokenizer l = new StringTokenizer(l1,"_");
			String Low = l.nextToken();
			String low = l.nextToken();
			StringBuffer sb2 = new StringBuffer();
			StringBuffer sb1 = new StringBuffer();
			String s1 = null;
			if (High.equals("-") && Mid.equals("-") && Low.equals("-"))
			{
				s1 = "none";
			}
			else
			{
				sb1.append(High);
				sb1.append("/");
				sb1.append(Mid);
				sb1.append("/");
				sb1.append(Low);
				s1 = sb1.toString();
			}
			String s2 = null;
			if (high.equals("-") && mid.equals("-") && low.equals("-"))
			{
				vect.add(s1);
				vect.add("none");
			}
			else
			{
				sb2.append(high);
				sb2.append("/");
				sb2.append(mid);
				sb2.append("/");
				sb2.append(low);
				s2 = sb2.toString();
				vect.add(s1);
				vect.add(s2);
			}
		}
		else
		{
			vect.add("none");
			vect.add("none");
		}
		return vect;
	}

	private String getThresholdValue(String grpFunction, String absValue,String relValue)
	{
		String strValue = "-";

		if (absValue.equals("-") && relValue.equals("-"))
		{
			strValue = "-";
		}
		else if (!absValue.equals("-") && !relValue.equals("-"))
		{
			strValue = grpFunction+"("+absValue+", "+getRelativeThreshold(relValue)+")";
		}
		else if (!absValue.equals("-"))
		{
			strValue = absValue;
		}
		else if (!relValue.equals("-"))
		{
			strValue = getRelativeThreshold(relValue);
		}

		return strValue;
	}

	private String getRelativeThreshold(String relValue)
	{
		//relValue: 3*sqc
		String strValue = "";
		if (relValue.indexOf("*") != -1)
		{
			int idx = relValue.indexOf("*");
			String s1 = relValue.substring(0,idx);
			double d_value = Double.valueOf(s1.trim()).doubleValue();
			d_value = d_value * 100;
			int i_value = (new Double(d_value)).intValue();
			strValue = Integer.toString(i_value) + "% of auto";
		}
		else
		{
			strValue = "auto";
		}
		return strValue;
	}

	public String removeNull(String compName)
	{
		if (compName != null && compName.length()>0)
		{
			return (compName.endsWith(":NULL"))?compName.substring(0,compName.indexOf(":NULL")):compName;
		}
		else
		{
			return "";
		}
	}
%>

<%
	//if(request.getQueryString()!=null)
	//{
			//session.setAttribute("adminLastAccessedURL","/final/admin/EgSpecificThresh.jsp?"+request.getQueryString());
	//}
	//else
	//{
			//session.setAttribute("adminLastAccessedURL","/final/admin/EgSpecificThresh.jsp?");
	//}
%>



