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

<%
/*
*	Processing page for Trouble Ticket Integration(Web Services) node in Manager Settings tree.
*	ini File : eg_services.ini
*/
%>

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

	JSONObject rootJson = new JSONObject();

	TransactionBean transBean = new TransactionBean();

	transBean.createSyncFile("eg_services.ini","Write");

	String TTSys = request.getParameter("TT_sys");
	String action = request.getParameter("action");
	String TT_Wrapper = request.getParameter("mainWrapperClass");
	String ArchieveFile = request.getParameter("ArchieveFile");
	String fileSeparator = System.getProperty("file.separator");
	String TTIntegrationFlag = request.getParameter("integrationFlagOld");
	String TTIntegrationEnable = request.getParameter("ttIntegrationEnabled");
	String TTIntegrationSys = request.getParameter("integrationSys");
	String TTIntSysFields = transBean.getStrValue("TT_INTERGRATION_SYSTEM_FIELDS",TTSys);

	TTIntegrationFlag = (TTIntegrationFlag != null && (TTIntegrationFlag.equalsIgnoreCase("Yes") || TTIntegrationFlag.equalsIgnoreCase("True")))? "Yes" : "No"; 

	if(TTIntegrationEnable != null)
	{
		TTIntegrationEnable = (TTIntegrationEnable.equalsIgnoreCase("Yes") || TTIntegrationEnable.equalsIgnoreCase("True"))?"Yes":"No";
	}


	boolean isIntegrationEnabled = (TTIntegrationEnable != null && (TTIntegrationEnable.equalsIgnoreCase("Yes") ||TTIntegrationEnable.equalsIgnoreCase("True")))? true : false;

	String TT_ITSys = "TT_INTEGRATION_"+TTSys;
	ArrayList keysList = new ArrayList();

	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","Manager Settings");
	auditLogThread.setValue("Activity","Configure");
	auditLogThread.setValue("Description","Manager settings have been configured successfully");


	boolean setPreviousValue = false;
	boolean setAuditLog = false;

	if(TTIntegrationSys.equalsIgnoreCase(TTSys))
	{
		setPreviousValue = true;
	}

	//System.out.println("TTIntegrationFlag : "+TTIntegrationFlag);
	//System.out.println("TTIntegrationEnable : "+TTIntegrationEnable);

	if(!TTIntegrationFlag.equalsIgnoreCase(TTIntegrationEnable))
	{
		if(setPreviousValue)
		{
			auditLogThread.setPreviousValue("Enable web service integration",TTIntegrationFlag);
		}
		auditLogThread.setCurrentValue("Enable web service integration",TTIntegrationEnable);
		setAuditLog = true;
	}
	if(isIntegrationEnabled)
	{

		if(!setPreviousValue)
		{
			auditLogThread.setCurrentValue("TT System",TTSys);
		}
		
		ArrayList fieldsList = new ArrayList();
		StringTokenizer st = new StringTokenizer(TTIntSysFields,",");
		while(st.hasMoreTokens())
		{
			String token = st.nextToken();
			String value = request.getParameter(token);
			if(token.equalsIgnoreCase("ttIntegratorClass") || token.equalsIgnoreCase("supportingJar"))
				continue;

			value=StringUtils.decodeSecurityChar(value);
			value=StringUtils.replace(value,"|slash|","\\");
			String perv = "$_"+token;
			String oldValue = request.getParameter(perv);
			oldValue=StringUtils.decodeSecurityChar(oldValue);
			oldValue=StringUtils.replace(oldValue,"|slash|","\\");
			if(value != null && !value.trim().equalsIgnoreCase(oldValue.trim()))
			{
				if(token.equals("password"))
				{
					value = "*********";
					oldValue = "****";
				}
				if(setPreviousValue)
				{
					if(oldValue != null && oldValue.length() > 0)
					{
						auditLogThread.setPreviousValue(token,oldValue);
					}
				}
				auditLogThread.setCurrentValue(token,value);
				setAuditLog = true;
			}
			else if(!setPreviousValue)
			{
				if(token.equals("password"))
				{
					value = "*********";
				}
				auditLogThread.setCurrentValue(token,value);
				setAuditLog = true;
			}
		}
	}
	if(setAuditLog)
	{
		auditLogThread.sendItToDB();
	}

	if (TTIntegrationEnable != null && TTIntegrationEnable.length() > 0)
	{
		transBean.setValue("TT_INTEGRATION", "ENABLE_TT_INTEGRATION", TTIntegrationEnable);
	}


	if(TTIntegrationEnable.equalsIgnoreCase("Yes") || TTIntegrationEnable.equalsIgnoreCase("True"))
	{
		if (TTSys != null && TTSys.length() > 0)
		{
			transBean.setValue("TT_INTEGRATION", "TT_INTEGRATION_ENABLED_SYSTEM", TTSys);
		}

		String TTSystemFields =  transBean.getStrValue("TT_INTERGRATION_SYSTEM_FIELDS",TTSys);
		StringTokenizer TTMEtokens = new StringTokenizer(TTSystemFields,",");
		while(TTMEtokens.hasMoreTokens())
		{
			String TTMEtokensvalue = TTMEtokens.nextToken();
			keysList.add(TTMEtokensvalue);
		}

		for(int i = 0; i < keysList.size(); i++)
		{
			String variable  = (String) keysList.get(i);
			String value = request.getParameter(variable);
			
			if(variable.equalsIgnoreCase("ttIntegratorClass") || variable.equalsIgnoreCase("supportingJar"))
				continue;

			value=StringUtils.decodeSecurityChar(value);
			value=StringUtils.replace(value,"|slash|","\\");
				if(value != null && value.length() > 0)
				{
					if(variable != null && (variable.equalsIgnoreCase("Password") || variable.equalsIgnoreCase("proxyPassword")))
					{
						value = EgCrypt.encrypt(value);
					}
					
					if(value != null ){
						value = value.replaceAll("[\n\r]", "");//replacing enter(new line) with empty string
					}
				
					transBean.setValue(TT_ITSys,variable,value);
				}
		}
		if (TT_Wrapper != null && TT_Wrapper.length() > 0)
		{
			String existingWrapperArchieve=transBean.getStrValue(TT_ITSys,"supportingJar");
			if ( existingWrapperArchieve != null && (!existingWrapperArchieve.equalsIgnoreCase("none") && !existingWrapperArchieve.equals(ArchieveFile)))
			{
				File getFile = new File(EgInstallInfo.getInstallDir()+fileSeparator+"manager"+fileSeparator+"lib/"+fileSeparator+existingWrapperArchieve);
				boolean result = getFile.delete();
			}
			if (ArchieveFile != null && ArchieveFile.length() > 0){
				if(ArchieveFile.indexOf(".") > -1)
				{
					ArchieveFile = ArchieveFile.substring(0,ArchieveFile.indexOf("."));
				}
				transBean.setValue(TT_ITSys, "supportingJar", ArchieveFile);
			}
			transBean.setValue(TT_ITSys, "ttIntegratorClass", TT_Wrapper);
		}
	}
	transBean.saveFileAndReleaseInstance();

	rootJson.put("success","true");
	out.println(rootJson.toString());
%>



