static void BuildTestCTE(UserSecurityContext context) { OrganizationHierarchyManager.Instance.Initialize(); Console.WriteLine("OHM state: " + OrganizationHierarchyManager.Instance.State); OrganizationHierarchyManager.Instance.Start(); Console.WriteLine("OHM state: " + OrganizationHierarchyManager.Instance.State); if (OrganizationHierarchyManager.Instance.State != Osrs.Runtime.RunState.Running) { Console.WriteLine("Cannot test OHM - not running"); return; } CompoundIdentity systemId = new CompoundIdentity(Guid.NewGuid(), Guid.NewGuid()); IEnumerable <Organization> orgs = OrganizationManager.Instance.GetOrganizationProvider(context).Get("Pacific Northwest National Lab"); Organization owner = null; foreach (Organization cur in orgs) { owner = cur; break; } OrganizationHierarchyProviderBase prov = OrganizationHierarchyManager.Instance.GetProvider(context); OrganizationHierarchy hier = null; if (prov.Exists("temp")) { hier = prov.Get("temp"); if (hier != null) { Console.WriteLine("Fetched hierarchy"); } } if (hier == null) { hier = prov.Create("temp", owner); if (hier != null) { Console.WriteLine("Created hierarchy"); } } if (hier != null) { List <CompoundIdentity> idList = new List <CompoundIdentity>(); for (int i = 0; i <= 100; i++) { idList.Add(new CompoundIdentity(Guid.NewGuid(), Guid.NewGuid())); } for (int i = 0; i < 10; i++) { hier.Add(idList[i], idList[i + 10]); hier.Add(idList[i + 10], idList[i + 20]); hier.Add(idList[i + 20], idList[i + 30]); hier.Add(idList[i + 30], idList[i + 40]); hier.Add(idList[i + 40], idList[i + 50]); hier.Add(idList[i + 50], idList[i + 60]); hier.Add(idList[i + 60], idList[i + 70]); hier.Add(idList[i + 70], idList[i + 80]); hier.Add(idList[i + 80], idList[i + 90]); } //Console.WriteLine("Targeting org: 0403e3d2-b8a2-42cf-80fc-848fcf86aaad,b32cad24-9338-45f4-8d99-bd1aeb43b804"); Console.WriteLine("Targeting org: " + idList[30].DataStoreIdentity.ToString() + "," + idList[30].Identity.ToString()); Console.WriteLine("Getting children - no limit specified"); IEnumerable <CompoundIdentity> orgIds = hier.GetChildrenIds(idList[30]); //IEnumerable<CompoundIdentity> orgIds = hier.GetChildrenIds(new CompoundIdentity(new Guid("0403e3d2-b8a2-42cf-80fc-848fcf86aaad"), new Guid("b32cad24-9338-45f4-8d99-bd1aeb43b804"))); int ct = 0; foreach (CompoundIdentity curId in orgIds) { //Console.Write(curId.DataStoreIdentity.ToString() + "," + curId.Identity.ToString()+" "); ct++; } Console.WriteLine(); Console.WriteLine("Got " + ct + " items"); Console.WriteLine("Getting children - max limit specified"); orgIds = hier.GetChildrenIds(idList[30], true); //orgIds = hier.GetChildrenIds(new CompoundIdentity(new Guid("0403e3d2-b8a2-42cf-80fc-848fcf86aaad"), new Guid("b32cad24-9338-45f4-8d99-bd1aeb43b804")), true); ct = 0; foreach (CompoundIdentity curId in orgIds) { //Console.Write(curId.DataStoreIdentity.ToString() + "," + curId.Identity.ToString()+" "); ct++; } Console.WriteLine(); Console.WriteLine("Got " + ct + " items"); if (prov.Delete(hier)) { Console.WriteLine("Deleted hierarchy"); } else { Console.WriteLine("Failed deleting hierarchy"); } } else { Console.WriteLine("Failed to create hierarchy"); } }
public static void Handle(UserSecurityContext user, string method, HttpContext context, CancellationToken cancel) { if (context.Request.Method == "POST") { if (method.Equals("all", StringComparison.OrdinalIgnoreCase)) { Get(user, context, cancel); return; } else if (method.Equals("children", StringComparison.OrdinalIgnoreCase)) { try { //parse request JToken token = JsonUtils.GetDataPayload(context.Request); CompoundIdentity cid = JsonUtils.ToId(token["parentid"]); string outputType = (token["outputType"] != null) ? token["outputType"].ToString() : ""; string recurse = token["recurse"] != null ? token["recurse"].ToString() : null; //get default hierarchy OrganizationHierarchyProviderBase provider = OrganizationHierarchyManager.Instance.GetProvider(user); OrganizationHierarchy hierarchy = provider.GetReporting(); //get children ids IEnumerable <CompoundIdentity> children = null; if (recurse != null) { children = hierarchy.GetChildrenIds(cid, Convert.ToBoolean(recurse)); } else { children = hierarchy.GetChildrenIds(cid); } if (children == null) { RestUtils.Push(context.Response, JsonOpStatus.Ok, "[]"); //empty return; } //return children ids or objects JArray jchildren = null; if (outputType.Equals("values")) { OrganizationProviderBase orgProvider = OrganizationManager.Instance.GetOrganizationProvider(user); List <Organization> orgs = new List <Organization>(); foreach (CompoundIdentity child in children) { orgs.Add(orgProvider.Get(child)); } jchildren = Jsonifier.ToJson(orgs); } else { jchildren = Jsonifier.ToJson(children); } if (jchildren != null) { RestUtils.Push(context.Response, JsonOpStatus.Ok, jchildren.ToString()); } else { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); //empty returned above } } catch { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); } return; } else if (method.Equals("parent", StringComparison.OrdinalIgnoreCase)) { try { CompoundIdentity cid = JsonUtils.ToId(JsonUtils.GetDataPayload(context.Request)); if (cid != null) { GetParent(cid, user, context, cancel); return; } RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); } catch { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed)); return; } } else if (method.Equals("add", StringComparison.OrdinalIgnoreCase)) { try { //parse request JToken token = JsonUtils.GetDataPayload(context.Request); CompoundIdentity parent = JsonUtils.ToId(token["parentid"]); HashSet <CompoundIdentity> child_cids = JsonUtils.ToIds(token["childid"]); //1 or more //get default hierarchy OrganizationHierarchyProviderBase provider = OrganizationHierarchyManager.Instance.GetProvider(user); OrganizationHierarchy hierarchy = provider.GetReporting(); //insert bool result = hierarchy.Add(parent, child_cids); if (result == true) { RestUtils.Push(context.Response, JsonOpStatus.Ok); } else { RestUtils.Push(context.Response, JsonOpStatus.Failed); } } catch { RestUtils.Push(context.Response, JsonOpStatus.Failed); } return; } else if (method.Equals("move", StringComparison.OrdinalIgnoreCase)) { try { //parse request JToken token = JsonUtils.GetDataPayload(context.Request); CompoundIdentity old_cid = JsonUtils.ToId(token["parentid"]); CompoundIdentity new_cid = JsonUtils.ToId(token["newparentid"]); HashSet <CompoundIdentity> cids = JsonUtils.ToIds(token["childid"]); //get default hierarchy OrganizationHierarchyProviderBase provider = OrganizationHierarchyManager.Instance.GetProvider(user); OrganizationHierarchy hierarchy = provider.GetReporting(); //move bool result = hierarchy.Move(old_cid, new_cid, cids); if (result == true) { RestUtils.Push(context.Response, JsonOpStatus.Ok); } else { RestUtils.Push(context.Response, JsonOpStatus.Failed); } } catch { RestUtils.Push(context.Response, JsonOpStatus.Failed); } return; } else if (method.Equals("remove", StringComparison.OrdinalIgnoreCase)) { try { //parse request JToken token = JsonUtils.GetDataPayload(context.Request); CompoundIdentity parent_cid = JsonUtils.ToId(token["parentid"]); HashSet <CompoundIdentity> child_cids = JsonUtils.ToIds(token["childid"]); //get default hierarchy OrganizationHierarchyProviderBase provider = OrganizationHierarchyManager.Instance.GetProvider(user); OrganizationHierarchy hierarchy = provider.GetReporting(); //remove membership bool result = hierarchy.Remove(parent_cid, child_cids); if (result == true) { RestUtils.Push(context.Response, JsonOpStatus.Ok); } else { RestUtils.Push(context.Response, JsonOpStatus.Failed); } } catch { RestUtils.Push(context.Response, JsonOpStatus.Failed); } return; } } context.Response.StatusCode = HttpStatusCodes.Status400BadRequest; }