public async Task<TreeNode<NavigationNode>> BuildTree(
            NavigationTreeBuilderService service)
        {
            // ultimately we will need to cache sitemap per site

            if (rootNode == null)
            {
                
                //await cache.ConnectAsync();
                byte[] bytes = await cache.GetAsync(cacheKey);
                if (bytes != null)
                {
                    string json = Encoding.UTF8.GetString(bytes);
                    rootNode = BuildTreeFromJson(json);
                }
                else
                {
                    rootNode = await BuildTree();
                    string json = rootNode.ToJsonCompact();

                    await cache.SetAsync(
                                        cacheKey,
                                        Encoding.UTF8.GetBytes(json),
                                        new DistributedCacheEntryOptions().SetSlidingExpiration(
                                            TimeSpan.FromSeconds(100))
                                            );
                }
            }

            return rootNode;
        }
        public async Task<TreeNode<NavigationNode>> BuildTree(
            NavigationTreeBuilderService service)
        {
           
            if (rootNode == null)
            { 
                rootNode = await BuildTreeInternal(service);  
            }

            return rootNode;
        }
 public NavigationTreeSiteMapNodeService(
     NavigationTreeBuilderService siteMapTreeBuilder,
     IUrlHelperFactory urlHelperFactory,
     IActionContextAccessor actionContextAccesor,
     IHttpContextAccessor contextAccessor,
     ILogger<NavigationTreeSiteMapNodeService> logger)
 {
     this.siteMapTreeBuilder = siteMapTreeBuilder;
     this.urlHelperFactory = urlHelperFactory;
     this.actionContextAccesor = actionContextAccesor;
     this.contextAccessor = contextAccessor; 
     log = logger;
 }
 public NavigationViewComponent(
     NavigationTreeBuilderService siteMapTreeBuilder,
     IEnumerable <INavigationNodePermissionResolver> permissionResolvers,
     IEnumerable <IFindCurrentNode> nodeFinders,
     IUrlHelperFactory urlHelperFactory,
     IActionContextAccessor actionContextAccesor,
     INodeUrlPrefixProvider prefixProvider,
     ILogger <NavigationViewComponent> logger)
 {
     builder = siteMapTreeBuilder;
     this.permissionResolvers  = permissionResolvers;
     this.nodeFinders          = nodeFinders;
     this.urlHelperFactory     = urlHelperFactory;
     this.actionContextAccesor = actionContextAccesor;
     if (prefixProvider == null)
     {
         this.prefixProvider = new DefaultNodeUrlPrefixProvider();
     }
     else
     {
         this.prefixProvider = prefixProvider;
     }
     log = logger;
 }
Esempio n. 5
0
        private async Task <TreeNode <NavigationNode> > BuildTreeInternal(NavigationTreeBuilderService service)
        {
            string filePath = ResolveFilePath();

            if (!File.Exists(filePath))
            {
                log.LogError("unable to build navigation tree, could not find the file " + filePath);

                NavigationNode rootNav = new NavigationNode();
                rootNav.Key = "filenotfound";
                //rootNav.IsRootNode = true;
                rootNav.Text = filePath + " not found";
                rootNav.Url  = "/";
                var treeRoot = new TreeNode <NavigationNode>(rootNav);

                return(treeRoot);
            }

            string xml;

            using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                using (StreamReader streamReader = new StreamReader(fileStream))
                {
                    xml = await streamReader.ReadToEndAsync();
                }
            }

            XDocument doc = XDocument.Parse(xml);

            NavigationTreeXmlConverter converter = new NavigationTreeXmlConverter();

            TreeNode <NavigationNode> result = await converter.FromXml(doc, service).ConfigureAwait(false);

            return(result);
        }
 public NavigationViewComponent(
     NavigationTreeBuilderService siteMapTreeBuilder,
     IEnumerable<INavigationNodePermissionResolver> permissionResolvers,
     IEnumerable<IFindCurrentNode> nodeFinders,
     IUrlHelperFactory urlHelperFactory,
     IActionContextAccessor actionContextAccesor,
     INodeUrlPrefixProvider prefixProvider,
     ILogger<NavigationViewComponent> logger)
 {
     builder = siteMapTreeBuilder;
     this.permissionResolvers = permissionResolvers;
     this.nodeFinders = nodeFinders;
     this.urlHelperFactory = urlHelperFactory;
     this.actionContextAccesor = actionContextAccesor;
     if (prefixProvider == null)
     {
         this.prefixProvider = new DefaultNodeUrlPrefixProvider();
     }
     else
     {
         this.prefixProvider = prefixProvider;
     }
     log = logger;
 }
        private async Task<TreeNode<NavigationNode>> BuildTreeInternal(NavigationTreeBuilderService service)
        {
            string filePath = ResolveFilePath();

            if (!File.Exists(filePath))
            {
                log.LogError("unable to build navigation tree, could not find the file " + filePath);

                NavigationNode rootNav = new NavigationNode();
                rootNav.Key = "filenotfound";
                rootNav.IsRootNode = true;
                rootNav.Text = filePath + " not found";
                rootNav.Url = "/";
                var treeRoot = new TreeNode<NavigationNode>(rootNav);

                return treeRoot;
            }

            string xml;
            using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                using (StreamReader streamReader = new StreamReader(fileStream))
                {
                    xml = await streamReader.ReadToEndAsync();
                }
            }

            XDocument doc = XDocument.Parse(xml);

            NavigationTreeXmlConverter converter = new NavigationTreeXmlConverter();

            TreeNode<NavigationNode> result = await converter.FromXml(doc, service).ConfigureAwait(false);

            return result;

        }
        private NavigationNode BuildNavNode(
            XElement xmlNode,
            NavigationTreeBuilderService service
            )
        {
            NavigationNode navNode = new NavigationNode();

            //var tb = xmlNode.Attribute("TreeBuilderName");
            //if (tb != null)
            //{
            //   return await service.GetTree(tb.Value).ConfigureAwait(false)
            //}

            var a = xmlNode.Attribute("key");

            if (a != null)
            {
                navNode.Key = a.Value;
            }

            //a = xmlNode.Attribute("parentKey");
            //if (a != null) { navNode.ParentKey = a.Value; }

            a = xmlNode.Attribute("controller");
            if (a != null)
            {
                navNode.Controller = a.Value;
            }

            a = xmlNode.Attribute("action");
            if (a != null)
            {
                navNode.Action = a.Value;
            }

            a = xmlNode.Attribute("area");
            if (a != null)
            {
                navNode.Area = a.Value;
            }

            a = xmlNode.Attribute("page");
            if (a != null)
            {
                navNode.Page = a.Value;
            }

            a = xmlNode.Attribute("namedRoute");
            if (a == null)
            {
                a = xmlNode.Attribute("named-route");           //this is not consistent was a mistake
            }
            if (a != null)
            {
                navNode.NamedRoute = a.Value;
            }

            a = xmlNode.Attribute("text");
            if (a != null)
            {
                navNode.Text = a.Value;
            }

            a = xmlNode.Attribute("title");
            if (a != null)
            {
                navNode.Title = a.Value;
            }

            a = xmlNode.Attribute("url");
            if (a != null)
            {
                navNode.Url = a.Value;
            }
            //else
            //{
            //    navNode.Url = navNode.ResolveUrl(); // this smells bad
            //}

            //a = xmlNode.Attribute("isRootNode");
            //if (a != null) { navNode.IsRootNode = Convert.ToBoolean(a.Value); }

            a = xmlNode.Attribute("excludeFromSearchSiteMap");
            if (a != null)
            {
                navNode.ExcludeFromSearchSiteMap = Convert.ToBoolean(a.Value);
            }



            a = xmlNode.Attribute("hideFromAuthenticated");
            if (a != null)
            {
                navNode.HideFromAuthenticated = Convert.ToBoolean(a.Value);
            }

            a = xmlNode.Attribute("hideFromAnonymous");
            if (a != null)
            {
                navNode.HideFromAnonymous = Convert.ToBoolean(a.Value);
            }

            //a = xmlNode.Attribute("includeAmbientValuesInUrl");
            //if (a != null) { navNode.IncludeAmbientValuesInUrl = Convert.ToBoolean(a.Value); }

            //a = xmlNode.Attribute("resourceName");
            //if (a != null) { navNode.ResourceName = a.Value; }

            //a = xmlNode.Attribute("resourceTextKey");
            //if (a != null) { navNode.ResourceTextKey = a.Value; }

            //a = xmlNode.Attribute("resourceTitleKey");
            //if (a != null) { navNode.ResourceTitleKey = a.Value; }

            a = xmlNode.Attribute("preservedRouteParameters");
            if (a != null)
            {
                navNode.PreservedRouteParameters = a.Value;
            }

            a = xmlNode.Attribute("componentVisibility");
            if (a != null)
            {
                navNode.ComponentVisibility = a.Value;
            }

            a = xmlNode.Attribute("authorizationPolicy");
            if (a != null)
            {
                navNode.AuthorizationPolicy = a.Value;
            }

            a = xmlNode.Attribute("viewRoles");
            if (a != null)
            {
                navNode.ViewRoles = a.Value;
            }

            a = xmlNode.Attribute("customData");
            if (a != null)
            {
                navNode.CustomData = a.Value;
            }


            a = xmlNode.Attribute("isClickable");
            if (a != null)
            {
                navNode.IsClickable = Convert.ToBoolean(a.Value);
            }

            a = xmlNode.Attribute("iconCssClass");
            if (a != null)
            {
                navNode.IconCssClass = a.Value;
            }

            a = xmlNode.Attribute("cssClass");
            if (a != null)
            {
                navNode.CssClass = a.Value;
            }

            a = xmlNode.Attribute("menuDescription");
            if (a != null)
            {
                navNode.MenuDescription = a.Value;
            }

            a = xmlNode.Attribute("target");
            if (a != null)
            {
                navNode.Target = a.Value;
            }

            var da = xmlNode.Element(XName.Get("DataAttributes"));

            if (da != null)
            {
                foreach (XElement childNode in da.Elements(XName.Get("DataAttribute")))
                {
                    var key = childNode.Attribute("attribute");
                    var val = childNode.Attribute("value");
                    if ((key != null) && (val != null))
                    {
                        var att = new DataAttribute();
                        att.Attribute = key.Value;
                        att.Value     = val.Value;
                        navNode.DataAttributes.Add(att);
                    }
                }
            }



            return(navNode);
        }
        public async Task <TreeNode <NavigationNode> > FromXml(
            XDocument xml,
            NavigationTreeBuilderService service
            )
        {
            if (xml.Root.Name != "NavNode")
            {
                throw new ArgumentException("Expected NavNode");
            }

            TreeNode <NavigationNode> treeRoot;
            var builderName = GetNodeBuilderName(xml.Root);

            if (string.IsNullOrEmpty(builderName))
            {
                NavigationNode rootNav = BuildNavNode(xml.Root, service);
                treeRoot = new TreeNode <NavigationNode>(rootNav);
            }
            else
            {
                var otherBuilderRoot = await service.GetTree(builderName).ConfigureAwait(false);

                if (otherBuilderRoot.Value.ChildContainerOnly)
                {
                    NavigationNode rootNav = BuildNavNode(xml.Root, service);
                    treeRoot = new TreeNode <NavigationNode>(rootNav);
                    foreach (var firstChild in otherBuilderRoot.Children)
                    {
                        treeRoot.AddChild(firstChild);
                    }
                }
                else
                {
                    treeRoot = otherBuilderRoot;
                }
            }

            var childrenNode = xml.Root.Elements(XName.Get("Children"));

            if (childrenNode != null)
            {
                foreach (XElement childNode in childrenNode.Elements(XName.Get("NavNode")))
                {
                    var childBuilder = GetNodeBuilderName(childNode);
                    if (string.IsNullOrEmpty(childBuilder))
                    {
                        await AddChildNode(treeRoot, childNode, service).ConfigureAwait(false);
                    }
                    else
                    {
                        var appendToBuilderNode = AppendToBuilderNode(childNode);
                        var childTreeRoot       = await service.GetTree(childBuilder).ConfigureAwait(false);

                        if (appendToBuilderNode)
                        {
                            var builderNode = BuildNavNode(childNode, service);
                            var bt          = treeRoot.AddChild(builderNode);
                            foreach (var subChild in childTreeRoot.Children)
                            {
                                bt.AddChild(subChild);
                            }
                        }
                        else
                        {
                            if (childTreeRoot.Value.ChildContainerOnly)
                            {
                                foreach (var subChild in childTreeRoot.Children)
                                {
                                    treeRoot.AddChild(subChild);
                                }
                            }
                            else
                            {
                                treeRoot.AddChild(childTreeRoot);
                            }
                        }
                    }
                }
            }



            //foreach (XElement childrenNode in xml.Root.Elements(XName.Get("Children")))
            //{
            //    foreach (XElement childNode in childrenNode.Elements(XName.Get("NavNode")))
            //    {
            //        var childBuilder = GetNodeBuilderName(childNode);
            //        if(string.IsNullOrEmpty(childBuilder))
            //        {
            //            await AddChildNode(treeRoot, childNode, service).ConfigureAwait(false);
            //        }
            //        else
            //        {
            //            var child = await service.GetTree(childBuilder).ConfigureAwait(false);
            //            if(child.Value.ChildContainerOnly)
            //            {
            //                foreach(var subChild in child.Children)
            //                {
            //                    treeRoot.AddChild(subChild);
            //                }
            //            }
            //            else
            //            {
            //                treeRoot.AddChild(child);
            //            }

            //        }


            //    }

            //}

            return(treeRoot);
        }
        private NavigationNode BuildNavNode(
            XElement xmlNode,
            NavigationTreeBuilderService service
            )
        {
            NavigationNode navNode = new NavigationNode();

            //var tb = xmlNode.Attribute("TreeBuilderName");
            //if (tb != null)
            //{
            //   return await service.GetTree(tb.Value).ConfigureAwait(false)
            //}

            var a = xmlNode.Attribute("key");
            if(a != null) {  navNode.Key = a.Value; }

            a = xmlNode.Attribute("parentKey");
            if (a != null) { navNode.ParentKey = a.Value; }

            a = xmlNode.Attribute("controller");
            if (a != null) { navNode.Controller = a.Value; }

            a = xmlNode.Attribute("action");
            if (a != null) { navNode.Action = a.Value; }

            a = xmlNode.Attribute("area");
            if (a != null) { navNode.Area = a.Value; }

            a = xmlNode.Attribute("namedRoute");
            if(a == null) a = xmlNode.Attribute("named-route"); //this is not consistent was a mistake
            if (a != null) { navNode.NamedRoute = a.Value; }

            a = xmlNode.Attribute("text");
            if (a != null) { navNode.Text = a.Value; }

            a = xmlNode.Attribute("title");
            if (a  != null) { navNode.Title = a.Value; }

            a = xmlNode.Attribute("url");
            if (a != null) { navNode.Url = a.Value; }
            else
            {
                navNode.Url = navNode.ResolveUrl(); // this smells bad
            }

            a = xmlNode.Attribute("isRootNode");
            if (a != null) { navNode.IsRootNode = Convert.ToBoolean(a.Value); }

            a = xmlNode.Attribute("hideFromAuthenticated");
            if (a != null) { navNode.HideFromAuthenticated = Convert.ToBoolean(a.Value); }

            a = xmlNode.Attribute("hideFromAnonymous");
            if (a != null) { navNode.HideFromAnonymous = Convert.ToBoolean(a.Value); }

            //a = xmlNode.Attribute("includeAmbientValuesInUrl");
            //if (a != null) { navNode.IncludeAmbientValuesInUrl = Convert.ToBoolean(a.Value); }

            //a = xmlNode.Attribute("resourceName");
            //if (a != null) { navNode.ResourceName = a.Value; }

            //a = xmlNode.Attribute("resourceTextKey");
            //if (a != null) { navNode.ResourceTextKey = a.Value; }

            //a = xmlNode.Attribute("resourceTitleKey");
            //if (a != null) { navNode.ResourceTitleKey = a.Value; }

            a = xmlNode.Attribute("preservedRouteParameters");
            if (a != null) { navNode.PreservedRouteParameters = a.Value; }

            a = xmlNode.Attribute("componentVisibility");
            if (a != null) { navNode.ComponentVisibility = a.Value; }

            a = xmlNode.Attribute("viewRoles");
            if (a != null) { navNode.ViewRoles = a.Value; }

            a = xmlNode.Attribute("customData");
            if (a != null) { navNode.CustomData = a.Value; }


            a = xmlNode.Attribute("isClickable");
            if (a != null) { navNode.IsClickable = Convert.ToBoolean(a.Value); }

            a = xmlNode.Attribute("iconCssClass");
            if (a != null) { navNode.IconCssClass = a.Value; }

            a = xmlNode.Attribute("cssClass");
            if (a != null) { navNode.CssClass = a.Value; }

            a = xmlNode.Attribute("menuDescription");
            if (a != null) { navNode.MenuDescription = a.Value; }

            a = xmlNode.Attribute("target");
            if (a != null) { navNode.Target = a.Value; }

            var da = xmlNode.Element(XName.Get("DataAttributes"));
            if (da != null)
            {
                foreach (XElement childNode in da.Elements(XName.Get("DataAttribute")))
                {
                    var key = childNode.Attribute("attribute");
                    var val = childNode.Attribute("value");
                    if ((key != null) && (val != null))
                    {
                        var att = new DataAttribute();
                        att.Attribute = key.Value;
                        att.Value = val.Value;
                        navNode.DataAttributes.Add(att);
                    }


                }

            }




            return navNode;
        }
        private async Task AddChildNode(
            TreeNode<NavigationNode> node, 
            XElement xmlNode,
            NavigationTreeBuilderService service
            )
        {
            NavigationNode navNode = BuildNavNode(xmlNode, service);
            TreeNode<NavigationNode> navNodeT = node.AddChild(navNode);

            foreach (XElement childrenNode in xmlNode.Elements(XName.Get("Children")))
            {
                foreach (XElement childNode in childrenNode.Elements(XName.Get("NavNode")))
                {
                    var childBuilder = GetNodeBuilderName(childNode);
                    if (string.IsNullOrEmpty(childBuilder))
                    {
                        await AddChildNode(navNodeT, childNode, service).ConfigureAwait(false); //recursion
                    }
                    else
                    {
                        var child = await service.GetTree(childBuilder).ConfigureAwait(false);
                        if (child.Value.ChildContainerOnly)
                        {
                            foreach (var subChild in child.Children)
                            {
                                navNodeT.AddChild(subChild);
                            }
                        }
                        else
                        {
                            navNodeT.AddChild(child);
                        }

                        
                    }

                       
                }
            }

            

        }
        public async Task<TreeNode<NavigationNode>> FromXml(
            XDocument xml,
            NavigationTreeBuilderService service
            )
        { 
            if(xml.Root.Name != "NavNode") { throw new ArgumentException("Expected NavNode"); }

            TreeNode<NavigationNode> treeRoot;
            var builderName = GetNodeBuilderName(xml.Root);
            if (string.IsNullOrEmpty(builderName))
            {
                NavigationNode rootNav = BuildNavNode(xml.Root, service);
                treeRoot = new TreeNode<NavigationNode>(rootNav);
               
            }
            else
            {
                var otherBuilderRoot = await service.GetTree(builderName).ConfigureAwait(false);
                if(otherBuilderRoot.Value.ChildContainerOnly)
                {
                    NavigationNode rootNav = BuildNavNode(xml.Root, service);
                    treeRoot = new TreeNode<NavigationNode>(rootNav);
                    foreach(var firstChild in otherBuilderRoot.Children)
                    {
                        treeRoot.AddChild(firstChild);
                    }

                }
                else
                {
                    treeRoot = otherBuilderRoot;
                }
            }

            var childrenNode = xml.Root.Elements(XName.Get("Children"));
            if (childrenNode != null)
            {
                foreach (XElement childNode in childrenNode.Elements(XName.Get("NavNode")))
                {
                    var childBuilder = GetNodeBuilderName(childNode);
                    if (string.IsNullOrEmpty(childBuilder))
                    {
                        await AddChildNode(treeRoot, childNode, service).ConfigureAwait(false);
                    }
                    else
                    {
                        var child = await service.GetTree(childBuilder).ConfigureAwait(false);
                        if (child.Value.ChildContainerOnly)
                        {
                            foreach (var subChild in child.Children)
                            {
                                treeRoot.AddChild(subChild);
                            }
                        }
                        else
                        {
                            treeRoot.AddChild(child);
                        }

                    }
                }
            }



            //foreach (XElement childrenNode in xml.Root.Elements(XName.Get("Children")))
            //{
            //    foreach (XElement childNode in childrenNode.Elements(XName.Get("NavNode")))
            //    {
            //        var childBuilder = GetNodeBuilderName(childNode);
            //        if(string.IsNullOrEmpty(childBuilder))
            //        {
            //            await AddChildNode(treeRoot, childNode, service).ConfigureAwait(false);
            //        }
            //        else
            //        {
            //            var child = await service.GetTree(childBuilder).ConfigureAwait(false);
            //            if(child.Value.ChildContainerOnly)
            //            {
            //                foreach(var subChild in child.Children)
            //                {
            //                    treeRoot.AddChild(subChild);
            //                }
            //            }
            //            else
            //            {
            //                treeRoot.AddChild(child);
            //            }

            //        }


            //    }

            //}

            return treeRoot;
        }