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="Scripts\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="Properties\AssemblyInfo.cs" />
<Content Include="Module.txt" />
@ -115,6 +112,8 @@
<ItemGroup>
<Compile Include="Commands\AutorouteCommands.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="Services\AliasResolverSelector.cs" />
<Compile Include="Services\HomeAliasService.cs" />

View File

@ -1,36 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Routing;
using System.Xml.Linq;
using Orchard.Autoroute.Services;
using Orchard.ContentManagement;
using Orchard.Events;
using Orchard.Localization;
using Orchard.Recipes.Services;
namespace Orchard.Autoroute.ImportExport {
public interface IExportEventHandler : IEventHandler {
void Exporting(dynamic context);
void Exported(dynamic context);
}
public class HomeAliasHandler : IExportEventHandler {
namespace Orchard.Autoroute.Recipes.Builders {
public class HomeAliasStep : RecipeBuilderStep {
private readonly IHomeAliasService _homeAliasService;
private readonly IContentManager _contentManager;
public HomeAliasHandler(IHomeAliasService homeAliasService, IContentManager contentManager) {
public HomeAliasStep(IHomeAliasService homeAliasService, IContentManager contentManager) {
_homeAliasService = homeAliasService;
_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 root = new XElement("HomeAlias", homeAliasRoute.Select(x => new XElement(Capitalize(x.Key), x.Value)));
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.
if (homePage != null) {
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) {
@ -53,5 +52,4 @@ namespace Orchard.Autoroute.ImportExport {
return Char.ToUpper(value[0]) + value.Substring(1);
}
}
}
}

View File

@ -6,33 +6,29 @@ using Orchard.ContentManagement;
using Orchard.Recipes.Models;
using Orchard.Recipes.Services;
namespace Orchard.Autoroute.ImportExport {
public class HomeAliasImportHandler : Component, IRecipeHandler {
private readonly IHomeAliasService _homeAliasService;
namespace Orchard.Autoroute.Recipes.Executors {
public class HomeAliasStep : RecipeExecutionStep {
private readonly IContentManager _contentManager;
private readonly IHomeAliasService _homeAliasService;
public HomeAliasImportHandler(IHomeAliasService homeAliasService, IContentManager contentManager) {
_homeAliasService = homeAliasService;
public HomeAliasStep(RecipeExecutionLogger logger, IContentManager contentManager, IHomeAliasService homeAliasService) : base(logger) {
_contentManager = contentManager;
_homeAliasService = homeAliasService;
}
public void ExecuteRecipeStep(RecipeContext recipeContext) {
if (!String.Equals(recipeContext.RecipeStep.Name, "HomeAlias", StringComparison.OrdinalIgnoreCase)) {
return;
}
var root = recipeContext.RecipeStep.Step;
public override string Name { get { return "HomeAlias"; } }
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 homePageIdentifier = root.Attr("Identifier");
var homePageIdentifier = root.Attr("Id");
var homePageIdentity = new ContentIdentity(homePageIdentifier);
var homePage = !String.IsNullOrEmpty(homePageIdentifier) ? _contentManager.ResolveIdentity(homePageIdentity) : default(ContentItem);
if(homePage != null)
if (homePage != null)
_homeAliasService.PublishHomeAlias(homePage);
else
_homeAliasService.PublishHomeAlias(new RouteValueDictionary(routeValueDictionary));
recipeContext.Executed = true;
}
}
}