<%@ include file="../../EgTrackerAJSON.jsp"%>
<%@ page import = "java.io.*"%>
<%@ page import = "java.text.*"%>
<%@ page import = "net.sf.json.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%
	JSONObject rootObj = new JSONObject();
	
	response.setHeader("Cache-Control","no-store");
	response.setHeader("Pragma","no-cache");
	response.setDateHeader("Expires", -1);
	response.setContentType("application/json");
	
	EgUserRole usrRole = _user.getUserRole();
	boolean hasAllComponentAccess = usrRole.hasAllComponentAccess();

	HashMap allUsersRoleTypeMap = new HashMap();

	EgDbAdapterUI egDb;
	egDb=EgDbAdapterTypeUI.getObject();
	String str_conn = egDb.setConnection();
	try
	{
		allUsersRoleTypeMap = egDb.getAllUsersRoleTypeMap();
	}
	catch (Exception e)
	{
		e.printStackTrace();
	}
	egDb.releaseConnection(true);
	EgDbAdapterTypeUI.releaseObject(egDb);

	String userStatus = request.getParameter("usertypes");
	if (userStatus == null || userStatus.trim().length() == 0)
	{
		userStatus = "all";
	}

	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");
	if (hasAllComponentAccess)
	{
		if(managerModel != null && managerModel.equalsIgnoreCase("msp"))
		{
			IniFile inifile1 = new IniFile(EgConfigInfo.egMgrConfigDir + "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 = "";
				boolean isDisabled = false;
				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 (userStatus.equalsIgnoreCase("active"))
					{
						if (!uvalidtill.equalsIgnoreCase("12/12/9999") && uvalidtilldate.compareTo(today) < 0)
						{
							continue;
						}
					}
					else if (userStatus.equalsIgnoreCase("expired"))
					{
						if (uvalidtill.equalsIgnoreCase("12/12/9999") || uvalidtilldate.compareTo(today) > 0)
						{
							isDisabled = true;
							exportDataObj.put("cls","disabledNode");
							//continue;
						}
					}
				}
				else
				{
					if (userStatus.equalsIgnoreCase("expired"))
					{
						//continue;
						isDisabled = true;
						exportDataObj.put("cls","disabledNode");
					}
				}

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

				String displayName = optVal;
				if (optVal.startsWith("samlsso/"))
				{
					int idx = optVal.indexOf("/");
					displayName = optVal.substring(idx+1) + " (SAML)";
				}
				else if (optVal.indexOf("/") != -1)
				{
					if (optVal.indexOf("/Grp/") != -1)
					{
						String grpDelimiter = "/Grp/";
						int idx = optVal.indexOf(grpDelimiter);
						String str_uname_A = optVal.substring(0,idx);
						int nextIdx = idx + grpDelimiter.length();
						String str_uname_B = optVal.substring(nextIdx);
						displayName = str_uname_A + "/" + str_uname_B + " (Group)";
					}
				}
				if (displayName.equalsIgnoreCase("egenterprise"))
				{
					exportDataObj.put("text",_mapping.getTextDescription("admin.user.egenterprise"));
				}
				else
				{
					exportDataObj.put("text",displayName);
				}
				exportDataObj.put("id",optVar);
				exportDataObj.put("role",allUsersRoleTypeMap.get(optVar));
				exportDataObj.put("iconCls","iconDomain");
				exportDataObj.put("expanded",true);
				JSONArray rootUserObjectArray = new JSONArray();;
				if (userStatus.equalsIgnoreCase("expired"))
				{
					TreeSet expiredUserSet = egUuidInfo.getAllExpiredUsers();
					if (expiredUserSet != null && !expiredUserSet.isEmpty())
					{
						rootUserObjectArray = egUuidInfo.buildTreeJsonForExpiredUsers(orgname,false,expiredUserSet);
					}
				}
				else
				{
					rootUserObjectArray = egUuidInfo.buildTreeJsonForAllUsers(orgname,"",false,userStatus);
				}

				exportDataObj.put("children",rootUserObjectArray);

				if (userStatus.equalsIgnoreCase("expired"))
				{
					if (isDisabled)
					{
						if (!rootUserObjectArray.isEmpty())
						{
							exportDataArr.add(exportDataObj);
						}
					}
					else
					{
						exportDataArr.add(exportDataObj);
					}
				}
				else
				{
					exportDataArr.add(exportDataObj);
				}
			}
			rootObj.put("exportDataArr",exportDataArr);

			if (exportDataArr.isEmpty())
			{
				rootObj.put("isUsersCreated",new Boolean(false));
				out.println(rootObj);
				return;
			}
		}
		else
		{
			JSONArray exportDataArr = new JSONArray();
			JSONObject exportDataObj = new JSONObject();
			JSONArray rootUserObjectArray = new JSONArray();
			if (userStatus.equalsIgnoreCase("expired"))
			{
				TreeSet expiredUserSet = egUuidInfo.getAllExpiredUsers();
				if (expiredUserSet != null && !expiredUserSet.isEmpty())
				{
					rootUserObjectArray = egUuidInfo.buildTreeJsonForExpiredUsers("egenterprise",false,expiredUserSet);
				}
			}
			else
			{
				rootUserObjectArray = egUuidInfo.buildTreeJsonForAllUsers("egenterprise","",false,userStatus);
			}

			if (rootUserObjectArray.isEmpty())
			{
				rootObj.put("isUsersCreated",new Boolean(false));
				out.println(rootObj);
				return;
			}
			else
			{
				if (userStatus.equalsIgnoreCase("expired"))
				{
					exportDataObj.put("cls","disabledNode");
				}
				exportDataObj.put("text",_mapping.getTextDescription("admin.user.egenterprise"));
				exportDataObj.put("id","egenterprise");
				exportDataObj.put("role",allUsersRoleTypeMap.get("egenterprise"));
				exportDataObj.put("iconCls","iconDomain");
				exportDataObj.put("expanded",true);
				exportDataObj.put("children",rootUserObjectArray);
				exportDataArr.add(exportDataObj);
				rootObj.put("exportDataArr",exportDataArr);
			}
		}
	}
	else
	{
		String orgname = "";
		boolean isSuperOrg = egUuidInfo.isSuperOrg(session_userid);
		boolean isOrg = egUuidInfo.isOrg(session_userid);
		if (isSuperOrg)
		{
			orgname = session_userid;
		}
		else if (isOrg)
		{
			orgname = session_userid;
		}
		else
		{
			orgname = egUuidInfo.getDirectDependentOU(session_userid);
		}
		
		JSONArray exportDataArr = new JSONArray();
		JSONObject exportDataObj = new JSONObject();

		String uvalidtill = egUuidInfo.getExpiryDateOfUser(orgname);
		if (uvalidtill != null)
		{
			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 (userStatus.equalsIgnoreCase("active"))
			{
				if (!uvalidtill.equalsIgnoreCase("12/12/9999") && uvalidtilldate.compareTo(today) < 0)
				{
					//continue;
					rootObj.put("isUsersCreated",new Boolean(false));
					out.println(rootObj);
					return;
				}
			}
			else if (userStatus.equalsIgnoreCase("expired"))
			{
				if (uvalidtill.equalsIgnoreCase("12/12/9999") || uvalidtilldate.compareTo(today) > 0)
				{
					//continue;
					//rootObj.put("isUsersCreated",new Boolean(false));
					//out.println(rootObj);
					//return;
					if (userStatus.equalsIgnoreCase("expired"))
					{
						exportDataObj.put("cls","disabledNode");
					}
				}
			}
		}
		else
		{
			if (userStatus.equalsIgnoreCase("expired"))
			{
				exportDataObj.put("cls","disabledNode");
			}
		}
		JSONArray rootUserObjectArray = new JSONArray();
		if (userStatus.equalsIgnoreCase("expired"))
		{
			TreeSet expiredUserSet = egUuidInfo.getAllExpiredUsers();
			if (expiredUserSet != null && !expiredUserSet.isEmpty())
			{
				rootUserObjectArray = egUuidInfo.buildTreeJsonForExpiredUsers(orgname,false,expiredUserSet);
			}
		}
		else
		{
			rootUserObjectArray = egUuidInfo.buildTreeJsonForAllUsers(orgname,"",false,userStatus);
		}

		if (rootUserObjectArray.isEmpty())
		{
			rootObj.put("isUsersCreated",new Boolean(false));
			out.println(rootObj);
			return;
		}
		else
		{

			String displayName = orgname;
			if (orgname.startsWith("samlsso/"))
			{
				int idx = orgname.indexOf("/");
				displayName = orgname.substring(idx+1) + " (SAML)";
			}
			else if (orgname.indexOf("/") != -1)
			{
				if (orgname.indexOf("/Grp/") != -1)
				{
					String grpDelimiter = "/Grp/";
					int idx = orgname.indexOf(grpDelimiter);
					String str_uname_A = orgname.substring(0,idx);
					int nextIdx = idx + grpDelimiter.length();
					String str_uname_B = orgname.substring(nextIdx);
					displayName = str_uname_A + "/" + str_uname_B + " (Group)";
				}
			}
			if (displayName.equalsIgnoreCase("egenterprise"))
			{
				exportDataObj.put("text",_mapping.getTextDescription("admin.user.egenterprise"));
			}
			else
			{
				exportDataObj.put("text",displayName);
			}
			exportDataObj.put("id",orgname);
			exportDataObj.put("role",allUsersRoleTypeMap.get(orgname));

			if (isSuperOrg)
			{
				exportDataObj.put("iconCls","iconDomain");
			}
			else
			{
				exportDataObj.put("iconCls","iconOtherUsers");
			}

			exportDataObj.put("expanded",true);
			exportDataObj.put("children",rootUserObjectArray);
			exportDataArr.add(exportDataObj);
			rootObj.put("exportDataArr",exportDataArr);
		}
	}

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



