<%@ include file = "../../EgTrackerAJSON.jsp" %>
<%@ page import = "egurkha.TransactionBean"%>
<%@ page import = "com.eg.EgCrypt"%>
<%@ page import = "net.sf.json.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

<%
/*
*	This page helps to load the data for Trouble Ticket Integration(Web Services) node in Manager Settings tree.
*	ini File : eg_services.ini
*/
%>

<%
	response.setContentType("application/json");

	TransactionBean transBean = new TransactionBean();

	transBean.createFile("eg_services.ini");

	JSONObject rootJson = new JSONObject();
	JSONArray FilterArray = new JSONArray();
	JSONArray ttIntegrationSystemArray = new JSONArray();


	String tt_integration = "TT_INTEGRATION";
	String TTOtherDispName = "";

	String TTIntegrationEnable = transBean.getStrValue(tt_integration,"ENABLE_TT_INTEGRATION");
	String TT_Integration_Systems=transBean.getStrValue(tt_integration,"TT_INTEGRATION_SYSTEMS");
	String TTSys = transBean.getStrValue(tt_integration,"TT_INTEGRATION_ENABLED_SYSTEM");
	TTIntegrationEnable = (TTIntegrationEnable != null && TTIntegrationEnable.length() > 0)? TTIntegrationEnable : "No";
	TT_Integration_Systems = (TT_Integration_Systems != null && TT_Integration_Systems.length() > 0)? TT_Integration_Systems : "SERVICE NOW,MANAGE ENGINE,AUTO TASK,REMEDY FORCE,PAGER DUTY";
	TTSys = (TTSys != null && TTSys.length() > 0)? TTSys : "MANAGE ENGINE";
	String OldTTSys = TTSys;

	String selectSysName = request.getParameter("selectSysName");
	if (selectSysName != null && selectSysName.length() > 0)
	{
		TTSys = selectSysName;
		//TTIntegrationEnable = "Yes";
	}

//	boolean booValue = false;
//	if (TTIntegrationEnable != null && (TTIntegrationEnable.equalsIgnoreCase("yes") || TTIntegrationEnable.equalsIgnoreCase("true")))
//	{
//		booValue = true;
//	}

	String defaultTTSys = "TT_INTEGRATION_"+TTSys;
	String TTUrl = transBean.getStrValue(defaultTTSys,"url");
	String TTPort = transBean.getStrValue(defaultTTSys,"port");
	String TTUsername = transBean.getStrValue(defaultTTSys,"user");
	String TTPassword = EgCrypt.decrypt(transBean.getStrValue(defaultTTSys,"password"));
	String TTCallerID = transBean.getStrValue(defaultTTSys,"callerId");
	String TTOutput = transBean.getStrValue(defaultTTSys,"outputFormat");
	String TTAssignGroup = transBean.getStrValue(defaultTTSys,"assignmentGroup");
	String TTAssignTo = transBean.getStrValue(defaultTTSys,"assignmentTo");
	String TTCategory = transBean.getStrValue(defaultTTSys,"category");
	String TTCriticalDueDate = transBean.getStrValue(defaultTTSys,"criticalDueDatePeriod");
	String TTMajorDueDate = transBean.getStrValue(defaultTTSys,"majorDueDatePeriod");
	String TTMinorDueDate = transBean.getStrValue(defaultTTSys,"minorDueDatePeriod");
	String TTTicketTitleFormat = transBean.getStrValue(defaultTTSys,"ticketTitleFormat");

	String isProxyEnabled = transBean.getStrValue(defaultTTSys,"isProxyEnabled");
	String proxyHostName = transBean.getStrValue(defaultTTSys,"proxyHostName");
	String proxyPort = transBean.getStrValue(defaultTTSys,"proxyPort");
	String proxyUserName = transBean.getStrValue(defaultTTSys,"proxyUserName");

	//String proxyPassword = transBean.getStrValue(defaultTTSys,"proxyPassword");
	
	String proxyPassword = EgCrypt.decrypt(transBean.getStrValue(defaultTTSys,"proxyPassword"));

	String TTWrapper = transBean.getStrValue(defaultTTSys,"ttIntegratorClass");
	String ArchieveFile = transBean.getStrValue(defaultTTSys,"supportingJar");
	String TTApiKey = transBean.getStrValue(defaultTTSys,"apiKey");
	String TTAllowedAlarms = transBean.getStrValue(tt_integration,"ALLOWED_ALARMS");
	String TTAcctDiscPeriod = transBean.getStrValue(defaultTTSys,"accountDiscoverPeriod");
	String TTQueueID = transBean.getStrValue(defaultTTSys,"queueId");
	String TTDefaultAcct = transBean.getStrValue(defaultTTSys,"defaultAccount");
	String TTTicketType = transBean.getStrValue(defaultTTSys,"ticketType");
	String TTWorkType = transBean.getStrValue(defaultTTSys,"workType");
	String TTPartnerUtl = transBean.getStrValue(defaultTTSys,"partnerurl");
	String TTServiceURL = transBean.getStrValue(defaultTTSys,"ServiceURL");
	String TTServiceKey = transBean.getStrValue(defaultTTSys,"ServiceKey");
	String TTTitleFormat = transBean.getStrValue(defaultTTSys,"TitleFormat");
	String TTDescFormat = transBean.getStrValue(defaultTTSys,"DescriptionFormat");
	String TTClient = transBean.getStrValue(defaultTTSys,"Client");
	String TTClientURL = transBean.getStrValue(defaultTTSys,"ClientURL");
	String TTManageEngine = transBean.getStrValue("TT_INTERGRATION_SYSTEM_FIELDS","MANAGE ENGINE");
	String TTAutoTask = transBean.getStrValue("TT_INTERGRATION_SYSTEM_FIELDS","AUTO TASK");
	String TTServiceNow = transBean.getStrValue("TT_INTERGRATION_SYSTEM_FIELDS","SERVICE NOW");
	String TTRemedyForce = transBean.getStrValue("TT_INTERGRATION_SYSTEM_FIELDS","REMEDY FORCE");
	String TTPagerDuty = transBean.getStrValue("TT_INTERGRATION_SYSTEM_FIELDS","PAGER DUTY");


	if ( TTAllowedAlarms != null && TTAllowedAlarms.indexOf("Critical") >= 0  )
	{
		String criticalAlarms = "checked";
		rootJson.put("criticalAlarms",criticalAlarms);
	}
	if ( TTAllowedAlarms != null && TTAllowedAlarms.indexOf("Major") >= 0  )
	{
		String majorAlarms = "checked";
		rootJson.put("majorAlarms",majorAlarms);
	}
	if ( TTAllowedAlarms != null && TTAllowedAlarms.indexOf("Minor") >= 0  )
	{
		String minorAlarms = "checked";
		rootJson.put("minorAlarms",minorAlarms);
	}
	if ( TTAllowedAlarms != null && TTAllowedAlarms.indexOf("Normal") >= 0  )
	{
		String normalAlarms = "checked";
		rootJson.put("normalAlarms",normalAlarms);
	}

	TTUrl = (TTUrl != null && TTUrl.length() > 0)? TTUrl : "$unconfigured";
	TTPort = (TTPort != null && TTPort.length() > 0)? TTPort : "$unconfigured";
	TTUsername = (TTUsername != null && TTUsername.length() > 0)? TTUsername : "$unconfigured";
	TTPassword = (TTPassword != null && TTPassword.length() > 0)? TTPassword : "$unconfigured";
	TTCallerID = (TTCallerID != null && TTCallerID.length() > 0)? TTCallerID : "$unconfigured";
	TTOutput = (TTOutput != null && TTOutput.length() > 0)? TTOutput : "<Operation>\n<Details>\n<requester>eG_manager</requester>\n<subject>$cname/$ctype/$pdesc/$prior</subject>\n<description>$pdesc</description>\n<callbackURL>CustomReportHandler.do</callbackURL>\n<requesttemplate>-</requesttemplate>\n<priority>$prior</priority>\n<layer>$layer</layer>\n<group>$user</group>\n<technician>eG_manager</technician>\n<level>1</level>\n<status>$status</status>\n<service>$Service</service>\n</Details>\n</Operation>";
	TTAssignGroup = (TTAssignGroup != null && TTAssignGroup.length() > 0)? TTAssignGroup : "$unconfigured";
	TTAssignTo = (TTAssignTo != null && TTAssignTo.length() > 0)? TTAssignTo : "$unconfigured";
	TTCategory = (TTCategory != null && TTCategory.length() > 0)? TTCategory : "request";
	TTCriticalDueDate = (TTCriticalDueDate != null && TTCriticalDueDate.length() > 0)? TTCriticalDueDate : "24";
	TTMajorDueDate = (TTMajorDueDate != null && TTMajorDueDate.length() > 0)? TTMajorDueDate : "36";
	TTMinorDueDate = (TTMinorDueDate != null && TTMinorDueDate.length() > 0)? TTMinorDueDate : "48";
	TTTicketTitleFormat = (TTTicketTitleFormat != null && TTTicketTitleFormat.length() > 0)? TTTicketTitleFormat : "Priority :$prior Component : $cname Component Type : $ctype Layer : $layer Problem Description : $pdesc";
	TTApiKey = (TTApiKey != null && TTApiKey.length() > 0)? TTApiKey : "$unconfigured";
	TTAcctDiscPeriod = (TTAcctDiscPeriod != null && TTAcctDiscPeriod.length() > 0)? TTAcctDiscPeriod : "3600000";
	TTQueueID = (TTQueueID != null && TTQueueID.length() > 0)? TTQueueID : "8";
	TTDefaultAcct = (TTDefaultAcct != null && TTDefaultAcct.length() > 0)? TTDefaultAcct : "$unconfigured";
	TTTicketType = (TTTicketType != null && TTTicketType.length() > 0)? TTTicketType : "3";
	TTWorkType = (TTWorkType != null && TTWorkType.length() > 0)? TTWorkType : "29683401";
	TTPartnerUtl = (TTPartnerUtl != null && TTPartnerUtl.length() > 0)? TTPartnerUtl : "$unconfigured";
	TTServiceURL = (TTServiceURL != null && TTServiceURL.length() > 0)? TTServiceURL : "https://events.pagerduty.com/generic/2010-04-15/create_event.json";
	TTServiceKey = (TTServiceKey != null && TTServiceKey.length() > 0)? TTServiceKey : "$unconfigured";
	TTTitleFormat = (TTTitleFormat != null && TTTitleFormat.length() > 0)? TTTitleFormat : "Priority :$prior Component : $cname Component Type : $ctype Layer : $layer Problem Description : $pdesc Start Time : $starttime";
	TTDescFormat = (TTDescFormat != null && TTDescFormat.length() > 0)? TTDescFormat : "$prior $cname $ctype $layer $pdesc $starttime";
	TTClient = (TTClient != null && TTClient.length() > 0)? TTClient : "$unconfigured";
	TTClientURL = (TTClientURL != null && TTClientURL.length() > 0)? TTClientURL : "$unconfigured";
	TTWrapper = (TTWrapper != null && TTWrapper.length() > 0)? TTWrapper : "";

	String ttWraperChecked = "No";
	String displayTTIntegrationArchive = "";
	String displayUploadButtonDiv = "";

	if(TTWrapper.equalsIgnoreCase("none"))
	{
		displayTTIntegrationArchive="block";
		displayUploadButtonDiv="block";
	}



	if(TT_Integration_Systems!=null && TT_Integration_Systems.length()>0)
	{
		StringTokenizer stkTTSys = new StringTokenizer(TT_Integration_Systems,",");
		while(stkTTSys.hasMoreTokens())
		{
			JSONObject ttSysObj = new JSONObject();
			String ttSys1=stkTTSys.nextToken();
			String selected="";
			ttSysObj.put("displayField",ttSys1);
			ttSysObj.put("valueField",ttSys1);

			if ( selectSysName != null )
			{
				if ( ttSys1.equals(selectSysName) )
				{
					rootJson.put("ttSys1",ttSys1);
					selected="selected";
				}
			}
			else if ( ttSys1.equals(TTSys) )
			{
				rootJson.put("ttSys1",ttSys1);
				selected="selected";
			}
			ttIntegrationSystemArray.add(ttSysObj);
		}
	}

	//String[] options={"24", "36", "48"};
	if( TTSys != null)
	{
		String TTSystemFields = transBean.getStrValue("TT_INTERGRATION_SYSTEM_FIELDS",TTSys);
		StringTokenizer TTMEtokens = new StringTokenizer(TTSystemFields,",");	
		while (TTMEtokens.hasMoreTokens())
		{
			String TTMEtokensvalue=TTMEtokens.nextToken();
			if(!(TTMEtokensvalue.equalsIgnoreCase("TTIntegratorClass")|| TTMEtokensvalue.equalsIgnoreCase("SupportingJar") || TTMEtokensvalue.endsWith("_optionNames") || TTMEtokensvalue.endsWith("_optionValues")))
			{
				JSONObject nextLineObj  = new JSONObject();
				//JSONObject hiddenFieldsObj  = new JSONObject();
				String TTSystemLabels = transBean.getStrValue("TT_INTEGRATION_TEXT_MAPPING",TTMEtokensvalue);
				if(TTMEtokensvalue!=null && TTMEtokensvalue.equals("isProxyEnabled"))
				{
					if(TTSys!=null && TTSys.equalsIgnoreCase("ATF"))
					{
						TTSystemLabels="Does ATF uses Proxy";
					}
					else if(TTSys!=null && !TTSys.equalsIgnoreCase("SERVICE NOW"))
					{
						TTSystemLabels="Does "+TTSys+" uses Proxy";
					}
				}
				else if((TTSys!=null && TTSys.equalsIgnoreCase("ATF")) && (TTMEtokensvalue!=null && TTMEtokensvalue.equals("category")))
				{
					TTSystemLabels="Default Category";
				}
				nextLineObj.put("lable",_mapping.getTextDescription("admin.tt."+TTSystemLabels)); 
				nextLineObj.put("id",TTMEtokensvalue);
				String TTFieldsType = transBean.getStrValue("TT_INTEGRATION_FIELD_MAPPING",TTMEtokensvalue);
				nextLineObj.put("type",TTFieldsType);
				String TTFieldsTypeselectedValue = transBean.getStrValue("TT_INTEGRATION_"+TTSys,TTMEtokensvalue);
				if(TTFieldsType != null && TTFieldsType.trim().equalsIgnoreCase("radioButton"))
				{	
					JSONArray yesCtrlFieldsArray = new JSONArray();
					JSONArray noCtrlFieldsArray = new JSONArray();
					String yesCtrlFields = transBean.getStrValue("TT_INTEGRATION_" + TTSys, TTMEtokensvalue + "_yesCtrlFields");
					String noCtrlFields = transBean.getStrValue("TT_INTEGRATION_" + TTSys, TTMEtokensvalue + "_noCtrlFields");
					if(yesCtrlFields != null && !yesCtrlFields.trim().equalsIgnoreCase("")){
						StringTokenizer yesCtrlFieldstokens = new StringTokenizer(yesCtrlFields, ",");	
						while (yesCtrlFieldstokens.hasMoreTokens())
						{
							String yesCtrlFieldstoken = yesCtrlFieldstokens.nextToken();
							yesCtrlFieldsArray.add(yesCtrlFieldstoken);
						}
						nextLineObj.put("yesCtrlFields", yesCtrlFieldsArray);
					}
					if(noCtrlFields != null && !noCtrlFields.trim().equalsIgnoreCase("")){
						StringTokenizer noCtrlFieldstokens = new StringTokenizer(noCtrlFields, ",");	
						while (noCtrlFieldstokens.hasMoreTokens())
						{
							String noCtrlFieldstoken = noCtrlFieldstokens.nextToken();
							noCtrlFieldsArray.add(noCtrlFieldstoken);
						}
						nextLineObj.put("noCtrlFields", noCtrlFieldsArray);
					}
				//	System.out.println("CtrlFieldsArray------>" + CtrlFieldsArray);

					if(TTFieldsTypeselectedValue != null && (TTFieldsTypeselectedValue.equalsIgnoreCase("true") || TTFieldsTypeselectedValue.equalsIgnoreCase("yes"))){
						nextLineObj.put("checked",true);
					} else {
						nextLineObj.put("checked",false);
					}

				}
				if(TTMEtokensvalue.equalsIgnoreCase("Password") || TTMEtokensvalue.equalsIgnoreCase("proxyPassword"))
				{
					TTFieldsTypeselectedValue=EgCrypt.decrypt(TTFieldsTypeselectedValue);
				}
				//nextLineObj.put("selectedValue",(TTFieldsTypeselectedValue.equalsIgnoreCase("") || TTFieldsTypeselectedValue.equalsIgnoreCase("none"))?"$unconfigured":TTFieldsTypeselectedValue);
				nextLineObj.put("selectedValue",(TTFieldsTypeselectedValue.equalsIgnoreCase(""))?"$unconfigured":TTFieldsTypeselectedValue);
				if(TTFieldsType.equalsIgnoreCase("Combo"))
				{
					JSONArray  timeLineComboValues = new JSONArray();
					String TTFieldsOptionNames = transBean.getStrValue("TT_INTEGRATION_" + TTSys, TTMEtokensvalue + "_optionNames");
					String TTFieldsOptionValues = transBean.getStrValue("TT_INTEGRATION_" + TTSys, TTMEtokensvalue + "_optionValues");



					if(TTFieldsOptionNames != null && TTFieldsOptionValues != null) {
						String[] optionNames = TTFieldsOptionNames.split(",");
						String[] optionValues = TTFieldsOptionValues.split(",");
						boolean isOptionsDataValid = false;
						
						if(optionNames != null && optionValues != null && (optionNames.length == optionValues.length)){
							isOptionsDataValid = true;
						}
						if(isOptionsDataValid) {
							nextLineObj.put("selectedValue", (TTFieldsTypeselectedValue.equalsIgnoreCase("none") || TTFieldsTypeselectedValue.equals(""))?optionNames[0]:TTFieldsTypeselectedValue);
						} else {
							nextLineObj.put("selectedValue", (TTFieldsTypeselectedValue.equalsIgnoreCase("none") || TTFieldsTypeselectedValue.equals(""))?optionValues[0]:TTFieldsTypeselectedValue);
						}

						for(int e_ctr=0; e_ctr < optionValues.length;e_ctr++)
						{
							JSONObject timelineOption = new JSONObject();
							if(isOptionsDataValid) {
								timelineOption.put("name", optionNames[e_ctr]);
							} else {
								timelineOption.put("name", optionValues[e_ctr]);
							}
							timelineOption.put("value", optionValues[e_ctr]);
							timeLineComboValues.add(timelineOption);
						} 
					}
					nextLineObj.put("options",timeLineComboValues);
				}
				//System.out.println("nextLineObj : "+nextLineObj);
				//hiddenFieldsObj.put("type","hidden");
				//hiddenFieldsObj.put("id","$_"+TTMEtokensvalue);
				//hiddenFieldsObj.put("name","$_"+TTMEtokensvalue);
				FilterArray.add(nextLineObj);
				//FilterArray.add(hiddenFieldsObj);
			}
		}
	}

	if(defaultTTSys.equals("TT_INTEGRATION_SERVICE NOW"))
	{
		rootJson.put("url",TTUrl);
		rootJson.put("password",TTPassword);
		rootJson.put("user",TTUsername);
		rootJson.put("port",TTPort);
		rootJson.put("callerId",TTCallerID);
		rootJson.put("assignmentGroup",TTAssignGroup);
		rootJson.put("assignmentTo",TTAssignTo);
		rootJson.put("category",TTCategory);
		rootJson.put("criticalDueDatePeriod",TTCriticalDueDate);
		rootJson.put("majorDueDatePeriod",TTMajorDueDate);
		rootJson.put("minorDueDatePeriod",TTMinorDueDate);
		rootJson.put("ticketTitleFormat",TTTicketTitleFormat);

		rootJson.put("proxyPassword",proxyPassword);
	}
	else if(defaultTTSys.equals("TT_INTEGRATION_MANAGE ENGINE"))
	{
		rootJson.put("url",TTUrl);
		rootJson.put("password",TTPassword);
		rootJson.put("user",TTUsername);
		rootJson.put("apiKey",TTApiKey);
		rootJson.put("outputFormat",TTOutput);
	}
	else if(defaultTTSys.equals("TT_INTEGRATION_AUTO TASK"))
	{
		rootJson.put("url",TTUrl);
		rootJson.put("password",TTPassword);
		rootJson.put("user",TTUsername);
		rootJson.put("accountDiscoverPeriod",TTAcctDiscPeriod);
		rootJson.put("queueId",TTQueueID);
		rootJson.put("defaultAccount",TTDefaultAcct);
		rootJson.put("ticketType",TTTicketType);
		rootJson.put("workType",TTWorkType);
		rootJson.put("criticalDueDatePeriod",TTCriticalDueDate);
		rootJson.put("majorDueDatePeriod",TTMajorDueDate);
		rootJson.put("minorDueDatePeriod",TTMinorDueDate);
		rootJson.put("ticketTitleFormat",TTTicketTitleFormat);
	}
	else if(defaultTTSys.equals("TT_INTEGRATION_REMEDY FORCE"))
	{
		rootJson.put("url",TTUrl);
		rootJson.put("partnerurl",TTPartnerUtl);
		rootJson.put("password",TTPassword);
		rootJson.put("user",TTUsername);
	}
	else if(defaultTTSys.equals("TT_INTEGRATION_PAGER DUTY"))
	{
		rootJson.put("ServiceURL",TTServiceURL);
		rootJson.put("ServiceKey",TTServiceKey);
		rootJson.put("TitleFormat",TTTitleFormat);
		rootJson.put("DescriptionFormat",TTDescFormat);
		rootJson.put("Client",TTClient);
		rootJson.put("ClientURL",TTClientURL);
	}
	rootJson.put("ttWraperChecked",ttWraperChecked);
	rootJson.put("displayUploadButtonDiv",displayUploadButtonDiv);
	rootJson.put("displayTTIntegrationArchive",displayTTIntegrationArchive);
	rootJson.put("TTWrapper",TTWrapper);
	rootJson.put("ArchieveFile",ArchieveFile);
	rootJson.put("filters",FilterArray);
	rootJson.put("TTIntegrationEnable",TTIntegrationEnable);
	rootJson.put("ttIntegrationSystemArray",ttIntegrationSystemArray);
	rootJson.put("TTSys",TTSys);
	rootJson.put("OldTTSys",OldTTSys);
	rootJson.put("TTManageEngine",TTManageEngine);
	rootJson.put("TTAutoTask",TTAutoTask);
	rootJson.put("TTServiceNow",TTServiceNow);
	rootJson.put("TTRemedyForce",TTRemedyForce);
	rootJson.put("TTPagerDuty",TTPagerDuty);

	out.println(rootJson);
%>
