<%@ include file="EgTrackerA.jsp"%>
<%@ page errorPage="EgProbs.jsp"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.File"%>
<%@ page import="com.eg.IniFile"%>
<%@ page import="com.eg.EgDbInfo"%>
<%@ page import="com.eg.EgInstallInfo"%>
<%@ page import="com.eg.EgConfigFilesSync"%>
<%@ page import="com.eg.EgAggregateInfo"%>
<%@ page import="com.eg.UpdateAndTransmitICInfo"%>
<%@ page import="com.egurkha.admin.AuditLogThread"%>
<%@ page import="egurkha.*" %>
<%@ page import="com.eg.EgConfigInfo"%>
<%@ page import = "com.eg.IniHelper"%>
<%@ 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);
%>
<jsp:useBean id="serviceBean" scope="page" class="egurkha.ServiceBean"/>
<%
	/* code for auditlog starts */
	boolean isAggregate = false;	
	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","Add/Modify Aggregate Test");
	auditLogThread.setValue("Activity","Delete Aggregate Test");
	/* code for auditlog ends */

	String test = request.getParameter("test");
	TestBean testBean = new TestBean(true);
	String dbDir = EgInstallInfo.getInstallDir()+"/bin/database";
	String sqlFileName=dbDir+"/"+test+"_MsSql.sql";
	String oracleFileName=dbDir+"/"+test+"_Oracle.sql";
	IniFile aggregateInfoSync = EgConfigFilesSync.getInstance(EgConfigInfo.egMgrConfigDir + "eg_aggregate.ini");
	ArrayList aggregateEntries = new ArrayList();
	String[] agg_fnts = aggregateInfoSync.getVariables(EgAggregateInfo.aggFunction);
	if (agg_fnts != null)
	{
		String thiskey = test+":";
		for(int i=0;i<agg_fnts.length;i++)
		{
			if (agg_fnts[i].startsWith(thiskey))
			{
				aggregateEntries.add(new IniHelper(IniHelper.DELETE,EgAggregateInfo.aggFunction,agg_fnts[i],""));
			}
		}
	} 
	String[] agg_test_infos = aggregateInfoSync.getVariables(EgAggregateInfo.testInfoExclude);
	if (agg_test_infos != null)
	{
		String thiskey = test+":";
		for(int i=0;i<agg_test_infos.length;i++)
		{
			if (agg_test_infos[i].startsWith(thiskey))
			{
				aggregateEntries.add(new IniHelper(IniHelper.DELETE,EgAggregateInfo.testInfoExclude,agg_test_infos[i],""));
			}
		}
	}
	String[] special_functions = aggregateInfoSync.getVariables("SPECIAL_FUNCTION");
	if (special_functions != null)
	{
		String thiskey = test+":";
		for(int i=0;i<special_functions.length;i++)
		{
			if (special_functions[i].startsWith(thiskey))
			{
				aggregateEntries.add(new IniHelper(IniHelper.DELETE,"SPECIAL_FUNCTION",special_functions[i],""));
			}
		}
	}
	aggregateEntries.add(new IniHelper(IniHelper.DELETE,EgAggregateInfo.aggTest,test,""));
	aggregateEntries.add(new IniHelper(IniHelper.DELETE,"MEASURES",test,""));
	for (int idx=0;idx<aggregateEntries.size();idx++)
	{
		IniHelper iniHelper = (IniHelper)aggregateEntries.get(idx);
		String iniAction = iniHelper.getAction();
		String sub = iniHelper.getSubject();
		String key = iniHelper.getVariable();
		String val = iniHelper.getValue();
		if(iniAction != null)
		{
			if(iniAction.equals(IniHelper.SET))
			{
				aggregateInfoSync.setValue(sub,key,val);
			}
			else if(iniAction.equals(IniHelper.DELETE))
			{
				aggregateInfoSync.deleteValue(sub,key);
			}
		}
	}			

	String sep = System.getProperty("file.separator");
	String dbServType = new EgDbInfo("manager").checkDBType();
	if(dbServType != null && dbServType.equals("EgMsSqlAdapter"))
	{
		String queryDir = EgInstallInfo.getInstallDir()+sep+"bin"+sep+"database"+sep+"mssql"+sep+"Aggregate_Test_Query";
		File dir = new File(queryDir);
		if(dir.isDirectory())
		{
			File queryFile = new File(queryDir+sep+test+".txt");
			if(queryFile.exists())
			{
				queryFile.delete();
			}
		}
	}
	else
	{
		String queryDir = EgInstallInfo.getInstallDir()+sep+"bin"+sep+"database"+sep+"Aggregate_Test_Query";
		File dir = new File(queryDir);
		if(dir.isDirectory())
		{
			File queryFile = new File(queryDir+sep+test+".txt");
			if(queryFile.exists())
			{
				queryFile.delete();
			}
		}
	}

	//IniFile lang_US = new IniFile(EgInstallInfo.getInstallDir()+"/manager/config/eg_lang_en_US.ini",true);
	IniFile lang_US = EgConfigFilesSync.getInstance(EgInstallInfo.getInstallDir()+"/manager/config/eg_lang_en_US.ini");
	lang_US.deleteValue("TEST_NAME_MAPPING",test);
	lang_US.saveFile();
	EgConfigFilesSync.releaseInstance(lang_US);

	IniFile egtests = EgConfigFilesSync.getInstance(EgInstallInfo.getInstallDir()+"/manager/config/eg_tests.ini");
	egtests.deleteValue("GLOBAL_THRESHOLDS",test);
	egtests.deleteValue("TREND_TESTS",test);
	egtests.saveFile();
	EgConfigFilesSync.releaseInstance(egtests);

	IniFile eguiini = EgConfigFilesSync.getInstance(EgInstallInfo.getInstallDir()+"/manager/config/eg_ui.ini");
	eguiini.deleteValue("TEST_PANEL_SUBTREE",test);
	String exIniVal = eguiini.getValue("TEST_PANEL_SUBTREE","SubTreeTestList");
	if(exIniVal != null && exIniVal.length()>0)
	{
		if(exIniVal.indexOf(test+",")==0)
		{
			eguiini.setValue("TEST_PANEL_SUBTREE","SubTreeTestList",exIniVal.substring(exIniVal.indexOf(",")+1));
		}
		else if(exIniVal.endsWith(","+test))
		{
			eguiini.setValue("TEST_PANEL_SUBTREE","SubTreeTestList",exIniVal.substring(0,exIniVal.lastIndexOf(",")));
		}
		else if(exIniVal.indexOf(","+test+",")>-1)
		{
			StringBuffer sb = new StringBuffer();
			String arr[] = StringUtils.getTokens(exIniVal,",");
			if(arr != null)
			{
				for(int i=0;i<arr.length;i++)
				{
					String t=arr[i];
					if(!test.equalsIgnoreCase(t))
					{
						sb.append(",");
						sb.append(t);
					}
				}
				String newVal = sb.toString();
				if(newVal.length()>0)
				{
					eguiini.setValue("TEST_PANEL_SUBTREE","SubTreeTestList",newVal.substring(1));
				}
			}
		}
	}
	eguiini.deleteValue("TEST_PANEL_SUBTREE","SecondLevelSubTreeList:"+test.trim());
	exIniVal = eguiini.getValue("TEST_PANEL_SUBTREE","SecondLevelSubTreeList");
	if(exIniVal != null && exIniVal.length()>0)
	{
		if(exIniVal.indexOf(test+",")==0)
		{
			eguiini.setValue("TEST_PANEL_SUBTREE","SecondLevelSubTreeList",exIniVal.substring(exIniVal.indexOf(",")+1));
		}
		else if(exIniVal.endsWith(","+test))
		{
			eguiini.setValue("TEST_PANEL_SUBTREE","SecondLevelSubTreeList",exIniVal.substring(0,exIniVal.lastIndexOf(",")));
		}
		else if(exIniVal.indexOf(","+test+",")>-1)
		{
			StringBuffer sb = new StringBuffer();
			String arr[] = StringUtils.getTokens(exIniVal,",");
			if(arr != null)
			{
				for(int i=0;i<arr.length;i++)
				{
					String t=arr[i];
					if(!test.equalsIgnoreCase(t))
					{
						sb.append(t);
						sb.append(",");
					}
				}
				String newVal = sb.toString();
				if(newVal.length()>0)
				{
					eguiini.setValue("TEST_PANEL_SUBTREE","SecondLevelSubTreeList",newVal);
				}
			}
		}
	}
	eguiini.saveFile();
	EgConfigFilesSync.releaseInstance(eguiini);

	ArrayList aggregateEntries_lang = new ArrayList();
	IniFile agglang = new IniFile(EgInstallInfo.getInstallDir()+"/manager/config/eg_aggregate_lang_en_US.ini");
	aggregateEntries_lang.add(new IniHelper(IniHelper.DELETE,"TEST_NAME_MAPPING",test,""));
	aggregateEntries_lang.add(new IniHelper(IniHelper.DELETE,"AGG_TEXT_NAME_MAPPING","admin.aggregate."+test,""));

	String variable_[] = agglang.getVariables("MEASURE_NAME_MAPPING");
	if(variable_ != null && variable_.length>0)
	{
		for(int i=0;i<variable_.length;i++)
		{
			if(variable_[i].startsWith(test+":"))
				aggregateEntries_lang.add(new IniHelper(IniHelper.DELETE,"MEASURE_NAME_MAPPING",variable_[i],""));
		}
	}
	if(aggregateEntries_lang.size() > 0)
	{
		IniFile aggregateLangSync = EgConfigFilesSync.getInstance(EgConfigInfo.egMgrConfigDir + "eg_aggregate_lang_en_US.ini");
		for(int idx=0;idx<aggregateEntries_lang.size();idx++)
		{
			IniHelper iniHelper = (IniHelper)aggregateEntries_lang.get(idx);
			String iniAction = iniHelper.getAction();
			String sub1 = iniHelper.getSubject();
			String key = iniHelper.getVariable();
			String val_1 = iniHelper.getValue();

			if(iniAction != null)
			{
				if (iniAction.equals(IniHelper.SET))
				{
					aggregateLangSync.setValue(sub1,key,val_1);
				}
				else if (iniAction.equals(IniHelper.DELETE))
				{
					aggregateLangSync.deleteValue(sub1,key);
				}
			}
		}			
		aggregateLangSync.saveFile();
		EgConfigFilesSync.releaseInstance(aggregateLangSync);
	}

	String variables[] = aggregateInfoSync.getVariables("SITE_AGGREGATE_TEST");
	if(variables != null && variables.length>0)
	{
		for(int i=0;i<variables.length;i++)
		{
			if(variables[i].equals(test))
			{
				aggregateEntries.add(new IniHelper(IniHelper.DELETE,"SITE_AGGREGATE_TEST",variables[i],""));
			}
		}
	}
	variables = aggregateInfoSync.getVariables("COLUMN_NAME_MAPPING");
	if(variables != null && variables.length>0)
	{
		for(int i=0;i<variables.length;i++)
		{
			String var = variables[i];
			if(var.startsWith(test+":"))
			{
				aggregateEntries.add(new IniHelper(IniHelper.DELETE,"COLUMN_NAME_MAPPING",var,""));
				aggregateEntries.add(new IniHelper(IniHelper.DELETE,"COMPARISON",var,""));
			}
		}
	}
	aggregateInfoSync.saveFile();
	EgConfigFilesSync.releaseInstance(aggregateInfoSync);

	if(aggregateEntries.size() > 0)
	{
		IniFile aggregateInfoSync1 = EgConfigFilesSync.getInstance(EgConfigInfo.egMgrConfigDir + "eg_aggregate.ini");
		for(int idx=0;idx<aggregateEntries.size();idx++)
		{
			IniHelper iniHelper = (IniHelper)aggregateEntries.get(idx);
			String iniAction = iniHelper.getAction();
			String sub1 = iniHelper.getSubject();
			String key = iniHelper.getVariable();
			String val_1 = iniHelper.getValue();

			if(iniAction != null)
			{
				if (iniAction.equals(IniHelper.SET))
				{
					aggregateInfoSync1.setValue(sub1,key,val_1);
				}
				else if (iniAction.equals(IniHelper.DELETE))
				{
					aggregateInfoSync1.deleteValue(sub1,key);
				}
			}
		}			
		aggregateInfoSync1.saveFile();
		EgConfigFilesSync.releaseInstance(aggregateInfoSync1);
	}

	testBean.deleteAggregateTest(test);
	/*
	* Remove entries in eg_format.ini, set while enabling detailed diagnosis
	* for tests enabled through IC.
	*
	* Remove these entries before removing any entry in any other INI file.
	*/
	testBean.removeDdEntries(test);
	testBean.deleteTest(test);
	DbBean dbBean = new DbBean(true);
	serviceBean.stopDiscovery();
	dbBean.removeTest(test);
	UpdateAndTransmitICInfo updateICInfo = new UpdateAndTransmitICInfo();
	updateICInfo.deleteTestFromDb(test);
	serviceBean.startDiscovery("1");
	EgTestParamBean testParamIni = new EgTestParamBean();
	//testParamIni.createFile("eg_testparam.ini","save");
	testParamIni.createSyncFile("eg_testparam.ini","save");
	testParamIni.deleteValues("RADIO",test);
	testParamIni.saveFile();
	testParamIni.releaseInstance();
	
	this.deleteUDQueryFile(sqlFileName);
	this.deleteUDQueryFile(oracleFileName);

	auditLogThread.setValue("Description","Test "+"$test$:"+test+" has been deleted");
	auditLogThread.sendItToDB();
%>
	<jsp:forward page="aggDisplayTests.jsp" />
<%!
	private void deleteUDQueryFile(String fileName)
	{
		if(fileName == null || fileName.equals(""))
			return ;
		File file =new File(fileName);
		if(file != null)
		{
			if(file.isFile())
				file.delete();
		}
	}
%>
