public void Add(ITextOrGroupModel item) { if (item == this) { throw new ArgumentException("It can't possibly be a good idea to add a group to itself."); } if (item == null) { throw new ArgumentException("Cannot add null item."); } if (item.GetLength() != _length) { throw new ArgumentException("Countable item must have the same length as the group to which it is added."); } if (_items.Any(x => x.GetName() == item.GetName())) { throw new ArgumentException($"Item {item} cannot be added to a group that it is already a member of."); } if (item is IGroupModel && ((IGroupModel)item).ContainsRecursive(this)) { throw new ArgumentException("Cannot add a group as a child to its own child or a child of its child."); } _items.Add(item); //if (item is IGroupModel) //{ // ((IGroupModel)item).Modified += new EventHandler(OnModified); //} item.Modified += new EventHandler(OnModified); OnModified(this, EventArgs.Empty); }
public void Remove(ITextOrGroupModel item) { if (item == null) { throw new ArgumentException("Cannot remove null item."); } if (!_items.Any(x => x.GetName() == item.GetName())) { throw new ArgumentException($"Group does not contain item: {item}."); } //if (item is IGroupModel) //{ // ((IGroupModel)item).Modified -= new EventHandler(OnModified); //} item.Modified -= new EventHandler(OnModified); _items.Remove(_items.FirstOrDefault(x => x.GetName() == item.GetName())); OnModified(this, EventArgs.Empty); }
public void RemoveItem(IGroupModel model, ITextOrGroupModel item) { string parentName = model.GetName(); Grouping parent = _db.Groupings.FirstOrDefault(x => x.Name == parentName); if (parent == null) { throw new ArgumentException("Cannot add to group since the group does not exist."); } string childName = item.GetName(); Text childText = _db.Texts.FirstOrDefault(x => x.Name == childName); Grouping childGroup = _db.Groupings.FirstOrDefault(x => x.Name == childName); if (childText == null) { if (childGroup == null) { throw new ArgumentException("Cannot remove item from group since the item does not exist."); } Grouping_Grouping association = _db.Grouping_Grouping.FirstOrDefault(x => x.ParentId == parent.Id && x.ChildId == childGroup.Id); if (association == null) { throw new ArgumentException("Cannot remove item from group since it is not a member of that group."); } _db.Grouping_Grouping.Remove(association); _db.SaveChanges(); } else { if (childGroup != null) { throw new InvalidOperationException("The database contains both a text and a group with the same name. That's not good."); } Text_Grouping assocation = _db.Text_Grouping.FirstOrDefault(x => x.TextId == childText.Id && x.GroupingId == parent.Id); if (assocation == null) { throw new ArgumentException("Cannot remove item from group since it is not a member of that group."); } _db.Text_Grouping.Remove(assocation); _db.SaveChanges(); } }
public bool Contains(IGroupModel group, ITextOrGroupModel item) { string groupName = group.GetName(); string itemName = item.GetName(); if (string.Equals(groupName, itemName)) { return(false); } Grouping parentGroup = _db.Groupings.FirstOrDefault(x => x.Name == groupName); if (parentGroup == null) { throw new ArgumentException("Parent group does not exist."); } //IQueryable<Grouping_Grouping> childGroupIds = _db.Grouping_Grouping.Where(x => x.ParentId == parentGroup.Id); List <Grouping> allGroups = new List <Grouping>() { parentGroup }; List <Grouping> nextGroups = new List <Grouping>(); int oldCount = allGroups.Count; int newCount = allGroups.Count; do { oldCount = allGroups.Count; foreach (Grouping grouping in allGroups) { nextGroups.AddRange(_db.Grouping_Grouping.Where(x => x.ParentId == grouping.Id).Join( _db.Groupings, gg => gg.ChildId, g => g.Id, (gg, g) => g )); } nextGroups = nextGroups.Distinct().ToList(); foreach (var nextGroup in nextGroups) { allGroups.Add(nextGroup); } allGroups = allGroups.Distinct().ToList(); newCount = allGroups.Count; nextGroups = new List <Grouping>(); } while (oldCount < newCount); List <Text> allTexts = new List <Text>(); foreach (Grouping g in allGroups) { IQueryable <Text> childTexts = _db.Text_Grouping.Where(x => x.GroupingId == g.Id).Join( _db.Texts, tg => tg.TextId, t => t.Id, (tg, t) => t ); allTexts.AddRange(childTexts); } allTexts = allTexts.Distinct().ToList(); return(allGroups.Any(x => x.Name == itemName) || allTexts.Any(x => x.Name == itemName)); //Grouping group = _db.Groupings.FirstOrDefault(x => x.Name == groupName); //Grouping groupItem = _db.Groupings.FirstOrDefault(x => x.Name == itemName); //Text textItem = _db.Texts.FirstOrDefault(x => x.Name == itemName); //if (textItem == null && groupItem == null) //{ // throw new ArgumentException("No item with the specified name exists."); //} //if (textItem == null) //{ // return _db.Grouping_Grouping.Any(x => x.ParentId == group.Id && x.ChildId == groupItem.Id); //} //return _db.Text_Grouping.Any(x => x.TextId == textItem.Id && x.GroupingId == group.Id); }
public void AddItem(IGroupModel model, ITextOrGroupModel item) { string parentName = model.GetName(); Grouping parent = _db.Groupings.FirstOrDefault(x => x.Name == parentName); if (parent == null) { throw new ArgumentException("Cannot add to group since the group does not exist."); } string childName = item.GetName(); Text childText = _db.Texts.FirstOrDefault(x => x.Name == childName); Grouping childGroup = _db.Groupings.FirstOrDefault(x => x.Name == childName); if (childText == null) { if (childGroup == null) { throw new ArgumentException("Cannot add item to group since the item does not exist."); } if (childGroup.Id == parent.Id) { throw new ArgumentException("Cannot add a group to itself."); } if (item is IGroupModel) { if (Contains((IGroupModel)item, model)) { throw new ArgumentException("Cannot add item to its own child or a child of its child or ..."); } } else { throw new ArgumentException("The item's name corresponds to a group in the db, but the item is not a group model. Something is wrong."); } if (_db.Grouping_Grouping.Any(x => x.ParentId == parent.Id && x.ChildId == childGroup.Id)) { throw new ArgumentException("Cannot add item to group since it is already a member."); } Grouping_Grouping association = new Grouping_Grouping() { ParentId = parent.Id, ChildId = childGroup.Id }; _db.Grouping_Grouping.Add(association); _db.SaveChanges(); } else { if (childGroup != null) { throw new InvalidOperationException("The database contains both a text and a group with the same name. That's not good."); } if (_db.Text_Grouping.Any(x => x.TextId == childText.Id && x.GroupingId == parent.Id)) { throw new ArgumentException("Cannot add item to group since it is already a member."); } Text_Grouping assocation = new Text_Grouping() { TextId = childText.Id, GroupingId = parent.Id }; _db.Text_Grouping.Add(assocation); _db.SaveChanges(); } }
public bool Contains(ITextOrGroupModel item) { return(_items.Any(x => x.GetName() == item.GetName())); }