/// <summary> /// Adds a new child to the node. /// </summary> /// <param name="Child">New child to add.</param> public virtual async Task AddAsync(INode Child) { MeteringNode Node = Child as MeteringNode; if (Node == null) throw new Exception("Child must be a metering node."); if (this.objectId == Guid.Empty) throw new Exception("Parent node must be persisted before you can add nodes to it."); if (!this.childrenLoaded) await this.LoadChildren(); Node.parentId = this.objectId; if (Node.objectId == Guid.Empty) { await Database.Insert(Node); MeteringTopology.RegisterNode(Node); } else { Node.updated = DateTime.Now; await Database.Update(Node); } lock (this.synchObject) { if (this.children == null) this.children = new List<MeteringNode>(); this.children.Add(Node); Node.parent = this; } this.RaiseUpdate(); }
private async Task LoadChildren() { IEnumerable<MeteringNode> Children = await Database.Find<MeteringNode>(new FilterFieldEqualTo("ParentId", this.objectId)); LinkedList<MeteringNode> Children2 = new LinkedList<MeteringNode>(); foreach (MeteringNode Node in Children) Children2.AddLast(MeteringTopology.RegisterNode(Node)); lock (this.synchObject) { this.children = null; foreach (MeteringNode Child in Children2) { if (this.children == null) this.children = new List<MeteringNode>(); this.children.Add(Child); Child.parent = this; } this.SortChildrenAfterLoadLocked(this.children); this.childrenLoaded = true; } }
private async Task<MeteringNode> LoadParent() { if (this.parent != null) return this.parent; if (this.parentId == Guid.Empty) return null; this.parent = await Database.LoadObject<MeteringNode>(this.parentId); MeteringTopology.RegisterNode(this.parent); return this.parent; }
/// <summary> /// Adds a new child to the node. /// </summary> /// <param name="Child">New child to add.</param> public virtual async Task AddAsync(INode Child) { MeteringNode Node = Child as MeteringNode; if (Node == null) { throw new Exception("Child must be a metering node."); } if (this.objectId == Guid.Empty) { throw new Exception("Parent node must be persisted before you can add nodes to it."); } if (!this.childrenLoaded) { await this.LoadChildren(); } Node.parentId = this.objectId; MeteringNode After = null; int c; lock (this.synchObject) { if (this.children == null) { this.children = new List <MeteringNode>(); } else if ((c = this.children.Count) > 0) { After = this.children[c - 1]; } this.children.Add(Node); Node.parent = this; } if (Node.objectId == Guid.Empty) { await Database.Insert(Node); MeteringTopology.RegisterNode(Node); Language Language = await Translator.GetDefaultLanguageAsync(); NodeAdded Event = new NodeAdded() { Parameters = await Node.GetDisplayableParameterAraryAsync(Language, RequestOrigin.Empty), NodeType = Node.GetType().FullName, Sniffable = this is ISniffable, DisplayName = await Node.GetTypeNameAsync(Language), HasChildren = Node.HasChildren, ChildrenOrdered = Node.ChildrenOrdered, IsReadable = Node.IsReadable, IsControllable = Node.IsControllable, HasCommands = Node.HasCommands, ParentId = this.NodeId, ParentPartition = this.Partition, Updated = Node.Updated, State = Node.State, NodeId = Node.NodeId, Partition = Node.Partition, LogId = MeteringTopology.EmptyIfSame(Node.LogId, Node.NodeId), LocalId = MeteringTopology.EmptyIfSame(Node.LocalId, Node.NodeId), SourceId = Node.SourceId, Timestamp = DateTime.Now }; if (this.ChildrenOrdered && After != null) { Event.AfterNodeId = After.nodeId; Event.AfterPartition = After.Partition; } await MeteringTopology.NewEvent(Event); } else { await Node.NodeUpdated(); } this.RaiseUpdate(); }