Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update BreadcrumbViewService to use SitemapNavigator #338

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@ protected HeaderBaseController(

public virtual ActionResult PageHeader()
{
var page = PageService.GetPage(SitemapNavigator.CurrentPageId);
var canonicalUrl = page != null ? UrlUtils.ToAbsolute(PageService.GetPageUrl(page.Id)) : string.Empty;

var pageHeaderViewModel = new PageHeaderViewModel
using (var connection = new DataConnection())
{
PageTitle = page.Title,
NoIndex = string.IsNullOrWhiteSpace(canonicalUrl),
CanonicalUrl = canonicalUrl
};
var currentPageNode = connection.SitemapNavigator.CurrentPageNode;
var pageUrl = currentPageNode?.Url;
var canonicalUrl = !string.IsNullOrEmpty(pageUrl) ? UrlUtils.ToAbsolute(pageUrl) : string.Empty;
var pageHeaderViewModel = new PageHeaderViewModel
{
PageTitle = currentPageNode?.Title,
NoIndex = string.IsNullOrWhiteSpace(canonicalUrl),
CanonicalUrl = canonicalUrl
};

return View("PageHeader", pageHeaderViewModel);
return View("PageHeader", pageHeaderViewModel);
}
}

protected virtual bool IsPageIndexed()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@
<Compile Include="Services\DataQuery\IDataQueryService.cs" />
<Compile Include="Services\Cache\ICacheService.cs" />
<Compile Include="Services\Cache\ICacheStore.cs" />
<Compile Include="Services\DataConnectionService.cs" />
<Compile Include="Services\IDataConnectionService.cs" />
<Compile Include="Services\IScheduler.cs" />
<Compile Include="Services\PreviewMode\PreviewModeService.cs" />
<Compile Include="Services\PreviewMode\IPreviewModeService.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/Orckestra.Composer.CompositeC1/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ private void RegisterDependencies(IComposerHost host)
host.Register<CultureService, ICultureService>(ComponentLifestyle.Singleton);
host.Register<GoogleAnalyticsNavigationUrlProvider>();
host.Register<NavigationMapper, INavigationMapper>();
host.Register<DataConnectionService, IDataConnectionService>(ComponentLifestyle.Singleton);

host.Register<SearchUrlProvider, ISearchUrlProvider>();
host.Register<CategoryBrowsingUrlProvider, ICategoryBrowsingUrlProvider>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using Composite.Data;
using Composite.Data.Types;
using Orckestra.Composer.Services.Breadcrumb;
using Orckestra.Composer.ViewModels.Breadcrumb;
Expand All @@ -13,60 +15,67 @@ public class BreadcrumbViewService : IBreadcrumbViewService
{
protected IPageService _pageService;
protected ISiteConfiguration SiteConfiguration { get; private set; }
protected IDataConnectionService DataConnectionService { get; private set; }

public BreadcrumbViewService(IPageService pageService, ISiteConfiguration siteConfiguration)
public BreadcrumbViewService(IPageService pageService, ISiteConfiguration siteConfiguration, IDataConnectionService dataConnectionService)
{
_pageService = pageService ?? throw new ArgumentNullException(nameof(pageService));
SiteConfiguration = siteConfiguration;
DataConnectionService = dataConnectionService;
}

public virtual BreadcrumbViewModel CreateBreadcrumbViewModel(GetBreadcrumbParam param)
{
if (param == null) { throw new ArgumentNullException(nameof(param)); }

var pageId = new Guid(param.CurrentPageId);
var page = _pageService.GetPage(pageId, param.CultureInfo) ??
throw new InvalidOperationException("Could not find any page matching this ID.");

var breadcrumbViewModel = new BreadcrumbViewModel
using (var connection = DataConnectionService.GetDataConnection(param.CultureInfo))
{
ActivePageName = HttpUtility.HtmlEncode(page.MenuTitle),
Items = CreateBreadcrumbItems(param, page)
};

return breadcrumbViewModel;
var page = connection.GetPageNodeById(pageId) ?? throw new InvalidOperationException("Could not find any page matching this ID."); ;
var breadcrumbViewModel = new BreadcrumbViewModel
{
ActivePageName = HttpUtility.HtmlEncode(page.MenuTitle),
Items = CreateBreadcrumbItems(param, page, connection)
};
return breadcrumbViewModel;
}
}

protected virtual List<BreadcrumbItemViewModel> CreateBreadcrumbItems(GetBreadcrumbParam param, IPage page)
protected virtual List<BreadcrumbItemViewModel> CreateBreadcrumbItems(GetBreadcrumbParam param, PageNode page, IDataConnectionAdapter connection)
{
var breadcrumbStack = new Stack<BreadcrumbItemViewModel>();
var parentPageId = _pageService.GetParentPageId(page);
var parentPageId = page.ParentPage?.Id ?? Guid.Empty;

while (parentPageId != Guid.Empty)
{
var parentPage = _pageService.GetPage(parentPageId, param.CultureInfo);

var itemVM = CreateParentPageItem(parentPage);
breadcrumbStack.Push(itemVM);

parentPageId = _pageService.GetParentPageId(parentPage);
var parentPage = connection.GetPageNodeById(parentPageId);
if (parentPage != null)
{
var itemVM = CreateBreadcrumbItemViewModel(parentPage);
breadcrumbStack.Push(itemVM);
parentPageId = parentPage.ParentPage?.Id ?? Guid.Empty;
}
else
{
parentPageId = Guid.Empty;
}
}

var items = UnrollStack(breadcrumbStack).ToList();
return items;
}

protected virtual BreadcrumbItemViewModel CreateParentPageItem(IPage parentPage)
protected virtual BreadcrumbItemViewModel CreateBreadcrumbItemViewModel(PageNode page)
{
var itemVM = new BreadcrumbItemViewModel
{
DisplayName = parentPage.MenuTitle
DisplayName = page.MenuTitle
};

var pagesConfiguration = SiteConfiguration.GetPagesConfiguration();
if (pagesConfiguration!= null && parentPage.PageTypeId != pagesConfiguration.FolderId)
if (pagesConfiguration != null && page.Page.PageTypeId != pagesConfiguration.FolderId)
{
itemVM.Url = _pageService.GetPageUrl(parentPage);
itemVM.Url = page.Url;
}

return itemVM;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Composite.Data;
using System;
using System.Globalization;

namespace Orckestra.Composer.CompositeC1.Services
{
public class DataConnectionService: IDataConnectionService
{
public IDataConnectionAdapter GetDataConnection(CultureInfo culture)
{
return new DataConnectionAdapter(culture);
}
}

public interface IDataConnectionAdapter : IDisposable
{
SitemapNavigator SitemapNavigator { get; }
PageNode GetPageNodeById(Guid id);
}

public class DataConnectionAdapter : IDataConnectionAdapter
{
private readonly DataConnection _dataConnection;
public DataConnectionAdapter(CultureInfo culture)
{
_dataConnection = new DataConnection(culture);
}

public virtual SitemapNavigator SitemapNavigator => _dataConnection.SitemapNavigator;

public virtual PageNode GetPageNodeById(Guid id)
{
return SitemapNavigator.GetPageNodeById(id);
}

public void Dispose()
{
_dataConnection.Dispose();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Composite.Data;
using System.Globalization;

namespace Orckestra.Composer.CompositeC1.Services
{
public interface IDataConnectionService
{
IDataConnectionAdapter GetDataConnection(CultureInfo cultore);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Composite.Core.Implementation;
using Composite.Data;
using Composite.Data.Types;
using Moq;
using Orckestra.Composer.CompositeC1.Services;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace Orckestra.Composer.CompositeC1.Tests.Mocks
{
public class DataConnectionServiceMock : IDataConnectionService
{
private readonly IEnumerable<IPage> _pagesSource;
public DataConnectionServiceMock(IEnumerable<IPage> pagesSource)
{
_pagesSource = pagesSource;
}

public IDataConnectionAdapter GetDataConnection(CultureInfo culture)
{
var mockDC = new Mock<IDataConnectionAdapter>();
var navigator = new Mock<SitemapNavigatorImplementation>();
mockDC.Setup(sm => sm.GetPageNodeById(It.IsAny<Guid>())).Returns<Guid>((id) =>
{
var page = _pagesSource.FirstOrDefault(p => p.Id == id) as PageMock;
if (page == null) return null;
var parentPage = _pagesSource.FirstOrDefault(p => page.ParentPageId == p.Id) as PageMock;
var parentPageNode = parentPage != null ? new PageNode(parentPage, navigator.Object) : null;
var pageNodeMock = new Mock<PageNode>(page, navigator.Object);
pageNodeMock.SetupGet(p => p.ParentPage).Returns(parentPageNode);
pageNodeMock.SetupGet(p => p.Page).Returns(page);
pageNodeMock.SetupGet(p => p.MenuTitle).Returns(page.MenuTitle);
pageNodeMock.SetupGet(p => p.Url).Returns(page.Url);
return pageNodeMock.Object;
});
return mockDC.Object;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
</Reference>
<Reference Include="Microsoft.Practices.EnterpriseLibrary.Common, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\C1CMS.Assemblies.6.12.8122.18346\lib\net471\Microsoft.Practices.EnterpriseLibrary.Common.dll</HintPath>
<Private>False</Private>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\C1CMS.Assemblies.6.12.8122.18346\lib\net471\Microsoft.Practices.EnterpriseLibrary.Logging.dll</HintPath>
Expand Down Expand Up @@ -178,6 +178,7 @@
</Compile>
<Compile Include="Factory\ProductPromotionsFactory_BuildProductBadgeValues.cs" />
<Compile Include="Mappers\FacetsMapper_ConvertToFacetSetting.cs" />
<Compile Include="Mocks\DataConnectionServiceMock.cs" />
<Compile Include="Mocks\MainMenuMock.cs" />
<Compile Include="Mocks\ISiteConfigurationMetaMock.cs" />
<Compile Include="Mocks\PageServiceMock.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
using Orckestra.Composer.CompositeC1.Services;
using Orckestra.Composer.CompositeC1.Tests.Mocks;
using Orckestra.Composer.Services.Breadcrumb;
using Orckestra.Composer.ViewModels.Breadcrumb;
using Orckestra.ExperienceManagement.Configuration;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using static Orckestra.Composer.Utils.MessagesHelper.ArgumentException;
using static Orckestra.Composer.Utils.ExpressionUtility;
using Orckestra.Composer.ViewModels.Breadcrumb;
using System.Linq.Expressions;

namespace Orckestra.Composer.CompositeC1.Tests.Services.BreadcrumbViewService
Expand Down Expand Up @@ -51,7 +49,10 @@ public void SetUp()

var dataSource = BuildPageDataSource(contentPageId);
var pageServiceMock = new PageServiceMock(dataSource);

var dataConnectionServiceMcok = new DataConnectionServiceMock(dataSource);
Container.Use<IPageService>(pageServiceMock);
Container.Use<IDataConnectionService>(dataConnectionServiceMcok);

var mockedSiteConfiguration = new Mock<ISiteConfiguration>();

Expand Down