Refactored CustomElements import/export step.

This commit is contained in:
Sipke Schoorstra
2015-07-17 13:09:58 +01:00
parent f82132102d
commit 860282bb44
4 changed files with 37 additions and 47 deletions

View File

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

View File

@@ -367,9 +367,8 @@
<Compile Include="Helpers\PrefixHelper.cs" />
<Compile Include="Helpers\JsonHelper.cs" />
<Compile Include="Helpers\StringHelper.cs" />
<Compile Include="ImportExport\ElementsExportHandler.cs" />
<Compile Include="ImportExport\ElementsExportStep.cs" />
<Compile Include="ImportExport\ElementsImportHandler.cs" />
<Compile Include="Recipes\Builders\CustomElementsStep.cs" />
<Compile Include="Recipes\Executors\CustomElementsStep.cs" />
<Compile Include="Providers\BlueprintElementHarvester.cs" />
<Compile Include="ResourceManifest.cs" />
<Compile Include="Services\CurrentControllerAccessor.cs" />

View File

@@ -1,40 +1,43 @@
using System.Collections.Generic;
using System.Linq;
using System.Linq;
using System.Xml.Linq;
using Orchard.Data;
using Orchard.Events;
using Orchard.Layouts.Models;
using Orchard.Localization;
using Orchard.Recipes.Services;
namespace Orchard.Layouts.ImportExport {
public interface IExportEventHandler : IEventHandler {
void Exporting(dynamic context);
void Exported(dynamic context);
}
namespace Orchard.Layouts.Recipes.Builders {
public class ElementsExportHandler : IExportEventHandler {
public class CustomElementsStep : RecipeBuilderStep {
private readonly IRepository<ElementBlueprint> _repository;
public ElementsExportHandler(IRepository<ElementBlueprint> repository) {
public CustomElementsStep(IRepository<ElementBlueprint> repository) {
_repository = repository;
}
public void Exporting(dynamic context) {
public override string Name
{
get { return "CustomElements"; }
}
public void Exported(dynamic context) {
public override LocalizedString DisplayName
{
get { return T("Custom Elements"); }
}
if (!((IEnumerable<string>)context.ExportOptions.CustomSteps).Contains("LayoutElements")) {
return;
}
public override LocalizedString Description
{
get { return T("Exports custom defined elements."); }
}
public override void Build(BuildContext context) {
var elements = _repository.Table.OrderBy(x => x.ElementTypeName).ToList();
if (!elements.Any()) {
return;
}
var root = new XElement("LayoutElements");
context.Document.Element("Orchard").Add(root);
var root = new XElement("CustomElements");
context.RecipeDocument.Element("Orchard").Add(root);
foreach (var element in elements) {
root.Add(new XElement("Element",

View File

@@ -1,23 +1,27 @@
using System;
using System.Collections.Generic;
using Orchard.Data;
using Orchard.Layouts.Models;
using Orchard.Recipes.Models;
using Orchard.Recipes.Services;
namespace Orchard.Layouts.ImportExport {
public class ElementsImportHandler : Component, IRecipeHandler {
namespace Orchard.Layouts.Recipes.Executors {
public class CustomElementsStep : RecipeExecutionStep {
private readonly IRepository<ElementBlueprint> _repository;
public ElementsImportHandler(IRepository<ElementBlueprint> repository) {
public CustomElementsStep(IRepository<ElementBlueprint> repository) {
_repository = repository;
}
public void ExecuteRecipeStep(RecipeContext recipeContext) {
if (!String.Equals(recipeContext.RecipeStep.Name, "LayoutElements", StringComparison.OrdinalIgnoreCase)) {
return;
}
public override string Name {
get { return "CustomElements"; }
}
foreach (var elementElement in recipeContext.RecipeStep.Step.Elements()) {
public override IEnumerable<string> Names {
get { return new[] { Name, "LayoutElements" }; }
}
public override void Execute(RecipeExecutionContext context) {
foreach (var elementElement in context.RecipeStep.Step.Elements()) {
var typeName = elementElement.Attribute("ElementTypeName").Value;
var element = GetOrCreateElement(typeName);
@@ -27,8 +31,6 @@ namespace Orchard.Layouts.ImportExport {
element.ElementCategory = elementElement.Attribute("ElementCategory").Value;
element.BaseElementState = elementElement.Element("BaseElementState").Value;
}
recipeContext.Executed = true;
}
private ElementBlueprint GetOrCreateElement(string typeName) {