protected List <EntityBundle> GetBundles(List <Guid> bundleIds) { if (bundleIds != null && bundleIds.Count > 0) { EntityBundleProvider prov = EntityBundleManager.Instance.GetProvider(this.Context); if (prov != null) { List <EntityBundle> tmp = new List <EntityBundle>(); foreach (Guid cur in bundleIds) { EntityBundle item = prov.Get(cur); if (item != null) { tmp.Add(item); } } if (tmp.Count == bundleIds.Count) { return(tmp); } } } return(null); }
public override void Handle(HttpContext context, CancellationToken cancel) { if (context != null) { UserIdentityBase user = Security.Session.GetUser(context); if (user != null) { UserSecurityContext ctx = new UserSecurityContext(user); string localUrl = RestUtils.LocalUrl(this, context.Request); string meth = RestUtils.StripLocal(this.BaseUrl + "/", localUrl); if (!string.IsNullOrEmpty(meth)) { try { if (meth.StartsWith(Get, StringComparison.OrdinalIgnoreCase)) { try { EntityBundleProvider prov = EntityBundleManager.Instance.GetProvider(ctx); if (prov != null) { JArray jbundles = Jsonifier.ToJson(prov.Get()); if (jbundles != null) { RestUtils.Push(context.Response, JsonOpStatus.Ok, jbundles.ToString()); return; } else { RestUtils.Push(context.Response, JsonOpStatus.Ok, "[]"); return; } } RestUtils.Push(context.Response, JsonOpStatus.Failed); } catch { RestUtils.Push(context.Response, JsonOpStatus.Failed); return; } } else if (meth.StartsWith(In, StringComparison.OrdinalIgnoreCase)) { //just need a bundle id to get {id:<id>} JToken token = JsonUtils.GetDataPayload(context.Request); if (token != null) { JObject o = token as JObject; if (o != null) { if (o[JsonUtils.Id] != null) { //Guid id = JsonUtils.ToGuid(token[JsonUtils.Id]); Guid id = JsonUtils.ToGuid(o[JsonUtils.Id] as JToken); if (!Guid.Empty.Equals(id)) { EntityBundleProvider prov = EntityBundleManager.Instance.GetProvider(ctx); EntityBundle bundle = prov.Get(id); if (bundle != null) { RestUtils.Push(context.Response, JsonOpStatus.Ok, Jsonifier.ToJson(bundle)); return; } } } } } } else if (meth.StartsWith(Create, StringComparison.OrdinalIgnoreCase)) { JToken token = JsonUtils.GetDataPayload(context.Request); if (token != null) { if (token[JsonUtils.Name] != null && token[JsonUtils.OwnerId] != null && token[Jsonifier.Type] != null && token[Jsonifier.Items] != null) { string name = token[JsonUtils.Name].ToString(); if (!string.IsNullOrEmpty(name)) { string type = token[Jsonifier.Type].ToString(); if (!string.IsNullOrEmpty(type)) { BundleDataType datType; if (type.StartsWith("site", StringComparison.OrdinalIgnoreCase)) { datType = BundleDataType.Site; } else if (type.StartsWith("taxa", StringComparison.OrdinalIgnoreCase)) { datType = BundleDataType.TaxaUnit; } else if (type.StartsWith("inst", StringComparison.OrdinalIgnoreCase)) { datType = BundleDataType.Instrument; } else { context.Response.StatusCode = HttpStatusCodes.Status400BadRequest; return; } CompoundIdentity cid = JsonUtils.ToId(token[JsonUtils.OwnerId]); if (cid != null && !cid.IsEmpty) { JArray itemsArr = Items.GetItems(token[Jsonifier.Items]); if (itemsArr != null && itemsArr.Count > 0) { List <Tuple <CompoundIdentity, string, string> > items = new List <Tuple <CompoundIdentity, string, string> >(); foreach (JToken cur in itemsArr) { Tuple <CompoundIdentity, string, string> tpl = Items.GetItem(cur); if (tpl != null) { items.Add(tpl); } } if (items.Count == itemsArr.Count) { if (Items.VandV(items, datType, ctx)) { EntityBundleProvider prov = EntityBundleManager.Instance.GetProvider(ctx); EntityBundle bundle = prov.Create(name, cid, datType); if (bundle != null) { foreach (Tuple <CompoundIdentity, string, string> tpl in items) { if (bundle.Add(tpl.Item1, tpl.Item2, tpl.Item3) == null) { RestUtils.Push(context.Response, JsonOpStatus.Failed, "\"failed to add item to bundle\""); return; } } if (prov.Update(bundle)) { RestUtils.Push(context.Response, JsonOpStatus.Ok, Jsonifier.ToJson(bundle)); return; } } } } } } } } } } } else if (meth.StartsWith(Update, StringComparison.OrdinalIgnoreCase)) //note, this will just append items to the existing bundle and update bundle display name { //needs to have same content as create mostly -- doesn't require orgid (we ignore that since it can't change), and name is optional JToken token = JsonUtils.GetDataPayload(context.Request); if (token != null) { if (token[JsonUtils.Id] != null && token[Jsonifier.Items] != null) { string name = null; if (token[JsonUtils.Name] != null) { name = token[JsonUtils.Name].ToString(); } Guid id = JsonUtils.ToGuid(token[JsonUtils.Id]); if (!Guid.Empty.Equals(id)) { JArray itemsArr = Items.GetItems(token[Jsonifier.Items]); if (itemsArr != null && itemsArr.Count > 0) { List <Tuple <CompoundIdentity, string, string> > items = new List <Tuple <CompoundIdentity, string, string> >(); foreach (JToken cur in itemsArr) { Tuple <CompoundIdentity, string, string> tpl = Items.GetItem(cur); if (tpl != null) { items.Add(tpl); } } if (items.Count == itemsArr.Count) { token = null; itemsArr = null; EntityBundleProvider prov = EntityBundleManager.Instance.GetProvider(ctx); EntityBundle bundle = prov.Get(id); if (bundle != null) { if (!string.IsNullOrEmpty(name) && !bundle.Name.Equals(name, StringComparison.OrdinalIgnoreCase)) { bundle.Name = name; } //now we can actually try to add the items as needed foreach (Tuple <CompoundIdentity, string, string> tpl in items) { if (!bundle.Contains(tpl.Item2)) { if (bundle.Add(tpl.Item1, tpl.Item2, tpl.Item3) == null) { RestUtils.Push(context.Response, JsonOpStatus.Failed, "\"Failed to update bundle\""); return; } } } if (prov.Update(bundle)) { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Ok)); return; } else { RestUtils.Push(context.Response, JsonOpStatus.Failed, "\"Failed to update bundle\""); return; } } } } } } } } } catch { context.Response.StatusCode = HttpStatusCodes.Status400BadRequest; return; } } } else { context.Response.StatusCode = HttpStatusCodes.Status401Unauthorized; return; } } context.Response.StatusCode = HttpStatusCodes.Status400BadRequest; }
//to support mutliple det types in a single request (which all share the same sample event), we have a collection of items as follows: //{id:<fieldtripid>, name:<sampleeventname - to be>, desc:<sampleeventdesc - to be>, princorgid: <id>, start:<date>, end:<date>, dets:[<det element>,...]} //each det element is an object as follows: //{type:<dettype>, privacy:<bool>, <<entity bundle ids specific to det>>} (example is sites:{id:bundleid,include:[key list]}) optional "exclude" instead of include would allow removing elements from bundle in det //returns fileid of the file to download // note that the name is required, desc is optional - name //for all types, the entity bundle format is: {id:<entitybundleid>, keys:[<id>, <id>, ...]} // note that keys are optional - if not provided it means "all items in bundle" // note that the bundle should be of the type expected for the parameter - e.g. if the provided bundle is for "sites", that bundle should be of sites internal static void Create(UserSecurityContext user, JToken dat, HttpContext context, CancellationToken cancel) { if (dat != null) { JObject payload = dat as JObject; if (payload != null) { //these are requried for any and all DETs to create CompoundIdentity ftId = JsonUtils.ToId(payload[JsonUtils.Id]); CompoundIdentity prOrgId = JsonUtils.ToId(payload[JsonUtils.OwnerId]); if (HasAffiliation(user, prOrgId)) { string name = JsonUtils.ToString(payload[JsonUtils.Name]); string desc = JsonUtils.ToString(payload[JsonUtils.Description]); ValueRange <DateTime> range = JsonUtils.ToRange(payload, JsonUtils.Start, JsonUtils.Finish); if (ftId != null && prOrgId != null && !string.IsNullOrEmpty(name)) { JToken dets = payload["dets"]; if (dets != null && dets is JArray) { JArray detItems = dets as JArray; if (detItems != null) { List <DetPayload> items = JsonDetExtractor.ExtractAll(detItems); if (items != null && items.Count > 0) //we actually have things to create { GeneralDetProcessor prov = DetProcessorManager.Instance.GetProvider(user); if (prov != null) { CompoundIdentity seId = prov.CreateSampleEvent(ftId, prOrgId, name, desc, range); EntityBundleProvider eProv = EntityBundleManager.Instance.GetProvider(user); if (eProv != null) { if (seId != null) { Dictionary <string, Guid> fileIds = new Dictionary <string, Guid>(); foreach (DetPayload cur in items) { if (cur.DetTypeName == KnownDets.Instance.WQ) { EntityBundle siteBundle = eProv.Get(cur.EntityBundles[JsonDetExtractor.Sites]); EntityBundle instBundle = eProv.Get(cur.EntityBundles[JsonDetExtractor.Instruments]); if (siteBundle != null && instBundle != null) { FilestoreFile fil = prov.CreateWQ(seId, siteBundle, instBundle, cur.IsPrivate); if (fil != null) { fileIds.Add(cur.DetTypeName, fil.FileId); fil.Dispose(); fil = null; } } } else if (cur.DetTypeName == KnownDets.Instance.Fish) { EntityBundle siteBundle = eProv.Get(cur.EntityBundles[JsonDetExtractor.Sites]); EntityBundle instBundle = eProv.Get(cur.EntityBundles[JsonDetExtractor.Nets]); EntityBundle fishBundle = eProv.Get(cur.EntityBundles[JsonDetExtractor.Fish]); EntityBundle macroBundle = eProv.Get(cur.EntityBundles[JsonDetExtractor.Macro]); if (siteBundle != null && instBundle != null && fishBundle != null && macroBundle != null) { FilestoreFile fil = prov.CreateFish(seId, siteBundle, instBundle, fishBundle, macroBundle, cur.IsPrivate); if (fil != null) { fileIds.Add(cur.DetTypeName, fil.FileId); fil.Dispose(); fil = null; } } } else if (cur.DetTypeName == KnownDets.Instance.Veg) { EntityBundle siteBundle = eProv.Get(cur.EntityBundles[JsonDetExtractor.Sites]); EntityBundle treeBundle = eProv.Get(cur.EntityBundles[JsonDetExtractor.Tree]); EntityBundle shrubBundle = eProv.Get(cur.EntityBundles[JsonDetExtractor.Shrub]); EntityBundle herbBundle = eProv.Get(cur.EntityBundles[JsonDetExtractor.Herb]); EntityBundle plotBundle = eProv.Get(cur.EntityBundles[JsonDetExtractor.Plots]); EntityBundle nlBundle = null; if (JsonDetExtractor.NonLiving != null) { nlBundle = eProv.Get(cur.EntityBundles[JsonDetExtractor.NonLiving]); } if (siteBundle != null && treeBundle != null && shrubBundle != null && herbBundle != null && plotBundle != null) { FilestoreFile fil = prov.CreateVeg(seId, siteBundle, plotBundle, shrubBundle, treeBundle, herbBundle, nlBundle, cur.IsPrivate); if (fil != null) { fileIds.Add(cur.DetTypeName, fil.FileId); fil.Dispose(); fil = null; } } } } //NOTE - we may want to deal with partial successes in case we have multiple DETs and some work, some don't //ok, now we do the response with a list of fileIds if (fileIds.Count > 0) { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Ok, JsonUtils.ToJson(fileIds).ToString())); return; } else { RestUtils.Push(context.Response, RestUtils.JsonOpStatus(JsonOpStatus.Failed, new JValue("\"No items succeeded\"").ToString())); return; } } } } } } } } } else { context.Response.StatusCode = HttpStatusCodes.Status401Unauthorized; //in this case, by way of affiliation return; } } } context.Response.StatusCode = HttpStatusCodes.Status400BadRequest; }