public void InsertNode([FromBody] NodeDTO node) { var parentNode = new ParentNodeDTO(); try { //Fetch the subtree of the Sponsor //A sponsor cannot be the direct parent of the new user when max number of children has been reached var subTreeList = _hierarchyBL.GetSubTreeDepth(node.SponsorId, HierarchyType.BinaryTree); if (subTreeList.Count > 1) { //Traverse the tree and search for the appropriate parent node parentNode = _binaryTreeBl.GetLogicalParentNode(subTreeList); } else if (subTreeList.Count == 1) { parentNode.NodeId = subTreeList.First().UserID; } else { //Master Parent Node Insert _binaryTreeBl.InsertMasterParentNode(node); return; } //Insert Node _binaryTreeBl.InsertNode(node, parentNode); } catch (Exception ex) { throw ex; } }
public void InsertNode([FromBody] ForcedMatrixNodeDTO node) { var parentNode = new ParentNodeDTO(); try { //Fetch the subtree of the Sponsor //A sponsor cannot be the direct parent of the new user when max number of children has been reached var subTreeList = _hierarchyBL.GetSubTreeDepth(node.SponsorId, HierarchyType.ForcedMatrix, node.ChildLimit, node.LevelLimit); if (subTreeList.Count != 0) { //Traverse the tree and search for the appropriate parent node parentNode = _forcedMatrixBL.GetLogicalParentNode(subTreeList, node.ChildLimit, node.LevelLimit); } else { //Master Parent Node Insert _forcedMatrixBL.InsertMasterParentNode(node); return; } //Insert Node _forcedMatrixBL.InsertNode(node, parentNode); } catch (ArgumentOutOfRangeException ex) { throw ex; } catch (Exception ex) { throw ex; } }
public void InsertNode(NodeDTO node, ParentNodeDTO parentNode) { if (parentNode.HasNoLeafNode) { _ctx.InsertLeafNode(node.NodeId, parentNode.NodeId, node.SponsorId, HierarchyType.BinaryTree.ToString()); } else { _ctx.InsertNode(node.NodeId, parentNode.NodeId, node.SponsorId, HierarchyType.BinaryTree.ToString()); } }
public void InsertNode(ForcedMatrixNodeDTO node, ParentNodeDTO parentNode) { var tableName = $"{HierarchyType.ForcedMatrix}{node.ChildLimit}x{node.LevelLimit}"; if (parentNode.HasNoLeafNode) { _ctx.InsertLeafNode(node.NodeId, parentNode.NodeId, node.SponsorId, tableName); } else { _ctx.InsertNode(node.NodeId, parentNode.NodeId, node.SponsorId, tableName); } }
public ParentNodeDTO GetLogicalParentNode(List <SubTreeDTO> subTreeList, int childLimit, int levelLimit) { var logicalParent = new ParentNodeDTO(); var maxDepth = subTreeList.Max(x => x.Depth); int currentDepth = maxDepth; int currentNode = 0; //Fetch Leaf Nodes from Database var leafNodes = _hierarchyBL.GetLeafNodes(HierarchyType.ForcedMatrix, childLimit, levelLimit).OrderBy(x => x.RGT).ToList(); var leafNodeData = from s in subTreeList join l in leafNodes on s.UserID equals l.USERID orderby s.Depth, l.RGT select new { UserId = s.UserID, Depth = s.Depth, LFT = l.LFT, RGT = l.RGT, ParentId = l.PARENTID }; currentDepth = leafNodeData.First().Depth; currentNode = leafNodeData.First().UserId; //Set current node default for parent node for now logicalParent.NodeId = currentNode; if (currentDepth < maxDepth) { //Retrieve Nodes from current depth ordered by left var curDepthNodes = subTreeList.Where(x => x.Depth == currentDepth).OrderBy(x => x.LFT); foreach (var node in curDepthNodes) { var childNodeCount = subTreeList.Where(x => x.PARENTID == node.UserID).Count(); if (childNodeCount < childLimit) { logicalParent.NodeId = node.UserID; break; } } } else { //Traverse Tree in progressive top to bottom - left to right manner for (int i = 0; i < maxDepth; i++) { //Retrieve Nodes from upper depth ordered by left var prevDepthNodes = subTreeList.Where(x => x.Depth == i).OrderBy(x => x.LFT); foreach (var node in prevDepthNodes) { var childNodeCount = subTreeList.Where(x => x.PARENTID == node.UserID).Count(); if (childNodeCount < childLimit) { logicalParent.NodeId = node.UserID; return(logicalParent); } } } //If parent node was not replaced, another level will be added to the matrix //Check for level limit if (logicalParent.NodeId == currentNode) { var fullMatrixDepth = _hierarchyBL.GetMasterParentPath(currentNode, HierarchyType.ForcedMatrix, childLimit, levelLimit).Count(); //Get the true depth of node relative to the full tree if (fullMatrixDepth >= levelLimit + 1) { throw new ArgumentOutOfRangeException("Max Forced Matrix level has been reached"); } } } return(logicalParent); }
public ParentNodeDTO GetLogicalParentNode(List <SubTreeDTO> subTreeList) { var logicalParent = new ParentNodeDTO(); var maxDepth = subTreeList.Max(x => x.Depth); int currentDepth = maxDepth; int currentNode = 0; //Fetch Leaf Nodes from Database var leafNodes = _hierarchyBL.GetLeafNodes(HierarchyType.BinaryTree).OrderBy(x => x.RGT).ToList(); var leafNodeData = from s in subTreeList join l in leafNodes on s.UserID equals l.USERID orderby s.Depth, l.RGT select new { UserId = s.UserID, Depth = s.Depth, LFT = l.LFT, RGT = l.RGT, ParentId = l.PARENTID }; currentDepth = leafNodeData.First().Depth; currentNode = leafNodeData.First().UserId; //Set current node default for parent node for now logicalParent.NodeId = currentNode; if (currentDepth < maxDepth) { //Retrieve Nodes from current depth ordered by left var curDepthNodes = subTreeList.Where(x => x.Depth == currentDepth).OrderBy(x => x.LFT); foreach (var node in curDepthNodes) { var childNodeCount = subTreeList.Where(x => x.PARENTID == node.UserID).Count(); if (childNodeCount < (int)ChildNodeLimit.BinaryTree) { logicalParent.NodeId = node.UserID; break; } } } else { //Traverse Tree in progressive top to bottom - left to right manner for (int i = 0; i < maxDepth; i++) { //Retrieve Nodes from current depth ordered by left var prevDepthNodes = subTreeList.Where(x => x.Depth == i).OrderBy(x => x.LFT); foreach (var node in prevDepthNodes) { var childNodeCount = subTreeList.Where(x => x.PARENTID == node.UserID).Count(); if (childNodeCount < (int)ChildNodeLimit.BinaryTree) { logicalParent.NodeId = node.UserID; break; } } } } return(logicalParent); }