<%@ page import = "com.egurkha.User"%>
<%@ page import = "com.egurkha.UserRegistry"%>
<%@ page import = "java.util.*"%>
<%@ page import = "java.text.*"%>
<%@ page import = "com.eg.EgInstallInfo" %>
<%@ page import = "com.eg.IniFile"%>
<%@ page import = "com.eg.EgUuidInfo"%>
<%@ page import = "net.sf.json.*"%>
<%@ page import = "com.eg.EgCrypt"%>
<%@ page import = "com.egurkha.admin.mfa.GoogleAuthenticator"%>
<%@ include file="../../../admin/EgTrackerAJSON.jsp" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

<%
	User user = registry.getUser(session.getId());
	String uname = user.getLoginID();
	EgUserRole usrRole = user.getUserRole();
	boolean hasAllComponentAccess = usrRole.hasAllComponentAccess();

	EgUuidInfo egUuidInfo = new EgUuidInfo();
	IniFile inifile = new IniFile(EgInstallInfo.getInstallDir()+"/manager/config/eg_services.ini");
	String managerModel = inifile.getValue("MISC_ARGS","eGMgrModel");
	String session_userid = (String)session.getAttribute("sess_userid");
	JSONObject rootObj = new JSONObject();
	
	if (hasAllComponentAccess)
	{
		if(managerModel != null && managerModel.equalsIgnoreCase("msp"))
		{
			IniFile inifile1 = new IniFile(EgInstallInfo.getInstallDir()+"/manager/config/eg_uuid.ini");
			String topLevelOrgs[] = inifile1.getVariables("ORG_UUID");

			ArrayList superOrgs = new ArrayList();
			if(topLevelOrgs != null && topLevelOrgs.length>0)
			{
				superOrgs.addAll(Arrays.asList(topLevelOrgs));
			}
			JSONArray exportDataArr = new JSONArray();
			for(int i=0;i<superOrgs.size();i++)
			{
				JSONObject exportDataObj = new JSONObject();
				String optVar = "";
				String optVal = "";

				String strDbChild = "";

				String orgname = (String)superOrgs.get(i);

				String uvalidtill = egUuidInfo.getExpiryDateOfUser(orgname);

				if (uvalidtill != null)
				{
					if (uvalidtill.trim().length() == 0)
					{
						continue;
					}
					if (uvalidtill.indexOf("9999-12-12 23:59:59") == -1)
					{
						uvalidtill = uvalidtill.substring(0,10);
						uvalidtill = com.egurkha.util.DateUtil.changeDateFormat(uvalidtill,"yyyy-MM-dd","MM/dd/yyyy");
					}
					else
					{
						uvalidtill = "12/12/9999";
					}

					java.util.Date today = new java.util.Date();
					SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
					java.util.Date uvalidtilldate = new java.util.Date();
					try
					{
						uvalidtilldate = formatter.parse(uvalidtill+" 23:59:59");
					}
					catch(Exception e){}

					if (!uvalidtill.equalsIgnoreCase("12/12/9999") && uvalidtilldate.compareTo(today) < 0)
					{
						continue;
					}
				}

				optVar = (String)superOrgs.get(i);
				optVal = (String)superOrgs.get(i);

				JSONArray rootUserObjectArray = egUuidInfo.buildTreeJsonForAllUsers(orgname,session_userid,false,"active");
				if (orgname.equalsIgnoreCase("egenterprise"))
				{
					//if (!rootUserObjectArray.isEmpty())
					{
						if (!session_userid.equalsIgnoreCase("egenterprise"))
						{
							if (optVal.equalsIgnoreCase("egenterprise"))
							{
								exportDataObj.put("text",_mapping.getTextDescription("admin.user.egenterprise"));
							}
							else
							{
								exportDataObj.put("text",optVal);
							}
							exportDataObj.put("id",optVar);
							exportDataObj.put("iconCls","iconDomain");
							exportDataObj.put("expanded",true);
							exportDataObj.put("children",rootUserObjectArray);
							exportDataArr.add(exportDataObj);
						}
						else
						{
							exportDataArr = rootUserObjectArray;
						}
					}
				}
				else
				{
					exportDataObj.put("text",optVal);
					exportDataObj.put("id",optVar);
					exportDataObj.put("iconCls","iconDomain");
					exportDataObj.put("expanded",true);
					exportDataObj.put("children",rootUserObjectArray);
					exportDataArr.add(exportDataObj);
				}
			}
			if (!exportDataArr.isEmpty())
			{
				rootObj.put("exportDataArr",exportDataArr);
				rootObj.put("RootNodeText","Choose Organization / Organizational Unit / User");
			}
		}
		else
		{
			JSONArray exportDataArr = new JSONArray();
			JSONObject exportDataObj = new JSONObject();

			JSONArray rootUserObjectArray = egUuidInfo.buildTreeJsonForAllUsers("egenterprise",session_userid,false,"active");
			
			if (!session_userid.equalsIgnoreCase("egenterprise"))
			{
				exportDataObj.put("text",_mapping.getTextDescription("admin.user.egenterprise"));
				exportDataObj.put("id","egenterprise");
				exportDataObj.put("iconCls","iconDomain");
				exportDataObj.put("expanded",true);
				exportDataObj.put("children",rootUserObjectArray);
				exportDataArr.add(exportDataObj);
				rootObj.put("RootNodeText","Choose Organizational Unit / User");
			}
			else
			{
				exportDataArr = rootUserObjectArray;
				rootObj.put("RootNodeText","Choose Organizational Unit / User");
			}

			if (!exportDataArr.isEmpty())
			{
				rootObj.put("exportDataArr",exportDataArr);
			}
		}
	}
	else
	{

		boolean isUserExpired = egUuidInfo.isUserExpired(uname);

		String orgName = uname;
		boolean isSuperOU = egUuidInfo.isSuperOrg(uname);
		boolean isOU = egUuidInfo.isOrg(uname);
		boolean hasOURole = egUuidInfo.hasOURole(uname);
		if (!isSuperOU && !isOU && hasOURole)
		{
			orgName = egUuidInfo.getDirectDependentOU(uname);
		}

		if (!isUserExpired)
		{
			JSONArray rootUserObjectArray = egUuidInfo.buildTreeJsonForAllUsers(orgName,uname,false,"active");

			if (!rootUserObjectArray.isEmpty())
			{
				rootObj.put("exportDataArr",rootUserObjectArray);
				rootObj.put("RootNodeText","Choose Organizational Unit / User");
			}
		}
	}

	String userDate = request.getParameter("dp");
	String enSecKey = request.getParameter("kpe");
	if (enSecKey != null && enSecKey.trim().length() > 0)
	{
		String secretKey = EgCrypt.decrypt(enSecKey);
		String randomNumber = GoogleAuthenticator.getTOTPCode(secretKey, userDate);
		String rpe = EgCrypt.encrypt(randomNumber);
		String dpe = EgCrypt.encrypt(userDate);
		rootObj.put("dpe",dpe);
		rootObj.put("rpe",rpe);
	}

	rootObj.put("currentUser",uname);
	out.println(rootObj.toString());
%>



