public TaxonomyNode GetSitemapItem(ILocalization localization)
 {
     try
     {
         var ns            = localization.Namespace();
         var publicationId = localization.PublicationId();
         var tree          = SitemapHelpers.GetEntireTree(Client, ns, publicationId, _descendantDepth);
         return((TaxonomyNode)SitemapHelpers.Convert(tree));
     }
     catch (GraphQLClientException e)
     {
         const string msg = "PCA client returned an unexpected response when retrieving sitemap items for sitemap.";
         Log.Error(msg, e.Message);
         throw new DxaException(msg, e);
     }
     catch (ApiException)
     {
         return(null);
     }
 }
        /// <summary>
        /// Replicate the behavior of the CIL implementation when it comes to requesting items rooted at
        /// a point with a specific depth level + include ancestors
        /// </summary>
        protected List <ISitemapItem> GetChildSitemapItemsInternal(string parentSitemapItemId, ILocalization localization, bool includeAncestors, int descendantLevels)
        {
            try
            {
                int pubId           = localization.PublicationId();
                ContentNamespace ns = localization.Namespace();

                // Check if we are requesting the entire tree
                if (descendantLevels == -1)
                {
                    var tree0 = SitemapHelpers.GetEntireTree(Client, ns, pubId, parentSitemapItemId, includeAncestors, _descendantDepth);
                    // If parent node not specified we return entire tree
                    if (parentSitemapItemId == null)
                    {
                        return(tree0);
                    }
                    // root node specified so return the direct children of this node
                    List <ISitemapItem> items0 = new List <ISitemapItem>();
                    foreach (TaxonomySitemapItem x in tree0.OfType <TaxonomySitemapItem>())
                    {
                        items0.AddRange(x.Items);
                    }
                    items0 = items0.OrderBy(i => i.OriginalTitle).ToList();
                    return(items0);
                }

                if (parentSitemapItemId == null && descendantLevels > 0)
                {
                    descendantLevels--;
                }

                if (parentSitemapItemId == null)
                {
                    // Requesting from root so just return descendants from root
                    var tree0 = Client.GetSitemapSubtree(ns, pubId, null, descendantLevels, includeAncestors ? Ancestor.INCLUDE : Ancestor.NONE, null);
                    return(tree0.Cast <ISitemapItem>().ToList());
                }

                if (includeAncestors)
                {
                    // We are looking for a particular item, we need to request the entire
                    // subtree first
                    var subtree0 = SitemapHelpers.GetEntireTree(Client, ns, pubId, parentSitemapItemId, true, _descendantDepth);

                    // Prune descendants from our deseried node
                    ISitemapItem node = SitemapHelpers.FindNode(subtree0, parentSitemapItemId);
                    SitemapHelpers.Prune(node, 0, descendantLevels);
                    return(subtree0);
                }

                var tree = Client.GetSitemapSubtree(ns, pubId, parentSitemapItemId, descendantLevels, Ancestor.NONE, null);
                List <ISitemapItem> items = new List <ISitemapItem>();
                foreach (TaxonomySitemapItem x in tree.Where(x => x.Items != null))
                {
                    items.AddRange(x.Items);
                }
                items = items.OrderBy(i => i.OriginalTitle).ToList();
                return(items);
            }
            catch (GraphQLClientException e)
            {
                const string msg =
                    "PCA client returned an unexpected response when retrieving child sitemap items for sitemap id {0}.";
                Log.Error(msg, parentSitemapItemId, e.Message);
                throw new DxaException(string.Format(msg, parentSitemapItemId), e);
            }
            catch (ApiException)
            {
            }
            return(new List <ISitemapItem>());
        }
 public SitemapItem[] GetChildSitemapItems(string parentSitemapItemId, ILocalization localization, bool includeAncestors, int descendantLevels)
 => SitemapHelpers.Convert(
     GetChildSitemapItemsInternal(
         parentSitemapItemId, localization, includeAncestors, descendantLevels
         )
     );