Esempio n. 1
0
        // Utility

        #region AddNodes
        private void AddNodes(
            List <CategoryNode> colNodeItemCollection,
            List <CategoryDTO> colTreeNodeCollection,
            CategoryDTO paramTreeNode)
        {
            // Get the children of the current item
            // This method may be called from the top level
            // or recuresively by one of the child items
            var ChildResults = from objNode in colNodeItemCollection
                               where objNode.ParentId == Convert.ToInt32(paramTreeNode.categoryId)
                               select objNode;

            // Loop thru each Child of the current Node
            foreach (var objChild in ChildResults)
            {
                // Create a new Node
                var objNewNode = new CategoryDTO();

                NodeDetailDTO objNewNodeDetail = new NodeDetailDTO();
                objNewNodeDetail.categoryId       = objChild.Id.ToString();
                objNewNodeDetail.CheckboxChecked  = false;
                objNewNodeDetail.selectable       = objChild.Selectable;
                objNewNodeDetail.requestorVisible = objChild.RequestorVisible;
                objNewNode.data = objNewNodeDetail;

                objNewNode.categoryId = objChild.Id.ToString();
                objNewNode.label      = objChild.NodeName;
                objNewNode.selectable = objChild.Selectable;

                // See if there is a Parent
                if (objChild.ParentId != null)
                {
                    // Set the Parent
                    objNewNode.parentId = Convert.ToInt32(objChild.ParentId);

                    // Get the Parent
                    CategoryDTO objParent =
                        colTreeNodeCollection.Where(x => x.categoryId == objChild.ParentId.ToString()).FirstOrDefault();

                    // See how many dots the Parent has
                    int CountOfParentDots = objParent.label.Count(x => x == '.');

                    // Update the label to add dots in front of the name
                    objNewNode.label = $"{AddDots(CountOfParentDots + 1)}{objChild.NodeName}";
                }
                else
                {
                    // There was no parent so don't add any dots
                    objNewNode.label = objChild.NodeName;
                }

                colTreeNodeCollection.Add(objNewNode);

                //Recursively call the AddChildren method adding all children
                AddNodes(colNodeItemCollection, colTreeNodeCollection, objNewNode);
            }
        }
Esempio n. 2
0
        private List <CategoryDTO> NewMethod()
        {
            // Collection to hold final TreeNodes
            List <CategoryDTO> colTreeNodes = new List <CategoryDTO>();

            var optionsBuilder = new DbContextOptionsBuilder <ADefHelpDeskContext>();

            optionsBuilder.UseSqlServer(GetConnectionString());

            using (var context = new ADefHelpDeskContext(optionsBuilder.Options))
            {
                // This returns all Nodes in the database
                var colNodes = (from objNode in context.AdefHelpDeskCategories
                                select new CategoryNode
                {
                    Id = objNode.CategoryId,
                    NodeName = objNode.CategoryName,
                    ParentId = objNode.ParentCategoryId
                }).OrderBy(x => x.ParentId).ThenBy(y => y.NodeName).ToList();

                // Create a '[None]' Node
                CategoryDTO objNoneNode = new CategoryDTO();

                objNoneNode.label    = "[None]";
                objNoneNode.parentId = 0;
                colTreeNodes.Add(objNoneNode);

                // Loop through Parent 'root' nodes
                // (meaning the NodeParentData is blank)
                foreach (CategoryNode objNode in colNodes
                         .Where(x => x.ParentId == null))
                {
                    // Create a new Node
                    CategoryDTO objNewNode = new CategoryDTO();

                    NodeDetailDTO objNewNodeDetail = new NodeDetailDTO();
                    objNewNodeDetail.categoryId       = objNode.Id.ToString();
                    objNewNodeDetail.CheckboxChecked  = false;
                    objNewNodeDetail.selectable       = objNode.Selectable;
                    objNewNodeDetail.requestorVisible = objNode.RequestorVisible;
                    objNewNode.data = objNewNodeDetail;

                    objNewNode.categoryId = objNode.Id.ToString();
                    objNewNode.label      = objNode.NodeName;
                    objNewNode.selectable = objNode.Selectable;

                    if (objNode.ParentId.ToString() != "")
                    {
                        objNewNode.parentId = Convert.ToInt32(objNode.ParentId);
                    }

                    colTreeNodes.Add(objNewNode);

                    // Add Nodes
                    AddNodes(colNodes, colTreeNodes, objNewNode);
                }
            }

            // This is not Queryable because we need the list
            // to stay in the correct order (by NodeParentData)
            return(colTreeNodes);
        }
        // Methods

        #region public static List<CategoryDTO> GetNodesMethod(bool UseCache, IMemoryCache _cache, string strConectionString)
        public static List <CategoryDTO> GetNodesMethod(bool UseCache, IMemoryCache _cache, string strConectionString)
        {
            // Collection to hold final TreeNodes
            List <CategoryDTO> colTreeNodes = new List <CategoryDTO>();

            if (Convert.ToBoolean(UseCache))
            {
                // Look for tree in cache
                if (_cache.TryGetValue("TreeNodes", out colTreeNodes))
                {
                    return(colTreeNodes);
                }
            }

            var optionsBuilder = new DbContextOptionsBuilder <ADefHelpDeskContext>();

            optionsBuilder.UseSqlServer(strConectionString);

            using (var context = new ADefHelpDeskContext(optionsBuilder.Options))
            {
                // This returns all Nodes in the database
                colTreeNodes = new List <CategoryDTO>();
                var colNodes = (from objNode in context.AdefHelpDeskCategories
                                select new CategoryNode
                {
                    Id = objNode.CategoryId,
                    NodeName = objNode.CategoryName,
                    ParentId = objNode.ParentCategoryId,
                    Selectable = objNode.Selectable,
                    RequestorVisible = objNode.RequestorVisible
                }).OrderBy(x => x.ParentId).ThenBy(y => y.NodeName).ToList();

                // Loop through Parent 'root' nodes
                // (meaning the NodeParentData is blank)
                foreach (CategoryNode objNode in colNodes
                         .Where(x => x.ParentId == null))
                {
                    // Create a new Node
                    CategoryDTO objNewNode = new CategoryDTO();

                    NodeDetailDTO objNewNodeDetail = new NodeDetailDTO();
                    objNewNodeDetail.categoryId       = objNode.Id.ToString();
                    objNewNodeDetail.CheckboxChecked  = false;
                    objNewNodeDetail.selectable       = objNode.Selectable;
                    objNewNodeDetail.requestorVisible = objNode.RequestorVisible;
                    objNewNode.data = objNewNodeDetail;

                    objNewNode.categoryId = objNode.Id.ToString();
                    objNewNode.label      = objNode.NodeName;
                    objNewNode.parentId   = 0;
                    objNewNode.children   = new List <CategoryDTO>();
                    objNewNode.selectable = true;

                    if (objNode.Selectable == true)
                    {
                        objNewNode.expandedIcon  = "fa fa-fw fa fa-folder-open";
                        objNewNode.collapsedIcon = "fa fa-fw fa fa-folder";
                        objNewNode.type          = "ShowCheckBox";
                    }

                    if (objNode.Selectable == false)
                    {
                        objNewNode.expandedIcon  = "fas fa-th";
                        objNewNode.collapsedIcon = "fas fa-th";
                        objNewNode.type          = "HideCheckBox";
                    }

                    colTreeNodes.Add(objNewNode);

                    // Add Child Nodes
                    AddChildren(colNodes, colTreeNodes, objNewNode);
                }
            }

            // Set cache options.
            var cacheEntryOptions = new MemoryCacheEntryOptions()
                                    // Keep in cache for this time, reset time if accessed.
                                    .SetSlidingExpiration(TimeSpan.MaxValue);

            // Save data in cache.
            _cache.Set("TreeNodes", colTreeNodes, cacheEntryOptions);

            return(colTreeNodes);
        }
        // Utility

        #region AddChildren
        private static void AddChildren(
            List <CategoryNode> colNodeItemCollection,
            List <CategoryDTO> colTreeNodeCollection,
            CategoryDTO paramTreeNode)
        {
            // Get the children of the current item
            // This method may be called from the top level
            // or recuresively by one of the child items
            var ChildResults = from objNode in colNodeItemCollection
                               where objNode.ParentId == Convert.ToInt32(paramTreeNode.categoryId)
                               select objNode;

            // Loop thru each Child of the current Node
            foreach (var objChild in ChildResults)
            {
                // Create a new Node
                var objNewNode = new CategoryDTO();

                objNewNode.categoryId = objChild.Id.ToString();
                objNewNode.label      = objChild.NodeName;
                objNewNode.parentId   = Convert.ToInt32(paramTreeNode.categoryId);
                objNewNode.children   = new List <CategoryDTO>();
                objNewNode.selectable = true;

                if (objChild.Selectable == true)
                {
                    objNewNode.expandedIcon  = "fa fa-fw fa fa-folder-open";
                    objNewNode.collapsedIcon = "fa fa-fw fa fa-folder";
                    objNewNode.type          = "ShowCheckBox";
                }

                if (objChild.Selectable == false)
                {
                    objNewNode.expandedIcon  = "fa-th";
                    objNewNode.collapsedIcon = "fa-th";
                    objNewNode.type          = "HideCheckBox";
                }

                // Search for the Node in colTreeNodeCollection
                // By looping through each 'root' Node
                // (meaning the NodeParentData is blank)
                foreach (CategoryNode objNode in colNodeItemCollection
                         .Where(x => x.ParentId == null))
                {
                    // See if Parent is in the colTreeNodeCollection
                    CategoryDTO objParent =
                        colTreeNodeCollection.Where(x => x.categoryId == objNode.Id.ToString()).FirstOrDefault();

                    if (objParent != null) // Parent exists in the colTreeNodeCollection
                    {
                        // Get the Parent Node for the current Child Node
                        CategoryDTO objParentTreeNode = objParent.Descendants()
                                                        .Where(x => x.categoryId == paramTreeNode.categoryId).FirstOrDefault();

                        if (objParentTreeNode != null)
                        {
                            // Add the Child node to the Parent
                            NodeDetailDTO objNewNodeDetail = new NodeDetailDTO();
                            objNewNodeDetail.categoryId       = objChild.Id.ToString();
                            objNewNodeDetail.CheckboxChecked  = false;
                            objNewNodeDetail.selectable       = objChild.Selectable;
                            objNewNodeDetail.requestorVisible = objChild.RequestorVisible;
                            objNewNode.data = objNewNodeDetail;

                            objParentTreeNode.children.Add(objNewNode);
                        }
                    }
                }

                //Recursively call the AddChildren method adding all children
                AddChildren(colNodeItemCollection, colTreeNodeCollection, objNewNode);
            }
        }