/// <summary> /// Removes a child from the node. /// </summary> /// <param name="Child">Child to remove.</param> /// <returns>If the Child node was found and removed.</returns> public async Task <bool> RemoveAsync(INode Child) { MeteringNode Node = Child as MeteringNode; if (Node == null) { throw new Exception("Child must be a metering node."); } if (!this.childrenLoaded) { await this.LoadChildren(); } int i; lock (this.synchObject) { i = this.children.IndexOf(Node); if (i >= 0) { this.children.RemoveAt(i); } } Node.parentId = Guid.Empty; if (Node.objectId != Guid.Empty) { await Database.Update(Child); this.RaiseUpdate(); } return(i >= 0); }
/// <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(); }
/// <summary> /// Tries to move the child node down. /// </summary> /// <param name="Child">Child node to move.</param> /// <param name="Caller">Information about caller.</param> /// <returns>If the child node was moved down.</returns> public virtual async Task <bool> MoveDownAsync(MeteringNode Child, RequestOrigin Caller) { if (!this.ChildrenOrdered || this.children == null) { return(false); } if (!await this.CanEditAsync(Caller) || !await Child.CanEditAsync(Caller)) { return(false); } lock (this.children) { int c = this.children.Count; int i = this.children.IndexOf(Child); if (i < 0 || i + 1 >= c) { return(false); } this.children.RemoveAt(i); this.children.Insert(i + 1, Child); } await MeteringTopology.NewEvent(new NodeMovedDown() { NodeId = Child.NodeId, Partition = Child.Partition, SourceId = Child.SourceId, Timestamp = DateTime.Now }); return(true); }
/// <summary> /// Removes a child from the node. /// </summary> /// <param name="Child">Child to remove.</param> /// <returns>If the Child node was found and removed.</returns> public virtual async Task <bool> RemoveAsync(INode Child) { MeteringNode Node = Child as MeteringNode; if (Node == null) { throw new Exception("Child must be a metering node."); } if (!this.childrenLoaded) { await this.LoadChildren(); } int i; lock (this.synchObject) { if (this.children != null) { i = this.children.IndexOf(Node); if (i >= 0) { this.children.RemoveAt(i); if (i == 0 && this.children.Count == 0) { this.children = null; } } } else { i = -1; } } Node.parentId = Guid.Empty; Node.parent = null; if (Node.objectId != Guid.Empty) { await Database.Update(Child); this.RaiseUpdate(); await MeteringTopology.NewEvent(new NodeRemoved() { NodeId = Node.NodeId, Partition = Node.Partition, SourceId = Node.SourceId, Timestamp = DateTime.Now }); } return(i >= 0); }
internal static void UnregisterNode(MeteringNode Node) { if (Node.SourceId == MeteringTopology.SourceID && string.IsNullOrEmpty(Node.Partition)) { lock (nodes) { if (nodes.TryGetValue(Node.NodeId, out MeteringNode Node2) && Node == Node2) { nodes.Remove(Node.NodeId); } } } }
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> /// Tries to move the node down. /// </summary> /// <param name="Caller">Information about caller.</param> /// <returns>If the node was moved down.</returns> public virtual Task <bool> MoveDownAsync(RequestOrigin Caller) { MeteringNode Parent = this.Parent as MeteringNode; if (Parent == null) { return(Task.FromResult <bool>(false)); } else { return(Parent.MoveDownAsync(this, Caller)); } }
internal static MeteringNode RegisterNode(MeteringNode Node) { if (Node.SourceId == MeteringTopology.SourceID && string.IsNullOrEmpty(Node.Partition)) { lock (nodes) { if (nodes.TryGetValue(Node.NodeId, out MeteringNode Node2)) { return(Node2); } else { nodes[Node.NodeId] = Node; return(Node); } } } else { return(Node); } }
/// <summary> /// Removes a child from the node. /// </summary> /// <param name="Child">Child to remove.</param> /// <returns>If the Child node was found and removed.</returns> public virtual async Task<bool> RemoveAsync(INode Child) { MeteringNode Node = Child as MeteringNode; if (Node == null) throw new Exception("Child must be a metering node."); if (!this.childrenLoaded) await this.LoadChildren(); int i; lock (this.synchObject) { if (this.children != null) { i = this.children.IndexOf(Node); if (i >= 0) { this.children.RemoveAt(i); if (i == 0 && this.children.Count == 0) this.children = null; } } else i = -1; } Node.parentId = Guid.Empty; Node.parent = null; if (Node.objectId != Guid.Empty) { await Database.Update(Child); this.RaiseUpdate(); } return i >= 0; }
/// <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(); }