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); }