<%
/*****************************************************************
*	Name            :       Egassociatethreshpolicyp.jsp
*	Purpose         :     
******************************************************************/
%>

<%@ include file = "../../EgTrackerAJSON.jsp" %>
<%@ page import = "com.eg.EgServerGroupBean" %>
<%@ page import = "com.eg.EgThresholdInfo" %>
<%@ page import = "com.egurkha.util.*" %>
<%@ page import = "com.egurkha.admin.AuditLogThread"%>
<%@ page import = "com.eg.EgConfigFilesSync"%>
<%@ page import = "com.eg.IniFile"%>
<%@ page import = "com.egurkha.util.StringUtils"%>
<%@ page import ="egurkha.EgDbAdapterUI"%>
<%@ page import ="com.eg.EgDbAdapterTypeUI"%>
<%@ page import = "com.eg.EgConfigFilesSync"%>
<%@ page import="java.sql.*" %>
<%@ page import="com.eg.EgConfigInfo" %>
<%@ page import="com.eg.EgSpecsInfo"%>
<%@ page import= "net.sf.json.*"%>
<%@ page import= "egurkha.EgUIUtils"%>


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

<%
	response.setHeader("Cache-Control","no-store");
	response.setHeader("Pragma","no-cache");
	response.setDateHeader("Expires", -1);
%>
<jsp:useBean id="prgBean" class="egurkha.EgTestParamBean" scope="page" />

<%
	JSONObject rootJson = new JSONObject();
	EgServerGroupBean descGrpp = new EgServerGroupBean();
	EgThresholdInfo threshInfo = new EgThresholdInfo("manager");
	EgSpecsInfo specsInfo = new EgSpecsInfo("manager");
	EgDbAdapterUI egDb;
	EgConfigInfo configInfo = new EgConfigInfo("manager");

	String subject = "THRESH_POLICY_MAPPING";
	String subject1 = "THRESHOLDPOLICIES";
	ResultSet rs = null;
	String status ="";
	TreeSet allComps = new TreeSet();
	ArrayList compWithInfoList = new ArrayList();
	ArrayList managedCompList = new ArrayList();
	ArrayList changedCompList = new ArrayList();
	ArrayList preManagedGrps = new ArrayList();
	ArrayList newManagedGrps = new ArrayList();
	ArrayList preInfos = new ArrayList();
	ArrayList newInfos = new ArrayList();
	boolean isCompAvailable = false;
	boolean isInfoAvailable = false;
	boolean isSpecificRule = false;
	String previouslyManaged = request.getParameter("pfmanage");
	String serverGrp = request.getParameter("fmanage");

	if(previouslyManaged == null)
	{
		previouslyManaged = (String)session.getAttribute("pfmanage");
	}
	if(serverGrp == null)
	{
		serverGrp = (String) session.getAttribute("managedServer");
	}
	if(serverGrp == null)
	{
		serverGrp = "";
	}

	String policyName = request.getParameter("policyName");
	String testName = descGrpp.getTestNameForPolicy(policyName);
	String infos = request.getParameter("fmanageInfos");

	if (!StringUtils.isNull(infos))
	{
		infos = EgUIUtils.decodeSecurityChar(infos);
	}

	String unManagedinfos = request.getParameter("funmanage");

	if (!StringUtils.isNull(unManagedinfos))
	{
		unManagedinfos = EgUIUtils.decodeSecurityChar(unManagedinfos);
	}
	String pageFrom = request.getParameter("pageFrom");
	String applyRule = request.getParameter("applyRule");
	Vector vec_policy = descGrpp.getAllPolicy(subject);
	pageFrom = ((pageFrom != null && pageFrom.length() >0)?pageFrom:"");
	applyRule = ((applyRule != null && applyRule.length() >0)?applyRule:"");
	String strComponent = request.getParameter("strComp");

	if (pageFrom.equalsIgnoreCase("InfoBased") && !StringUtils.isNull(strComponent) && (strComponent.equals("managed") || strComponent.equals("unmanaged")))
	{
		String manageComps = "";
		String strOriginalManagedInfo = request.getParameter("strOriginalManagedInfo");
		if (!StringUtils.isNull(strOriginalManagedInfo))
		{
			strOriginalManagedInfo = EgUIUtils.decodeSecurityChar(strOriginalManagedInfo);
		}

		String strOriginalUnManagedInfo = request.getParameter("strOriginalUnManagedInfo");
		if (!StringUtils.isNull(strOriginalUnManagedInfo))
		{
			strOriginalUnManagedInfo = EgUIUtils.decodeSecurityChar(strOriginalUnManagedInfo);
		}
		ArrayList manInfoData = StringUtils.egTokenizer(strOriginalManagedInfo,",");
		ArrayList unManInfoFromCombo = StringUtils.egTokenizer(unManagedinfos,",");
		ArrayList manInfoFromCombo = StringUtils.egTokenizer(infos,",");
		if ((unManInfoFromCombo != null && unManInfoFromCombo.size() > 0) && (manInfoData != null && manInfoData.size() > 0))
		{
			manInfoData.removeAll(unManInfoFromCombo);
		}
		
		if (manInfoData == null || manInfoData.size() == 0)
		{
			manInfoData = new ArrayList();
		}

		if (manInfoFromCombo != null && manInfoFromCombo.size() > 0)
		{
			for(int inf=0;inf<manInfoFromCombo.size();inf++)
			{
				String tmpManInfo = manInfoFromCombo.get(inf).toString();
				if(manInfoData != null && !manInfoData.contains(tmpManInfo))
				{
					manInfoData.add(tmpManInfo);
				}
			}
		}
		
		if (manInfoData != null && manInfoData.size() > 0)
		{
			for (int m=0; m<manInfoData.size(); m++)
			{
				String temp = manInfoData.get(m).toString();
				manageComps += temp+",";
			}
			manageComps = manageComps.substring(0,manageComps.length()-1);
		}
		infos =","+manageComps;
	}

	/* 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");
	auditLogThread.setValue("Activity","Threshold Rule Association");
	/* code for auditlog ends */
	//if(infos != null && !infos.equals(""))//if(pageFrom.equalsIgnoreCase("Infos"))
	IniFile iniFileWr = null;

// code starts here for new ini entry for immediate update in DB 

	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]);
			}
		}
	}

	String[] userThreshVal = prgBean.getVariables("USER_THRESHOLDS");
	ArrayList userThreshList = new ArrayList();
	String[] arrHostTests = specsInfo.getDefaultTestsForServer("Host_system");
	ArrayList hostLevelTests = null;
	if (arrHostTests != null)
	{
		hostLevelTests = new ArrayList(Arrays.asList(arrHostTests));
	}

	if(userThreshVal.length > 0)
	{
		for(int r=0; r<userThreshVal.length; r++)
		{
			if(userThreshVal[r].startsWith(testName))
			{
				userThreshList.add(userThreshVal[r]);
			}
		}
	}

	IniFile serIniWr = EgConfigFilesSync.getInstance(EgInstallInfo.getInstallDir()+"/manager/config/eg_threshupdate.ini");
	Hashtable ht = new Hashtable();
	String subjectVal = "Newtarget_Update_EgThresholdManager";
	boolean isSaveIni = false;

	if(!StringUtils.isNull(testName))
	{
		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);
					ht.put(hostVal+":"+portVal+"#"+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);
		}
	}
	
	allComps = descGrpp.getTestColnComponentsForServerType();
	ArrayList allCompList = new ArrayList();
	ArrayList preCompList = new ArrayList();
	ArrayList newCompList = new ArrayList();
	Iterator itrAll = allComps.iterator();
	while(itrAll.hasNext())
	{
		String tmpData = (String) itrAll.next();
		if(tmpData.startsWith(testName))
		{
			allCompList.add(tmpData);
		}
	}

	if(!StringUtils.isNull(previouslyManaged))
	{
		preManagedGrps = StringUtils.egTokenizer(previouslyManaged,",");
	}
	if(!StringUtils.isNull(serverGrp))
	{
		newManagedGrps = StringUtils.egTokenizer(serverGrp,",");
	}

// code ends here for new ini entry for immediate update in DB
	ArrayList specificInfos = new ArrayList();
	TreeSet tSet = new TreeSet();

	if(pageFrom.equalsIgnoreCase("InfoBased"))
	{
		Vector sess_vec = new Vector();
		sess_vec.add(_mapping.getTextDescription("admin.threshold.Associate / Disassociate descriptors"));
		String servType = (String)session.getAttribute("serverTypeForPolicy");
		String test_Name = (String)session.getAttribute("TestNameForPolicy");
		if(!StringUtils.isNull(servType) && !StringUtils.isNull(test_Name) && servType.equalsIgnoreCase("Groups"))
		{
			sess_vec.add("EgEntryForServerGroup.jsp?ServerType="+servType+"&TestName="+test_Name);
		}
		else
		{
			sess_vec.add("EgEntryForServerGroup.jsp?serv="+servType+"&test="+test_Name);
		}
		sess_vec.add(_mapping.getTextDescription("admin.threshold.Associate / Disassociate descriptors"));
		sess_vec.add(_mapping.getTextDescription("admin.threshold.Your changes have been updated successfully")+".");
		session.putValue("vec_Results",sess_vec);
		tSet = descGrpp.getAllManagedInfosForTest(testName);
		Iterator itr = tSet.iterator();
		while(itr.hasNext())
		{
			preInfos.add(itr.next());
		}

		if(vec_policy != null && vec_policy.size() > 0 && vec_policy.contains(policyName))
		{
			iniFileWr = EgConfigFilesSync.getInstance(EgInstallInfo.getInstallDir()+"/manager/config/eg_infogroup.ini");
			//String varToDel = descGrpp.getMappingEntryForPolicyToDelete(subject,policyName,"Specific");
			ArrayList allVarToDel = descGrpp.getMappingAllEntryForPolicyToDelete(subject,policyName,"Specific");
			Vector delVar = new Vector();
			for(int v=0;v<allVarToDel.size();v++)
			{
				String delVariable = (String) allVarToDel.get(v);
				delVar.add(delVariable);
				//iniFileWr.deleteValue(subject,delVariable);
			}
			iniFileWr.deleteValues(subject,delVar);

			String variable = "";
			StringTokenizer stok = new StringTokenizer(serverGrp,",");
			while(stok.hasMoreTokens())
			{
				String serverGroup = stok.nextToken();
				StringTokenizer stok1 = new StringTokenizer(infos,",");
				while(stok1.hasMoreTokens())
				{
					String tmpInfo = stok1.nextToken();
					if (tmpInfo.indexOf("#") > -1)
					{
						tmpInfo = StringUtils.replace(tmpInfo,"#","0eGHash1");
					}
					variable = serverGroup+"#"+testName+"#"+StringUtils.encodeSecurityChar(tmpInfo);
					String descIniVal = iniFileWr.getValue(subject,serverGroup+"#"+testName+"#"+tmpInfo);
					if(!StringUtils.isNull(descIniVal))
					{
						iniFileWr.deleteValue(subject,serverGroup+"#"+testName+"#"+tmpInfo);
					}
					iniFileWr.setValue(subject,variable,policyName);
				}
			}

			String strHideInfoVal = request.getParameter("strHideInfoVal");
			if(!StringUtils.isNull(strHideInfoVal))
			{
				strHideInfoVal = strHideInfoVal.substring(1);
				StringTokenizer stHidInfo = new StringTokenizer(strHideInfoVal,",");
				while(stHidInfo.hasMoreTokens())
				{
					variable = stHidInfo.nextToken();
					String descIniVal = iniFileWr.getValue(subject,StringUtils.decodeSecurityChar(variable));
					if(!StringUtils.isNull(descIniVal))
					{
						iniFileWr.deleteValue(subject,StringUtils.decodeSecurityChar(variable));
					}
					iniFileWr.setValue(subject,variable,policyName);
				}
			}

			iniFileWr.saveFile();
			EgConfigFilesSync.releaseInstance(iniFileWr);


			/* code for auditlog starts */
			boolean isAssociated = false;
			boolean isDisassociated = false;
			ArrayList currentComponents = new ArrayList();
			StringTokenizer st = new StringTokenizer(serverGrp,",");
			if(st.countTokens()>0)
			{
				while(st.hasMoreTokens())
				{
					String componentName = (String) st.nextToken();
					if (componentName.endsWith(":NULL"))
						componentName = componentName.substring(0, componentName.indexOf(":"));
					currentComponents.add(componentName);
					//auditLogThread.setCurrentValue("Components",componentName);
				}
			}

			st = new StringTokenizer(previouslyManaged,",");
			if(st.countTokens()>0)
			{
				while(st.hasMoreTokens())
				{
					String componentName = (String) st.nextToken();
					if (componentName.endsWith(":NULL"))
						componentName = componentName.substring(0, componentName.indexOf(":"));
					if(!currentComponents.contains(componentName))
					{
						auditLogThread.setCurrentValue("Disassociated component groups",componentName);
						isDisassociated = true;
					}
					else
						currentComponents.remove(componentName);
				}
			}
			for(int i=0;i<currentComponents.size();i++)
			{
				auditLogThread.setCurrentValue("Associated component groups",(String) currentComponents.get(i));
				isAssociated = true;
			}

			if(isAssociated && isDisassociated)
				auditLogThread.setValue("Description","Threshold rule "  + policyName + " has been associated to and disassociated from infos of component groups");
			else if(isAssociated)
				auditLogThread.setValue("Description","Threshold rule "  + policyName + " has been associated to infos of component groups");
			else if(isDisassociated)
				auditLogThread.setValue("Description","Threshold rule "  + policyName + " has been disassociated from infos of component groups");
			if(isAssociated || isDisassociated)
				auditLogThread.sendItToDB();
			/* code for auditlog ends */
		}
		else
		{
			iniFileWr = EgConfigFilesSync.getInstance(EgInstallInfo.getInstallDir()+"/manager/config/eg_infogroup.ini");
			String variable = "";
			StringTokenizer stok = new StringTokenizer(serverGrp,",");
			while(stok.hasMoreTokens())
			{
				String serverGroup = stok.nextToken();
				StringTokenizer stok1 = new StringTokenizer(infos,",");
				while(stok1.hasMoreTokens())
				{
					String tmpInfo = stok1.nextToken();
					if (tmpInfo.indexOf("#") > -1)
					{
						tmpInfo = StringUtils.replace(tmpInfo,"#","0eGHash1");
					}
					variable = serverGroup+"#"+testName+"#"+StringUtils.encodeSecurityChar(tmpInfo);
					String descIniVal = iniFileWr.getValue(subject,serverGroup+"#"+testName+"#"+tmpInfo);
					if(!StringUtils.isNull(descIniVal))
					{
						iniFileWr.deleteValue(subject,serverGroup+"#"+testName+"#"+tmpInfo);
					}
					iniFileWr.setValue(subject,variable,policyName);
				}
			}
			iniFileWr.saveFile();
			EgConfigFilesSync.releaseInstance(iniFileWr);
			
			/* code for auditlog starts */
			StringTokenizer st = new StringTokenizer(serverGrp,",");
			if(st.countTokens()>0)
			{
				while(st.hasMoreTokens())
				{
					String componentName = (String) st.nextToken();
					if (componentName.endsWith(":NULL"))
						componentName = componentName.substring(0, componentName.indexOf(":"));
					auditLogThread.setCurrentValue("Associated Component groups",componentName);
				}
			}
			auditLogThread.setValue("Description","Threshold rule " + policyName + " has been associated to infos of component groups");
			auditLogThread.sendItToDB();
			/* code for auditlog ends */
		}
		
		allComps = descGrpp.getTestColnComponentsForServerType();
		Iterator itrAll2 = allComps.iterator();
		while(itrAll2.hasNext())
		{
			String tmpData = (String) itrAll2.next();
			if(tmpData.startsWith(testName) && !allCompList.contains(tmpData))
			{
				allCompList.add(tmpData);
			}
		}
		
		String groupNames = serverGrp;
		if(StringUtils.isNull(groupNames))
		{
			groupNames = previouslyManaged;
		}

		StringTokenizer st1 = new StringTokenizer(groupNames,",");
		while(st1.hasMoreTokens())
		{
			String tmpComp = st1.nextToken();
			Vector vecComp = descGrpp.getComponentNamesWithType(tmpComp);
			for(int i=0;i<vecComp.size();i++)
			{
				String cmpData = (String) vecComp.get(i);
				String tmpCompData = "";
				if(hostLevelTests.contains(testName))
				{
					tmpCompData = cmpData.substring(cmpData.indexOf("#")+1);
					tmpCompData = tmpCompData.substring(0,tmpCompData.indexOf(":"));
					tmpCompData +=":NULL";
				}
				else
				{
					tmpCompData = cmpData.substring(cmpData.indexOf("#")+1);
				}
				managedCompList.add(tmpCompData);
			}
		}

		if(preManagedGrps != null && preManagedGrps.size() > 0)
		{
			for(int p=0;p<preManagedGrps.size();p++)
			{
				String pGrpData = (String) preManagedGrps.get(p);
				Vector preCompDatas = descGrpp.getComponentNames(pGrpData);
				if (preCompDatas == null || preCompDatas.isEmpty())
				{
					continue;
				}
				for(int pp=0;pp<preCompDatas.size();pp++)
				{
					preCompList.add((String)preCompDatas.get(pp));
				}
			}
		}

		if(newManagedGrps != null && newManagedGrps.size() > 0)
		{
			for(int n=0;n<newManagedGrps.size();n++)
			{
				String nGrpData = (String) newManagedGrps.get(n);
				Vector newCompDatas = descGrpp.getComponentNames(nGrpData);
				if (newCompDatas == null || newCompDatas.isEmpty())
				{
					continue;
				}
				for(int nn=0;nn<newCompDatas.size();nn++)
				{
					newCompList.add((String)newCompDatas.get(nn));
				}
			}
		}

		for(int pre=0;pre<preCompList.size();pre++)
		{
			String previousData = (String) preCompList.get(pre);
			if(!newCompList.contains(previousData))
			{
				isCompAvailable = true;
				changedCompList.add(testName+":"+previousData);
			}
		}
		for(int n=0;n<newCompList.size();n++)
		{
			String recentData = (String) newCompList.get(n);
			if(!preCompList.contains(recentData))
			{
				isCompAvailable = true;
				changedCompList.add(testName+":"+recentData);
			}
		}

		tSet = descGrpp.getAllManagedInfosForTest(testName);
		Iterator itr1 = tSet.iterator();
		while(itr1.hasNext())
		{
			String itrData = (String) itr1.next();
			if(!newInfos.contains(itrData))
			{
				newInfos.add(itrData);
			}
		}

		for(int pre=0;pre<preInfos.size();pre++)
		{
			String previousData = (String) preInfos.get(pre);
			if(!newInfos.contains(previousData))
			{
				isInfoAvailable = true;
				specificInfos.add(previousData);
			}
		}
		for(int n=0;n<newInfos.size();n++)
		{
			String recentData = (String) newInfos.get(n);
			if(!preInfos.contains(recentData))
			{
				isInfoAvailable = true;
				specificInfos.add(recentData);
			}
		}

		if(!isCompAvailable && isInfoAvailable)
		{
			changedCompList.addAll(allCompList);
			preInfos = new ArrayList(specificInfos);
			newInfos = new ArrayList(specificInfos);
		}

		if(preInfos != null && preInfos.size() > 0)
		{
			for(int si=0;si<preInfos.size();si++)
			{
				String descInfoData = (String) preInfos.get(si);
				if(changedCompList != null && changedCompList.size() > 0)
				{
					for(int a=0;a<changedCompList.size();a++)
					{
						String compData = (String) changedCompList.get(a);
						compData = compData.substring(compData.indexOf(":")+1);
						compWithInfoList.add(compData+"#"+descInfoData);
					}
				}
			}
		}

		if(isCompAvailable && isInfoAvailable)
		{
			newInfos = new ArrayList(specificInfos);
		}

		if(newInfos != null && newInfos.size() > 0)
		{
			for(int si=0;si<newInfos.size();si++)
			{
				String descInfoData = (String) newInfos.get(si);
				if(!preInfos.contains(descInfoData))
				{
					if(newCompList != null && newCompList.size() > 0)
					{
						for(int a=0;a<newCompList.size();a++)
						{
							String compData = (String) newCompList.get(a);
							//compData = compData.substring(compData.indexOf(":")+1);
							compWithInfoList.add(compData+"#"+descInfoData);
						}
					}
				}
			}
		}

		for(int ct = 0;ct<compWithInfoList.size();ct++)
		{
			String compWithInfo = (String)compWithInfoList.get(ct);
			String tempComp = compWithInfo.substring(0,compWithInfo.indexOf("#"));
			String tmpTrgtHost = tempComp.substring(0,tempComp.indexOf(":"));
			String portVal = tempComp.substring(tempComp.indexOf(":")+1);
			String tmpInfo = compWithInfo.substring(compWithInfo.indexOf("#")+1);
			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);
					}

					String port = "";
					String oraSid = "";
					if(portVal.indexOf(":") != -1)
					{
						port = portVal.substring(0,portVal.indexOf(":"));
						oraSid = portVal.substring(portVal.indexOf(":")+1);
					}
					else
					{
						port = portVal;
					}

					if(htKey.startsWith(tmpTrgtHost+":"+port))
					{
						if(hostLevelTests.contains(testName))
						{
							String strUserVal = testName+":"+tmpTrgtHost;
							String nickWithNullPort = tmpTrgtHost+":NULL";
							String strDescVal = testName+":"+nickWithNullPort+"#"+StringUtils.encodeSecurityChar(str_info);
							String strGrpDescVal = testName+":"+nickWithNullPort+"#"+str_info;
							if(userThreshList != null && userThreshList.contains(strUserVal))
							{
								break;
							}
							else if(descThreshList != null && (descThreshList.contains(strDescVal) || descThreshList.contains(strGrpDescVal)))
							{
								continue;
							}
							else
							{
								if(!StringUtils.isNull(tmpInfo) && tmpInfo.equals(str_info))
								{
									String htValues = (String) ht.get(htKey);
									isSaveIni = true;
									serIniWr.setValue(subjectVal,htValues,testName);
								}
								else
								{
									continue;
								}
							}
						}
						else
						{
							String strUserVal = testName+":"+tempComp;
							String strDescVal = testName+":"+tempComp+"#"+StringUtils.encodeSecurityChar(str_info);
							String strGrpDescVal = testName+":"+tempComp+"#"+str_info;

							if(userThreshList != null && userThreshList.contains(strUserVal))
							{
								break;
							}
							else if(descThreshList != null && (descThreshList.contains(strDescVal) || descThreshList.contains(strGrpDescVal)))
							{
								continue;
							}
							else
							{
								if(!StringUtils.isNull(tmpInfo) && tmpInfo.equals(str_info))
								{
									String htValues = (String) ht.get(htKey);
									isSaveIni = true;
									serIniWr.setValue(subjectVal,htValues,testName);
								}
								else
								{
									continue;
								}
							}
						}
					}
				}
			}
		}
		if(isSaveIni)
		{
			serIniWr.saveFile();
		}
		EgConfigFilesSync.releaseInstance(serIniWr);

		rootJson.put("success","true");
		rootJson.put("successUpdate",true);
		rootJson.put("successMsg",_mapping.getTextDescription("admin.common.Your changes have been updated successfully"));
	}
	else
	{
		Vector sess_vec = new Vector();
		sess_vec.add(_mapping.getTextDescription("admin.threshold.Configure threshold component group"));
		String servType = (String)session.getAttribute("serverTypeForPolicy");
		String test_Name = (String)session.getAttribute("TestNameForPolicy");
		if(servType != null && servType.equalsIgnoreCase("Groups"))
		{
			sess_vec.add("EgEntryForServerGroup.jsp?ServerType="+servType+"&TestName="+test_Name);
		}
		else
		{
			sess_vec.add("EgEntryForServerGroup.jsp?serv="+servType+"&test="+test_Name);
		}
		sess_vec.add(_mapping.getTextDescription("admin.threshold.Configure threshold component group"));
		sess_vec.add(_mapping.getTextDescription("admin.threshold.Your changes have been updated successfully")+".");
		session.putValue("vec_Results",sess_vec);
		tSet = descGrpp.getAllManagedInfosForTest(testName);
		Iterator itr = tSet.iterator();
		while(itr.hasNext())
		{
			specificInfos.add(itr.next());
		}
		if(vec_policy != null && vec_policy.size() > 0 && vec_policy.contains(policyName))
		{
			iniFileWr = EgConfigFilesSync.getInstance(EgInstallInfo.getInstallDir()+"/manager/config/eg_infogroup.ini");
			ArrayList allVarToDel = descGrpp.getMappingAllEntryForPolicyToDelete(subject,policyName,applyRule);
			//String varToDel = descGrpp.getMappingEntryForPolicyToDelete(subject,policyName,applyRule);
			String strHiddenVal = request.getParameter("strHiddenVal");
			ArrayList hiddenList = new ArrayList();
			if(!StringUtils.isNull(strHiddenVal))
			{
				strHiddenVal = strHiddenVal.substring(1);
				hiddenList = StringUtils.egTokenizer(strHiddenVal,",");
			}
			for(int v=0;v<allVarToDel.size();v++)
			{
				String delVariable = (String) allVarToDel.get(v);
				String tmpStrGrpName = delVariable.substring(0,delVariable.indexOf("#"));
				if(hiddenList != null && hiddenList.contains(tmpStrGrpName))
				{
					continue;
				}
				iniFileWr.deleteValue(subject,delVariable);
			}

			StringTokenizer stok = new StringTokenizer(serverGrp,",");
			while(stok.hasMoreTokens())
			{
				String variable = stok.nextToken()+"#"+testName;
				iniFileWr.setValue(subject,variable,policyName);
			}
			iniFileWr.saveFile();
			EgConfigFilesSync.releaseInstance(iniFileWr);

			/* code for auditlog starts */
			boolean isAssociated = false;
			boolean isDisassociated = false;
			ArrayList currentServerGroupList = new ArrayList();
			ArrayList currentComponents = new ArrayList();
			StringTokenizer st = new StringTokenizer(serverGrp,",");
			if(st.countTokens()>0)
			{
				while(st.hasMoreTokens())
				{
					String componentName = (String) st.nextToken();
					currentServerGroupList.add(componentName);
					if (componentName.endsWith(":NULL"))
						componentName = componentName.substring(0, componentName.indexOf(":"));
					currentComponents.add(componentName);
				}
			}
			ArrayList previousServerGroupList = new ArrayList();
			st = new StringTokenizer(previouslyManaged,",");
			if(st.countTokens()>0)
			{
				while(st.hasMoreTokens())
				{
					String componentName = (String) st.nextToken();
					previousServerGroupList.add(componentName);
					if (componentName.endsWith(":NULL"))
						componentName = componentName.substring(0, componentName.indexOf(":"));
					if(!currentComponents.contains(componentName))
					{
						auditLogThread.setCurrentValue("Disassociated component groups",componentName);
						isDisassociated = true;
					}
					else
						currentComponents.remove(componentName);
				}
			}
			for(int i=0;i<currentComponents.size();i++)
			{
				auditLogThread.setCurrentValue("Associated component groups",(String) currentComponents.get(i));
				isAssociated = true;
			}
		
			Collections.sort(currentServerGroupList);
			Collections.sort(previousServerGroupList);

			if(isAssociated && isDisassociated)
				auditLogThread.setValue("Description","Threshold rule "  + policyName + " has been associated to and disassociated from component groups");
			else if(isAssociated)
				auditLogThread.setValue("Description","Threshold rule "  + policyName + " has been associated to component groups");
			else if(isDisassociated)
				auditLogThread.setValue("Description","Threshold rule "  + policyName + " has been disassociated from component groups");

			if(isAssociated || isDisassociated)
				auditLogThread.sendItToDB();
			/* code for auditlog ends */
		}
		else
		{
			iniFileWr = EgConfigFilesSync.getInstance(EgInstallInfo.getInstallDir()+"/manager/config/eg_infogroup.ini");	
			StringTokenizer stok = new StringTokenizer(serverGrp,",");
			while(stok.hasMoreTokens())
			{
				String variable = stok.nextToken()+"#"+testName;
				iniFileWr.setValue(subject,variable,policyName);
			}
			iniFileWr.saveFile();
			EgConfigFilesSync.releaseInstance(iniFileWr);

			/* code for auditlog starts */
			StringTokenizer st = new StringTokenizer(serverGrp,",");
			if(st.countTokens()>0)
			{
				while(st.hasMoreTokens())
				{
					String componentName = (String) st.nextToken();
					if (componentName.endsWith(":NULL"))
						componentName = componentName.substring(0, componentName.indexOf(":"));
					auditLogThread.setCurrentValue("Associated component groups",componentName);
				}
			}
			auditLogThread.setValue("Description","Threshold rule " + policyName + " has been associated to component groups");
			auditLogThread.sendItToDB();
			/* code for auditlog ends */
		}

		isSpecificRule = (!StringUtils.isNull(applyRule) && applyRule.equalsIgnoreCase("specific"));
		String groupNames = serverGrp;
		if(StringUtils.isNull(groupNames))
		{
			groupNames = previouslyManaged;
		}
		StringTokenizer st2 = new StringTokenizer(groupNames,",");
		while(st2.hasMoreTokens())
		{
			String tmpComp = st2.nextToken();
			Vector vecComp = descGrpp.getComponentNamesWithType(tmpComp);
			for(int i=0;i<vecComp.size();i++)
			{
				String cmpData = (String) vecComp.get(i);
				String tmpCompData = "";
				if(hostLevelTests.contains(testName))
				{
					tmpCompData = cmpData.substring(cmpData.indexOf("#")+1);
					tmpCompData = tmpCompData.substring(0,tmpCompData.indexOf(":"));
					tmpCompData +=":NULL";
				}
				else
				{
					tmpCompData = cmpData.substring(cmpData.indexOf("#")+1);
				}
				managedCompList.add(testName+":"+tmpCompData);
			}
		}

		if(preManagedGrps != null && preManagedGrps.size() > 0)
		{
			for(int p=0;p<preManagedGrps.size();p++)
			{
				String pGrpData = (String) preManagedGrps.get(p);
				Vector preCompDatas = descGrpp.getComponentNames(pGrpData);
				for(int pp=0;pp<preCompDatas.size();pp++)
				{
					preCompList.add((String)preCompDatas.get(pp));
				}
			}
		}

		if(newManagedGrps != null && newManagedGrps.size() > 0)
		{
			for(int n=0;n<newManagedGrps.size();n++)
			{
				String nGrpData = (String) newManagedGrps.get(n);
				Vector newCompDatas = descGrpp.getComponentNames(nGrpData);
				for(int nn=0;nn<newCompDatas.size();nn++)
				{
					newCompList.add((String)newCompDatas.get(nn));
				}
			}
		}

		for(int pre=0;pre<preCompList.size();pre++)
		{
			String previousData = (String) preCompList.get(pre);
			if(!newCompList.contains(previousData))
			{
				changedCompList.add(previousData);
			}
		}
		for(int n=0;n<newCompList.size();n++)
		{
			String recentData = (String) newCompList.get(n);
			if(!preCompList.contains(recentData))
			{
				changedCompList.add(recentData);
			}
		}

		if(isSpecificRule)
		{
			for(int pi=0;pi<specificInfos.size();pi++)
			{
				String preInfoData = (String) specificInfos.get(pi);
				if(changedCompList != null && changedCompList.size() > 0)
				{
					for(int a=0;a<changedCompList.size();a++)
					{
						String compData = (String) changedCompList.get(a);
						if(hostLevelTests.contains(testName))
						{
							compData = compData.substring(0,compData.indexOf(":"));
							compData+=":NULL";
							if(!compWithInfoList.contains(compData))
							{
								compWithInfoList.add(compData+"#"+preInfoData);
							}
						}
						else
						{
							if(!compWithInfoList.contains(compData))
							{
								compWithInfoList.add(compData+"#"+preInfoData);
							}
						}
					}
				}
			}
		}
		else
		{
			if(changedCompList != null && changedCompList.size() > 0)
			{
				for(int a=0;a<changedCompList.size();a++)
				{
					String compData = (String) changedCompList.get(a);
					if(hostLevelTests.contains(testName))
					{
						compData = compData.substring(0,compData.indexOf(":"));
						compData+=":NULL";
						if(!compWithInfoList.contains(compData))
						{
							compWithInfoList.add(compData);
						}
					}
					else
					{
						if(!compWithInfoList.contains(compData))
						{
							compWithInfoList.add(compData);
						}
					}
				}
			}
		}

		for(int ct = 0;ct<compWithInfoList.size();ct++)
		{
			String compWithPort = (String)compWithInfoList.get(ct);
			String tempComp = "";
			String tmpInfo = "";
			if(isSpecificRule)
			{
				tempComp = compWithPort.substring(0,compWithPort.indexOf("#"));
				tmpInfo = compWithPort.substring(compWithPort.indexOf("#")+1);
			}
			else
			{
				tempComp = compWithPort;
			}
			String tmpTrgtHost = tempComp.substring(0,tempComp.indexOf(":"));
			String portVal = tempComp.substring(tempComp.indexOf(":")+1);
			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.lastIndexOf("#")+1);
					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);
					}

					String port = "";
					String oraSid = "";
					if(portVal.indexOf(":") != -1)
					{
						port = portVal.substring(0,portVal.indexOf(":"));
						oraSid = portVal.substring(portVal.indexOf(":")+1);
					}
					else
					{
						port = portVal;
					}

					if(htKey.startsWith(tmpTrgtHost+":"+port))
					{

						if(hostLevelTests.contains(testName))
						{
							String strUserVal = testName+":"+tmpTrgtHost;
							String nickWithNullPort = tmpTrgtHost+":NULL";
							String strDescVal = testName+":"+nickWithNullPort+"#"+StringUtils.encodeSecurityChar(str_info);
							String strGrpDescVal = testName+":"+nickWithNullPort+"#"+str_info;
							if(userThreshList != null && userThreshList.contains(strUserVal))
							{
								continue;
							}
							else if(descThreshList != null && (descThreshList.contains(strDescVal) || descThreshList.contains(strGrpDescVal)))
							{
								continue;
							}
							else
							{
								if(isSpecificRule)
								{
									if(!StringUtils.isNull(tmpInfo) && tmpInfo.equals(str_info))
									{
										String htValues = (String) ht.get(htKey);
										isSaveIni = true;
										serIniWr.setValue(subjectVal,htValues,testName);
									}
								}
								else
								{
									String htValues = (String) ht.get(htKey);
									isSaveIni = true;
									serIniWr.setValue(subjectVal,htValues,testName);
								}
							}
						}
						else
						{
							String strUserVal = testName+":"+tempComp;
							String strDescVal = testName+":"+tempComp+"#"+StringUtils.encodeSecurityChar(str_info);
							String strGrpDescVal = testName+":"+tempComp+"#"+str_info;
							if(userThreshList != null && userThreshList.contains(strUserVal))
							{
								break;
							}
							else if(descThreshList != null && (descThreshList.contains(strDescVal) || descThreshList.contains(strGrpDescVal)))
							{
								continue;
							}
							else
							{
								if(isSpecificRule)
								{
									if(!StringUtils.isNull(tmpInfo) && tmpInfo.equals(str_info))
									{
										String htValues = (String) ht.get(htKey);
										isSaveIni = true;
										serIniWr.setValue(subjectVal,htValues,testName);
									}
								}
								else
								{
									String htValues = (String) ht.get(htKey);
									isSaveIni = true;
									serIniWr.setValue(subjectVal,htValues,testName);
								}
							}
						}
					}
				}
			}
		}
		if(isSaveIni)
		{
			serIniWr.saveFile();
		}
		EgConfigFilesSync.releaseInstance(serIniWr);
		rootJson.put("success","true");
		rootJson.put("successUpdate",true);
		rootJson.put("successMsg",_mapping.getTextDescription("admin.common.Your changes have been updated successfully"));
	}

	out.println(rootJson.toString());
%>
