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

<%@ include file="../../EgTrackerAJSON.jsp" %>
<%@ page import="java.util.*" %>
<%@ page import="com.egurkha.ini.*" %>
<%@ page import="net.sf.json.*"%>

<%@ page import ="egurkha.EgDbAdapterUI"%>
<%@ page import ="com.eg.EgDbAdapterTypeUI"%>
<%@ page import="java.sql.*" %>
<%@ page import="com.eg.EgServerGroupBean" %>
<%@ page import="com.egurkha.util.StringUtils" %>
<%@ 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);
%>

<%
	JSONObject rootJson = new JSONObject();
	JSONObject hiddenObj = new JSONObject();
	JSONObject screenLabels = new JSONObject();

	ResultSet rs=null;
	EgDbAdapterUI egDb;
	// -- ORG -- egDb=EgDbAdapterTypeUI.getObject();
	//int limit = 20;
	int limit = 10;
	EgServerGroupBean descGrpp = new EgServerGroupBean();

	Vector vec_inf = new Vector();
	Vector vec_comp = new Vector();
	String compName = "";
	String compType = "";
	String testName = "";
	String strOriginalManagedInfo="";
	String strOriginalUnManagedInfo="";
	String policyName = request.getParameter("policyN");
	String managedSer = request.getParameter("managedServers");
	String strHiddenVal = request.getParameter("strHiddenVal");
	String seletedRuleType = request.getParameter("seletedRuleType");
	String strHideInfoVal = "";
	session.setAttribute("managedServer",managedSer);
	String strSearch = request.getParameter("strSearch");
	String strComponent = request.getParameter("strComponent");


	String strManagedSearch = request.getParameter("strManagedSearch");
	String strUnmanagedSearch = request.getParameter("strUnmanagedSearch");
	boolean isEnabledSearchResult = (strManagedSearch != null && !strManagedSearch.equalsIgnoreCase("") && !strManagedSearch.equals("Search") && !strManagedSearch.equals("undefined"));
	boolean isDisabledSearchResult =(strUnmanagedSearch != null && !strUnmanagedSearch.equalsIgnoreCase("") && !strUnmanagedSearch.equals("Search") && !strUnmanagedSearch.equals("undefined"));
	ArrayList enabledSearchList = new ArrayList();
	ArrayList disabledSearchList = new ArrayList();

	if (isEnabledSearchResult)
	{
		StringTokenizer strTokens = new StringTokenizer(strManagedSearch);
		while (strTokens.hasMoreTokens())
		{
			String thisToken = strTokens.nextToken().trim();
			if (thisToken.equals("")) continue;
			enabledSearchList.add(thisToken);
		}
	}
	if (isDisabledSearchResult)
	{
		StringTokenizer strTokens = new StringTokenizer(strUnmanagedSearch);
		while (strTokens.hasMoreTokens())
		{
			String thisToken = strTokens.nextToken().trim();
			if (thisToken.equals("")) continue;
			disabledSearchList.add(thisToken);
		}
	}
	//boolean isSearchResult = (strSearch != null && !strSearch.equalsIgnoreCase(""));
	//ArrayList searchList = new ArrayList();
	//if (isSearchResult)
	//{
		//StringTokenizer strTokens = new StringTokenizer(strSearch);
		//while (strTokens.hasMoreTokens())
		//{
			//String thisToken = strTokens.nextToken().trim();
			//if (thisToken.equals("")) continue;
			//searchList.add(thisToken);
		//}
	//}
	ArrayList serverGroupList = new ArrayList();
	int sizOfal = 0;
	if(managedSer != null && !managedSer.equals(""))
	{
		StringTokenizer stok = new StringTokenizer(managedSer,",");
		while(stok.hasMoreTokens())
		{
			serverGroupList.add(stok.nextToken());
		}
		sizOfal = serverGroupList.size();
	}

	String test = descGrpp.getTestNameForPolicy(policyName);
	TreeSet tss = descGrpp.getAllManagedInfosForTest(test);
	TreeSet tset = descGrpp.getManagedInfosForPolicy(policyName, serverGroupList);
	
	if(!StringUtils.isNull(strHiddenVal))
	{
		StringTokenizer stHide = new StringTokenizer(strHiddenVal,",");
		while(stHide.hasMoreTokens())
		{
			String tmpStrToken = stHide.nextToken();
			Iterator itrInfo = tss.iterator();
			String tempInfoVar = "";
			while(itrInfo.hasNext())
			{
				tempInfoVar = (String) itrInfo.next();
				if (tempInfoVar.indexOf("0eGHash1") > -1)
				{
					tempInfoVar = StringUtils.replace(tempInfoVar,"0eGHash1","#");
				}
				strHideInfoVal += ","+tmpStrToken+"#"+test+"#"+StringUtils.encodeSecurityChar(tempInfoVar);
			}
		}
	}

	if(serverGroupList != null && serverGroupList.size() != 0)
	{
		for(int q=0; q<sizOfal ; q++)
		{
			String serverGroup = (String)serverGroupList.get(q);
			testName = descGrpp.getTestNameForPolicy(policyName);
			compType = descGrpp.getServerType(serverGroup);
			vec_comp = descGrpp.getComponentNames(serverGroup);
			if(vec_comp != null && vec_comp.size() > 0)
			{
				for(int x=0;x<vec_comp.size();x++)
				{
					compName = (String) vec_comp.get(x);
					egDb=EgDbAdapterTypeUI.getObject();
					try
					{
						boolean isConnected = false;
						String status = egDb.setConnection();
						if(status.equals("CONNECTED"))
							isConnected = true;
						if(isConnected)
						{
							String query = egDb.getInfosForDescGroup(compName,compType,testName);
							rs=egDb.SelectExec(query);
							while(rs.next())
							{
								String inf = rs.getString("info");
								if(!inf.equals("+"))
								{
									String info = inf.substring(inf.indexOf("+")+1);
									if(!vec_inf.contains(info))
										vec_inf.add(info);
								}
							}
						}
						rs.close();
						egDb.releaseConnection(false);
						EgDbAdapterTypeUI.releaseObject(egDb);
					}
					catch(Exception e)
					{
						if(rs != null)
							rs.close();
						egDb.releaseConnection(true);
						EgDbAdapterTypeUI.releaseObject(egDb);
					}
				}
			}
		}
	}

	TreeSet tsetTemp = new TreeSet();

	if(tset != null && tset.size() > 0)
	{
		Iterator itr1 = tset.iterator();
		String tempVar = "";
		while(itr1.hasNext())
		{
			tempVar = (String) itr1.next();
			if (tempVar.indexOf("0eGHash1") > -1)
			{
				tempVar = StringUtils.replace(tempVar,"0eGHash1","#");
			}
			tsetTemp.add(tempVar);
		}
		tset = tsetTemp;
	}

	if(tset != null && tset.size() > 0)
	{
		vec_inf.removeAll(tset);
	}

	boolean Check = false;
	boolean test1 = (tss != null && tss.size() > 0 ) ? true:false;
	boolean test2 = (vec_inf != null && vec_inf.size() > 0) ? true:false;

	if((test1 && test2) || (!test1 && test2) || (test1 && !test2))
	{
		Check = true;
	}

	session.setAttribute("egPageDesc","EgAssociateInfos.jsp");
	session.setAttribute("egPageDesc","EgAssociateInfos.jsp");
	screenLabels.put("title","Associate / Disassociate Descriptor");

	
	screenLabels.put("rule_Label",_mapping.getTextDescription("admin.threshold.Rule Name"));
	rootJson.put("ruleName",policyName);

	if(Check)
	{
		rootJson.put("isNoData",false);

		boolean isNeedManagedInfoSearchBox = false;
		boolean isNeedUnManagedInfoSearchBox = false;
		if (tset != null)
		{
			isNeedManagedInfoSearchBox = (tset != null && tset.size() >= limit);
		}
		if(vec_inf != null)
		{
			isNeedUnManagedInfoSearchBox = (vec_inf != null && vec_inf.size() >= limit);
		}
		rootJson.put("isNeedManagedInfoSearchBox",isNeedManagedInfoSearchBox);
		rootJson.put("isNeedUnManagedInfoSearchBox",isNeedUnManagedInfoSearchBox);
		if(isNeedManagedInfoSearchBox)
		{

		}

		if(isNeedUnManagedInfoSearchBox && !isNeedManagedInfoSearchBox)
		{
		}
		else if(!isNeedUnManagedInfoSearchBox && !isNeedManagedInfoSearchBox)
		{

		}
		screenLabels.put("admin.threshold.Associated Descriptors",_mapping.getTextDescription("admin.threshold.Associated Descriptors"));
		JSONArray grpAssoDescArr = new JSONArray();

		if(tset != null && tset.size() > 0)
		{
			Iterator itr = tset.iterator();
			while(itr.hasNext())
			{
				JSONObject grpAssoDescObj = new JSONObject();
				String s_val = (String) itr.next();
				strOriginalManagedInfo +=","+s_val;
				
				if (enabledSearchList !=null && enabledSearchList.size() > 0 && isEnabledSearchResult)
				{
					if(isItemExists(enabledSearchList,s_val))
					{
						grpAssoDescObj.put("displayField",s_val);
						grpAssoDescObj.put("valueField",s_val);
						grpAssoDescArr.add(grpAssoDescObj);
					}
				}
				else
				{
					grpAssoDescObj.put("displayField",s_val);
					grpAssoDescObj.put("valueField",s_val);
					grpAssoDescArr.add(grpAssoDescObj);
				}
			}
			if(!StringUtils.isNull(strOriginalManagedInfo) && strOriginalManagedInfo.startsWith(","))
			{
				strOriginalManagedInfo = strOriginalManagedInfo.substring(1);
			}
			rootJson.put("grpAssoDescArr",grpAssoDescArr);
		}

		
		screenLabels.put("admin.threshold.Disassociated Descriptors",_mapping.getTextDescription("admin.threshold.Disassociated Descriptors"));
		JSONArray grpDisAssoDescArr = new JSONArray();


		if (vec_inf.size()>0)
		{
			for(int z=0;z<vec_inf.size();z++)
			{
				JSONObject grpDisAssoDescObj = new JSONObject();
				String value = (String) vec_inf.get(z);
				strOriginalUnManagedInfo +=","+value;
				if (disabledSearchList !=null && disabledSearchList.size() > 0 && isDisabledSearchResult)
				{
					if(isItemExists(disabledSearchList,value))
					{
						grpDisAssoDescObj.put("displayField",value);
						grpDisAssoDescObj.put("valueField",value);
						grpDisAssoDescArr.add(grpDisAssoDescObj);
					}
				}
				else
				{
					grpDisAssoDescObj.put("displayField",value);
					grpDisAssoDescObj.put("valueField",value);
					grpDisAssoDescArr.add(grpDisAssoDescObj);
				}
			}
			if(!StringUtils.isNull(strOriginalUnManagedInfo) && strOriginalUnManagedInfo.startsWith(","))
			{
				strOriginalUnManagedInfo = strOriginalUnManagedInfo.substring(1);
			}
			rootJson.put("grpDisAssoDescArr",grpDisAssoDescArr);
		}

	}
	else
	{
		rootJson.put("isNoData",true);
		rootJson.put("No_data",_mapping.getTextDescription("admin.threshold.No descriptors currently available"));
		rootJson.put("No_data_retun_label",_mapping.getTextDescription("admin.threshold.Choose rule to manage/unmanage server group Click here"));
		rootJson.put("No_data_retun_url","EgAssociateThreshPolicy.jsp");
	}
	if(StringUtils.isNull(strComponent))
	{
		hiddenObj.put("strComp","");
	}
	else
	{
		hiddenObj.put("strComp",strComponent);
	}
	hiddenObj.put("strHideInfoVal",strHideInfoVal);
	hiddenObj.put("sHiddenVal",strHiddenVal);
	hiddenObj.put("seletedRuleType",seletedRuleType);
	hiddenObj.put("mServers",managedSer);
	hiddenObj.put("pageFrom","InfoBased");
	
	//hiddenObj.put("strOriginalManagedInfo",strOriginalManagedInfo);
	//hiddenObj.put("strOriginalUnManagedInfo",strOriginalUnManagedInfo);


	rootJson.put("strOriginalManagedInfo",strOriginalManagedInfo);
	rootJson.put("strOriginalUnManagedInfo",strOriginalUnManagedInfo);

	rootJson.put("screenLabels",screenLabels);
	rootJson.put("hiddenObj",hiddenObj);
	out.println(rootJson.toString());

	//if(request.getQueryString()!=null)
	//	session.setAttribute("adminLastAccessedURL","/final/admin/EgGroupThreshold.jsp?"+request.getQueryString());
	//else session.setAttribute("adminLastAccessedURL","/final/admin/EgGroupThreshold.jsp");
%>
<%!
	private boolean isItemExists(List searchList, String server)
	{
		Iterator i = searchList.iterator();
		while(i.hasNext())
		{
			String nextStr = (String) i.next();
			if (server.indexOf(nextStr)!= -1 || (server.toUpperCase().indexOf(nextStr.toUpperCase())) != -1)
			{
				return true;
			}
		}
		return false;
	}
%>



