/// <summary> /// Delete a container from the data source /// </summary> /// <param name="o">The container to delete</param> /// <param name="bypassChecks">Whether to bypass any checks made to stop deletion by a front end user</param> public void Delete(object o, bool bypassChecks) { using (var dataSource = DataSourceFactory.Create(false)) { var idProp = LinqX.GetIdProp(o.GetType(), this.IdName); object noId = ReflectionX.GetDefault(idProp.PropertyType); var eventData = new RepositoryEventData(o, bypassChecks); var eventResult = EventHub.Instance.ProcessEvent("Repository.Set.Delete", this, eventData); var itemDel = ((RepositoryEventData)eventResult.Data).Container; bool wasHandled = ((RepositoryEventData)eventResult.Data).WasHandled; if (itemDel != null) { if (eventResult.EventName.EndsWith("Add")) { DoAdd(dataSource, itemDel, idProp, wasHandled); } else if (eventResult.EventName.EndsWith("Update") && !wasHandled) { dataSource.Update(itemDel); } else if (!wasHandled) { dataSource.Delete(itemDel); } if (!wasHandled) { dataSource.SaveChanges(); } } var savedEventData = new RepositoryEventData(o, bypassChecks); EventHub.Instance.ProcessEvent(eventResult.EventName.Replace("Set", "Saved"), this, savedEventData); } }
/// <summary> /// Set (create or update) a list of containers to the data source /// </summary> /// <param name="items">The list of containers</param> /// <param name="setOptions">Options for setting</param> /// <returns>List of flags for whether the corresponding by position item was created (rather than updated)</returns> public List <bool> Set(List <object> items, Dictionary <string, object> setOptions) { if (!(items.All(i => i is ContentItem))) { throw new ArgumentException("Content repository can only set ContentItem type"); } var createds = new List <bool>(); bool?create = setOptions.Get <bool?>("create"); bool bypassChecks = setOptions.Get <bool>("bypassChecks", false); using (var dataSource = DataSourceFactory.Create(false)) { var ciSaved = new List <Tuple <ContentItem, bool> >(); bool anyUnhandled = false; foreach (ContentItem ci in items) { if (ChangeProblems.Any(cp => cp.TypeName == ci.DataType) && !BypassChangeProblems) { throw new LyniconUpdateException("Changes in the structure of the data may cause data loss, please advise an administrator"); } bool isAdd = (create == null ? ci.Id == Guid.Empty : create.Value); var eventData = new RepositoryEventData(ci, bypassChecks); var eventResult = EventHub.Instance.ProcessEvent("Repository.Set." + (isAdd ? "Add" : "Update"), this, eventData); var ciSave = (ContentItem)((RepositoryEventData)eventResult.Data).Container; bool wasHandled = ((RepositoryEventData)eventResult.Data).WasHandled; if (!wasHandled) { anyUnhandled = true; } isAdd = eventResult.EventName.EndsWith("Add"); createds.Add(isAdd); if (isAdd) { DoAdd(dataSource, ciSave, wasHandled); } else if (!wasHandled) { dataSource.Update(ciSave); } ciSaved.Add(Tuple.Create(ciSave, isAdd)); } if (ciSaved.Count > 0 && anyUnhandled) { dataSource.SaveChanges(); } foreach (var sv in ciSaved) { var savedEventData = new RepositoryEventData(sv.Item1, bypassChecks); EventHub.Instance.ProcessEvent("Repository.Saved." + (sv.Item2 ? "Add" : "Update"), this, savedEventData); } } return(createds); }
/// <summary> /// Delete a container from the data source /// </summary> /// <param name="o">The container to delete</param> /// <param name="bypassChecks">Whether to bypass any checks made to stop deletion by a front end user</param> public virtual void Delete(object o, bool bypassChecks) { if (!(o is ContentItem)) { throw new ArgumentException("Content repository can only delete ContentItem type"); } ContentItem ci = o as ContentItem; using (var dataSource = DataSourceFactory.Create(false)) { var options = new Dictionary <string, object> { { "bypassChecks", bypassChecks } }; var eventData = new RepositoryEventData(ci, options); var eventResult = System.Events.ProcessEvent("Repository.Set.Delete", this, eventData); var ciSave = (ContentItem)((RepositoryEventData)eventResult.Data).Container; bool wasHandled = ((RepositoryEventData)eventResult.Data).WasHandled; if (ciSave != null) { if (eventResult.EventName.EndsWith("Add")) { DoAdd(dataSource, ciSave, wasHandled); } else if (eventResult.EventName.EndsWith("Update") && !wasHandled) { dataSource.Update(ciSave); } else if (!wasHandled) { dataSource.Delete(ciSave); } if (!wasHandled) { dataSource.SaveChanges(); } var savedEventData = new RepositoryEventData(ciSave, options); System.Events.ProcessEvent(eventResult.EventName.Replace("Set", "Saved"), this, savedEventData); } } }
/// <summary> /// Delete a container from the data source /// </summary> /// <param name="o">The container to delete</param> /// <param name="bypassChecks">Whether to bypass any checks made to stop deletion by a front end user</param> public void Delete(object o, bool bypassChecks) { var db = GetDb(); try { var idProp = LinqX.GetIdProp(o.GetType(), this.IdName); object noId = ReflectionX.GetDefault(idProp.PropertyType); var eventData = new RepositoryEventData(o, bypassChecks); var eventResult = EventHub.Instance.ProcessEvent("Repository.Set.Delete", this, eventData); var itemDel = ((RepositoryEventData)eventResult.Data).Container; bool wasHandled = ((RepositoryEventData)eventResult.Data).WasHandled; if (itemDel != null) { if (eventResult.EventName.EndsWith("Add")) { DoAdd(db, itemDel, idProp, wasHandled); } else if (eventResult.EventName.EndsWith("Update") && !wasHandled) { db.Entry(itemDel).State = EntityState.Modified; } else if (!wasHandled) { db.Entry(itemDel).State = EntityState.Deleted; } if (!wasHandled) { db.SaveChanges(); } } var savedEventData = new RepositoryEventData(o, bypassChecks); EventHub.Instance.ProcessEvent(eventResult.EventName.Replace("Set", "Saved"), this, savedEventData); } finally { EndCallDb(db); } }
/// <summary> /// Delete a container from the data source /// </summary> /// <param name="o">The container to delete</param> /// <param name="bypassChecks">Whether to bypass any checks made to stop deletion by a front end user</param> public virtual void Delete(object o, bool bypassChecks) { using (var dataSource = DataSourceFactory.Create(false)) { var idProp = LinqX.GetIdProp(o.GetType(), this.IdName); Dictionary <string, object> options = new Dictionary <string, object> { { "bypassChecks", bypassChecks } }; var eventData = new RepositoryEventData(o, options); var eventResult = System.Events.ProcessEvent("Repository.Set.Delete", this, eventData); var itemDel = ((RepositoryEventData)eventResult.Data).Container; bool wasHandled = ((RepositoryEventData)eventResult.Data).WasHandled; if (itemDel != null) { if (eventResult.EventName.EndsWith("Add")) { DoAdd(dataSource, itemDel, idProp, wasHandled); } else if (eventResult.EventName.EndsWith("Update") && !wasHandled) { dataSource.Update(itemDel); } else if (!wasHandled) { dataSource.Delete(itemDel); } if (!wasHandled) { dataSource.SaveChanges(); } } var savedEventData = new RepositoryEventData(o, options); System.Events.ProcessEvent(eventResult.EventName.Replace("Set", "Saved"), this, savedEventData); } }
/// <summary> /// Set (create or update) a list of containers to the data source /// </summary> /// <param name="items">The list of containers</param> /// <param name="setOptions">Options for setting</param> /// <returns>List of flags for whether the corresponding by position item was created (rather than updated)</returns> public virtual List <bool> Set(List <object> items, Dictionary <string, object> setOptions) { var db = GetDb(); var createds = new List <bool>(); bool?create = setOptions.Get <bool?>("create"); bool bypassChecks = setOptions.Get <bool>("bypassChecks", false); bool anyUnhandled = false; try { var savedItems = new List <Tuple <object, bool> >(); foreach (object item in items) { var idProp = LinqX.GetIdProp(item.GetType(), this.IdName); bool isAdd; if (create == null) { object noId = ReflectionX.GetDefault(idProp.PropertyType); isAdd = idProp.GetValue(item).Equals(noId); } else { isAdd = create.Value; } var eventData = new RepositoryEventData(item, bypassChecks); var eventResult = EventHub.Instance.ProcessEvent("Repository.Set." + (isAdd ? "Add" : "Update"), this, eventData); var itemSave = ((RepositoryEventData)eventResult.Data).Container; bool wasHandled = ((RepositoryEventData)eventResult.Data).WasHandled; if (!wasHandled) { anyUnhandled = true; } isAdd = eventResult.EventName.EndsWith("Add"); if (isAdd) { DoAdd(db, itemSave, idProp, wasHandled); } else if (!wasHandled) { db.Entry(itemSave).State = EntityState.Modified; } savedItems.Add(Tuple.Create(itemSave, isAdd)); createds.Add(isAdd); } if (savedItems.Count > 0 && anyUnhandled) { db.SaveChanges(); } foreach (var savedItem in savedItems) { var eventData = new RepositoryEventData(savedItem.Item1, bypassChecks); EventHub.Instance.ProcessEvent("Repository.Saved." + (savedItem.Item2 ? "Add" : "Update"), this, eventData); } return(createds); } finally { EndCallDb(db); } }