	<%
/*******************************************************************************
*	File name			:	ConfigureControlAction.jsp
*	Purpose				:	Configure Page for Control Actions
*	Version				:	eG 3.0
*	Creation Date	:	?
*	Last Modified		:	11-09-2003	-	John Williams		eG 3.0 Modifications
*								26-09-2003	-	John Williams		Footer Page Include
*	Authtor				:	?
*	Design Changes		:	Jaiwin
******************************************************************************/
%>
<%@ include file="EgMonitorTracker.jsp" %>
<%@ page import = "java.util.*" %>
<%@ page import = "java.sql.*" %>
<%@ page import = "com.egurkha.ini.*" %>
<%@ page import = "com.egurkha.monitor.alarms.AlarmPriorities" %>
<%@ page import = "com.egurkha.monitor.remotecontrol.PreAgentInfo" %>
<%@ page import = "com.eg.IniFile"%>
<%@ page import = "com.eg.EgInstallInfo" %>
<%@ page import = "com.eg.EgStateManager" %>
<%@ page import = "com.eg.EgControlInfo" %>
<%@ page import = "com.eg.PollingServlet" %>
<%@ page import = "com.eg.EgDbAdapterTypeUI"%>
<%@ page import = "egurkha.EgDbAdapterUI"%>
<%@ page import = "egurkha.*" %>
<%@ taglib prefix="egui" uri="../WEB-INF/eg-taglib.tld" %>
<%@ page import = "com.eg.AgentConfig"%>
<%@ page import = "com.egurkha.util.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="com.eg.EgURLEncoder"%>
<%@ page import="com.eg.EgURLDecoder"%>
<%@ page import="net.sf.json.JSONObject"%>
<%@ page import="net.sf.json.JSONArray"%>
<%!
	private String[] testTypes = {"Custom","Script/Batch File","Sql Query","Perfmon","Snmp"};
%>
<%
	String milliSec ="";
	String fromDashBoardLayerModel=request.getParameter("fromDashBoardLayerModel");

	String QString = (String)session.getAttribute("QString");//access from dispCompMeasure.jsp
	String pageTo = "";
	if(QString != null && !QString.equals(""))
	{
		 pageTo = "/final/monitor/EgDispLayers.jsp?"+QString;
	}
	UserRegistry userReg = UserRegistry.getInstance();
	User user = null;//userReg.getUser(session.getId());
	if(from_sm)
	{
		user = userReg.getUser(sessionId_sm);
	}
	else
	{
		user = userReg.getUser(session.getId());
	}

	String currentUser = user.getLoginID();
	IniUI ui = (IniUI)com.egurkha.ini.IniUI.getInstance();
	EgControlInfo ctrlInfo = new EgControlInfo("manager");

	response.setHeader("Cache-Control","no-store");
	/*response.setHeader("Pragma","no-cache");
	response.setDateHeader("Expires", -1);*/
	String tempStr = request.getParameter("agentIP");
	String agentIP = "";
	if(tempStr!=null)
		agentIP = EgURLDecoder.decode(tempStr,"UTF-8");
	String agentMode = request.getParameter("agentMode");
	List runningAgents = null;//PreAgentInfo.getRunningAgents();
	Vector agentsNotRunning = null;//EgStateManager.notRunningAgents;
	boolean valid = false;

		//Remote control changes - preconfigured commands
	String timeOut = null;
	String otherCmdsEnabledUsrs = null;
	String[] validUsers = null;
	IniFile iniControls = new IniFile(EgInstallInfo.getInstallDir() + "/manager/config/eg_controls.ini", true);
	IniFile iniUi = new IniFile(EgInstallInfo.getInstallDir() + "/manager/config/eg_ui.ini", true);
	String mode = "";
	String agentOS = null;
	if(EgInstallInfo.isSuperManager)
	{
		Hashtable paramsHt = new Hashtable();
		paramsHt.put("operation","getOS");
		paramsHt.put("agentID",agentIP);
		
		String controlModeOnManager = (String)session.getAttribute("controlModeOnManager");
		paramsHt.put("managerId",controlModeOnManager);
		SMRemoteControlHelper remoteControlFromSMHelper = new SMRemoteControlHelper();
		Hashtable ht = (Hashtable)remoteControlFromSMHelper.getResult(paramsHt);
		if(ht != null && ht.containsKey("agentOS"))
		{
			agentOS = (String)ht.get("agentOS");
		}
	}
	else
	{
		AgentConfig agentconfig =  AgentConfig.getAgentConfig();
		if(agentIP != null){
			agentOS = agentconfig.getAgentOS(agentIP.trim());
		}
	}
	// Change to get Timout Period
	timeOut = iniControls.getValue("CONTROL_DEFAULTS","TimeOut");
	//out.println("Timeout  :"+timeOut);
	//End
	/*otherCmdsEnabledUsrs = iniControls.getValue("CONTROL_DEFAULTS","AllowOtherCommands");
	validUsers = StringUtils.getTrimmedTokens(otherCmdsEnabledUsrs,",");

	
	if(validUsers != null){
		for(int j=0; j<validUsers.length;j++){
			if(currentUser != null && currentUser.equalsIgnoreCase(validUsers[j])){
				enable = true;
			}
		}
	}*/
	JSONObject json = new JSONObject();
	JSONObject screenLabels = new JSONObject();
	JSONObject pageParams = new JSONObject();
	StringBuffer sbHiddenParams = new StringBuffer();
	screenLabels.put("monitor.remotecontrol.REMOTE CONTROL",mappingInfo.getTextDescription("monitor.remotecontrol.REMOTE CONTROL"));
	screenLabels.put("monitor.remotecontrol.Back",mappingInfo.getTextDescription("monitor.remotecontrol.Back"));
	screenLabels.put("monitor.remotecontrol.Agent IP",mappingInfo.getTextDescription("monitor.remotecontrol.Agent IP")+" / "+mappingInfo.getTextDescription("monitor.remotecontrol.Nick name"));
	screenLabels.put("monitor.remotecontrol.Mode of operation",mappingInfo.getTextDescription("monitor.remotecontrol.Mode of operation"));
	screenLabels.put("monitor.remotecontrol.Command",mappingInfo.getTextDescription("monitor.remotecontrol.Command"));
	screenLabels.put("monitor.remotecontrol.Arguments",mappingInfo.getTextDescription("monitor.remotecontrol.Arguments"));
	screenLabels.put("monitor.remotecontrol.Command syntax",mappingInfo.getTextDescription("monitor.remotecontrol.Command syntax"));
	screenLabels.put("monitor.remotecontrol.Output required",mappingInfo.getTextDescription("monitor.remotecontrol.Output required"));
	screenLabels.put("monitor.remotecontrol.Timeout",mappingInfo.getTextDescription("monitor.remotecontrol.Timeout")+" ("+mappingInfo.getTextDescription("monitor.remotecontrol.in sec")+")");
	screenLabels.put("monitor.remotecontrol.Execute",mappingInfo.getTextDescription("monitor.remotecontrol.Execute"));
	
	
	if(fromDashBoardLayerModel==null || (fromDashBoardLayerModel!=null && fromDashBoardLayerModel.equals("null")))
	{
		if(QString != null && !QString.equals(""))
		{
			pageParams.put("backButtonUrl",pageTo);
		}
		else
		{
			pageParams.put("backButtonUrl","historyBack");
		}
		pageParams.put("backButtonNeeded","Yes");
	}
	else
	{
		pageParams.put("backButtonNeeded","No");
	}

	pageParams.put("selectedAgent",agentIP);
	String agentMode1 = agentMode;
	agentMode1 = StringUtils.replace(agentMode1,'_',' ');
	pageParams.put("selectedAgentMode",mappingInfo.getTextDescription("monitor.remotecontrol."+agentMode1));

	sbHiddenParams.append("&agentMode="+agentMode);
	sbHiddenParams.append("&$_agentMode="+agentMode);
	sbHiddenParams.append("&txtUserName=");
	sbHiddenParams.append("&txtPassWord=");
	if(fromDashBoardLayerModel!=null)
		sbHiddenParams.append("&fromDashBoardLayerModel="+fromDashBoardLayerModel);

	String subject = null;
	String[] variables = null;
	String value = null;
	String keyword = "_COMMANDS";
	if(agentOS != null){
		if(agentOS.toLowerCase().indexOf("window") != -1){
			subject = "WINDOWS"+keyword;
		}else{
			subject = agentOS.toUpperCase()+keyword;
		}
		if(subject != null){
			variables = iniUi.getVariables(subject);
		}
	}

	JSONArray commandOpts = new JSONArray();
	if(variables != null){
		JSONObject jOpt = new JSONObject();
		jOpt.put("displayField",mappingInfo.getTextDescription("monitor.remotecontrol.Please select a command"));
		jOpt.put("valueField","-1");
		commandOpts.add(jOpt);
		for(int inc=0; inc<variables.length;inc++){
			value = iniUi.getValue(subject,variables[inc]);
			value = StringUtils.replace(value,"'","&&&");
			if(value != null && !value.equals("")){
				jOpt = new JSONObject();
				jOpt.put("displayField",mappingInfo.getTextDescription("monitor.remotecontrol."+variables[inc]));
				jOpt.put("valueField",value);
				commandOpts.add(jOpt);
			}
		}
	}// end if

	if(u_ser.getRCExecuteAllCommands())
	{
		JSONObject jOpt = new JSONObject();
		jOpt.put("displayField",mappingInfo.getTextDescription("monitor.remotecontrol.Other"));
		jOpt.put("valueField","-2");
		commandOpts.add(jOpt);
	}

	if(commandOpts!=null && commandOpts.isEmpty())
	{
		JSONObject jOpt = new JSONObject();
		jOpt.put("displayField",mappingInfo.getTextDescription("monitor.remotecontrol.Not Applicable"));
		jOpt.put("valueField","-1");
		commandOpts.add(jOpt);
	}
	pageParams.put("selectedCmd","-1");
	json.put("commandOpts",commandOpts);
	pageParams.put("arguments",mappingInfo.getTextDescription("monitor.remotecontrol.none"));
	synchronized(milliSec)
	{
		milliSec = System.currentTimeMillis() + "";
		Thread.currentThread().sleep(1);
	}
	sbHiddenParams.append("&txtTimeStamp="+String.valueOf(System.currentTimeMillis()));

	JSONArray yesNoOpts = new JSONArray();

	JSONObject yesOpt = new JSONObject();
	yesOpt.put("displayField",mappingInfo.getTextDescription("monitor.remotecontrol.Yes"));
	yesOpt.put("valueField","Yes");

	yesNoOpts.add(yesOpt);

	JSONObject noOpt = new JSONObject();
	noOpt.put("displayField",mappingInfo.getTextDescription("monitor.remotecontrol.No"));
	noOpt.put("valueField","No");

	yesNoOpts.add(noOpt);

	json.put("yesNoOpts",yesNoOpts);
	pageParams.put("outputRequired","Yes");

	if(timeOut != null && timeOut.equals(""))
		timeOut = "60";

	pageParams.put("selectedTimeOutPeriod",String.valueOf(timeOut));
	
	if(sbHiddenParams!=null)
	{
		json.put("hiddenQStr",sbHiddenParams.toString());
	}		
	json.put("pageParams",pageParams);
	json.put("screenLabels",screenLabels);
	json.put("agentOS",agentOS);
	
	response.getWriter().write(json.toString());
%>




