<%@ include file = "../../EgTrackerAJSON.jsp" %>
<%@ page import = "com.eg.IniFile"%>
<%@ page import = "com.eg.EgConfigFilesSync"%>
<%@ page import = "com.egurkha.util.StringUtils"%>
<%@ page import = "com.egurkha.admin.AuditLogThread"%>
<%@ page import = "com.egurkha.util.StringUtils"%>
<%@ page import="net.sf.json.*"%>

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

	JSONObject rootJson = new JSONObject();

	IniFile inifile = EgConfigFilesSync.getInstance(EgInstallInfo.getInstallDir()+"/manager/config/eg_services.ini",true);

	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","Delete User Accounts");

	boolean isAuditChange = false;

	String managerModel = inifile.getValue("MISC_ARGS","eGMgrModel");
	String deleteoption = "no";
	if(managerModel != null && managerModel.equalsIgnoreCase("msp"))
	{
		String allowDeleteOrg = request.getParameter("allowDeleteOrg");

		String preallowDeleteOrg = inifile.getValue("MISC_ARGS","AllowDeleteOrg");
		String preDeleteOrg = inifile.getValue("MISC_ARGS","DeleteOrg");

		String allowDeleteOrgStr = "";
		String deleteOrgStr = "";
		if(allowDeleteOrg != null && (allowDeleteOrg.trim().equalsIgnoreCase("true") || allowDeleteOrg.trim().equalsIgnoreCase("yes")))
		{
			inifile.setValue("MISC_ARGS","AllowDeleteOrg","yes");
			allowDeleteOrgStr = "yes";
			deleteoption = "yes";
			String delete_org = request.getParameter("delete_org");
			if(delete_org != null && (delete_org.trim().equalsIgnoreCase("true") || delete_org.trim().equalsIgnoreCase("yes")))
			{
				inifile.setValue("MISC_ARGS","DeleteOrg","yes");
				deleteOrgStr = "yes";
			}
			else
			{
				inifile.setValue("MISC_ARGS","DeleteOrg","no");
				deleteOrgStr = "no";
			}
		}
		else
		{
			inifile.setValue("MISC_ARGS","AllowDeleteOrg","no");
			allowDeleteOrgStr = "no";
			inifile.setValue("MISC_ARGS","DeleteOrg","no");
			deleteOrgStr = "no";
		}
		inifile.setValue("MISC_ARGS","DeleteSubOU","no");

		if (preallowDeleteOrg != null && !preallowDeleteOrg.equalsIgnoreCase(allowDeleteOrgStr))
		{
			isAuditChange = true;
			String displayName = "00~~"+"Delete the infrastructure elements associated with organization/organizational unit";
			auditLogThread.setPreviousValue(displayName,preallowDeleteOrg);
			auditLogThread.setCurrentValue(displayName,allowDeleteOrgStr);
		}

		if (allowDeleteOrgStr.equalsIgnoreCase("yes"))
		{
			String currentValue = "Organizational unit";
			String previousValue = "Organizational unit";
			if (deleteOrgStr.equalsIgnoreCase("yes"))
			{
				currentValue = "Organization";
			}
			
			if (preallowDeleteOrg.equalsIgnoreCase("yes"))
			{
				if (preDeleteOrg.equalsIgnoreCase("yes"))
				{
					previousValue = "Organization";
				}
			}
			else
			{
				previousValue = "-";
			}
			if (previousValue != null && !previousValue.equalsIgnoreCase(currentValue))
			{
				isAuditChange = true;
				String displayName = "01~~"+"Delete the infrastructure elements associated with";
				auditLogThread.setPreviousValue(displayName,previousValue);
				auditLogThread.setCurrentValue(displayName,currentValue);
			}
		}
	}
	else
	{
		String delete_subou = request.getParameter("delete_subou");
		delete_subou = delete_subou != null && (delete_subou.equals("true") || delete_subou.equals("yes"))?"yes":"no";
		if (delete_subou.equalsIgnoreCase("yes"))
		{
			deleteoption = "yes";
		}

		String predelete_subou = inifile.getValue("MISC_ARGS","DeleteSubOU");
		if (predelete_subou != null && !predelete_subou.equalsIgnoreCase(delete_subou))
		{
			isAuditChange = true;
			String displayName = "00~~"+"Delete the infrastructure elements associated with organizational unit";
			auditLogThread.setPreviousValue(displayName,predelete_subou);
			auditLogThread.setCurrentValue(displayName,delete_subou);
		}

		inifile.setValue("MISC_ARGS","DeleteSubOU",delete_subou);
		inifile.setValue("MISC_ARGS","AllowDeleteOrg","no");
		inifile.setValue("MISC_ARGS","DeleteOrg","no");
	}

	// common settings
	String val = request.getParameter("selected_elements");
	if(val != null && val.trim().length()>0 && !val.trim().equalsIgnoreCase("null"))
	{
		boolean hasPreviousValue = false;
		String preval = inifile.getValue("MISC_ARGS","DeleteOptions");
		if (StringUtils.isNull(preval))
		{
			hasPreviousValue = true;
		}

		if (preval != null && !preval.equalsIgnoreCase(val))
		{
			isAuditChange = true;
			String displayName = "02~~"+"Infrastructure elements for deletion";
			if (preval.trim().length() > 0)
			{
				auditLogThread.setPreviousValue(displayName,StringUtils.replace(getDisplayName(preval),"$",", "));
			}
			else
			{
				auditLogThread.setPreviousValue(displayName,"-");
			}
			auditLogThread.setCurrentValue(displayName,StringUtils.replace(getDisplayName(val),"$",", "));
		}

		inifile.setValue("MISC_ARGS","DeleteOptions",val.trim());
		String array[]=StringUtils.getTokens(val,"$");
		ArrayList list = new ArrayList();
		list.addAll(Arrays.asList(array));
		if(list.contains("components"))
		{
			String deleteImmediate = request.getParameter("delete_immediately");
			deleteImmediate = deleteImmediate != null && (deleteImmediate.equals("true") || deleteImmediate.equals("yes"))?"yes":"no";

			String predeleteImmediate = inifile.getValue("MISC_ARGS","ImmediateDelete");
			if (hasPreviousValue)
			{
				String preValue = "-";
				String currValue = "All";
				if (predeleteImmediate.equalsIgnoreCase("no"))
				{
					preValue = "-";
				}
				if (deleteImmediate.equalsIgnoreCase("no"))
				{
					currValue = "Not reporting";
				}
				isAuditChange = true;
				String displayName = "03~~"+"Components to be deleted";
				auditLogThread.setPreviousValue(displayName,preValue);
				auditLogThread.setCurrentValue(displayName,currValue);
			}
			else if (predeleteImmediate != null && !predeleteImmediate.equalsIgnoreCase(deleteImmediate))
			{
				String preValue = "All";
				String currValue = "All";
				if (predeleteImmediate.equalsIgnoreCase("no"))
				{
					preValue = "Not reporting";
				}
				if (deleteImmediate.equalsIgnoreCase("no"))
				{
					currValue = "Not reporting";
				}
				isAuditChange = true;
				String displayName = "03~~"+"Components to be deleted";
				auditLogThread.setPreviousValue(displayName,preValue);
				auditLogThread.setCurrentValue(displayName,currValue);
			}

			inifile.setValue("MISC_ARGS","ImmediateDelete",deleteImmediate);

			if(deleteImmediate.equals("no"))
			{
				int compsNotReportingDays = 30;
				String compsNotReportingForStr = request.getParameter("delete_comps_after_x_days");
				try
				{
					if(compsNotReportingForStr != null)
					{
						compsNotReportingDays = Integer.parseInt(compsNotReportingForStr.trim());
						if(compsNotReportingDays<1)
						{
							compsNotReportingDays=30;
						}
					}
				}
				catch(Exception e)
				{
					compsNotReportingDays=30;
				}
				String compsNotReportingDaysStr = String.valueOf(compsNotReportingDays);
				String precompsNotReportingDays = inifile.getValue("MISC_ARGS","daysCompsNotReporting");
				if (hasPreviousValue)
				{
					isAuditChange = true;
					String displayName = "04~~"+"Components not reporting for last";
					auditLogThread.setPreviousValue(displayName,"-");
					auditLogThread.setCurrentValue(displayName,compsNotReportingDaysStr+" days");
				}
				else if (precompsNotReportingDays != null && !preval.equalsIgnoreCase(compsNotReportingDaysStr))
				{
					isAuditChange = true;
					String displayName = "04~~"+"Components not reporting for last";
					auditLogThread.setPreviousValue(displayName,precompsNotReportingDays+" days");
					auditLogThread.setCurrentValue(displayName,compsNotReportingDaysStr+" days");
				}

				inifile.setValue("MISC_ARGS","daysCompsNotReporting",compsNotReportingDaysStr);
			}
		}
	}
	else
	{
		String preDeleteOptions = inifile.getValue("MISC_ARGS","DeleteOptions");
		if (deleteoption.equalsIgnoreCase("yes") && preDeleteOptions != null && preDeleteOptions.trim().length() > 0)
		{
			isAuditChange = true;
			String displayName = "02~~"+"Infrastructure elements for deletion";
			auditLogThread.setPreviousValue(displayName,StringUtils.replace(getDisplayName(preDeleteOptions),"$",", "));
			auditLogThread.setCurrentValue(displayName,"-");
		}

		inifile.setValue("MISC_ARGS","DeleteOptions","");
		inifile.setValue("MISC_ARGS","ImmediateDelete","yes");
	}

	// settings for auto delete users
	String autoDelete = request.getParameter("autoDelete");
	autoDelete = autoDelete != null && (autoDelete.equals("true") || autoDelete.equals("yes"))?"yes":"no";

	String preAutoDelete = inifile.getValue("MISC_ARGS","AutoDeleteExpiredUsers");
	if (preAutoDelete != null && !preAutoDelete.equalsIgnoreCase(autoDelete))
	{
		isAuditChange = true;
		String displayName = "05~~"+"Auto delete expired user accounts";
		auditLogThread.setPreviousValue(displayName,preAutoDelete);
		auditLogThread.setCurrentValue(displayName,autoDelete);
	}

	inifile.setValue("MISC_ARGS","AutoDeleteExpiredUsers",autoDelete);

	if(autoDelete != null && autoDelete.equalsIgnoreCase("yes"))
	{
		int autoDeleteDays=30;
		try
		{
			String temp = request.getParameter("autoDeleteDays");
			if(temp != null)
			{
				autoDeleteDays=Integer.parseInt(temp.trim());
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
			autoDeleteDays=30;
		}
		String autoDeleteDaysStr = String.valueOf(autoDeleteDays);
		String preAutoDeleteDays = inifile.getValue("MISC_ARGS","AutoDeleteExpiredUsersAfterDays");
		if (preAutoDeleteDays != null && !preAutoDeleteDays.equalsIgnoreCase(autoDeleteDaysStr))
		{
			isAuditChange = true;
			String displayName = "06~~"+"Delete user accounts after";
			auditLogThread.setPreviousValue(displayName,preAutoDeleteDays+" days of expiry");
			auditLogThread.setCurrentValue(displayName,autoDeleteDaysStr+" days of expiry");
		}

		inifile.setValue("MISC_ARGS","AutoDeleteExpiredUsersAfterDays",autoDeleteDaysStr);
	}

	if (isAuditChange)
	{
		auditLogThread.setValue("Description","Delete user accounts settings have been modified successfully");
		auditLogThread.sendItToDB();
	}

	inifile.saveFile();
	EgConfigFilesSync.releaseInstance(inifile);

	out.println(rootJson.toString());
%>

<%!
	public String getDisplayName(String name)
	{
		String displayName = "";

		if (name != null && name.trim().length() > 0)
		{
			String array[] = StringUtils.getTokens(name,"$");
			ArrayList list = array==null?new ArrayList():new ArrayList(Arrays.asList(array));
			for (int i=0;i<list.size();i++)
			{
				String tmpname = (String)list.get(i);
				if (tmpname.equalsIgnoreCase("servicegroups"))
				{
					tmpname = "Service Groups";
				}
				else if (tmpname.equalsIgnoreCase("exremote"))
				{
					tmpname = "External/Remote Agents";
				}
				else
				{
					String firstLetter = tmpname.substring(0,1);
					String remainLetter = tmpname.substring(1);
					tmpname = firstLetter.toUpperCase()+remainLetter;
				}
				displayName += tmpname+", ";
			}
			displayName = displayName.substring(0,displayName.length()-2);
		}
		else
		{
			displayName = "-";
		}

		return displayName;
	}
%>



