<%@ include file="../../EgTrackerAJSON.jsp"%>

<%@ page import = "egurkha.ServiceBean"%>
<%@ page import = "com.eg.EgCrypt" %>
<%@ page import = "com.eg.EgDiscInfo" %>
<%@ page import = "com.eg.EgInstallInfo"%>
<%@ page import = "com.eg.EgAutoDeleteInfo"%>
<%@ page import = "net.sf.json.JSONObject"%>

<%@ page import = "com.egurkha.admin.AuditLogThread"%>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<jsp:useBean id="DiscBean" scope="page" class="egurkha.ServiceBean"/>

<%
/*
*	Processing page for Auto delete node in Discovery tree.
*/
%>

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

	JSONObject rootJson = new JSONObject();

	EgDiscInfo discInfo = new EgDiscInfo("manager");

	String SETTINGS = "Settings";

	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","Discovery");

	String autoDelLabel = "Enable automatic actions on components that are not reporting";
	String MgdCompDelLabel = "Take action on managed components also ?";
	String chkTimeLabel = "Take this action when components have not been reporting for";
	String immediateDelLabel = "Take action immediately if the components have been removed from the IT infrastructure";
	String purgePeriodLabel = "Auto delete purge period";
	String actionOnCompsLabel = "What action would you like to take ?";
	String checkAutoManageLabel = "Take action only if the component type is in the auto-manage list";
	String delManualAddedCompLabel = "Take action even if the components have been manually added";

	String from = request.getParameter("from");
	String autodeleteOption = request.getParameter("autodeleteOption");	
	String managedCompDel = request.getParameter("managedCompDel");
	String checkTime = request.getParameter("checkTime");
	String immediateDel = request.getParameter("immediateDel");
	String actionOnComps = request.getParameter("actionOnComps");
	String checkAutoManage = request.getParameter("checkAutoManage");

	rootJson.put("success","false");

	if(from != null && from.equalsIgnoreCase("autodeleteSettings"))
	{
		auditLogThread.setValue("Activity","Auto unmanage/delete");
		if(autodeleteOption != null && autodeleteOption.length() > 0)
		{
			String delManualAddedComp = "";
			String $_delManualAddedComp = "";
			String $_autodeleteOption = request.getParameter("$_autoDelOption");
			String $_managedCompDel = request.getParameter("$_mgdCompDelOpt");
			String $_immediateDel = request.getParameter("$_immediateDelOpt");
			String $_checkTime = request.getParameter("$_cktTime");
			String $_actionOnComp = request.getParameter("$_actionOnComp");
			String $_checkAutoManage = request.getParameter("$_checkAutoManage");

			delManualAddedComp = request.getParameter("delManualAddedComp");
			$_delManualAddedComp = request.getParameter("$_delManualAddedComp");


			$_autodeleteOption = ($_autodeleteOption != null && ($_autodeleteOption.equalsIgnoreCase("Yes") || $_autodeleteOption.equalsIgnoreCase("True")))? "yes" :"no";
			$_managedCompDel = ($_managedCompDel != null && ($_managedCompDel.equalsIgnoreCase("Yes") || $_managedCompDel.equalsIgnoreCase("True")))? "yes" :"no";
			$_immediateDel = ($_immediateDel != null && ($_immediateDel.equalsIgnoreCase("Yes") || $_immediateDel.equalsIgnoreCase("True")))? "yes" :"no";
			$_checkTime = ($_checkTime != null && !$_checkTime.equals(""))? $_checkTime : "10";
			$_checkAutoManage = ($_checkAutoManage != null && ($_checkAutoManage.equalsIgnoreCase("Yes") || $_checkAutoManage.equalsIgnoreCase("True")))? "yes" :"no";
			$_delManualAddedComp = ($_delManualAddedComp != null && ($_delManualAddedComp.equalsIgnoreCase("Yes") || $_delManualAddedComp.equalsIgnoreCase("True")))? "yes" :"no";

			autodeleteOption = (autodeleteOption.equalsIgnoreCase("Yes") || autodeleteOption.equalsIgnoreCase("True"))? "yes" :"no";
			managedCompDel = (managedCompDel != null && (managedCompDel.equalsIgnoreCase("Yes") || managedCompDel.equalsIgnoreCase("True")))? "yes" :"no";
			checkTime = (checkTime != null && !checkTime.equals(""))? checkTime : "10";
			immediateDel = (immediateDel != null && (immediateDel.equalsIgnoreCase("Yes") || immediateDel.equalsIgnoreCase("True")))? "yes" :"no";
			checkAutoManage = (checkAutoManage != null && (checkAutoManage.equalsIgnoreCase("Yes") || checkAutoManage.equalsIgnoreCase("True")))? "yes" :"no";
			delManualAddedComp = (delManualAddedComp != null && (delManualAddedComp.equalsIgnoreCase("Yes") || delManualAddedComp.equalsIgnoreCase("True")))? "yes" :"no";

			if(!$_autodeleteOption.equalsIgnoreCase(autodeleteOption))
			{
				auditLogThread.setPreviousValue("00~~"+autoDelLabel,$_autodeleteOption);
				auditLogThread.setCurrentValue("00~~"+autoDelLabel,autodeleteOption);
			}

			if(autodeleteOption.equalsIgnoreCase("yes"))
			{
				if (!$_actionOnComp.equalsIgnoreCase(actionOnComps))
				{
					auditLogThread.setPreviousValue("01~~"+actionOnCompsLabel,$_actionOnComp);
					auditLogThread.setCurrentValue("01~~"+actionOnCompsLabel,actionOnComps);
				}

				int tmp_chkTime = Integer.parseInt(checkTime);
				int $_tmp_chkTime = Integer.parseInt($_checkTime);
				if($_tmp_chkTime != tmp_chkTime)
				{
					String dayStr =" days";
					String $_dayStr =" days";
					if ($_tmp_chkTime == 1)
					{
						$_dayStr = " day";
					}
					if (tmp_chkTime == 1)
					{
						dayStr = " day";
					}
					auditLogThread.setPreviousValue("02~~"+chkTimeLabel,$_checkTime+$_dayStr);
					auditLogThread.setCurrentValue("02~~"+chkTimeLabel,checkTime+dayStr);
				}

				if(!$_checkAutoManage.equalsIgnoreCase(checkAutoManage))
				{
					auditLogThread.setPreviousValue("03~~"+checkAutoManageLabel,$_checkAutoManage);
					auditLogThread.setCurrentValue("03~~"+checkAutoManageLabel,checkAutoManage);
				}

				if(!$_managedCompDel.equalsIgnoreCase(managedCompDel))
				{
					auditLogThread.setPreviousValue("04~~"+MgdCompDelLabel,$_managedCompDel);
					auditLogThread.setCurrentValue("04~~"+MgdCompDelLabel,managedCompDel);
				}

				if (!$_delManualAddedComp.equalsIgnoreCase(delManualAddedComp))
				{
					auditLogThread.setPreviousValue("05~~"+delManualAddedCompLabel,$_delManualAddedComp);
					auditLogThread.setCurrentValue("05~~"+delManualAddedCompLabel,delManualAddedComp);
				}

				if(!$_immediateDel.equalsIgnoreCase(immediateDel))
				{
					auditLogThread.setPreviousValue("06~~"+immediateDelLabel,$_immediateDel);
					auditLogThread.setCurrentValue("06~~"+immediateDelLabel,immediateDel);
				}
			}

			IniFile autoDelObj = EgConfigFilesSync.getInstance(EgInstallInfo.getInstallDir() + "/manager/config/eg_autodelete.ini");
			autoDelObj.setValue(SETTINGS,"AutoDelete",autodeleteOption);
			if(autodeleteOption.equalsIgnoreCase("yes"))
			{
				autoDelObj.setValue(SETTINGS,"ManagedCompDeletion",managedCompDel);
				autoDelObj.setValue(SETTINGS,"AutoDeleteCheckTime",checkTime);
				autoDelObj.setValue(SETTINGS,"ImmediateDeletionForApiDiscovery",immediateDel);
				autoDelObj.setValue(SETTINGS,"ActionOnComponents",actionOnComps);
				autoDelObj.setValue(SETTINGS,"CheckAutoManageTypes",checkAutoManage);
				autoDelObj.setValue(SETTINGS,"AutoDeleteManuallyAddedComps",delManualAddedComp);
			}
			autoDelObj.saveFile();
			EgConfigFilesSync.releaseInstance(autoDelObj);
			auditLogThread.setValue("Description","Auto unmanage/delete settings have been updated successfully");
			auditLogThread.sendItToDB();
			rootJson.put("success","true");
		}
	}
	out.println(rootJson.toString());
%>



