<%@ include file="../../EgTrackerAJSON.jsp" %>
<%@ page import = "java.util.*" %>
<%@ page import="java.sql.*" %>
<%@ page import = "java.lang.*" %>
<%@ page import="com.eg.EgInstallInfo"%>
<%@ page import="com.eg.IniFile" %>
<%@ page import = "com.eg.EgConfigFilesSync"%>
<%@ page import="com.eg.EgSpecsInfo"%>
<%@ page import="com.eg.EgThresholdInfo" %>
<%@ page import = "com.egurkha.admin.AuditLogThread"%>
<%@ page import = "com.egurkha.util.StringUtils"%>
<%@ page import="com.eg.EgMappingInfo"%>
<%@ page import = "com.egurkha.ini.IniUI"%>
<%@ page import ="egurkha.EgDbAdapterUI"%>
<%@ page import ="com.eg.EgDbAdapterTypeUI"%>
<%@ page import="com.eg.UpdateAndTransmitTrendInfo" %>
<%@ page import="com.eg.EgDateInfo" %>
<%@ page import="com.eg.EgDbInfo" %>
<%@ page import="com.eg.EgDbaseInfo" %>
<%@ page import="com.eg.EgTestInfo" %>
<%@ page import="com.eg.EgConfigInfo" %>
<%@ page import ="com.eg.EgServerGroupBean"%>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

<%@ page import="net.sf.json.*"%>

<jsp:useBean id="prgBean" class="egurkha.EgTestParamBean" scope="page" />
<jsp:useBean id="threshBean" class="egurkha.EgThresholdBean" scope="page" />

<%
	JSONObject rootJson = new JSONObject();
	IniUI iniUI = IniUI.getInstance();
	//user language
	String userLanguage = iniUI.getDefaultLanguage();
	EgDbAdapterUI egDb;
	EgDateInfo dat=new EgDateInfo();
	EgDbInfo dbInfo=new EgDbInfo("manager");
	EgSpecsInfo specsInfo = new EgSpecsInfo("manager");
	EgThresholdInfo egThresh = new EgThresholdInfo("manager");
	EgDbaseInfo dbaseInfo = new EgDbaseInfo("manager");
	EgTestInfo testInfo = new EgTestInfo("manager");
	EgConfigInfo configInfo = new EgConfigInfo("manager");
	EgServerGroupBean serverGrpBean = new EgServerGroupBean();

	UpdateAndTransmitTrendInfo trendHelper = new UpdateAndTransmitTrendInfo();
	IniFile servicesIni = new IniFile(EgInstallInfo.getInstallDir()+"/manager/config/eg_services.ini");
	IniFile testsIniObj = new IniFile(EgInstallInfo.getInstallDir()+"/manager/config/eg_tests.ini");

	boolean isUpdateToDb= "true".equalsIgnoreCase(servicesIni.getValue("MISC_ARGS","updateDefaultThreshToDb"));
	Hashtable ht = new Hashtable();
	String subjectVal = "Newtarget_Update_EgThresholdManager";
	boolean isSaveIni = false;

	if (_user != null)
	{
		userLanguage = _user.getLanguage();
	}
	EgMappingInfo mappingInfo = new EgMappingInfo(userLanguage);
	String operation = request.getParameter("operation");
	String que="";
	String hostname="";
	String measUpdate="";
	String threshUpdate="";
	String minUpdate="";
	String maxUpdate="";
	String currentTime="";
	String threshTable="";
	String trgtHost="";
	String host="";
	String sid="";
	String port="";
	String portNo="";
	String info="";
	String inf="";
	String status="";
	String startTime="";
	Vector v_info = new Vector();
	String user_thresh = "USER_THRESHOLDS";
	String descr_thresh = "DESCRIPTOR_THRESHOLDS"; // SIFY
	StringTokenizer st1 = null;
	ResultSet rs = null;

	int num = 0;
	long tim = 0l;
	long current = 0l;
	String time="";
	String measur="";

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

	String strComponentType = request.getParameter("componentType");
	String strComponentName = request.getParameter("componentName");
	String from = request.getParameter("from");
	String strDescName = request.getParameter("descriptorName");
	String testName = request.getParameter("testName");
	String sidVal = "";
	String str_info = "";
	String trgt_host = "";
	String query = "";
	if(!StringUtils.isNull(strDescName))
	{
		strDescName = StringUtils.decodeSecurityChar(strDescName);

		if(!StringUtils.isNull(strComponentType) && strComponentType.equals("Oracle_server"))
		{
			trgt_host = strComponentName.substring(0,strComponentName.indexOf(":"));
			sidVal = strComponentName.substring(strComponentName.lastIndexOf(":")+1);
			if(hostLevelTests.contains(testName))
			{
				str_info = "+"+strDescName;
			}
			else
			{
				str_info = sidVal+"+"+strDescName;
			}
		}
		else
		{
			trgt_host = strComponentName.substring(0,strComponentName.indexOf(":"));
			str_info = "+"+strDescName;
		}

		query = "SELECT TRGT_HOST,PORT_NO,SITE_NAME,INFO,MSMT_HOST FROM MEASURE_LAST WHERE TEST_NAME='"+testName+"' AND TRGT_HOST='"+trgt_host+"' AND INFO='"+str_info+"'";
	}
	else
	{
		if(!StringUtils.isNull(strComponentType) && strComponentType.equals("Oracle_server"))
		{
			trgt_host = strComponentName.substring(0,strComponentName.indexOf(":"));
			sidVal = strComponentName.substring(strComponentName.lastIndexOf(":")+1);
			if(hostLevelTests.contains(testName))
			{
				str_info = "+";
			}
			else
			{
				str_info = sidVal+"+";
			}
		}
		else
		{
			trgt_host = strComponentName.substring(0,strComponentName.indexOf(":"));
			str_info = "+";
		}

		query = "SELECT TRGT_HOST,PORT_NO,SITE_NAME,INFO,MSMT_HOST FROM MEASURE_LAST WHERE TEST_NAME='"+testName+"' AND TRGT_HOST='"+trgt_host+"'";
	}
	
	prgBean.createFile("eg_thresholds.ini");
	String []descThreshVal = prgBean.getVariables("DESCRIPTOR_THRESHOLDS");
	ArrayList descThreshList = new ArrayList();
	if(descThreshVal.length > 0)
	{
		for(int r=0; r<descThreshVal.length; r++)
		{
			if(descThreshVal[r].startsWith(testName))
			{
				descThreshList.add(descThreshVal[r]);
			}
		}
	}

	if(!StringUtils.isNull(testName))
	{
		egDb=EgDbAdapterTypeUI.getObject();
		try
		{
			boolean isConnected = false;
			status = egDb.setConnection();
			if(status.equals("CONNECTED"))
			{
				isConnected = true;
			}
			if(isConnected)
			{
				rs = egDb.SelectExec(query);
				while(rs.next())
				{
					String hostVal = rs.getString(1);
					String portVal = rs.getString(2);
					String siteVal = rs.getString(3);
					String infoVal = rs.getString(4);
					String msmthostVal = rs.getString(5);

					String updateDbData = hostVal+"#&#"+portVal+"#&#"+siteVal+"#&#"+StringUtils.encodeSecurityChar(infoVal)+"#&#"+msmthostVal+"#&#"+testName;
					int r = infoVal.indexOf(EgConfigInfo.infoFieldSeparator);
					//ht.put(hostVal+":"+portVal+"#"+infoVal,updateDbData);
					String tmpStr_info="";
					String tmpSidVal="";
					if(r != -1)
					{
						tmpStr_info = infoVal.substring(r+1).trim();
						tmpSidVal = infoVal.substring(0,r);
					}
					if(StringUtils.isNull(tmpSidVal))
					{
						ht.put(hostVal+":"+portVal+"#"+infoVal+"#"+msmthostVal,updateDbData);
					}
					else
					{
						ht.put(hostVal+":"+portVal+":"+tmpSidVal+"#"+infoVal+"#"+msmthostVal,updateDbData);
					}
				}
			}
			rs.close();
			egDb.releaseConnection(false);
			EgDbAdapterTypeUI.releaseObject(egDb);
		}
		catch(Exception e)
		{
			if(rs != null)
			{
				rs.close();
			}
			egDb.releaseConnection(true);
			EgDbAdapterTypeUI.releaseObject(egDb);
		}

	}

	ArrayList htKeyValues = new ArrayList();
	if(ht != null && ht.size() > 0)
	{
		Iterator iterAll = ht.keySet().iterator();
		while(iterAll.hasNext())
		{
			String tempKeys = (String) iterAll.next();
			htKeyValues.add(tempKeys);
		}
	}
	
	if (operation.equals("alarmPolicyDesc"))
	{
		String alarmPolicy = request.getParameter("alarmPolicy");
		IniFile thresholdsIni = new IniFile(EgInstallInfo.getInstallDir()+"/manager/config/eg_thresholds.ini");
		String strDescription = thresholdsIni.getValue("ALARM_POLICY",alarmPolicy);
		int i_idx = strDescription.indexOf(",");
		String windowSize = strDescription.substring(0,i_idx);
		String numCrossings = strDescription.substring(i_idx+1);
		if (windowSize.equals("1") && numCrossings.equals("1"))
		{
			out.println(numCrossings + " threshold violation out of " + windowSize + " measurement");
		}
		else
		{
			out.println(numCrossings + " threshold violations out of " + windowSize + " measurements");
		}
	}
	else if(operation.equals("updateThresholds"))
	{
		String threshold = request.getParameter("threshold");
		String measure = threshold.substring(0,threshold.indexOf(":"));
		measure+=":";
		IniFile defaultIni = new IniFile(EgInstallInfo.getInstallDir()+"/manager/config/eg_thresholds.ini");
		String iniVariable = "";
		String iniValue = "";
		boolean isDescriptorThresholds = false;
		hostname = removeNull(strComponentName);
		
		if(from != null && from.equalsIgnoreCase("specificTest"))
		{
			if(!StringUtils.isNull(strDescName))
			{
				isDescriptorThresholds = true;
				String nickWithoutPort = "";
				String nickWithNullPort = "";
				if(hostLevelTests.contains(testName))
				{
					if(strComponentName.indexOf(":") != -1)
					{
						nickWithoutPort = strComponentName.substring(0,strComponentName.indexOf(":"));
						nickWithNullPort = nickWithoutPort+":NULL";
					}
				}
				else
				{
					nickWithoutPort = strComponentName;
					nickWithNullPort = strComponentName;
				}

				iniVariable = testName+":"+nickWithNullPort+"#"+StringUtils.encodeSecurityChar(strDescName);
				iniValue = defaultIni.getValue("DESCRIPTOR_THRESHOLDS",iniVariable);
				if(StringUtils.isNull(iniValue) && iniValue.length() == 0)
				{
					iniVariable = testName+":"+nickWithNullPort+"#"+strDescName;
					iniValue = defaultIni.getValue("DESCRIPTOR_THRESHOLDS",iniVariable);
				}
			}
			else
			{
				String nickWithoutPort = "";
				String nickWithNullPort = "";
				if(hostLevelTests.contains(testName))
				{
					if(strComponentName.indexOf(":") != -1)
					{
						nickWithoutPort = strComponentName.substring(0,strComponentName.indexOf(":"));
						nickWithNullPort = nickWithoutPort+":NULL";
					}
				}
				else
				{
					nickWithoutPort = strComponentName;
					nickWithNullPort = strComponentName;
				}

				iniVariable = testName+":"+nickWithoutPort;
				iniValue = defaultIni.getValue("USER_THRESHOLDS",iniVariable);
			}
			if(StringUtils.isNull(iniValue) && iniValue.length() == 0)
			{
				String tmpCompName = "";
				if(hostLevelTests.contains(testName))
				{
					tmpCompName = removeNull(strComponentName);
				}
				else
				{
					tmpCompName = strComponentName;
				}
				iniVariable = testName+":"+tmpCompName;
				iniValue = defaultIni.getValue("USER_THRESHOLDS",iniVariable);

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

						if (policyName != null && policyName.length() > 0)
						{
							iniValue = serverGrpBean.getPolicyValue(policyName);
						}
					}
					else
					{
						serverGroup = serverGrpBean.getServerGroup(strComponentName,testName,b_portRelev);

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

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

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

				if(StringUtils.isNull(iniValue) && iniValue.length() == 0 )
				{
					iniValue = defaultIni.getValue("DEFAULT_THRESHOLDS",testName);
				}
			}
		}
		else if(from != null && from.equalsIgnoreCase("grpRuleTest"))
		{
			Vector serverGroup = new Vector();
			boolean b_portRelev = specsInfo.isPortRelevantForTest(testName);
			if(strDescName != null && !strDescName.equals("null") && strDescName.length() > 0)
			{
				serverGroup = serverGrpBean.getServerGroup(strComponentName,testName,b_portRelev,strDescName);
				if (serverGroup != null && serverGroup.size() > 0)
				{
					String thisGroup = (String) serverGroup.get(0);
					String policyName = serverGrpBean.getPolicyForTest(thisGroup,testName,strDescName);

					if (policyName != null && policyName.length() > 0)
					{
						iniValue = serverGrpBean.getPolicyValue(policyName);
					}
				}
				else
				{
					serverGroup = serverGrpBean.getServerGroup(strComponentName,testName,b_portRelev);

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

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

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

			if(StringUtils.isNull(iniValue) && iniValue.length() == 0 )
			{
				iniValue = defaultIni.getValue("DEFAULT_THRESHOLDS",testName);
			}
		}
		else if(from != null && from.equalsIgnoreCase("defaultDescriptorTest"))
		{
			String strVariable = testName+":"+strDescName;
			iniValue = defaultIni.getValue("DESCRIPTOR_DEFAULT_THRESHOLDS",strVariable);
			isDescriptorThresholds = true;
			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(testName+":") && 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(strDescName, searchString) )
						{
							iniValue = defaultIni.getValue("DESCRIPTOR_DEFAULT_THRESHOLDS",testName+":"+searchString);
							break;
						}
					}
				}

				if(StringUtils.isNull(iniValue))
				{
					iniValue = defaultIni.getValue("DEFAULT_THRESHOLDS",testName);
				}
			}
		}
		else if(from != null && from.equalsIgnoreCase("defaultTest"))
		{
			if(strDescName != null && !strDescName.equals("null") && strDescName.length() > 0)
			{
				isDescriptorThresholds = true;
			}
			iniValue = defaultIni.getValue("DEFAULT_THRESHOLDS",testName);
		}
		
//		if(iniValue != null && iniValue.length() == 0)
//		{
//			iniValue = defaultIni.getValue("DEFAULT_THRESHOLDS",testName);
//		}
		String newDefaultThreshold = "";
		StringBuffer strbuffer = new StringBuffer();
		ArrayList previousValues = new ArrayList();

		StringTokenizer st = new StringTokenizer(iniValue,",");
		while (st.hasMoreTokens())
		{
			String strTok = st.nextToken();
			previousValues.add(strTok);
			if (strTok.startsWith(measure))
			{
				strbuffer.append(threshold).append(",");
			}
			else
			{
				strbuffer.append(strTok).append(",");
			}
		}
		/* code for auditlog starts */
		AuditLogThread auditLogThread = new AuditLogThread();	
		auditLogThread.setValue("Category","Admin");
		auditLogThread.setValue("User_id",_user.getLoginID());
		auditLogThread.setValue("Host_ip",_user.getUserIP(session.getId()));
		auditLogThread.setValue("Session_id",session.getId());
		auditLogThread.setValue("Module","Thresholds");

		for(int i=0;i<previousValues.size();i++)
		{
			String preData = (String) previousValues.get(i);
			if(preData.startsWith(measure))
			{
				ArrayList tempPrevious = StringUtils.egTokenizer(preData,":");
				ArrayList tempCurrent = StringUtils.egTokenizer(threshold,":");

				String auditMeasure = "$measure$:"+testName+":"+tempPrevious.get(0).toString().trim();;
				String previousMin = tempPrevious.get(2).toString().trim();
				String previousMax = tempPrevious.get(3).toString().trim();
				String alarmPolicyPrevious = tempPrevious.get(4).toString().trim();

				String currentMin = tempCurrent.get(2).toString().trim();
				String currentMax = tempCurrent.get(3).toString().trim();
				String alarmPolicyCurrent = tempCurrent.get(4).toString().trim();

				if(!previousMin.equals(currentMin))
				{
					if(previousMin.equals("-1"))
					{
						previousMin = "none";
					}
					if(currentMin.equals("-1"))
					{
						currentMin = "none";
					}
					auditLogThread.setPreviousValue(auditMeasure,"Minimum-Threshold " + previousMin);
					auditLogThread.setCurrentValue(auditMeasure,"Minimum-Threshold " + currentMin);
				}

				if(!previousMax.equals(currentMax))
				{
					if(previousMax.equals("-1"))
					{
						previousMax = "none";
					}
					if(currentMax.equals("-1"))
					{
						currentMax = "none";
					}
					auditLogThread.setPreviousValue(auditMeasure,"Maximum-Threshold " + previousMax);
					auditLogThread.setCurrentValue(auditMeasure,"Maximum-Threshold " + currentMax);
				}

				if(!alarmPolicyPrevious.equals(alarmPolicyCurrent))
				{
					auditLogThread.setPreviousValue(auditMeasure,"Alarms " + alarmPolicyPrevious);
					auditLogThread.setCurrentValue(auditMeasure,"Alarms " + alarmPolicyCurrent);
				}
			}
		}
		String nullRemoved = strComponentName.endsWith(":NULL")?strComponentName.substring(0,strComponentName.lastIndexOf(":")):strComponentName;
		auditLogThread.setValue("Activity","Configure Thresholds");
		if(!StringUtils.isNull(strDescName))
		{
			auditLogThread.setValue("Description","The specific thresholds for descriptor "+strDescName+" of " + mappingInfo.getTestDescription(testName) + " test for component " + nullRemoved + " (" +mappingInfo.getComponentTypeDescription(strComponentType) + ") have been modified");
		}
		else
		{
			auditLogThread.setValue("Description","The specific thresholds of " + mappingInfo.getTestDescription(testName) + " test for component " + nullRemoved + " (" +mappingInfo.getComponentTypeDescription(strComponentType) + ") have been modified");
		}
		auditLogThread.sendItToDB();
		/* code for auditlog ends */

		// DB updation code starts
		prgBean.createFile("eg_thresholds.ini");
		if(StringUtils.isNull(strDescName))
		{
			//From Update button click(Specific thresholds)

			v_info = egThresh.getInfosForThresholds(testName,hostname);
		}
		if(hostname.indexOf(":")<0)//if there is no port
		{
			host=hostname;
			port="NULL";
			if(host.indexOf("(default)")>(-1))
			{
				int position=host.indexOf("(default)");
				host=host.substring(0,position);
			}
			if(!StringUtils.isNull(strDescName))
			{
				info = "+"+strDescName;
				info = info.trim();
				num = 3; // Srinivas - should this be 1
			}
		}
		else
		{
			st1=new StringTokenizer(hostname,":");
			num=st1.countTokens();
			if(num==2) //if there is no info
			{
				host=st1.nextToken();
				port=st1.nextToken();
				if(port.indexOf("(default)")>(-1))
				{
					int posit=port.indexOf("(default)");
					port=port.substring(0,posit);
				}
				if(!StringUtils.isNull(strDescName))
				{
					info = "+"+strDescName;
					info = info.trim();
					num = 3;
				}
			}
			else if(num==3)	//if info is present(Specific to Oracle_server)
			{
				host=st1.nextToken();
				port=st1.nextToken();
				sid=st1.nextToken();
				if(!StringUtils.isNull(strDescName))
				{
					info = sid+"+"+strDescName;
					info = info.trim();
				}
				else info=sid+"+";

				if(port.indexOf("(default)")>(-1))
				{
					int pos=port.indexOf("(default)");
					port=port.substring(0,pos);
				}
			}
		}


		if(false)//isUpdateToDb)
		{
			egDb=EgDbAdapterTypeUI.getObject();
			try
			{
				startTime=dbInfo.measurementStartTimeColumn;
				trgtHost=dbInfo.targetHostColumn;
				portNo= dbInfo.portNoColumn;
				inf=dbInfo.infoColumn;
				tim=dat.egetUTCDate();
				current=(threshBean.trimTimeToHr(tim))-1000;
				time = dat.egetDate(current);
				ArrayList a = new ArrayList();
				ArrayList minarray=new ArrayList();
				ArrayList maxarray=new ArrayList();
				ArrayList updateArr = new ArrayList();
				ArrayList parametersToSec= new ArrayList();
				prgBean.createFile("eg_db.ini");
				
				for(int y=0;y<previousValues.size();y++)
				{
					ArrayList params = new ArrayList();
					String previousData = (String) previousValues.get(y);
					if(previousData.startsWith(measure))
					{
						threshTable =dbInfo.getThresholdTableForTest(testName);
						st = new StringTokenizer(threshold,":");
						measUpdate=st.nextToken();
						threshUpdate=st.nextToken();
						minUpdate=st.nextToken();
						if(minUpdate.equals("none"))
							minUpdate="-1";
						maxUpdate=st.nextToken();
						if(maxUpdate.equals("none"))
							maxUpdate="-1";
						String measures[] =egThresh.getMeasurementsForTest(testName);
						String[] mColumns=new String[2];
						mColumns = dbInfo.getThresholdColumnsForMeasure(testName,measures[y]);
						String variab = testName+":"+measures[y];
						int place=mColumns[0].lastIndexOf("_MIN");
						measur=mColumns[0].substring(0,place);
						
						if(threshUpdate != null && threshUpdate.equals("mix"))
						{
							a.add(measur);
							if (minUpdate.indexOf("sqc") > -1 || maxUpdate.indexOf("sqc") > -1)
							{
								if(minUpdate.equals("-1") || minUpdate.indexOf("sqc") > -1)
								{
									minarray.add(minUpdate);
								}
								else
								{
									minarray.add("sqc");
								}
								
								if(maxUpdate.equals("-1") || maxUpdate.indexOf("sqc") > -1)
								{
									maxarray.add(maxUpdate);
								}
								else
								{
									maxarray.add("sqc");
								}
							}
							else
							{
								String allInfos=testsIniObj.getValue("NOTHRESHOLD_DESCRIPTORS",testName.trim());
								ArrayList al_nothresh=testInfo.egTokenizer(allInfos,",");
								String tempinfo="";
								if (al_nothresh!=null)
								{
									if (v_info==null)
										v_info=new Vector();
									for (int m=0;m<al_nothresh.size();m++)
									{
										if (info!=null && info.equals(""))
											tempinfo="+"+al_nothresh.get(m);
										else
											tempinfo=info+al_nothresh.get(m);

										if (!v_info.contains(tempinfo))
											v_info.add(tempinfo);
									}
								}
								params.add(threshTable);
								params.add(mColumns);
								params.add(minUpdate);
								params.add(maxUpdate);
								params.add(startTime);
								params.add(time);
								params.add(trgtHost);
								params.add(host);
								params.add(portNo);
								params.add(port);
								params.add(String.valueOf(num));
								params.add(inf);
								params.add(info);
								params.add(v_info);
								parametersToSec.add(params);
							}
						}
					}
				}

				/*Check whether the table exist in DB for the specified test*/
				boolean isTableExistInDB = dbaseInfo.isTableExistInDB(testName);
				if (isTableExistInDB && parametersToSec.size() > 0)
				{
					boolean result = trendHelper.updateThresholds(parametersToSec,"Specific");
				}
			}
			catch(Exception e)
			{
//				//System.out.println("Error Updating DataBase");
				if(egDb != null)
					EgDbAdapterTypeUI.releaseObject(egDb);

			}
			EgDbAdapterTypeUI.releaseObject(egDb);
		}

		if(ht != null && ht.size() > 0)
		{
			IniFile serIniWr = EgConfigFilesSync.getInstance(EgInstallInfo.getInstallDir()+"/manager/config/eg_threshupdate.ini");
			for(int h=0;h<htKeyValues.size();h++)
			{
				String htKey = (String) htKeyValues.get(h);
				String infoData = htKey.substring(htKey.indexOf("#")+1);
				infoData = infoData.substring(0,infoData.indexOf("#"));
				int r = infoData.indexOf(EgConfigInfo.infoFieldSeparator);
				String strInfo="";
				String sidValue="";
				if(r != -1)
				{
					strInfo = infoData.substring(r+1).trim();
					sidValue = infoData.substring(0,r);
				}
				String tempComp = "";
				if(hostLevelTests.contains(testName))
				{
					tempComp = strComponentName.substring(0,strComponentName.indexOf(":"));
					tempComp+=":NULL";
				}
				else
				{
					tempComp = strComponentName;
				}

				if(!StringUtils.isNull(strDescName))
				{
					String htValues = (String) ht.get(htKey);
					isSaveIni = true;
					serIniWr.setValue(subjectVal,htValues,testName);
				}
				else
				{
					if(htKey.startsWith(tempComp))
					{
						if(hostLevelTests.contains(testName))
						{
							String nickWithNullPort = trgt_host+":NULL";
							String strDescVal = testName+":"+nickWithNullPort+"#"+StringUtils.encodeSecurityChar(strInfo);
							String strGrpDescVal = testName+":"+nickWithNullPort+"#"+strInfo;

							if(descThreshList != null && (descThreshList.contains(strDescVal) || descThreshList.contains(strGrpDescVal)))
							{
									continue;
							}
							else
							{
								String htValues = (String) ht.get(htKey);
								isSaveIni = true;
								serIniWr.setValue(subjectVal,htValues,testName);
							}
						}
						else
						{
							String strDescVal = testName+":"+strComponentName+"#"+StringUtils.encodeSecurityChar(strInfo);
							String strGrpDescVal = testName+":"+strComponentName+"#"+strInfo;
							if(descThreshList != null && (descThreshList.contains(strDescVal) || descThreshList.contains(strGrpDescVal)))
							{
								continue;
							}
							else
							{
								String htValues = (String) ht.get(htKey);
								isSaveIni = true;
								serIniWr.setValue(subjectVal,htValues,testName);
							}
						}
					}
				}
			}
			if(isSaveIni)
			{
				serIniWr.saveFile();
			}
			EgConfigFilesSync.releaseInstance(serIniWr);
		}

		newDefaultThreshold = strbuffer.toString();
		int len = newDefaultThreshold.length()-1;
		newDefaultThreshold = newDefaultThreshold.substring(0,len);
		String strThresholdIni = EgInstallInfo.getInstallDir()+"/manager/config/eg_thresholds.ini";
		IniFile ThresholdIniWrite = EgConfigFilesSync.getInstance(strThresholdIni);
		if(isDescriptorThresholds)
		{
			String nickWithoutPort = "";
			String nickWithNullPort = "";
			if(hostLevelTests.contains(testName))
			{
				if(strComponentName.indexOf(":") != -1)
				{
					nickWithoutPort = strComponentName.substring(0,strComponentName.indexOf(":"));
					nickWithNullPort = nickWithoutPort+":NULL";
				}
			}
			else
			{
				nickWithNullPort = strComponentName;
			}
			String descIniVariable = testName+":"+nickWithNullPort+"#"+StringUtils.encodeSecurityChar(strDescName);
			String descIniVal = defaultIni.getValue("DESCRIPTOR_THRESHOLDS",testName+":"+nickWithNullPort+"#"+strDescName);
			if(!StringUtils.isNull(descIniVal))
			{
				ThresholdIniWrite.deleteValue("DESCRIPTOR_THRESHOLDS",testName+":"+nickWithNullPort+"#"+strDescName);
			}

			ThresholdIniWrite.setValue("DESCRIPTOR_THRESHOLDS",descIniVariable,newDefaultThreshold);
			ThresholdIniWrite.saveFile();
		}
		else
		{
			String nickWithoutPort = "";
			if(hostLevelTests.contains(testName))
			{
				if(strComponentName.indexOf(":") != -1)
				{
					nickWithoutPort = strComponentName.substring(0,strComponentName.indexOf(":"));
				}
			}
			else
			{
				nickWithoutPort = strComponentName;
			}
			String userIniVariable = testName+":"+nickWithoutPort;
			ThresholdIniWrite.setValue("USER_THRESHOLDS",userIniVariable,newDefaultThreshold);
			ThresholdIniWrite.saveFile();
		}
		rootJson.put("successUpdate",true);
		EgConfigFilesSync.releaseInstance(ThresholdIniWrite);
		EgThresholdInfo.setUpdated();
	}
	else if(operation.equals("deleteThresholds"))
	{
		IniFile serIniWr = EgConfigFilesSync.getInstance(EgInstallInfo.getInstallDir()+"/manager/config/eg_threshupdate.ini");
		String iniSubject = "";
		String iniVariable = "";
		String tempComp = "";
		if(hostLevelTests.contains(testName))
		{
			tempComp = strComponentName.substring(0,strComponentName.indexOf(":"));
			tempComp+=":NULL";
		}
		else
		{
			tempComp = strComponentName;
		}

		/* code for auditlog starts */
		AuditLogThread auditLogThread = new AuditLogThread();	
		auditLogThread.setValue("Category","Admin");
		auditLogThread.setValue("User_id",_user.getLoginID());
		auditLogThread.setValue("Host_ip",_user.getUserIP(session.getId()));
		auditLogThread.setValue("Session_id",session.getId());
		auditLogThread.setValue("Module","Thresholds");
		String nullRemoved = strComponentName.endsWith(":NULL")?strComponentName.substring(0,strComponentName.lastIndexOf(":")):strComponentName;
		auditLogThread.setValue("Activity","Delete Thresholds");
		if(!StringUtils.isNull(strDescName))
		{
			auditLogThread.setValue("Description","The specific thresholds for descriptor "+strDescName+" of "+ mappingInfo.getTestDescription(testName) + " test for component " + nullRemoved + " (" +mappingInfo.getComponentTypeDescription(strComponentType) + ") have been deleted");
		}
		else
		{
			auditLogThread.setValue("Description","The specific thresholds of " + mappingInfo.getTestDescription(testName) + " test for component " + nullRemoved + " (" +mappingInfo.getComponentTypeDescription(strComponentType) + ") have been deleted");
		}
		auditLogThread.sendItToDB();
		/* code for auditlog ends */



		if(from != null && from.equalsIgnoreCase("specificTest"))
		{
			if(strDescName != null && !strDescName.equals("null") && strDescName.length() > 0)
			{
				String nickWithoutPort = "";
				String nickWithNullPort = "";
				if(hostLevelTests.contains(testName))
				{
					if(strComponentName.indexOf(":") != -1)
					{
						nickWithoutPort = strComponentName.substring(0,strComponentName.indexOf(":"));
						nickWithNullPort = nickWithoutPort+":NULL";
					}
				}
				else
				{
					nickWithNullPort = strComponentName;
				}

				iniSubject = "DESCRIPTOR_THRESHOLDS";
				iniVariable = testName+":"+nickWithNullPort+"#"+StringUtils.encodeSecurityChar(strDescName);
			}
			else
			{
				String nickWithoutPort = "";
				if(hostLevelTests.contains(testName))
				{
					if(strComponentName.indexOf(":") != -1)
					{
						nickWithoutPort = strComponentName.substring(0,strComponentName.indexOf(":"));
					}
				}
				else
				{
					nickWithoutPort = strComponentName;
				}
				iniSubject = "USER_THRESHOLDS";
				iniVariable = testName+":"+nickWithoutPort;
			}
		}

		if(ht != null && ht.size() > 0)
		{
			for(int h=0;h<htKeyValues.size();h++)
			{
				String htKey = (String) htKeyValues.get(h);
				String infoData = htKey.substring(htKey.indexOf("#")+1);
				infoData = infoData.substring(0,infoData.indexOf("#"));
				int r = infoData.indexOf(EgConfigInfo.infoFieldSeparator);
				String strInfo="";
				String sidValue="";
				if(r != -1)
				{
					strInfo = infoData.substring(r+1).trim();
					sidValue = infoData.substring(0,r);
				}
				if(!StringUtils.isNull(strDescName))
				{
					String htValues = (String) ht.get(htKey);
					isSaveIni = true;
					serIniWr.setValue(subjectVal,htValues,testName);
				}
				else
				{
					if(htKey.startsWith(tempComp))
					{
						if(hostLevelTests.contains(testName))
						{

							String nickWithNullPort = trgt_host+":NULL";
							String strDescVal = testName+":"+nickWithNullPort+"#"+StringUtils.encodeSecurityChar(strInfo);
							if(descThreshList != null && descThreshList.contains(strDescVal))
							{
									continue;
							}
							else
							{
								String htValues = (String) ht.get(htKey);
								isSaveIni = true;
								serIniWr.setValue(subjectVal,htValues,testName);
							}
						}
						else
						{
							String strDescVal = testName+":"+strComponentName+"#"+StringUtils.encodeSecurityChar(strInfo);
							if(descThreshList != null && descThreshList.contains(strDescVal))
							{
								continue;
							}
							else
							{
								String htValues = (String) ht.get(htKey);
								isSaveIni = true;
								serIniWr.setValue(subjectVal,htValues,testName);
							}
						}
					}
				}
			}
		}
		if(isSaveIni)
		{
			serIniWr.saveFile();
		}
		EgConfigFilesSync.releaseInstance(serIniWr);

		String strThresholdIni = EgInstallInfo.getInstallDir()+"/manager/config/eg_thresholds.ini";
		IniFile ThresholdIniWrite = EgConfigFilesSync.getInstance(strThresholdIni);
		
		ThresholdIniWrite.deleteValue(iniSubject,iniVariable);
		if (iniSubject.equals("DESCRIPTOR_THRESHOLDS"))
		{
			ThresholdIniWrite.deleteValue(iniSubject,StringUtils.decodeSecurityChar(iniVariable));
		}
		ThresholdIniWrite.saveFile();
		
		EgConfigFilesSync.releaseInstance(ThresholdIniWrite);
		EgThresholdInfo.setUpdated();
	}
	out.println(rootJson.toString());
%>

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