<%@ include file="../../EgTrackerAJSON.jsp" %>
<%@ page import = "java.util.*" %>
<%@ page import = "java.sql.*" %>
<%@ page import = "java.lang.*" %>
<%@ page import = "com.eg.EgInstallInfo"%>
<%@ page import = "com.eg.IniFile" %>
<%@ page import = "com.eg.EgConfigFilesSync"%>
<%@ page import = "com.eg.EgSpecsInfo"%>
<%@ page import = "com.eg.EgThresholdInfo" %>
<%@ page import = "com.egurkha.admin.AuditLogThread"%>
<%@ page import = "com.egurkha.util.StringUtils"%>
<%@ page import = "com.eg.EgMappingInfo"%>
<%@ page import = "com.egurkha.ini.IniUI"%>
<%@ page import = "egurkha.EgDbAdapterUI"%>
<%@ page import = "com.eg.EgDbAdapterTypeUI"%>
<%@ page import = "com.eg.EgConfigInfo" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

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

<%
	IniUI iniUI = IniUI.getInstance();
	//user language
	String userLanguage = iniUI.getDefaultLanguage();
	EgDbAdapterUI egDb;
	EgSpecsInfo specsInfo = new EgSpecsInfo("manager");
	EgThresholdInfo egThresh = new EgThresholdInfo("manager");

	Hashtable ht = new Hashtable();
	String subjectVal = "Newtarget_Update_EgThresholdManager";

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

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

	String strComponentType = request.getParameter("componentType");
	String strComponentName = request.getParameter("componentName");
	String from = request.getParameter("from");
	String strDescName = request.getParameter("descriptorName");
	String testName = request.getParameter("testName");
	String operation = request.getParameter("operation");
	String status="";
	String sidVal = "";
	String str_info = "";
	String trgt_host = "";
	String query = "";
	ResultSet rs = null;
	ArrayList sessionCompList = new ArrayList();
	boolean isAppTestForOracle = false;
	if(!StringUtils.isNull(strDescName))
	{
		strDescName = StringUtils.decodeSecurityChar(strDescName);
		if (strComponentName.indexOf(",") != -1)
		{
			if(!StringUtils.isNull(strComponentType) && strComponentType.equals("Oracle_server") && !hostLevelTests.contains(testName))
			{
				isAppTestForOracle = true;
				egDb=EgDbAdapterTypeUI.getObject();
				List compList = StringUtils.egTokenizer(strComponentName,",");
				sessionCompList.addAll(compList);
				for (int i = 0; i < compList.size(); i++)
				{
					String cName = (String)compList.get(i);
					String strTrgtHost = cName.substring(0,cName.indexOf(":"));
					String strSID = cName.substring(cName.lastIndexOf(":")+1);
					String strInfo = strSID+"+"+strDescName;
					strTrgtHost = "'"+strTrgtHost+"'";
					try
					{
						boolean isDBConnected = false;
						status = egDb.setConnection();
						if(status.equals("CONNECTED"))
						{
							isDBConnected = true;
						}
						
						if(isDBConnected)
						{
							query = egDb.getQueryForApplyAllThresholdPage(strTrgtHost,testName,strInfo);
							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 tmpStr_info="";
								String tmpSidVal="";
								if(r != -1)
								{
									tmpStr_info = infoVal.substring(r+1).trim();
									tmpSidVal = infoVal.substring(0,r);
								}
								if(StringUtils.isNull(tmpSidVal))
								{
									ht.put(hostVal+":"+portVal+"#"+infoVal+"#"+msmthostVal,updateDbData);
								}
								else
								{
									ht.put(hostVal+":"+portVal+":"+tmpSidVal+"#"+infoVal+"#"+msmthostVal,updateDbData);
								}
							}
						}
						rs.close();
						egDb.releaseConnection(false);
						EgDbAdapterTypeUI.releaseObject(egDb);
					}
					catch(Exception e)
					{
						if(rs != null)
						{
							rs.close();
						}
						egDb.releaseConnection(true);
						EgDbAdapterTypeUI.releaseObject(egDb);
					}
				}
			}
			else
			{
				List compList = StringUtils.egTokenizer(strComponentName,",");
				sessionCompList.addAll(compList);
				for (int i = 0; i < compList.size(); i++)
				{
					String cName = (String)compList.get(i);
					cName = cName.substring(0,cName.indexOf(":"));
					trgt_host = trgt_host + "'" + cName + "'" + ",";
				}
				trgt_host = trgt_host.substring(0, trgt_host.lastIndexOf(","));
				str_info = "+"+strDescName;
			}
		}
		else
		{
			trgt_host = strComponentName.substring(0,strComponentName.indexOf(":"));
			trgt_host = "'" + trgt_host + "'";
			str_info = "+"+strDescName;
			sessionCompList.add(strComponentName);
		}
	}
	else
	{
		if (strComponentName.indexOf(",") != -1)
		{
			List compList = StringUtils.egTokenizer(strComponentName,",");
			sessionCompList.addAll(compList);
			for (int i = 0; i < compList.size(); i++)
			{
				String cName = (String)compList.get(i);
				cName = cName.substring(0,cName.indexOf(":"));
				trgt_host = trgt_host + "'" + cName + "'" + ",";
			}
			trgt_host = trgt_host.substring(0, trgt_host.lastIndexOf(","));
		}
		else
		{
			trgt_host = strComponentName.substring(0,strComponentName.indexOf(":"));
			trgt_host = "'" + trgt_host + "'";
			str_info = "+";
			sessionCompList.add(strComponentName);
		}
	}

	if (sessionCompList != null && !sessionCompList.isEmpty())
	{
		session.setAttribute("sessionCompList",sessionCompList);
	}

	prgBean.createFile("eg_thresholds.ini");
	String []descThreshVal = prgBean.getVariables("DESCRIPTOR_THRESHOLDS");
	ArrayList descThreshList = new ArrayList();
	if(descThreshVal.length > 0)
	{
		for(int r=0; r<descThreshVal.length; r++)
		{
			if(descThreshVal[r].startsWith(testName))
			{
				descThreshList.add(descThreshVal[r]);
			}
		}
	}

	if(!isAppTestForOracle && !StringUtils.isNull(testName))
	{
		egDb=EgDbAdapterTypeUI.getObject();
		try
		{
			boolean isConnected = false;
			status = egDb.setConnection();
			if(status.equals("CONNECTED"))
			{
				isConnected = true;
			}
			if(isConnected)
			{
				String strQuery = "";
				if(!StringUtils.isNull(strDescName))
				{
					strQuery = egDb.getQueryForApplyAllThresholdPage(trgt_host,testName,str_info);
				}
				else
				{
					strQuery = egDb.getQueryForSameTypeComponents(trgt_host,testName);
				}
				rs = egDb.SelectExec(strQuery);
				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 tmpStr_info="";
					String tmpSidVal="";
					if(r != -1)
					{
						tmpStr_info = infoVal.substring(r+1).trim();
						tmpSidVal = infoVal.substring(0,r);
					}
					if(StringUtils.isNull(tmpSidVal))
					{
						ht.put(hostVal+":"+portVal+"#"+infoVal+"#"+msmthostVal,updateDbData);
					}
					else
					{
						ht.put(hostVal+":"+portVal+":"+tmpSidVal+"#"+infoVal+"#"+msmthostVal,updateDbData);
					}
				}
			}
			rs.close();
			egDb.releaseConnection(false);
			EgDbAdapterTypeUI.releaseObject(egDb);
		}
		catch(Exception e)
		{
			if(rs != null)
			{
				rs.close();
			}
			egDb.releaseConnection(true);
			EgDbAdapterTypeUI.releaseObject(egDb);
		}
	}

	ArrayList htKeyValues = new ArrayList();
	if(ht != null && ht.size() > 0)
	{
		Iterator iterAll = ht.keySet().iterator();
		while(iterAll.hasNext())
		{
			String tempKeys = (String) iterAll.next();
			htKeyValues.add(tempKeys);
		}
	}

	if(!StringUtils.isNull(operation) && operation.equals("updateThresholds"))
	{
		IniFile defaultIni = new IniFile(EgInstallInfo.getInstallDir()+"/manager/config/eg_thresholds.ini");
		String iniVariable = "";
		String iniValue = "";
		boolean isDescriptorThresholds = false;
		String parentComp = request.getParameter("parentComp");
		if(from != null && from.equalsIgnoreCase("specificTest"))
		{
			if(!StringUtils.isNull(strDescName))
			{
				isDescriptorThresholds = true;
				String nickWithoutPort = "";
				String nickWithNullPort = "";
				if(hostLevelTests.contains(testName))
				{
					if(parentComp.indexOf(":") != -1)
					{
						nickWithoutPort = parentComp.substring(0,parentComp.indexOf(":"));
						nickWithNullPort = nickWithoutPort+":NULL";
					}
				}
				else
				{
					nickWithoutPort = parentComp;
					nickWithNullPort = parentComp;
				}

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

		String newDefaultThreshold = "";
		/* code for auditlog starts */
		String nullRemoved = parentComp.endsWith(":NULL")?parentComp.substring(0,parentComp.lastIndexOf(":")):parentComp;
		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","Configure Thresholds");
		if(!StringUtils.isNull(strDescName))
		{
			auditLogThread.setValue("Description","The specific thresholds for descriptor "+strDescName+" of "+ mappingInfo.getTestDescription(testName) + " test for component " + nullRemoved + " (" +mappingInfo.getComponentTypeDescription(strComponentType) + ") have been applied to other components");
		}
		else
		{
			auditLogThread.setValue("Description","Threshold values of " + mappingInfo.getTestDescription(testName) + " test for component " + nullRemoved + " of type " + mappingInfo.getComponentTypeDescription(strComponentType) + " have been applied to other components");
		}
		auditLogThread.setCurrentValue("THRESHOLDS CONFIGURED FOR OTHER COMPONENTS",strComponentName);
		auditLogThread.sendItToDB();

		/* code for auditlog ends */

		// DB updation code starts
		if(ht != null && ht.size() > 0)
		{
			boolean isSaveIni = false;
			IniFile serIniWr = EgConfigFilesSync.getInstance(EgInstallInfo.getInstallDir()+"/manager/config/eg_threshupdate.ini");
			for(int h=0;h<htKeyValues.size();h++)
			{
				String htKey = (String) htKeyValues.get(h);
				String infoData = htKey.substring(htKey.indexOf("#")+1);
				infoData = infoData.substring(0,infoData.indexOf("#"));
				int r = infoData.indexOf(EgConfigInfo.infoFieldSeparator);
				String strInfo="";
				String sidValue="";
				if(r != -1)
				{
					strInfo = infoData.substring(r+1).trim();
					sidValue = infoData.substring(0,r);
				}

				if(!StringUtils.isNull(strDescName))
				{
					String htValues = (String) ht.get(htKey);
					isSaveIni = true;
					serIniWr.setValue(subjectVal,htValues,testName);
				}
				else
				{
					if(strComponentName.indexOf(",") != -1)
					{
						List compList = StringUtils.egTokenizer(strComponentName,",");
						for (int i = 0; i < compList.size(); i++)
						{
							String tempComp = "";
							String strCName = (String) compList.get(i);
							if(hostLevelTests.contains(testName))
							{
								tempComp = strCName.substring(0,strCName.indexOf(":"));
								tempComp+=":NULL";
							}
							else
							{
								tempComp = strCName;
							}

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

									if(descThreshList != null && (descThreshList.contains(strDescVal) || descThreshList.contains(strGrpDescVal)))
									{
										continue;
									}
									else
									{
										String htValues = (String) ht.get(htKey);
										isSaveIni = true;
										serIniWr.setValue(subjectVal,htValues,testName);
									}
								}
							}
						}
					}
					else
					{
						String tempComp = "";
						if(hostLevelTests.contains(testName))
						{
							tempComp = strComponentName.substring(0,strComponentName.indexOf(":"));
							tempComp+=":NULL";
						}
						else
						{
							tempComp = strComponentName;
						}

						if(htKey.startsWith(tempComp))
						{
							if(hostLevelTests.contains(testName))
							{

								String nickWithNullPort = tempComp;
								String strDescVal = testName+":"+nickWithNullPort+"#"+StringUtils.encodeSecurityChar(strInfo);
								String strGrpDescVal = testName+":"+nickWithNullPort+"#"+strInfo;

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

		newDefaultThreshold = iniValue.toString();
		String strThresholdIni = EgInstallInfo.getInstallDir()+"/manager/config/eg_thresholds.ini";
		IniFile ThresholdIniWrite = EgConfigFilesSync.getInstance(strThresholdIni);
		if(isDescriptorThresholds)
		{
			if(strComponentName.indexOf(",") != -1)
			{
				List compList = StringUtils.egTokenizer(strComponentName,",");
				for (int i = 0; i < compList.size(); i++)
				{
					String strCName = (String) compList.get(i);
					String nickWithoutPort = "";
					String nickWithNullPort = "";
					if(hostLevelTests.contains(testName))
					{
						if(strCName.indexOf(":") != -1)
						{
							nickWithoutPort = strCName.substring(0,strCName.indexOf(":"));
							nickWithNullPort = nickWithoutPort+":NULL";
						}
					}
					else
					{
						nickWithNullPort = strCName;
					}
					String descIniVariable = testName+":"+nickWithNullPort+"#"+StringUtils.encodeSecurityChar(strDescName);
					String descIniVal = defaultIni.getValue("DESCRIPTOR_THRESHOLDS",testName+":"+nickWithNullPort+"#"+strDescName);
					if(!StringUtils.isNull(descIniVal))
					{
						ThresholdIniWrite.deleteValue("DESCRIPTOR_THRESHOLDS",testName+":"+nickWithNullPort+"#"+strDescName);
					}
					ThresholdIniWrite.setValue("DESCRIPTOR_THRESHOLDS",descIniVariable,newDefaultThreshold);
				}
			}
			else
			{
				String nickWithoutPort = "";
				String nickWithNullPort = "";
				if(hostLevelTests.contains(testName))
				{
					if(strComponentName.indexOf(":") != -1)
					{
						nickWithoutPort = strComponentName.substring(0,strComponentName.indexOf(":"));
						nickWithNullPort = nickWithoutPort+":NULL";
					}
				}
				else
				{
					nickWithNullPort = strComponentName;
				}
				String descIniVariable = testName+":"+nickWithNullPort+"#"+StringUtils.encodeSecurityChar(strDescName);
				String descIniVal = defaultIni.getValue("DESCRIPTOR_THRESHOLDS",testName+":"+nickWithNullPort+"#"+strDescName);
				if(!StringUtils.isNull(descIniVal))
				{
					ThresholdIniWrite.deleteValue("DESCRIPTOR_THRESHOLDS",testName+":"+nickWithNullPort+"#"+strDescName);
				}
				ThresholdIniWrite.setValue("DESCRIPTOR_THRESHOLDS",descIniVariable,newDefaultThreshold);
			}
		}
		else
		{
			if(strComponentName.indexOf(",") != -1)
			{
				List compList = StringUtils.egTokenizer(strComponentName,",");
				for (int i = 0; i < compList.size(); i++)
				{
					String strCName = (String) compList.get(i);
					String nickWithoutPort = "";
					if(hostLevelTests.contains(testName))
					{
						if(strCName.indexOf(":") != -1)
						{
							nickWithoutPort = strCName.substring(0,strCName.indexOf(":"));
						}
					}
					else
					{
						nickWithoutPort = strCName;
					}
					String userIniVariable = testName+":"+nickWithoutPort;
					ThresholdIniWrite.setValue("USER_THRESHOLDS",userIniVariable,newDefaultThreshold);
				}
			}
			else
			{
				String nickWithoutPort = "";
				if(hostLevelTests.contains(testName))
				{
					if(strComponentName.indexOf(":") != -1)
					{
						nickWithoutPort = strComponentName.substring(0,strComponentName.indexOf(":"));
					}
				}
				else
				{
					nickWithoutPort = strComponentName;
				}
				String userIniVariable = testName+":"+nickWithoutPort;
				ThresholdIniWrite.setValue("USER_THRESHOLDS",userIniVariable,newDefaultThreshold);
			}
		}
		ThresholdIniWrite.saveFile();
		EgConfigFilesSync.releaseInstance(ThresholdIniWrite);
		EgThresholdInfo.setUpdated();
	} 
%>

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



