Beispiel #1
0
        internal void Add(TEntity item, DateTime?moment, bool fireEvents)
        {
            if (item is null)
            {
                return;
            }

            LazyLoad();
            LazySet();

            if (EagerLoadLogic is not null)
            {
                EagerLoadLogic.Invoke(item);
            }

            if (fireEvents)
            {
                if (ParentProperty?.RaiseOnChange((OGMImpl)Parent, default(TEntity), item, moment, OperationEnum.Add) ?? false)
                {
                    return;
                }
            }

            ExecuteAction(AddAction(item, moment));
        }
        internal void Add(TEntity item, DateTime?moment, bool fireEvents)
        {
            if (item is null)
            {
                return;
            }

            LazyLoad();
            LazySet();

            if (EagerLoadLogic != null)
            {
                EagerLoadLogic.Invoke(item);
            }

            if (fireEvents)
            {
                if (ParentProperty?.RaiseOnChange((OGMImpl)Parent, default(TEntity), item, moment, OperationEnum.Add) ?? false)
                {
                    return;
                }
            }

            CollectionItem <TEntity> oldItem = InnerData.FirstOrDefault(item => item.Item.GetKey() == item.Item.GetKey());

            ExecuteAction(AddAction(item, (oldItem != null) ? oldItem.StartDate : moment));
        }
        internal void AddRange(IEnumerable <TEntity> items, DateTime?moment, bool fireEvents)
        {
            LazyLoad();
            LazySet();

            LinkedList <RelationshipAction> actions = new LinkedList <RelationshipAction>();

            foreach (var item in items)
            {
                if (item is null)
                {
                    continue;
                }

                if (EagerLoadLogic != null)
                {
                    EagerLoadLogic.Invoke(item);
                }

                if (fireEvents)
                {
                    if (ParentProperty?.RaiseOnChange((OGMImpl)Parent, default(TEntity), item, moment, OperationEnum.Add) ?? false)
                    {
                        continue;
                    }
                }

                CollectionItem <TEntity> oldItem = InnerData.FirstOrDefault(item => item.Item.GetKey() == item.Item.GetKey());
                actions.AddLast(AddAction(item, (oldItem != null) ? oldItem.StartDate : moment));
            }

            ExecuteAction(actions);
        }
Beispiel #4
0
        internal void Remove(TEntity item, DateTime?moment, bool fireEvents)
        {
            if (ForeignProperty is not null && ForeignProperty.PropertyType == PropertyType.Lookup && !ForeignProperty.Nullable)
            {
                throw new PersistenceException(string.Format("Due to a nullability constraint, you cannot delete {0} relationships directly. Consider removing the {1} objects instead.", ParentProperty?.Relationship?.Neo4JRelationshipType, ForeignEntity.Name));
            }

            if (item is null)
            {
                return;
            }

            LazyLoad();

            if (EagerLoadLogic is not null)
            {
                EagerLoadLogic.Invoke(item);
            }

            if (fireEvents)
            {
                if (ParentProperty?.RaiseOnChange <OGM>((OGMImpl)Parent, item, default(TEntity), moment, OperationEnum.Remove) ?? false)
                {
                    return;
                }
            }

            ExecuteAction(RemoveAction(item, moment));
        }
Beispiel #5
0
        internal void AddRange(IEnumerable <TEntity> items, DateTime?moment, bool fireEvents)
        {
            LazyLoad();
            LazySet();

            LinkedList <RelationshipAction> actions = new LinkedList <RelationshipAction>();

            foreach (var item in items)
            {
                if (item is null)
                {
                    continue;
                }

                if (EagerLoadLogic is not null)
                {
                    EagerLoadLogic.Invoke(item);
                }

                if (fireEvents)
                {
                    if (ParentProperty?.RaiseOnChange((OGMImpl)Parent, default(TEntity), item, moment, OperationEnum.Add) ?? false)
                    {
                        continue;
                    }
                }

                actions.AddLast(AddAction(item, moment));
            }

            ExecuteAction(actions);
        }
        internal sealed override void Add(TEntity item, bool fireEvents)
        {
            if (item is null)
            {
                return;
            }

            LazyLoad();
            LazySet();

            if (EagerLoadLogic != null)
            {
                EagerLoadLogic.Invoke(item);
            }

            if (fireEvents)
            {
                if (ParentProperty?.RaiseOnChange((OGMImpl)Parent, default(TEntity), item, null, OperationEnum.Add) ?? false)
                {
                    return;
                }
            }

            ExecuteAction(AddAction(item, null));
        }
        internal void Clear(DateTime?moment, bool fireEvents)
        {
            if (ForeignProperty != null && ForeignProperty.PropertyType == PropertyType.Lookup && !ForeignProperty.Nullable)
            {
                throw new PersistenceException(string.Format("Due to a nullability constraint, you cannot delete {0} relationships directly. Consider removing the {1} objects instead.", ParentProperty?.Relationship?.Neo4JRelationshipType, ForeignEntity.Name));
            }

            LazyLoad();

            if (InnerData.Count == 0)
            {
                return;
            }

            LazySet();

            if (fireEvents)
            {
                HashSet <CollectionItem> cancel = new HashSet <CollectionItem>();
                ForEach(delegate(int index, CollectionItem item)
                {
                    if (item is null)
                    {
                        return;
                    }

                    if (EagerLoadLogic != null)
                    {
                        EagerLoadLogic.Invoke((TEntity)item.Item);
                    }

                    if (ParentProperty?.RaiseOnChange((OGMImpl)Parent, item.Item, default(TEntity), moment, (OperationEnum)OperationEnum.Remove) ?? false)
                    {
                        cancel.Add((CollectionItem)item);
                    }
                });

                if (cancel.Count != 0)
                {
                    LinkedList <RelationshipAction> actions = new LinkedList <RelationshipAction>();

                    ForEach(delegate(int index, CollectionItem item)
                    {
                        if (cancel.Contains(item))
                        {
                            return;
                        }

                        actions.AddLast(RemoveAction(item, moment));
                    });

                    ExecuteAction(actions);

                    return;
                }
            }

            ExecuteAction(ClearAction(moment));
        }
        internal bool RemoveRange(IEnumerable <TEntity> items, DateTime?moment, bool fireEvents)
        {
            if (ForeignProperty != null && ForeignProperty.PropertyType == PropertyType.Lookup && !ForeignProperty.Nullable)
            {
                throw new PersistenceException(string.Format("Due to a nullability constraint, you cannot delete {0} relationships directly. Consider removing the {1} objects instead.", ParentProperty?.Relationship?.Neo4JRelationshipType, ForeignEntity.Name));
            }

            LazyLoad();

            LinkedList <RelationshipAction> actions = new LinkedList <RelationshipAction>();

            foreach (var item in items)
            {
                if (item != null && EagerLoadLogic != null)
                {
                    EagerLoadLogic.Invoke(item);
                }

                if (fireEvents)
                {
                    bool cancel = false;
                    ForEach(delegate(int index, CollectionItem current)
                    {
                        if (current.Item.Equals(item) && (!moment.HasValue || current.EndDate > moment.Value))
                        {
                            if (current.Item.Equals(item))
                            {
                                if (ParentProperty?.RaiseOnChange <OGM>((OGMImpl)Parent, current.Item, default(TEntity), moment, OperationEnum.Remove) ?? false)
                                {
                                    cancel = true;
                                }
                            }
                        }
                    });
                    if (cancel)
                    {
                        return(false);
                    }
                }

                ForEach(delegate(int index, CollectionItem current)
                {
                    if (current.Item.Equals(item) && (!moment.HasValue || current.EndDate > moment.Value))
                    {
                        ParentProperty?.RaiseOnChange <OGM>((OGMImpl)Parent, current.Item, default(TEntity), moment, OperationEnum.Remove);
                        actions.AddLast(RemoveAction(current, moment));
                    }
                });
            }

            if (actions.Count > 0)
            {
                ExecuteAction(actions);
                LazySet();
            }
            return(actions.Count > 0);
        }
        protected override void SetItem(TEntity?item, DateTime?moment)
        {
            LazyLoad();
            LazySet();

            if (!moment.HasValue)
            {
                moment = RunningTransaction.TransactionDate;
            }

            if (item != null && EagerLoadLogic != null)
            {
                EagerLoadLogic.Invoke(item);
            }

            List <CollectionItem <TEntity> > currentItem = InnerData.Where(e => e.Overlaps(moment, null)).ToList();

            if (!currentItem.FirstOrDefault()?.Item?.Equals(item) ?? !ReferenceEquals(item, null))
            {
                if (ForeignProperty != null && ForeignProperty.PropertyType == PropertyType.Lookup)
                {
                    OGM?oldForeignValue = (item is null) ? null : (OGM)ForeignProperty.GetValue(item, moment);
                    if (oldForeignValue != null)
                    {
                        ParentProperty?.ClearLookup(oldForeignValue, null);
                    }

                    foreach (TEntity entity in currentItem.Select(iitem => iitem.Item).Distinct())
                    {
                        ForeignProperty.ClearLookup(entity, moment);
                    }
                }

                if (item == null)
                {
                    if (ParentProperty?.PropertyType == PropertyType.Lookup)
                    {
                        Remove(currentItem[0].Item, moment);
                    }

                    if (currentItem.Count > 0)
                    {
                        Clear(moment, false);
                    }
                }
                else
                {
                    if (currentItem.Count == 1 && currentItem[0].Item.Equals(item))
                    {
                        return;
                    }

                    if (ParentProperty?.PropertyType == PropertyType.Lookup && currentItem.Count == 1)
                    {
                        Remove(currentItem[0].Item, moment);
                    }

                    if (currentItem.Count > 0)
                    {
                        Clear(moment, false);
                    }

                    if (CountAt(moment) == 0)
                    {
                        Add(item, moment, false);
                    }
                }
            }
        }
Beispiel #10
0
        protected override void SetItem(TEntity?item, DateTime?moment)
        {
            if (ParentProperty?.PropertyType != PropertyType.Lookup)
            {
                throw new NotSupportedException("You cannot use SetItem on a property thats not a lookup.");
            }

            LazyLoad();
            LazySet();

            //if (!moment.HasValue)
            //	moment = RunningTransaction.TransactionDate;

            if (item is not null && EagerLoadLogic is not null)
            {
                EagerLoadLogic.Invoke(item);
            }

            List <CollectionItem <TEntity> > currentItem = InnerData.Where(e => e.Overlaps(moment, null)).ToList();

            if (NeedsToAssign(currentItem, item, moment))
            {
                if (ForeignProperty is not null && ForeignProperty.PropertyType == PropertyType.Lookup)
                {
                    OGM?oldForeignValue = (item is null) ? null : (OGM)ForeignProperty.GetValue(item, moment);
                    if (oldForeignValue is not null)
                    {
                        ParentProperty?.ClearLookup(oldForeignValue, moment);
                    }

                    foreach (TEntity entity in currentItem.Select(iitem => iitem.Item).Distinct())
                    {
                        ForeignProperty.ClearLookup(entity, moment);
                    }
                }

                if (item is null)
                {
                    if (currentItem.Count > 0)
                    {
                        if (ParentProperty?.PropertyType == PropertyType.Lookup)
                        {
                            foreach (CollectionItem <TEntity> current in currentItem)
                            {
                                if (current?.Item is not null)
                                {
                                    Remove(current.Item, moment);
                                }
                            }
                        }

                        if (Count() > 0)
                        {
                            Clear(moment, false);                             // Clear should not be called here as this is for lookup.
                        }
                    }
                }
                else
                {
                    if (currentItem.Count > 0)
                    {
                        if (ParentProperty?.PropertyType == PropertyType.Lookup)
                        {
                            foreach (CollectionItem <TEntity> current in currentItem)
                            {
                                if (current?.Item is not null)
                                {
                                    Remove(current.Item, moment);
                                }
                            }
                        }

                        if (Count() > 0)
                        {
                            Clear(moment, false);                             // Clear should not be called here as this is for lookup.
                        }
                    }

                    if (Count() == 0)
                    {
                        Add(item, moment, false);
                    }
                }
            }
        protected override void SetItem(TEntity?item, DateTime?moment)
        {
            LazyLoad();
            LazySet();

            if (item != null && EagerLoadLogic != null)
            {
                EagerLoadLogic.Invoke(item);
            }

            List <CollectionItem <TEntity> > currentItem = InnerData.ToList();

            if (!currentItem.FirstOrDefault()?.Item?.Equals(item) ?? !ReferenceEquals(item, null))
            {
                if (ForeignProperty != null && ForeignProperty.PropertyType == PropertyType.Lookup)
                {
                    OGM?oldForeignValue = (item == null) ? null : (OGM)ForeignProperty.GetValue(item, moment);
                    if (oldForeignValue != null)
                    {
                        ParentProperty?.ClearLookup(oldForeignValue, null);
                    }

                    foreach (TEntity entity in currentItem.Select(iitem => iitem.Item).Distinct())
                    {
                        ForeignProperty.ClearLookup(entity, null);
                    }
                }

                if (item == null)
                {
                    if (currentItem.Count > 0)
                    {
                        if (ParentProperty?.PropertyType == PropertyType.Lookup)
                        {
                            Remove(currentItem[0].Item);
                        }

                        if (Count > 0)
                        {
                            Clear(false); // Clear should not be called here as this is for lookup.
                        }
                    }
                }
                else
                {
                    if (currentItem.Count == 1 && currentItem[0].Item.Equals(item))
                    {
                        return;
                    }

                    if (currentItem.Count > 0)
                    {
                        if (ParentProperty?.PropertyType == PropertyType.Lookup)
                        {
                            Remove(currentItem[0].Item);
                        }

                        if (Count > 0)
                        {
                            Clear(false); // Clear should not be called here as this is for lookup.
                        }
                    }

                    if (Count == 0)
                    {
                        Add(item, false);
                    }
                }
            }
        }