<%
/*
 *		Name		:	Egconfigtestparamp.jsp
 *		Purpose		:	To modify IC test deails process page
 */
%>

<%@ include file="../../EgTrackerAJSON.jsp"%>
<%@page import = "com.egurkha.util.StringUtils"%>
<%@ page language = "java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import = "com.egurkha.admin.AuditLogThread"%>
<%@ page import = "egurkha.*" %>
<%@ page import = "com.eg.EgConfigFilesSync"%>
<%@ page import = "net.sf.json.JSONObject"%>
<%@ page import= "egurkha.EgUIUtils"%>

<%
	response.setHeader("Cache-Control","no-store");
	response.setHeader("Pragma","no-cache");
	response.setDateHeader("Expires", -1);
	response.setContentType("application/json;charset=UTF-8");

	JSONObject rootJSON = new JSONObject();
	rootJSON.put("success","true");

	ServiceBean serviceBean = new ServiceBean();
	String mod = request.getParameter("mod");
	TestBean testBean = new TestBean(true);
	EgSpecsInfo specsInfo = new EgSpecsInfo("manager");
	String test = request.getParameter("test");
	String testType = request.getParameter("testtype");
	String type = request.getParameter("type");
	String port = request.getParameter("port");
	String os = request.getParameter("os");
	String dbType = request.getParameter("dbtype");
	String param = "";
	String value = "";
	boolean portBased = false;
	boolean internal = false;
	boolean status = false;
	/* 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","Integration Console");
	//auditLogThread.setValue("Activity","Tests");
	/* code for auditlog ends */
	//skm
	String testname = request.getParameter("testname"); 

	if(mod.equals("test"))
	{
		portBased = port.equalsIgnoreCase("Yes");
 		internal = type.equalsIgnoreCase("Internal");
		//The following line is to ensure that the stopDiscovery & startDiscovery runs inside a sync block
		IniFile infraIni = EgConfigFilesSync.getInstance(EgInstallInfo.getInstallDir() + "/manager/config/eg_infrastructure.ini");
		serviceBean.stopDiscovery();
		testBean.addTest(test, testType, internal, portBased, os, dbType);
		serviceBean.startDiscovery("1");
		EgConfigFilesSync.releaseInstance(infraIni);
		String ur = "EgConfigTestParamJason.jsp?test=" + test + "&testtype=" + testType;

		rootJSON.put("AlertType","Result");
		rootJSON.put("AlertTitle",_mapping.getTextDescription("admin.ic.Modify Test"));
		rootJSON.put("AlertMsg",_mapping.getTextDescription("admin.ic.Test has been modified successfully"));
		rootJSON.put("forwardPage",true);
		rootJSON.put("forwardURL",ur);
	}
	else if(mod.equals("param"))
	{
		/* code for auditlog starts */
		auditLogThread.setValue("Activity","Configure Test");
		/* code for auditlog ends */
		param = EgUIUtils.decodeSecurityChar(request.getParameter("param"));
		value = request.getParameter("value");
		if(value.indexOf(",")!=-1)
			value=StringUtils.replace(value, ',', '|'); //modified to replace , with # to write to eg_udtests.ini
		if(testBean.doesParamExistFor(param, test))
		{
	        rootJSON.put("AlertType","Error");
			rootJSON.put("AlertTitle",_mapping.getTextDescription("admin.error.Add Parameter To Test"));
			String msg = _mapping.getTextDescription("admin.error.You are trying to add a parameter which is already existing for this test")+". ";
			msg += _mapping.getTextDescription("admin.error.Please enter a new parameter name")+".";
			rootJSON.put("AlertMsg",msg);
			/* code for auditlog starts */
			if(test.endsWith("_ex"))
				auditLogThread.setValue("Description","You are trying to add a parameter " + param +  " which is already existing for the test " + test);
			else
				auditLogThread.setValue("Description","You are trying to add a parameter " + param +  " which is already existing for the test " + "$test$:"+test);
			auditLogThread.sendItToDB();
			/* code for auditlog ends */
		}
		else
		{
			/* code for auditlog starts */	
			auditLogThread.setCurrentValue("01~~Parameter",param);
			auditLogThread.setCurrentValue("02~~Default Value",value);
			//auditLogThread.setCurrentValue(param,value);
			if(test.endsWith("_ex"))
				auditLogThread.setValue("Description","Parameter " + param + " has been added for the test " + test);
			else
				auditLogThread.setValue("Description","Parameter " + param + " has been added for test " + "$test$:"+test);
			auditLogThread.sendItToDB();
			/* code for auditlog ends */
			testBean.addParamForTest(test, param, value);
			Hashtable ht = testBean.getTestParam(test);
			portBased = testBean.isTestPortBased(test);
			internal = !specsInfo.isTestExternal(test);
			//The following line is to ensure that the stopDiscovery & startDiscovery runs inside a sync block
			IniFile infraIni = EgConfigFilesSync.getInstance(EgInstallInfo.getInstallDir() + "/manager/config/eg_infrastructure.ini");
			serviceBean.stopDiscovery();
			testBean.addTestToSpecs(test,portBased,internal,ht,os,dbType);
			serviceBean.startDiscovery("1");
			EgConfigFilesSync.releaseInstance(infraIni);
			String ur = "EgConfigTestParamJson.jsp?" + "test=" + test + "&testtype=" + testType;

			rootJSON.put("forwardPage",true);
			rootJSON.put("forwardURL",ur);
			rootJSON.put("AlertType","Result");
			rootJSON.put("AlertTitle",_mapping.getTextDescription("admin.error.Add Parameter To Test"));
			rootJSON.put("AlertMsg",_mapping.getTextDescription("admin.ic.Parameter has been added successfully"));
		}
	}
	else if(mod.equals("modify_params"))
	{
		/* code for auditlog starts */
		auditLogThread.setValue("Activity","Configure Test");
		/* code for auditlog ends */

		param = request.getParameter("one");
		value = EgUIUtils.decodeSecurityChar(request.getParameter("two"));
		String org_param = EgUIUtils.decodeSecurityChar(request.getParameter("org_param"));

		if(value.indexOf(",")!=-1)
			value=StringUtils.replace(value, ',', '|'); //replace , with # to write to eg_udtests.ini
		if(org_param.indexOf(",")!=-1)
			org_param=StringUtils.replace(org_param, ',', '|');

		status = testBean.modifyParamForTest(test, param, value, org_param);
		if(status)
		{
			Hashtable ht = testBean.getTestParam(test);
			portBased = testBean.isTestPortBased(test);
			internal = !specsInfo.isTestExternal(test);
			//The following line is to ensure that the stopDiscovery & startDiscovery runs inside a sync block
			IniFile infraIni = EgConfigFilesSync.getInstance(EgInstallInfo.getInstallDir() + "/manager/config/eg_infrastructure.ini");
			serviceBean.stopDiscovery();
			testBean.addTestToSpecs(test,portBased,	internal,ht,os, dbType);
			serviceBean.startDiscovery("1");
			EgConfigFilesSync.releaseInstance(infraIni);
			String ur = "EgConfigTestParamJson.jsp?" 
				+ "test=" + test + "&testtype=" + testType;
			/* code for auditlog starts */
			auditLogThread.setPreviousValue("01~~Parameter",org_param.substring(0,org_param.indexOf(":")));
			auditLogThread.setPreviousValue("02~~Default Value",org_param.substring(org_param.indexOf(":")+1));
			auditLogThread.setCurrentValue("01~~Parameter",param);
			auditLogThread.setCurrentValue("02~~Default Value",value);

			if(!org_param.substring(org_param.indexOf(":")+1).equals(value))
			{
				if(test.endsWith("_ex"))
					auditLogThread.setValue("Description","Parameter " + param + " of test " + test + " has been modified");
				else
					auditLogThread.setValue("Description","Parameter " + param + " of test " + "$test$:"+test + " has been modified");
			}
			if(!org_param.substring(0,org_param.indexOf(":")).equals(param) || !org_param.substring(org_param.indexOf(":")+1).equals(value))
				auditLogThread.sendItToDB();
			/* code for auditlog ends */
			rootJSON.put("AlertType","Result");
			rootJSON.put("AlertTitle",_mapping.getTextDescription("admin.error.Modify test parameters"));
			rootJSON.put("AlertMsg",_mapping.getTextDescription("admin.ic.Parameter has been modified successfully"));
			rootJSON.put("forwardPage",true);
			rootJSON.put("forwardURL",ur);
		}
		else
		{
			/* code for auditlog starts */
			if(test.endsWith("_ex"))
				auditLogThread.setValue("Description","Error while modifying test parameter for the test " + test);
			else
				auditLogThread.setValue("Description","Error while modifying test parameter for the test " + "$test$:"+test);
			auditLogThread.sendItToDB();
			/* code for auditlog ends */

			rootJSON.put("AlertType","Result");
			rootJSON.put("AlertTitle",_mapping.getTextDescription("admin.error.Modify test parameters"));
			rootJSON.put("AlertMsg",_mapping.getTextDescription("admin.error.Error while modifying test parameters")+".");
		}
	}
	else if(mod.equals("modify_test"))
	{
		/* code for auditlog starts */
		auditLogThread.setValue("Activity","Modify Test");
		/* code for auditlog ends */

		portBased = port.equalsIgnoreCase("Yes");
 		internal = type.equalsIgnoreCase("Internal");
		//The following line is to ensure that the stopDiscovery & startDiscovery runs inside a sync block
		IniFile infraIni = EgConfigFilesSync.getInstance(EgInstallInfo.getInstallDir() + "/manager/config/eg_infrastructure.ini");
		serviceBean.stopDiscovery();

		String ur = null;
		String $_type = request.getParameter("$_type");
		String $_port = request.getParameter("$_port");
		String $_os = request.getParameter("$_os");
		String $_dbtype = request.getParameter("$_dbtype");
	
		/* code for auditlog starts */
		auditLogThread.setPreviousValue("Execution type", $_type.trim());
		auditLogThread.setPreviousValue("Port based", $_port.trim());
		if($_os != null && !$_os.equalsIgnoreCase("null") && $_os.trim().length()>0)
			auditLogThread.setPreviousValue("Operating system", $_os.trim());
		if($_dbtype != null && !$_dbtype.equalsIgnoreCase("null") && $_dbtype.trim().length()>0)
			auditLogThread.setPreviousValue("DB type", $_dbtype.trim());

		auditLogThread.setCurrentValue("Execution type", type.trim());
		auditLogThread.setCurrentValue("Port based", port.trim());
		if(os != null && !os.equalsIgnoreCase("null") && os.trim().length()>0)
			auditLogThread.setCurrentValue("Operating system", os.trim());
		if(dbType != null && !dbType.equalsIgnoreCase("null") && dbType.trim().length()>0)
			auditLogThread.setCurrentValue("DB type", dbType.trim());
		
		if(test.endsWith("_ex"))
			auditLogThread.setValue("Description","Test "+test+" has been modified");
		else
			auditLogThread.setValue("Description","Test "+"$test$:"+test+" has been modified");
		auditLogThread.sendItToDB();
		/* code for auditlog ends */
		testBean.modifyTest(test, testType, internal, portBased, os, dbType);
		ur = "EgConfigTestParamJson.jsp?test=" + test + "&testtype=" + testType;
		serviceBean.startDiscovery("1");
		EgConfigFilesSync.releaseInstance(infraIni);
		rootJSON.put("AlertType","Result");
		rootJSON.put("AlertTitle",_mapping.getTextDescription("admin.ic.Modify Test"));
		rootJSON.put("AlertMsg",_mapping.getTextDescription("admin.ic.Test has been modified successfully"));
		rootJSON.put("forwardPage",true);
		rootJSON.put("forwardURL",ur);
	}
	else if(mod.equals("delParam"))
	{
		/* code for auditlog starts */
		auditLogThread.setValue("Activity","Configure Test");
		
		/* code for auditlog ends */
		param = request.getParameter("param");
		testBean.deleteParam(test, param);
		Hashtable ht = testBean.getTestParam(test);
		/* code for auditlog starts */
		
		if(test.endsWith("_ex"))
			auditLogThread.setValue("Description","Parameter " + param + " of test " + test + " has been deleted");
		else
			auditLogThread.setValue("Description","Parameter " + param + " of test " + "$test$:"+test + " has been deleted");
		auditLogThread.sendItToDB();
		/* code for auditlog ends */
		portBased = testBean.isTestPortBased(test);
		internal = !specsInfo.isTestExternal(test);;
		//The following line is to ensure that the stopDiscovery & startDiscovery runs inside a sync block
		IniFile infraIni = EgConfigFilesSync.getInstance(EgInstallInfo.getInstallDir() + "/manager/config/eg_infrastructure.ini");
		serviceBean.stopDiscovery();
		testBean.addTestToSpecs(test, portBased, internal, ht, os, dbType);
		serviceBean.startDiscovery("1");
		EgConfigFilesSync.releaseInstance(infraIni);
		String ur = "EgConfigTestParamJson.jsp?" + "test=" + test + "&testtype=" + testType;
		rootJSON.put("AlertType","Result");
		rootJSON.put("AlertTitle",_mapping.getTextDescription("admin.error.Modify test parameters"));
		rootJSON.put("AlertMsg",_mapping.getTextDescription("admin.ic.Parameter has been deleted successfully"));
		rootJSON.put("forwardPage",true);
		rootJSON.put("forwardURL",ur);
	}
	out.println(rootJSON);

%>



