Re-implemented HomeAlias custom export step as recipe builder steps.

This commit is contained in:
Sipke Schoorstra
2015-08-13 14:43:05 +01:00
parent 25fa422a5e
commit 641236f358
4 changed files with 32 additions and 53 deletions

View File

@@ -1,14 +0,0 @@
using System.Collections.Generic;
using Orchard.Events;
namespace Orchard.Autoroute.ImportExport {
public interface ICustomExportStep : IEventHandler {
void Register(IList<string> steps);
}
public class HomeAliasExportStep : ICustomExportStep {
public void Register(IList<string> steps) {
steps.Add("HomeAlias");
}
}
}

View File

@@ -81,9 +81,6 @@
<Content Include="Web.config" /> <Content Include="Web.config" />
<Content Include="Scripts\Web.config" /> <Content Include="Scripts\Web.config" />
<Content Include="Styles\Web.config" /> <Content Include="Styles\Web.config" />
<Compile Include="ImportExport\HomeAliasExportStep.cs" />
<Compile Include="ImportExport\HomeAliasExportHandler.cs" />
<Compile Include="ImportExport\HomeAliasImportHandler.cs" />
<Compile Include="Permissions.cs" /> <Compile Include="Permissions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Content Include="Module.txt" /> <Content Include="Module.txt" />
@@ -115,6 +112,8 @@
<ItemGroup> <ItemGroup>
<Compile Include="Commands\AutorouteCommands.cs" /> <Compile Include="Commands\AutorouteCommands.cs" />
<Compile Include="Providers\ContentDefinition\ContentDefinitionEventHandler.cs" /> <Compile Include="Providers\ContentDefinition\ContentDefinitionEventHandler.cs" />
<Compile Include="Recipes\Builders\HomeAliasStep.cs" />
<Compile Include="Recipes\Executors\HomeAliasStep.cs" />
<Compile Include="ResourceManifest.cs" /> <Compile Include="ResourceManifest.cs" />
<Compile Include="Services\AliasResolverSelector.cs" /> <Compile Include="Services\AliasResolverSelector.cs" />
<Compile Include="Services\HomeAliasService.cs" /> <Compile Include="Services\HomeAliasService.cs" />

View File

@@ -1,36 +1,35 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Web.Routing; using System.Web.Routing;
using System.Xml.Linq; using System.Xml.Linq;
using Orchard.Autoroute.Services; using Orchard.Autoroute.Services;
using Orchard.ContentManagement; using Orchard.ContentManagement;
using Orchard.Events; using Orchard.Localization;
using Orchard.Recipes.Services;
namespace Orchard.Autoroute.ImportExport { namespace Orchard.Autoroute.Recipes.Builders {
public interface IExportEventHandler : IEventHandler { public class HomeAliasStep : RecipeBuilderStep {
void Exporting(dynamic context);
void Exported(dynamic context);
}
public class HomeAliasHandler : IExportEventHandler {
private readonly IHomeAliasService _homeAliasService; private readonly IHomeAliasService _homeAliasService;
private readonly IContentManager _contentManager; private readonly IContentManager _contentManager;
public HomeAliasHandler(IHomeAliasService homeAliasService, IContentManager contentManager) { public HomeAliasStep(IHomeAliasService homeAliasService, IContentManager contentManager) {
_homeAliasService = homeAliasService; _homeAliasService = homeAliasService;
_contentManager = contentManager; _contentManager = contentManager;
} }
public void Exporting(dynamic context) { public override string Name {
get { return "HomeAlias"; }
} }
public void Exported(dynamic context) { public override LocalizedString DisplayName {
get { return T("Home Alias"); }
if (!((IEnumerable<string>)context.ExportOptions.CustomSteps).Contains("HomeAlias")) {
return;
} }
public override LocalizedString Description {
get { return T("Exports home alias."); }
}
public override void Build(BuildContext context) {
var homeAliasRoute = _homeAliasService.GetHomeRoute() ?? new RouteValueDictionary(); var homeAliasRoute = _homeAliasService.GetHomeRoute() ?? new RouteValueDictionary();
var root = new XElement("HomeAlias", homeAliasRoute.Select(x => new XElement(Capitalize(x.Key), x.Value))); var root = new XElement("HomeAlias", homeAliasRoute.Select(x => new XElement(Capitalize(x.Key), x.Value)));
var homePage = _homeAliasService.GetHomePage(VersionOptions.Latest); var homePage = _homeAliasService.GetHomePage(VersionOptions.Latest);
@@ -40,10 +39,10 @@ namespace Orchard.Autoroute.ImportExport {
// so we can't rely on the route values in that case. // so we can't rely on the route values in that case.
if (homePage != null) { if (homePage != null) {
var homePageIdentifier = _contentManager.GetItemMetadata(homePage).Identity.ToString(); var homePageIdentifier = _contentManager.GetItemMetadata(homePage).Identity.ToString();
root.Attr("Identifier", homePageIdentifier); root.Attr("Id", homePageIdentifier);
} }
context.Document.Element("Orchard").Add(root); context.RecipeDocument.Element("Orchard").Add(root);
} }
private string Capitalize(string value) { private string Capitalize(string value) {
@@ -54,4 +53,3 @@ namespace Orchard.Autoroute.ImportExport {
} }
} }
} }

View File

@@ -6,24 +6,22 @@ using Orchard.ContentManagement;
using Orchard.Recipes.Models; using Orchard.Recipes.Models;
using Orchard.Recipes.Services; using Orchard.Recipes.Services;
namespace Orchard.Autoroute.ImportExport { namespace Orchard.Autoroute.Recipes.Executors {
public class HomeAliasImportHandler : Component, IRecipeHandler { public class HomeAliasStep : RecipeExecutionStep {
private readonly IHomeAliasService _homeAliasService;
private readonly IContentManager _contentManager; private readonly IContentManager _contentManager;
private readonly IHomeAliasService _homeAliasService;
public HomeAliasImportHandler(IHomeAliasService homeAliasService, IContentManager contentManager) { public HomeAliasStep(RecipeExecutionLogger logger, IContentManager contentManager, IHomeAliasService homeAliasService) : base(logger) {
_homeAliasService = homeAliasService;
_contentManager = contentManager; _contentManager = contentManager;
_homeAliasService = homeAliasService;
} }
public void ExecuteRecipeStep(RecipeContext recipeContext) { public override string Name { get { return "HomeAlias"; } }
if (!String.Equals(recipeContext.RecipeStep.Name, "HomeAlias", StringComparison.OrdinalIgnoreCase)) {
return;
}
var root = recipeContext.RecipeStep.Step; public override void Execute(RecipeExecutionContext context) {
var root = context.RecipeStep.Step;
var routeValueDictionary = root.Elements().ToDictionary(x => x.Name.LocalName.ToLower(), x => (object)x.Value); var routeValueDictionary = root.Elements().ToDictionary(x => x.Name.LocalName.ToLower(), x => (object)x.Value);
var homePageIdentifier = root.Attr("Identifier"); var homePageIdentifier = root.Attr("Id");
var homePageIdentity = new ContentIdentity(homePageIdentifier); var homePageIdentity = new ContentIdentity(homePageIdentifier);
var homePage = !String.IsNullOrEmpty(homePageIdentifier) ? _contentManager.ResolveIdentity(homePageIdentity) : default(ContentItem); var homePage = !String.IsNullOrEmpty(homePageIdentifier) ? _contentManager.ResolveIdentity(homePageIdentity) : default(ContentItem);
@@ -31,8 +29,6 @@ namespace Orchard.Autoroute.ImportExport {
_homeAliasService.PublishHomeAlias(homePage); _homeAliasService.PublishHomeAlias(homePage);
else else
_homeAliasService.PublishHomeAlias(new RouteValueDictionary(routeValueDictionary)); _homeAliasService.PublishHomeAlias(new RouteValueDictionary(routeValueDictionary));
recipeContext.Executed = true;
} }
} }
} }