<%@ 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.EgMappingInfo"%>
<%@ page import="com.eg.IniFile" %>
<%@ page import = "com.egurkha.ini.IniUI"%>
<%@ page import = "com.eg.EgConfigFilesSync"%>
<%@ page import = "com.egurkha.admin.AuditLogThread"%>
<%@ page import = "com.egurkha.util.StringUtils"%>
<%@ page import ="egurkha.EgDbAdapterUI"%>
<%@ page import ="com.eg.EgDbAdapterTypeUI"%>
<%@ page import="com.eg.UpdateAndTransmitTrendInfo" %>
<%@ page import="com.eg.EgTesterInfo" %>
<%@ page import="com.eg.EgDateInfo" %>
<%@ page import="com.eg.EgDbInfo" %>
<%@ page import="com.eg.EgThresholdInfo" %>
<%@ page import="com.eg.EgSpecsInfo" %>
<%@ page import="com.eg.EgConfigInfo" %>
<%@ page import="com.eg.EgDiscInfo" %>
<%@ page import="com.eg.EgDiscInfo2" %>
<%@ page import="com.eg.EgDisableInfo" %>
<%@ page import="com.eg.EgServerGroupBean" %>
<%@ 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");
	EgThresholdInfo dbThresh =new EgThresholdInfo("manager");
	EgTesterInfo testerInfo =new EgTesterInfo("manager");
	EgSpecsInfo egSpecs = new EgSpecsInfo("manager");
	EgDiscInfo2 discInfo2 = new EgDiscInfo2("manager");
	EgConfigInfo configInfo = new EgConfigInfo("manager");
	EgServerGroupBean serverGrpBean = new EgServerGroupBean();
	EgDisableInfo egDisable = new EgDisableInfo("manager");

	ResultSet rs = null;
	String status ="";
	String defaultDescSubject = "DESCRIPTOR_DEFAULT_THRESHOLDS";

	ArrayList threshGrpTestList = new ArrayList();
	TreeSet threshGrpTreeSet = serverGrpBean.getTestColnComponentsForServerType();
	threshGrpTestList.addAll(threshGrpTreeSet);
	UpdateAndTransmitTrendInfo updateHelper = new UpdateAndTransmitTrendInfo();
	IniFile servicesIni = new IniFile(EgInstallInfo.getInstallDir()+"/manager/config/eg_services.ini");
	boolean isUpdateToDb= "true".equalsIgnoreCase(servicesIni.getValue("MISC_ARGS","updateDefaultThreshToDb"));
	ArrayList globalThresholds = testerInfo.getGlobalThresholds();

	if (_user != null)
	{
		userLanguage = _user.getLanguage();
	}
	EgMappingInfo mappingInfo = new EgMappingInfo(userLanguage);
	String operation = request.getParameter("operation");
	String user_thresh = "USER_THRESHOLDS";
	String descr_thresh = "DESCRIPTOR_THRESHOLDS"; // SIFY

	if(!StringUtils.isNull(operation) && operation.equals("reset"))
	{
		String testName = request.getParameter("testName");
		String selectAll = request.getParameter("selectAll");
		String availLevelForTest = request.getParameter("availLevelForTest");
		String strSelectedLevels = request.getParameter("selectedThreshLevels");
		String isDefaultChange = request.getParameter("isDefaultChange");
		String isDefaultDescriptorChange = request.getParameter("isDefaultDescriptorChange");

		String[] selectedThreshLevels = null;
		ArrayList availLevelList = StringUtils.egTokenizer(availLevelForTest,",");
		
		if (!StringUtils.isNull(strSelectedLevels))
		{
			selectedThreshLevels = StringUtils.getTokens(strSelectedLevels,",");
		}
		ArrayList previousValues = new ArrayList();
		boolean isNeedToUpdateNewTrgtIni = true;

		StringBuffer strbuffer = new StringBuffer();
		String newDefaultThreshold = "";
	
		/* code for auditlog starts */
		String strCurrentData = "";
		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");
		/* code for auditlog ends */

		prgBean.createFile("eg_thresholds.ini");
		IniFile resetDefaultIni = new IniFile(EgInstallInfo.getInstallDir()+"/manager/factory-config/eg_thresholds.ini");
		String factoryThreshold = resetDefaultIni.getValue("DEFAULT_THRESHOLDS",testName);
		String[] defaultDescVars = prgBean.getVariables(defaultDescSubject);

		HashMap resetMeasureMap = new HashMap();
		boolean isGlobalTrgtUpdate = false;
		boolean isGlobalThreshSelected = false;
		boolean isUserThreshSelected = false;
		boolean isDescThreshSelected = false;
		boolean isGroupThreshSelected = false;
		boolean isDescDefaultThreshSelected = false;
		boolean isDefaultThreshSelected = false;

		ArrayList newTrgtGlobalTests = (ArrayList) session.getAttribute("newTrgtGlobalTests");
		ArrayList globalToDefaultThresholdsTest = (ArrayList) session.getAttribute("globalToDefaultThresholdsTest");
		HashMap newTrgtGlobalMapTests = (HashMap) session.getAttribute("newTrgtGlobalMapTests");
		HashMap modifyDefaultDescMapTests = (HashMap) session.getAttribute("modifyDefaultDescMapTests");
		ArrayList newGlobalThresholdsTest = (ArrayList) session.getAttribute("newGlobalThresholdsTest");
		ArrayList modifyDefaultDescTestList = (ArrayList) session.getAttribute("modifyDefaultDescTestList");

		if (selectedThreshLevels != null && selectedThreshLevels.length > 0)
		{
			ArrayList levelTokensList = new ArrayList(Arrays.asList(selectedThreshLevels));
			strCurrentData = StringUtils.getString(levelTokensList);

			if (levelTokensList != null && levelTokensList.size() > 0)
			{
				if (levelTokensList.contains("Global thresholds"))
				{
					isGlobalThreshSelected = true;
					if (newTrgtGlobalMapTests != null && newTrgtGlobalMapTests.containsKey(testName))
					{
						isGlobalTrgtUpdate = ((Boolean) newTrgtGlobalMapTests.get(testName)).booleanValue();
					}
				}
				
				if (levelTokensList.contains("Component thresholds"))
				{
					isUserThreshSelected = true;
				}
				
				if (levelTokensList.contains("Descriptor thresholds"))
				{
					isDescThreshSelected = true;
				}
				
				if (levelTokensList.contains("Group thresholds"))
				{
					isGroupThreshSelected = true;
				}
				
				if (levelTokensList.contains("Default thresholds"))
				{
					if (!StringUtils.isNull(isDefaultDescriptorChange) && isDefaultDescriptorChange.equals("true"))
					{
						isDescDefaultThreshSelected = true;
					}

					if (!StringUtils.isNull(isDefaultChange) && isDefaultChange.equals("true"))
					{
						isDefaultThreshSelected = true;
					}
				}
			}
		}

		if(isGlobalThreshSelected)
		{
			newDefaultThreshold = factoryThreshold;
			if (newTrgtGlobalMapTests != null && newTrgtGlobalMapTests.containsKey(testName))
			{
				isNeedToUpdateNewTrgtIni = ((Boolean) newTrgtGlobalMapTests.get(testName)).booleanValue();
			}
		}
		else if(isDefaultThreshSelected)
		{
			newDefaultThreshold = factoryThreshold;
			if (globalToDefaultThresholdsTest != null && globalToDefaultThresholdsTest.contains(testName))
			{
				isNeedToUpdateNewTrgtIni = false;
			}
		}
		else if(isDescDefaultThreshSelected)
		{
			if (globalThresholds != null && globalThresholds.contains(testName))
			{
				isNeedToUpdateNewTrgtIni = false;
			}
		}

		/* code for auditlog starts */
		auditLogThread.setValue("Activity","Delete Thresholds");
		if (!StringUtils.isNull(strCurrentData))
		{
			auditLogThread.setCurrentValue("Deleted Thresholds",strCurrentData);
			auditLogThread.setValue("Description", mappingInfo.getTestDescription(testName) + " test thresholds have been deleted");

			auditLogThread.sendItToDB();
		}
	
		/* code for auditlog ends */

		ArrayList aListThreshUpdateIniEntries = new ArrayList();
		ArrayList aListThreshIniEntries = new ArrayList();
		ArrayList aListInfoThreshIniEntries = new ArrayList();
		if (isNeedToUpdateNewTrgtIni)
		{
			ArrayList aListModifyThreshIniTests = new ArrayList();
			ArrayList aListThreshIniTests = new ArrayList();
			ArrayList aListUserThreshIniTests = new ArrayList();
			ArrayList aListDescThreshIniTests = new ArrayList();

			if(!StringUtils.isNull(testName))
			{
				Hashtable ht = new Hashtable();
				String subjectVal = "Newtarget_Update_EgThresholdManager";
				boolean isSaveIni = false;
				egDb=EgDbAdapterTypeUI.getObject();
				try
				{
					boolean isConnected = false;
					status = egDb.setConnection();
					if(status.equals("CONNECTED"))
					{
						isConnected = true;
					}

					if(isConnected)
					{
						String query ="SELECT TRGT_HOST,PORT_NO,SITE_NAME,INFO,MSMT_HOST FROM MEASURE_LAST WHERE TEST_NAME='"+testName+"'" ;
						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);
							String str_info="";
							String sidVal="";
							if(r != -1)
							{
								str_info = infoVal.substring(r+1).trim();
								sidVal = infoVal.substring(0,r);
							}
							if(StringUtils.isNull(sidVal))
							{
								ht.put(hostVal+":"+portVal+"#"+infoVal+"#"+msmthostVal,updateDbData);
							}
							else
							{
								ht.put(hostVal+":"+portVal+":"+sidVal+"#"+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 (isGlobalTrgtUpdate)
				{
					if(ht != null && ht.size() > 0)
					{
						for(int h=0;h<htKeyValues.size();h++)
						{
							String htKey = (String) htKeyValues.get(h);
							String htValues = (String) ht.get(htKey);

							String nickWithPort = htKey.substring(0,htKey.indexOf("#"));
							if (!isTestDisabledForNick(nickWithPort,testName))
							{
								aListThreshUpdateIniEntries.add(new IniHelper(IniHelper.SET,subjectVal,htValues,testName));
							}
						}
					}

					if (isDescDefaultThreshSelected)
					{
						if (globalThresholds != null && globalThresholds.contains(testName))
						{
							//default descriptor threshold
							ArrayList delDefaultDescTestList = new ArrayList();
							ArrayList modifyDefaultDescTests = new ArrayList();

							if (defaultDescVars != null && defaultDescVars.length > 0)
							{
								for (int dd=0; dd<defaultDescVars.length; dd++)
								{
									String factDefaultDescVal = resetDefaultIni.getValue(defaultDescSubject,defaultDescVars[dd]);
									String currDefaultDescVal = prgBean.getStrValue(defaultDescSubject,defaultDescVars[dd]);
									if (factDefaultDescVal == null || factDefaultDescVal.length() == 0)
									{
										delDefaultDescTestList.add(defaultDescVars[dd]);
									}
									else if (!StringUtils.isNull(currDefaultDescVal) && !StringUtils.isNull(factDefaultDescVal))
									{
										if (!currDefaultDescVal.equals(factDefaultDescVal))
										{
											modifyDefaultDescTests.add(defaultDescVars[dd]);
										}
									}
								}
							}


							if(delDefaultDescTestList != null)
							{
								for(int r=0; r<delDefaultDescTestList.size(); r++)
								{
									if(delDefaultDescTestList.get(r).toString().startsWith(testName))
									{
										aListThreshIniEntries.add(new IniHelper(IniHelper.DELETE,defaultDescSubject,(String)delDefaultDescTestList.get(r),""));
									}
								}
							}

							if (modifyDefaultDescTests != null && modifyDefaultDescTests.size() > 0)
							{
								for (int md=0;md<modifyDefaultDescTests.size();md++)
								{
									String strModifyDefaultDesc = (String) modifyDefaultDescTests.get(md);
									String defaultDescValue = resetDefaultIni.getValue(defaultDescSubject,strModifyDefaultDesc);
									aListThreshIniEntries.add(new IniHelper(IniHelper.SET,defaultDescSubject,strModifyDefaultDesc,defaultDescValue));
								}
							}
						}
					}
				}
				else
				{
					String[] userThreshVal = prgBean.getVariables(user_thresh);
					ArrayList getAllCompsForServer = getAllManagedComps();

					ArrayList userThreshList = new ArrayList();
					ArrayList descThreshList = new ArrayList();
					String[] hostTests = egSpecs.getDefaultTestsForServer("Host_system");
					ArrayList hostLevelTests = new ArrayList();

					ArrayList tmpHostList = new ArrayList();
					if (hostTests != null)
					{
						hostLevelTests = new ArrayList(Arrays.asList(hostTests));
					}

					if(userThreshVal.length > 0)
					{
						for(int r=0; r<userThreshVal.length; r++)
						{
							if(userThreshVal[r].startsWith(testName))
							{
								userThreshList.add(userThreshVal[r]);
							}
						}
					}
					String []descThreshVal = prgBean.getVariables(descr_thresh);
					if(descThreshVal.length > 0)
					{
						for(int r=0; r<descThreshVal.length; r++)
						{
							if(descThreshVal[r].startsWith(testName))
							{
								descThreshList.add(descThreshVal[r]);
							}
						}
					}
					
					ArrayList infoBasedGroupList = new ArrayList();
					ArrayList groupTestsList = new ArrayList();
					ArrayList tmpManagedInfos = new ArrayList();
					TreeSet managedInfos = serverGrpBean.getAllManagedInfosForTest(testName);
					tmpManagedInfos.addAll(managedInfos);
					for(int g=0;g<getAllCompsForServer.size(); g++)
					{
						String fullServerName = (String) getAllCompsForServer.get(g);
						String serverType = fullServerName.substring(0,fullServerName.indexOf(":"));
						String component = fullServerName.substring(fullServerName.indexOf(":")+1);
						String tmpComp = "";

						if(hostLevelTests.contains(testName))
						{
							tmpComp = component.substring(0,component.indexOf(":"));
							tmpComp +=":NULL";
						}
						else
						{
							tmpComp = component;
						}

						boolean b = egSpecs.isPortRelevantForTest(testName);
						boolean isInfoBaseGrp = false;
						if (b)
						{
							isInfoBaseGrp = (!serverGrpBean.isServerAssociatedToTest(component,testName) && serverGrpBean.isServerAssociatedToTestUI(component,testName));
						}
						else
						{
							isInfoBaseGrp = (serverGrpBean.isServerAssociatedToTest(component,testName) && serverGrpBean.isServerAssociatedToTestUI(component,testName));
						}
						if (isInfoBaseGrp)
						{
							if(tmpManagedInfos != null && tmpManagedInfos.size() > 0)
							{
								for(int v=0;v<tmpManagedInfos.size();v++)
								{
									String vecInfo = (String) tmpManagedInfos.get(v);
									infoBasedGroupList.add(testName+":"+tmpComp+"#"+vecInfo);
								}
							}
						}
						if(serverGrpBean.isServerAssociatedToTest(component,testName))
						{
							groupTestsList.add(testName+":"+tmpComp);
						}
					}

					for(int ct = 0;ct<getAllCompsForServer.size();ct++)
					{
						String fullServerName = (String) getAllCompsForServer.get(ct);
						String serverType = fullServerName.substring(0,fullServerName.indexOf(":"));

						String tempComp = fullServerName.substring(fullServerName.indexOf(":")+1);
						String tmpTrgtHost = tempComp.substring(0,tempComp.indexOf(":"));
						boolean isTestDisabled = false;
						if(hostLevelTests.contains(testName))
						{
							if(!tempComp.endsWith(":NULL"))
							{
								tempComp = tempComp.substring(0,tempComp.indexOf(":"));
								tempComp+=":NULL";
							}
							String testDisabledServer = tempComp.substring(0,tempComp.indexOf(":"));
							isTestDisabled = egDisable.isTestDisabled("Default_server",testDisabledServer,testName);
						}
						else
						{
							isTestDisabled = egDisable.isTestDisabled(serverType,tempComp,testName);
						}

						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 str_info="";
								String sidVal="";
								if(r != -1)
								{
									str_info = infoData.substring(r+1).trim();
									sidVal = infoData.substring(0,r);
								}

								boolean isNewTrgtEntry = false;
								if(htKey.startsWith(tempComp))
								{
									if(hostLevelTests.contains(testName))
									{
										String strUserVal = testName+":"+tmpTrgtHost;
										String strGrpVal = testName+":"+tempComp;
										String nickWithNullPort = tmpTrgtHost+":NULL";
										String strDescVal = testName+":"+nickWithNullPort+"#"+StringUtils.encodeSecurityChar(str_info);
										String strGrpDescVal = testName+":"+nickWithNullPort+"#"+str_info;

										if(isUserThreshSelected)
										{
											if(userThreshList != null && userThreshList.contains(strUserVal))
											{
												if (aListUserThreshIniTests != null && !aListUserThreshIniTests.contains(strUserVal))
												{
													aListUserThreshIniTests.add(strUserVal);
												}
												isNewTrgtEntry = true;
											}
										}

										if(isDescThreshSelected)
										{
											if(descThreshList != null && (descThreshList.contains(strDescVal) || descThreshList.contains(strGrpDescVal)))
											{
												if (aListDescThreshIniTests != null && !aListDescThreshIniTests.contains(strDescVal))
												{
													aListDescThreshIniTests.add(strDescVal);
												}
												isNewTrgtEntry = true;
											}
										}
									
										if(isGroupThreshSelected)
										{
											if(groupTestsList != null && groupTestsList.contains(strGrpVal))
											{
												if (aListInfoThreshIniEntries != null && !aListInfoThreshIniEntries.contains(testName))
												{
													aListInfoThreshIniEntries.add(testName);
												}
												isNewTrgtEntry = true;
											}
											if(infoBasedGroupList != null && infoBasedGroupList.contains(strGrpDescVal))
											{
												if (aListInfoThreshIniEntries != null && !aListInfoThreshIniEntries.contains(testName+"#"+str_info))
												{
													aListInfoThreshIniEntries.add(testName+"#"+str_info);
												}
												isNewTrgtEntry = true;
											}
										}

										if (!isNewTrgtEntry)
										{
											if(userThreshList != null && userThreshList.contains(strUserVal))
											{
												continue;
											}
											else if((descThreshList != null && (descThreshList.contains(strDescVal) || descThreshList.contains(strGrpDescVal))) || (infoBasedGroupList != null && infoBasedGroupList.contains(strGrpDescVal)))
											{
												continue;
											}
											else if(groupTestsList != null && groupTestsList.contains(strGrpVal))
											{
												continue;
											}
											else 
											{
												boolean isNewTrgtForDefaultDescriptor = false;
												if(isDescDefaultThreshSelected)
												{
													if (modifyDefaultDescTestList != null && modifyDefaultDescTestList.size() > 0)
													{
														if (modifyDefaultDescTestList.size() == 1)
														{
															String tmpPattern = (String) modifyDefaultDescTestList.get(0);
															String strDefaultDescVars = tmpPattern;
															boolean  isModifyDefaultDescriptor = ((Boolean) modifyDefaultDescMapTests.get(strDefaultDescVars)).booleanValue();
															tmpPattern = tmpPattern.substring(tmpPattern.indexOf(":")+1);

															if (isModifyDefaultDescriptor)
															{
																if (aListModifyThreshIniTests != null &&  !aListModifyThreshIniTests.contains(strDefaultDescVars))
																{
																	aListModifyThreshIniTests.add(strDefaultDescVars);
																}
															}
															else
															{
																if (aListThreshIniTests != null &&  !aListThreshIniTests.contains(strDefaultDescVars))
																{
																	aListThreshIniTests.add(strDefaultDescVars);
																}
															}
															if (com.egurkha.util.EgUtilities.isMatch(str_info, StringUtils.decodeSecurityChar(tmpPattern)))
															{
																isNewTrgtForDefaultDescriptor = true;
															}

														}
														else
														{
															for (int m=0;m<modifyDefaultDescTestList.size();m++)
															{
																String tmpPattern = (String) modifyDefaultDescTestList.get(m);
																String strDefaultDescVars = tmpPattern;
																boolean  isModifyDefaultDescriptor = ((Boolean) modifyDefaultDescMapTests.get(strDefaultDescVars)).booleanValue();

																tmpPattern = tmpPattern.substring(tmpPattern.indexOf(":")+1);

																if (com.egurkha.util.EgUtilities.isMatch(str_info, StringUtils.decodeSecurityChar(tmpPattern)))
																{
																	isNewTrgtForDefaultDescriptor = true;
																}

																if (isModifyDefaultDescriptor)
																{
																	if (aListModifyThreshIniTests != null &&  !aListModifyThreshIniTests.contains(strDefaultDescVars))
																	{
																		aListModifyThreshIniTests.add(strDefaultDescVars);
																	}


																}
																else
																{
																	if (aListThreshIniTests != null &&  !aListThreshIniTests.contains(strDefaultDescVars))
																	{
																		aListThreshIniTests.add(strDefaultDescVars);
																	}
																}
															}
														}
													}

													if (isNewTrgtForDefaultDescriptor && !isTestDisabled)
													{
														if (globalToDefaultThresholdsTest != null && !globalToDefaultThresholdsTest.contains(testName) || !isDefaultThreshSelected)
														{
															String htValues = (String) ht.get(htKey);
															aListThreshUpdateIniEntries.add(new IniHelper(IniHelper.SET,subjectVal,htValues,testName));
														}
													}
												}

												if (!isNewTrgtForDefaultDescriptor && isDefaultThreshSelected && !isTestDisabled)
												{
													if (globalToDefaultThresholdsTest != null && !globalToDefaultThresholdsTest.contains(testName))
													{
														if (modifyDefaultDescTestList != null && modifyDefaultDescTestList.size() > 0)
														{
															if (modifyDefaultDescTestList.size() == 1)
															{
																String tmpPattern = (String) modifyDefaultDescTestList.get(0);
																tmpPattern = tmpPattern.substring(tmpPattern.indexOf(":")+1);

																if (!com.egurkha.util.EgUtilities.isMatch(str_info, StringUtils.decodeSecurityChar(tmpPattern)))
																{
																	isNewTrgtForDefaultDescriptor = true;
																	String htValues = (String) ht.get(htKey);
																	aListThreshUpdateIniEntries.add(new IniHelper(IniHelper.SET,subjectVal,htValues,testName));

																}
															}
															else
															{
																for (int m=0;m<modifyDefaultDescTestList.size();m++)
																{
																	String tmpPattern = (String) modifyDefaultDescTestList.get(m);

																	tmpPattern = tmpPattern.substring(tmpPattern.indexOf(":")+1);

																	if	(!com.egurkha.util.EgUtilities.isMatch(str_info, StringUtils.decodeSecurityChar(tmpPattern)))
																	{
																		isNewTrgtForDefaultDescriptor = true;
																		String htValues = (String) ht.get(htKey);
																		aListThreshUpdateIniEntries.add(new IniHelper(IniHelper.SET,subjectVal,htValues,testName));
																	}
																}
															}
														}
														else
														{
															isNewTrgtForDefaultDescriptor = true;
															String htValues = (String) ht.get(htKey);
															aListThreshUpdateIniEntries.add(new IniHelper(IniHelper.SET,subjectVal,htValues,testName));
														}
													}
												}
											}
										}
										else 
										{
											if (!isTestDisabled)
											{
												if (globalToDefaultThresholdsTest != null && !globalToDefaultThresholdsTest.contains(testName) || !isDefaultThreshSelected)
												{
													String htValues = (String) ht.get(htKey);
													aListThreshUpdateIniEntries.add(new IniHelper(IniHelper.SET,subjectVal,htValues,testName));
												}
											}
										}
										
									}
									else
									{
										String strUserVal = testName+":"+tempComp;
										String strDescVal = testName+":"+tempComp+"#"+StringUtils.encodeSecurityChar(str_info);
										String strGrpDescVal = testName+":"+tempComp+"#"+str_info;

										if(isUserThreshSelected)
										{
											if(userThreshList != null && userThreshList.contains(strUserVal))
											{
												if (aListUserThreshIniTests != null && !aListUserThreshIniTests.contains(strUserVal))
												{
													aListUserThreshIniTests.add(strUserVal);
												}
												isNewTrgtEntry = true;
											}
										}
										
										if(isDescThreshSelected)
										{
											if(descThreshList != null && (descThreshList.contains(strDescVal) || descThreshList.contains(strGrpDescVal)))
											{
												if (aListDescThreshIniTests != null && !aListDescThreshIniTests.contains(strDescVal))
												{
													aListDescThreshIniTests.add(strDescVal);
												}
												isNewTrgtEntry = true;
											}
										}
										
										if(isGroupThreshSelected)
										{
											if(groupTestsList != null && groupTestsList.contains(strUserVal))
											{
												if (aListInfoThreshIniEntries != null && !aListInfoThreshIniEntries.contains(testName))
												{
													aListInfoThreshIniEntries.add(testName);
												}
												isNewTrgtEntry = true;
											}
											if(infoBasedGroupList != null && infoBasedGroupList.contains(strGrpDescVal))
											{
												if (aListInfoThreshIniEntries != null && !aListInfoThreshIniEntries.contains(testName+"#"+str_info))
												{
													aListInfoThreshIniEntries.add(testName+"#"+str_info);
												}
												isNewTrgtEntry = true;
											}
										}

										if (!isNewTrgtEntry)
										{
											if(userThreshList != null && userThreshList.contains(strUserVal))
											{
												continue;
											}
											else if((descThreshList != null && (descThreshList.contains(strDescVal) || descThreshList.contains(strGrpDescVal))) || (infoBasedGroupList != null && infoBasedGroupList.contains(strGrpDescVal)))
											{
												continue;
											}
											else if(groupTestsList != null && groupTestsList.contains(strUserVal))
											{
												continue;
											}
											else 
											{
												boolean isNewTrgtForDefaultDescriptor = false;
												if(isDescDefaultThreshSelected)
												{
													if (modifyDefaultDescTestList != null && modifyDefaultDescTestList.size() > 0)
													{
														if (modifyDefaultDescTestList.size() == 1)
														{
															String tmpPattern = (String) modifyDefaultDescTestList.get(0);

															String strDefaultDescVars = tmpPattern;
															boolean  isModifyDefaultDescriptor = ((Boolean) modifyDefaultDescMapTests.get(strDefaultDescVars)).booleanValue();

															tmpPattern = tmpPattern.substring(tmpPattern.indexOf(":")+1);

															if (isModifyDefaultDescriptor)
															{
																if (aListModifyThreshIniTests != null &&  !aListModifyThreshIniTests.contains(strDefaultDescVars))
																{
																	aListModifyThreshIniTests.add(strDefaultDescVars);
																}
															}
															else
															{
																if (aListThreshIniTests != null &&  !aListThreshIniTests.contains(strDefaultDescVars))
																{
																	aListThreshIniTests.add(strDefaultDescVars);
																}
															}

															if (com.egurkha.util.EgUtilities.isMatch(str_info, StringUtils.decodeSecurityChar(tmpPattern)))
															{
																isNewTrgtForDefaultDescriptor = true;
															}

														}
														else
														{
															for (int m=0;m<modifyDefaultDescTestList.size();m++)
															{
																String tmpPattern = (String) modifyDefaultDescTestList.get(m);
																String strDefaultDescVars = tmpPattern;
																boolean  isModifyDefaultDescriptor = ((Boolean) modifyDefaultDescMapTests.get(strDefaultDescVars)).booleanValue();
																tmpPattern = tmpPattern.substring(tmpPattern.indexOf(":")+1);

																if (com.egurkha.util.EgUtilities.isMatch(str_info, StringUtils.decodeSecurityChar(tmpPattern)))
																{
																	isNewTrgtForDefaultDescriptor = true;
																}
																
																if (isModifyDefaultDescriptor)
																{
																	if (aListModifyThreshIniTests != null &&  !aListModifyThreshIniTests.contains(strDefaultDescVars))
																	{
																		aListModifyThreshIniTests.add(strDefaultDescVars);
																	}


																}
																else
																{
																	if (aListThreshIniTests != null &&  !aListThreshIniTests.contains(strDefaultDescVars))
																	{
																		aListThreshIniTests.add(strDefaultDescVars);
																	}
																}
															}
														}
													}

													if (isNewTrgtForDefaultDescriptor && !isTestDisabled)
													{
														if (globalToDefaultThresholdsTest != null && !globalToDefaultThresholdsTest.contains(testName) || !isDefaultThreshSelected)
														{
															String htValues = (String) ht.get(htKey);
															aListThreshUpdateIniEntries.add(new IniHelper(IniHelper.SET,subjectVal,htValues,testName));
														}
													}
												}


												if (!isNewTrgtForDefaultDescriptor && isDefaultThreshSelected && !isTestDisabled)
												{
													if (globalToDefaultThresholdsTest != null && !globalToDefaultThresholdsTest.contains(testName))
													{
														if (modifyDefaultDescTestList != null && modifyDefaultDescTestList.size() > 0)
														{
															if (modifyDefaultDescTestList.size() == 1)
															{
																String tmpPattern = (String) modifyDefaultDescTestList.get(0);
																tmpPattern = tmpPattern.substring(tmpPattern.indexOf(":")+1);

																if (!com.egurkha.util.EgUtilities.isMatch(str_info, StringUtils.decodeSecurityChar(tmpPattern)))
																{
																	isNewTrgtForDefaultDescriptor = true;
																	String htValues = (String) ht.get(htKey);
																	aListThreshUpdateIniEntries.add(new IniHelper(IniHelper.SET,subjectVal,htValues,testName));

																}
															}
															else
															{
																for (int m=0;m<modifyDefaultDescTestList.size();m++)
																{
																	String tmpPattern = (String) modifyDefaultDescTestList.get(m);

																	tmpPattern = tmpPattern.substring(tmpPattern.indexOf(":")+1);

																	if	(!com.egurkha.util.EgUtilities.isMatch(str_info, StringUtils.decodeSecurityChar(tmpPattern)))
																	{
																		isNewTrgtForDefaultDescriptor = true;
																		String htValues = (String) ht.get(htKey);
																		aListThreshUpdateIniEntries.add(new IniHelper(IniHelper.SET,subjectVal,htValues,testName));
																	}
																}
															}
														}
														else
														{
															isNewTrgtForDefaultDescriptor = true;
															String htValues = (String) ht.get(htKey);
															aListThreshUpdateIniEntries.add(new IniHelper(IniHelper.SET,subjectVal,htValues,testName));
														}
													}
												}
											}
										}
										else 
										{
											if (!isTestDisabled)
											{
												if (globalToDefaultThresholdsTest != null && !globalToDefaultThresholdsTest.contains(testName) || isDefaultThreshSelected)
												{
													String htValues = (String) ht.get(htKey);
													aListThreshUpdateIniEntries.add(new IniHelper(IniHelper.SET,subjectVal,htValues,testName));
												}
											}
										}
									}
								}
								else
								{
									if(isUserThreshSelected)
									{
										if(userThreshList != null )
										{
											for (int ut=0;ut<userThreshList.size();ut++)
											{
												String strUserThresh = (String) userThreshList.get(ut);
												if (strUserThresh.startsWith(testName))
												{
													if (aListUserThreshIniTests != null && !aListUserThreshIniTests.contains(strUserThresh))
													{
														aListUserThreshIniTests.add(strUserThresh);
													}
												}
											}
										}
									}

									if(isDescThreshSelected)
									{
										if(descThreshList != null )
										{
											for (int dt=0;dt<descThreshList.size();dt++)
											{
												String strDescThresh = (String) descThreshList.get(dt);
												if (strDescThresh.startsWith(testName))
												{
													if (aListDescThreshIniTests != null && !aListDescThreshIniTests.contains(strDescThresh))
													{
														aListDescThreshIniTests.add(strDescThresh);
													}
												}
											}
										}
									}

									if(isGroupThreshSelected)
									{
										if(groupTestsList != null)
										{
											for (int gt=0;gt<groupTestsList.size();gt++)
											{
												String strGroupThresh = (String) groupTestsList.get(gt);
												if (strGroupThresh.startsWith(testName))
												{
													if (aListInfoThreshIniEntries != null && !aListInfoThreshIniEntries.contains(testName))
													{
														aListInfoThreshIniEntries.add(testName);
													}
												}
											}
										}

										if(infoBasedGroupList != null)
										{
											for (int igt=0;igt<infoBasedGroupList.size();igt++)
											{
												String strInfoGroupThresh = (String) infoBasedGroupList.get(igt);
												String strInfoThresh = strInfoGroupThresh.substring(strInfoGroupThresh.lastIndexOf("#")+1); 
												if (strInfoGroupThresh.startsWith(testName))
												{
													if (aListInfoThreshIniEntries != null && !aListInfoThreshIniEntries.contains(testName+"#"+strInfoThresh))
													{
														aListInfoThreshIniEntries.add(testName+"#"+strInfoThresh);
													}
												}
											}
										}
									}

									if(isDescDefaultThreshSelected)
									{
										if (modifyDefaultDescTestList != null && modifyDefaultDescTestList.size() > 0)
										{
											if (modifyDefaultDescTestList.size() == 1)
											{
												String tmpPattern = (String) modifyDefaultDescTestList.get(0);
												boolean  isModifyDefaultDescriptor = ((Boolean) modifyDefaultDescMapTests.get(tmpPattern)).booleanValue();

												if (tmpPattern.startsWith(testName))
												{

													if (isModifyDefaultDescriptor)
													{
														if (aListModifyThreshIniTests != null &&  !aListModifyThreshIniTests.contains(tmpPattern))
														{
															aListModifyThreshIniTests.add(tmpPattern);
														}

													}
													else
													{
														if (aListThreshIniTests != null &&  !aListThreshIniTests.contains(tmpPattern))
														{
															aListThreshIniTests.add(tmpPattern);
														}
													}
												}
											}
											else
											{
												for (int m=0;m<modifyDefaultDescTestList.size();m++)
												{
													String tmpPattern = (String) modifyDefaultDescTestList.get(m);
													boolean  isModifyDefaultDescriptor = ((Boolean) modifyDefaultDescMapTests.get(tmpPattern)).booleanValue();

													if (tmpPattern.startsWith(testName))
													{
														if (isModifyDefaultDescriptor)
														{
															if (aListModifyThreshIniTests != null &&  !aListModifyThreshIniTests.contains(tmpPattern))
															{
																aListModifyThreshIniTests.add(tmpPattern);
															}
														}
														else
														{
															if (aListThreshIniTests != null &&  !aListThreshIniTests.contains(tmpPattern))
															{
																aListThreshIniTests.add(tmpPattern);
															}
														}
													}
												}
											}
										}
									}
								}
							}
						}
						else
						{
							if(isUserThreshSelected)
							{
								if(userThreshList != null )
								{
									for (int ut=0;ut<userThreshList.size();ut++)
									{
										String strUserThresh = (String) userThreshList.get(ut);
										if (strUserThresh.startsWith(testName))
										{
											if (aListUserThreshIniTests != null && !aListUserThreshIniTests.contains(strUserThresh))
											{
												aListUserThreshIniTests.add(strUserThresh);
											}
										}
									}
								}
							}
										
							if(isDescThreshSelected)
							{
								if(descThreshList != null )
								{
									for (int dt=0;dt<descThreshList.size();dt++)
									{
										String strDescThresh = (String) descThreshList.get(dt);
										if (strDescThresh.startsWith(testName))
										{
											if (aListDescThreshIniTests != null && !aListDescThreshIniTests.contains(strDescThresh))
											{
												aListDescThreshIniTests.add(strDescThresh);
											}
										}
									}
								}
							}

							if(isGroupThreshSelected)
							{
								if(groupTestsList != null)
								{
									for (int gt=0;gt<groupTestsList.size();gt++)
									{
										String strGroupThresh = (String) groupTestsList.get(gt);
										if (strGroupThresh.startsWith(testName))
										{
											if (aListInfoThreshIniEntries != null && !aListInfoThreshIniEntries.contains(testName))
											{
												aListInfoThreshIniEntries.add(testName);
											}
										}
									}
								}

								if(infoBasedGroupList != null)
								{
									for (int igt=0;igt<infoBasedGroupList.size();igt++)
									{
										String strInfoGroupThresh = (String) infoBasedGroupList.get(igt);
										String strInfoForNotRunning = strInfoGroupThresh.substring(strInfoGroupThresh.lastIndexOf("#")+1); 
										if (strInfoGroupThresh.startsWith(testName))
										{
											if (aListInfoThreshIniEntries != null && !aListInfoThreshIniEntries.contains(testName+"#"+strInfoForNotRunning))
											{
												aListInfoThreshIniEntries.add(testName+"#"+strInfoForNotRunning);
											}
										}
									}
								}
							}

							if(isDescDefaultThreshSelected)
							{
								if (modifyDefaultDescTestList != null && modifyDefaultDescTestList.size() > 0)
								{
									if (modifyDefaultDescTestList.size() == 1)
									{
										String tmpPattern = (String) modifyDefaultDescTestList.get(0);
										boolean  isModifyDefaultDescriptor = ((Boolean) modifyDefaultDescMapTests.get(tmpPattern)).booleanValue();

										if (tmpPattern.startsWith(testName))
										{
											if (isModifyDefaultDescriptor)
											{
												if (aListModifyThreshIniTests != null &&  !aListModifyThreshIniTests.contains(tmpPattern))
												{
													aListModifyThreshIniTests.add(tmpPattern);
												}
											}
											else
											{
												if (aListThreshIniTests != null &&  !aListThreshIniTests.contains(tmpPattern))
												{
													aListThreshIniTests.add(tmpPattern);
												}
											}
										}
									}
									else
									{
										for (int m=0;m<modifyDefaultDescTestList.size();m++)
										{
											String tmpPattern = (String) modifyDefaultDescTestList.get(m);
											boolean  isModifyDefaultDescriptor = ((Boolean) modifyDefaultDescMapTests.get(tmpPattern)).booleanValue();

											if (tmpPattern.startsWith(testName))
											{
												if (isModifyDefaultDescriptor)
												{
													if (aListModifyThreshIniTests != null &&  !aListModifyThreshIniTests.contains(tmpPattern))
													{
														aListModifyThreshIniTests.add(tmpPattern);
													}
												}
												else
												{
													if (aListThreshIniTests != null &&  !aListThreshIniTests.contains(tmpPattern))
													{
														aListThreshIniTests.add(tmpPattern);
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}

			if (aListUserThreshIniTests != null && aListUserThreshIniTests.size() > 0)
			{
				for (int u=0;u<aListUserThreshIniTests.size();u++)
				{
					String alistUserThreshStr = (String) aListUserThreshIniTests.get(u);
					aListThreshIniEntries.add(new IniHelper(IniHelper.DELETE,user_thresh,alistUserThreshStr,""));
				}
			}

			if (aListDescThreshIniTests != null && aListDescThreshIniTests.size() > 0)
			{
				for (int d=0;d<aListDescThreshIniTests.size();d++)
				{
					String alistDescThreshStr = (String) aListDescThreshIniTests.get(d);
					aListThreshIniEntries.add(new IniHelper(IniHelper.DELETE,"DESCRIPTOR_THRESHOLDS",alistDescThreshStr,""));
				}
			}

			if (aListModifyThreshIniTests != null && aListModifyThreshIniTests.size() > 0)
			{
				for (int md=0;md<aListModifyThreshIniTests.size();md++)
				{
					String strModifyDefaultDesc = (String) aListModifyThreshIniTests.get(md);
					String defaultDescValue = resetDefaultIni.getValue(defaultDescSubject,strModifyDefaultDesc);
					aListThreshIniEntries.add(new IniHelper(IniHelper.SET,defaultDescSubject,strModifyDefaultDesc,defaultDescValue));
				}
			}

			if (aListThreshIniTests != null && aListThreshIniTests.size() > 0)
			{
				for (int d=0;d<aListThreshIniTests.size();d++)
				{
					aListThreshIniEntries.add(new IniHelper(IniHelper.DELETE,defaultDescSubject,(String)aListThreshIniTests.get(d),""));
				}
			}
		}
		else
		{
			if (isDefaultThreshSelected)
			{
				if (globalToDefaultThresholdsTest != null && globalToDefaultThresholdsTest.contains(testName))
				{
					ArrayList threshIniEntries = new ArrayList();
					ArrayList infoGrpIniEntries = new ArrayList();

					String groupSubject = "THRESH_POLICY_MAPPING";
					String ruleSubject = "THRESHOLDPOLICIES";
					String[] delUsrThreshVal = prgBean.getVariables(user_thresh);
					String[] delDescThreshVal = prgBean.getVariables(descr_thresh);
					
					//default descriptor threshold
					ArrayList delDefaultDescTestList = new ArrayList();
					ArrayList modifyDefaultDescTests = new ArrayList();

					//group threshold
					IniFile infoGroupIni = new IniFile(EgInstallInfo.getInstallDir()+"/manager/config/eg_infogroup.ini");
					String[] threshPolicyVars = infoGroupIni.getVariables(groupSubject);
					String[] threshPolicyRuleVars = infoGroupIni.getVariables(ruleSubject);

					//removing eg_threshupdate.ini starts here...
					IniFile threshUpdateIni = new IniFile(EgInstallInfo.getInstallDir()+"/manager/config/eg_threshupdate.ini");
					String subject = "Newtarget_Update_EgThresholdManager";
					String[] threshUpdateArrData = threshUpdateIni.getVariables(subject);
					ArrayList threshUpdateIniEntries = new ArrayList();

					//removing eg_threshupdate.ini ends here...

					if (defaultDescVars != null && defaultDescVars.length > 0)
					{
						for (int dd=0; dd<defaultDescVars.length; dd++)
						{
							String factDefaultDescVal = resetDefaultIni.getValue(defaultDescSubject,defaultDescVars[dd]);
							String currDefaultDescVal = prgBean.getStrValue(defaultDescSubject,defaultDescVars[dd]);
							if (factDefaultDescVal == null || factDefaultDescVal.length() == 0)
							{
								delDefaultDescTestList.add(defaultDescVars[dd]);
							}
							else if (!StringUtils.isNull(currDefaultDescVal) && !StringUtils.isNull(factDefaultDescVal))
							{
								if (!currDefaultDescVal.equals(factDefaultDescVal))
								{
									modifyDefaultDescTests.add(defaultDescVars[dd]);
								}
							}
						}
					}

					if(delDescThreshVal.length > 0)
					{
						for(int r=0; r<delDescThreshVal.length; r++)
						{
							if(delDescThreshVal[r].startsWith(testName))
							{
								threshIniEntries.add(new IniHelper(IniHelper.DELETE,descr_thresh,delDescThreshVal[r],""));
							}
						}
					}

					if(delUsrThreshVal.length > 0)
					{
						for(int r=0; r<delUsrThreshVal.length; r++)
						{
							if(delUsrThreshVal[r].startsWith(testName))
							{
								threshIniEntries.add(new IniHelper(IniHelper.DELETE,user_thresh,delUsrThreshVal[r],""));
							}
						}
					}

					if(threshPolicyVars != null && threshPolicyVars.length > 0)
					{
						int x = threshPolicyVars.length;
						for(int ii=0;ii<x;ii++)
						{
							StringTokenizer stok = new StringTokenizer(threshPolicyVars[ii],"#");
							String servergroup = stok.nextToken();
							String grpTestName = stok.nextToken();
							if(grpTestName.startsWith(testName))
							{
								infoGrpIniEntries.add(new IniHelper(IniHelper.DELETE,groupSubject,threshPolicyVars[ii],""));
							}
						}
					}

					if(threshPolicyRuleVars != null && threshPolicyRuleVars.length > 0)
					{
						int x = threshPolicyRuleVars.length;
						for(int ii=0;ii<x;ii++)
						{
							String grpTestName = threshPolicyRuleVars[ii];
							if(grpTestName.startsWith(testName))
							{
								infoGrpIniEntries.add(new IniHelper(IniHelper.DELETE,ruleSubject,threshPolicyRuleVars[ii],""));
							}
						}
					}

					if(delDefaultDescTestList != null)
					{
						for(int r=0; r<delDefaultDescTestList.size(); r++)
						{
							if(delDefaultDescTestList.get(r).toString().startsWith(testName))
							{
								threshIniEntries.add(new IniHelper(IniHelper.DELETE,defaultDescSubject,(String)delDefaultDescTestList.get(r),""));
							}
						}
					}

					if (modifyDefaultDescTests != null && modifyDefaultDescTests.size() > 0)
					{
						for (int md=0;md<modifyDefaultDescTests.size();md++)
						{
							String strModifyDefaultDesc = (String) modifyDefaultDescTests.get(md);
							String defaultDescValue = resetDefaultIni.getValue(defaultDescSubject,strModifyDefaultDesc);
							threshIniEntries.add(new IniHelper(IniHelper.SET,defaultDescSubject,strModifyDefaultDesc,defaultDescValue));
						}
					}

					//removing eg_threshupdate.ini code starts here...
					if(threshUpdateArrData != null && threshUpdateArrData.length > 0 )
					{
						for(int tu=0;tu<threshUpdateArrData.length;tu++)
						{
							String tmpThreshUpdateData = threshUpdateArrData[tu];
							if(tmpThreshUpdateData.endsWith("#&#"+testName))
							{
								threshUpdateIniEntries.add(new IniHelper(IniHelper.DELETE,subject,tmpThreshUpdateData,""));

							}
						}
					}

					if (threshUpdateIniEntries != null && threshUpdateIniEntries.size() > 0)
					{
						IniFile threshUpdateIniObj = EgConfigFilesSync.getInstance(EgInstallInfo.getInstallDir()+"/manager/config/eg_threshupdate.ini");
						for (int i=0; i<threshUpdateIniEntries.size(); i++)
						{
							IniHelper ini = (IniHelper)threshUpdateIniEntries.get(i);
							String action = ini.getAction();
							String sub = ini.getSubject();
							String var = ini.getVariable();
							String val = ini.getValue();
							if (action != null && action.equals(IniHelper.DELETE))
							{
								threshUpdateIniObj.deleteValue(sub,var);
							}
						}
						threshUpdateIniObj.saveFile();
						EgConfigFilesSync.releaseInstance(threshUpdateIniObj);
					}
					//removing eg_threshupdate.ini code ends here...

					if (threshIniEntries != null && threshIniEntries.size() > 0)
					{
						IniFile threshIniSync = EgConfigFilesSync.getInstance(EgConfigInfo.egMgrConfigDir + EgConfigInfo.egThresholdConfigFile);
						for (int i=0; i<threshIniEntries.size(); i++)
						{
							IniHelper ini = (IniHelper)threshIniEntries.get(i);
							String action = ini.getAction();
							String sub = ini.getSubject();
							String var = ini.getVariable();
							String val = ini.getValue();
							if (action != null && action.equals(IniHelper.DELETE))
							{
								threshIniSync.deleteValue(sub,var);
							}
							else if (action != null && action.equals(IniHelper.SET))
							{
								threshIniSync.setValue(sub,var,val);
							}
						}
						threshIniSync.saveFile();
						EgConfigFilesSync.releaseInstance(threshIniSync);
					}

					if (infoGrpIniEntries != null && infoGrpIniEntries.size() > 0)
					{
						IniFile infoGrpSync = EgConfigFilesSync.getInstance(EgConfigInfo.egMgrConfigDir + EgConfigInfo.egInfoGroupFile); 
						for (int i=0; i<infoGrpIniEntries.size(); i++)
						{
							IniHelper ini = (IniHelper)infoGrpIniEntries.get(i);
							String action = ini.getAction();
							String sub = ini.getSubject();
							String var = ini.getVariable();
							String val = ini.getValue();
							if (action != null && action.equals(IniHelper.DELETE))
							{
								infoGrpSync.deleteValue(sub,var);
							}
						}
						infoGrpSync.saveFile();
						EgConfigFilesSync.releaseInstance(infoGrpSync);
					}
				}
			}

			if (isDescDefaultThreshSelected)
			{
				if (globalThresholds != null && globalThresholds.contains(testName))
				{
					ArrayList threshIniEntries = new ArrayList();

					//default descriptor threshold
					ArrayList delDefaultDescTestList = new ArrayList();
					ArrayList modifyDefaultDescTests = new ArrayList();


					if (defaultDescVars != null && defaultDescVars.length > 0)
					{
						for (int dd=0; dd<defaultDescVars.length; dd++)
						{
							String factDefaultDescVal = resetDefaultIni.getValue(defaultDescSubject,defaultDescVars[dd]);
							String currDefaultDescVal = prgBean.getStrValue(defaultDescSubject,defaultDescVars[dd]);
							if (factDefaultDescVal == null || factDefaultDescVal.length() == 0)
							{
								delDefaultDescTestList.add(defaultDescVars[dd]);
							}
							else if (!StringUtils.isNull(currDefaultDescVal) && !StringUtils.isNull(factDefaultDescVal))
							{
								if (!currDefaultDescVal.equals(factDefaultDescVal))
								{
									modifyDefaultDescTests.add(defaultDescVars[dd]);
								}
							}
						}
					}


					if(delDefaultDescTestList != null)
					{
						for(int r=0; r<delDefaultDescTestList.size(); r++)
						{
							if(delDefaultDescTestList.get(r).toString().startsWith(testName))
							{
								threshIniEntries.add(new IniHelper(IniHelper.DELETE,defaultDescSubject,(String)delDefaultDescTestList.get(r),""));
							}
						}
					}

					if (modifyDefaultDescTests != null && modifyDefaultDescTests.size() > 0)
					{
						for (int md=0;md<modifyDefaultDescTests.size();md++)
						{
							String strModifyDefaultDesc = (String) modifyDefaultDescTests.get(md);
							String defaultDescValue = resetDefaultIni.getValue(defaultDescSubject,strModifyDefaultDesc);
							threshIniEntries.add(new IniHelper(IniHelper.SET,defaultDescSubject,strModifyDefaultDesc,defaultDescValue));
						}
					}

					if (threshIniEntries != null && threshIniEntries.size() > 0)
					{
						IniFile threshIniSync = EgConfigFilesSync.getInstance(EgConfigInfo.egMgrConfigDir + EgConfigInfo.egThresholdConfigFile);
						for (int i=0; i<threshIniEntries.size(); i++)
						{
							IniHelper ini = (IniHelper)threshIniEntries.get(i);
							String action = ini.getAction();
							String sub = ini.getSubject();
							String var = ini.getVariable();
							String val = ini.getValue();
							if (action != null && action.equals(IniHelper.DELETE))
							{
								threshIniSync.deleteValue(sub,var);
							}
							else if (action != null && action.equals(IniHelper.SET))
							{
								threshIniSync.setValue(sub,var,val);
							}
						}
						threshIniSync.saveFile();
						EgConfigFilesSync.releaseInstance(threshIniSync);
					}
				}
			}
		}

		if (!StringUtils.isNull(newDefaultThreshold))
		{
			String strThresholdIni = EgInstallInfo.getInstallDir()+"/manager/config/eg_thresholds.ini";
			IniFile ThresholdIniWrite = EgConfigFilesSync.getInstance(strThresholdIni);
			ThresholdIniWrite.setValue("DEFAULT_THRESHOLDS",testName,newDefaultThreshold);
			ThresholdIniWrite.saveFile();
			EgConfigFilesSync.releaseInstance(ThresholdIniWrite);
		}

		if (aListThreshIniEntries != null && aListThreshIniEntries.size() > 0)
		{
			IniFile eg_threshIniSync = EgConfigFilesSync.getInstance(EgConfigInfo.egMgrConfigDir + "eg_thresholds.ini");
			for (int i=0;i<aListThreshIniEntries.size();i++)
			{
				IniHelper iniHelper= (IniHelper)aListThreshIniEntries.get(i);
				String iniAction = iniHelper.getAction();
				String sub = iniHelper.getSubject();
				String key = iniHelper.getVariable();
				String val = iniHelper.getValue();
				if (iniAction != null)
				{
					if (iniAction.equals(IniHelper.DELETE))
					{
						eg_threshIniSync.deleteValue(sub,key);
					}
					else if (iniAction.equals(IniHelper.SET))
					{
						eg_threshIniSync.setValue(sub,key,val);
					}
				}
			}
			eg_threshIniSync.saveFile();
			EgConfigFilesSync.releaseInstance(eg_threshIniSync);
		}

		if (aListInfoThreshIniEntries != null && aListInfoThreshIniEntries.size() > 0)
		{
			IniFile eg_infoGrpIniSync = EgConfigFilesSync.getInstance(EgConfigInfo.egMgrConfigDir + "eg_infogroup.ini");
			String infoGrpSubject = "THRESH_POLICY_MAPPING";
			String[] infoGrpAllVars = eg_infoGrpIniSync.getVariables(infoGrpSubject);
			ArrayList infoGrpVarList = new ArrayList(Arrays.asList(infoGrpAllVars));
			if (infoGrpAllVars != null)
			{
				if(infoGrpAllVars.length == 1 )
				{
					aListInfoThreshIniEntries = new ArrayList();
					aListInfoThreshIniEntries.add(infoGrpAllVars[0]);
				}
				else
				{
					ArrayList newInfoGrpList = new ArrayList();
					for(int in=0;in<infoGrpAllVars.length;in++)
					{
						String tmpInfoGrp = infoGrpAllVars[in];
						for (int al=0;al<aListInfoThreshIniEntries.size();al++)
						{
							if (StringUtils.decodeSecurityChar(tmpInfoGrp).endsWith(aListInfoThreshIniEntries.get(al).toString()))
							{
								newInfoGrpList.add(tmpInfoGrp);
							}
						}
					}

					aListInfoThreshIniEntries = new ArrayList();
					aListInfoThreshIniEntries.addAll(newInfoGrpList);
				}
			}

			for (int i=0;i<aListInfoThreshIniEntries.size();i++)
			{
				String keyVariable = (String) aListInfoThreshIniEntries.get(i);
				if (keyVariable != null )
				{
					String value = eg_infoGrpIniSync.getValue(infoGrpSubject,keyVariable);
					if (!StringUtils.isNull(value))
					{
						eg_infoGrpIniSync.deleteValue(infoGrpSubject,keyVariable);
					}
				}
			}
			eg_infoGrpIniSync.saveFile();
			EgConfigFilesSync.releaseInstance(eg_infoGrpIniSync);
		}

		if (aListThreshUpdateIniEntries != null && aListThreshUpdateIniEntries.size() > 0)
		{
			IniFile serIniWr = EgConfigFilesSync.getInstance(EgConfigInfo.egMgrConfigDir + "eg_threshupdate.ini");
			for (int i=0;i<aListThreshUpdateIniEntries.size();i++)
			{
				IniHelper iniHelper= (IniHelper)aListThreshUpdateIniEntries.get(i);
				String iniAction = iniHelper.getAction();
				String sub = iniHelper.getSubject();
				String key = iniHelper.getVariable();
				String val = iniHelper.getValue();
				if (iniAction != null)
				{
					if (iniAction.equals(IniHelper.SET))
					{
						serIniWr.setValue(sub,key,val);
					}
				}
			}
			serIniWr.saveFile();
			EgConfigFilesSync.releaseInstance(serIniWr);
		}

		if (isGlobalThreshSelected || isDefaultThreshSelected)
		{
			String strTestsIni = EgInstallInfo.getInstallDir()+"/manager/config/eg_tests.ini";
			IniFile testIniWrite = EgConfigFilesSync.getInstance(strTestsIni);

			boolean isNotExistGlobalTest = false;
			if (newTrgtGlobalMapTests != null && newTrgtGlobalMapTests.containsKey(testName))
			{
				isNotExistGlobalTest = ((Boolean) newTrgtGlobalMapTests.get(testName)).booleanValue();
			}
			
			if (isNotExistGlobalTest && isGlobalThreshSelected)
			{
				testIniWrite.deleteValue("GLOBAL_THRESHOLDS",testName);
			}
			
			if (globalToDefaultThresholdsTest != null && globalToDefaultThresholdsTest.contains(testName) && isDefaultThreshSelected)
			{
				testIniWrite.setValue("GLOBAL_THRESHOLDS",testName,"yes");
			}
			testIniWrite.saveFile();
			EgConfigFilesSync.releaseInstance(testIniWrite);
		}

		rootJson.put("success",true);
		rootJson.put("successUpdate",true);
	}
	out.println(rootJson.toString());
%>

<%!

	public ArrayList getAllManagedComps()
	{
		ArrayList aListPorts = new ArrayList();
		EgDiscInfo2 discInfo2 = new EgDiscInfo2("manager");
		List allHosts = discInfo2.getAllManagedComponents();
		for (int i=0;i<allHosts.size();i++)
		{
			String strTemp = "";
			String allData = (String)allHosts.get(i);
			StringTokenizer st = new StringTokenizer(allData,",");
			String serType = st.nextToken();
			String ipVal = st.nextToken();
			String nick = "";
			String port = "";
			String sid = "";
			if(serType.equals("Oracle_server"))
			{
				nick = st.nextToken();
				port = st.nextToken();
				String isPassive = st.nextToken();
				if (!st.hasMoreTokens())
					continue;
				sid = st.nextToken();
				strTemp = serType+":"+nick+":"+port+":"+sid;
			}
			else
			{
				nick = st.nextToken();
				port = st.nextToken();
				strTemp = serType+":"+nick+":"+port;
			}
			aListPorts.add(strTemp);
		}
		return aListPorts;
	}

	public boolean isTestDisabledForNick(String nickNameWithPort,String testName)
	{
		String nickName = nickNameWithPort.substring(0,nickNameWithPort.indexOf(":"));
		EgDiscInfo egDiscs = new EgDiscInfo("manager");
		EgDisableInfo egDisable = new EgDisableInfo("manager");

		List serverTypeList = egDiscs.getServerTypesForNick(nickName);
		if (serverTypeList != null && serverTypeList.size() > 0)
		{
			if (serverTypeList.size() == 1)
			{
				String tmpServer = (String) serverTypeList.get(0);
				ArrayList excludeTestsForSingleServer = egDisable.getExcludedTestsForComponent(nickNameWithPort,tmpServer);

				if (excludeTestsForSingleServer != null && excludeTestsForSingleServer.contains(testName))
				{
					return true;
				}
			}
			else
			{
				ArrayList excludeTestForMultiServers = new ArrayList();
				for (int s=0;s<serverTypeList.size();s++)
				{
					String server = (String) serverTypeList.get(s);
					ArrayList excludeTests = egDisable.getExcludedTestsForComponent(nickNameWithPort,server);
					if (excludeTests != null)
					{
						excludeTestForMultiServers.addAll(excludeTests);
					}
				}

				if (excludeTestForMultiServers != null && excludeTestForMultiServers.contains(testName))
				{
					return true;
				}
			}
		}
		return false;
	}
%>



