mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
Refactored Rules import/export step.
This commit is contained in:
@@ -1,14 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using Orchard.Events;
|
||||
|
||||
namespace Orchard.Rules.ImportExport {
|
||||
public interface ICustomExportStep : IEventHandler {
|
||||
void Register(IList<string> steps);
|
||||
}
|
||||
|
||||
public class RulesCustomExportStep : ICustomExportStep {
|
||||
public void Register(IList<string> steps) {
|
||||
steps.Add("Rules");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -89,9 +89,8 @@
|
||||
<Content Include="Scripts\Web.config" />
|
||||
<Content Include="Styles\Web.config" />
|
||||
<Compile Include="Handlers\ContentHandler.cs" />
|
||||
<Compile Include="ImportExport\RulesExportEventHandler.cs" />
|
||||
<Compile Include="ImportExport\RulesCustomExportStep.cs" />
|
||||
<Compile Include="ImportExport\RulesRecipeHandler.cs" />
|
||||
<Compile Include="Recipes\Builders\RulesStep.cs" />
|
||||
<Compile Include="Recipes\Executors\RulesStep.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Content Include="Module.txt" />
|
||||
<Content Include="Web.config">
|
||||
|
||||
@@ -1,63 +1,63 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
using Orchard.Events;
|
||||
using Orchard.Rules.Services;
|
||||
|
||||
namespace Orchard.Rules.ImportExport {
|
||||
public interface IExportEventHandler : IEventHandler {
|
||||
void Exporting(dynamic context);
|
||||
void Exported(dynamic context);
|
||||
}
|
||||
|
||||
public class RulesExportHandler : IExportEventHandler {
|
||||
private readonly IRulesServices _rulesServices;
|
||||
|
||||
public RulesExportHandler(IRulesServices rulesServices) {
|
||||
_rulesServices = rulesServices;
|
||||
}
|
||||
|
||||
public void Exporting(dynamic context) {
|
||||
}
|
||||
|
||||
public void Exported(dynamic context) {
|
||||
|
||||
if (!((IEnumerable<string>)context.ExportOptions.CustomSteps).Contains("Rules")) {
|
||||
return;
|
||||
}
|
||||
|
||||
var allRules = _rulesServices.GetRules().ToList();
|
||||
|
||||
if(!allRules.Any()) {
|
||||
return;
|
||||
}
|
||||
|
||||
var root = new XElement("Rules");
|
||||
context.Document.Element("Orchard").Add(root);
|
||||
|
||||
foreach(var rule in allRules) {
|
||||
root.Add(new XElement("Rule",
|
||||
new XAttribute("Name", rule.Name),
|
||||
new XAttribute("Enabled", rule.Enabled.ToString(CultureInfo.InvariantCulture)),
|
||||
new XElement("Actions", rule.Actions.Select(action =>
|
||||
new XElement("Action",
|
||||
new XAttribute("Type", action.Type ?? string.Empty),
|
||||
new XAttribute("Category", action.Category ?? string.Empty),
|
||||
new XAttribute("Parameters", action.Parameters ?? string.Empty),
|
||||
new XAttribute("Position", action.Position)
|
||||
)
|
||||
)),
|
||||
new XElement("Events", rule.Events.Select(e =>
|
||||
new XElement("Event",
|
||||
new XAttribute("Type", e.Type ?? string.Empty),
|
||||
new XAttribute("Category", e.Category ?? string.Empty),
|
||||
new XAttribute("Parameters", e.Parameters ?? string.Empty)
|
||||
)
|
||||
))
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Recipes.Services;
|
||||
using Orchard.Rules.Services;
|
||||
|
||||
namespace Orchard.Rules.Recipes.Builders {
|
||||
|
||||
public class RulesStep : RecipeBuilderStep {
|
||||
private readonly IRulesServices _rulesServices;
|
||||
|
||||
public RulesStep(IRulesServices rulesServices) {
|
||||
_rulesServices = rulesServices;
|
||||
}
|
||||
|
||||
public override string Name {
|
||||
get { return "Rules"; }
|
||||
}
|
||||
|
||||
public override LocalizedString DisplayName {
|
||||
get { return T("Rules"); }
|
||||
}
|
||||
|
||||
public override LocalizedString Description {
|
||||
get { return T("Exports rules."); }
|
||||
}
|
||||
|
||||
public override void Build(BuildContext context) {
|
||||
var allRules = _rulesServices.GetRules().ToList();
|
||||
|
||||
if (!allRules.Any()) {
|
||||
return;
|
||||
}
|
||||
|
||||
var root = new XElement("Rules");
|
||||
context.RecipeDocument.Element("Orchard").Add(root);
|
||||
|
||||
foreach (var rule in allRules) {
|
||||
root.Add(new XElement("Rule",
|
||||
new XAttribute("Name", rule.Name),
|
||||
new XAttribute("Enabled", rule.Enabled.ToString(CultureInfo.InvariantCulture)),
|
||||
new XElement("Actions", rule.Actions.Select(action =>
|
||||
new XElement("Action",
|
||||
new XAttribute("Type", action.Type ?? string.Empty),
|
||||
new XAttribute("Category", action.Category ?? string.Empty),
|
||||
new XAttribute("Parameters", action.Parameters ?? string.Empty),
|
||||
new XAttribute("Position", action.Position)
|
||||
)
|
||||
)),
|
||||
new XElement("Events", rule.Events.Select(e =>
|
||||
new XElement("Event",
|
||||
new XAttribute("Type", e.Type ?? string.Empty),
|
||||
new XAttribute("Category", e.Category ?? string.Empty),
|
||||
new XAttribute("Parameters", e.Parameters ?? string.Empty)
|
||||
)
|
||||
))
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,57 +1,46 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Logging;
|
||||
using Orchard.Recipes.Models;
|
||||
using Orchard.Recipes.Services;
|
||||
using Orchard.Rules.Models;
|
||||
using Orchard.Rules.Services;
|
||||
|
||||
namespace Orchard.Rules.ImportExport {
|
||||
public class RulesRecipeHandler : IRecipeHandler {
|
||||
private readonly IRulesServices _rulesServices;
|
||||
|
||||
public RulesRecipeHandler(IRulesServices rulesServices) {
|
||||
_rulesServices = rulesServices;
|
||||
Logger = NullLogger.Instance;
|
||||
T = NullLocalizer.Instance;
|
||||
}
|
||||
|
||||
public Localizer T { get; set; }
|
||||
public ILogger Logger { get; set; }
|
||||
|
||||
// <Data />
|
||||
// Import Data
|
||||
public void ExecuteRecipeStep(RecipeContext recipeContext) {
|
||||
if (!String.Equals(recipeContext.RecipeStep.Name, "Rules", StringComparison.OrdinalIgnoreCase)) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var rule in recipeContext.RecipeStep.Step.Elements()) {
|
||||
|
||||
var ruleRecord = _rulesServices.CreateRule(rule.Attribute("Name").Value);
|
||||
ruleRecord.Enabled = bool.Parse(rule.Attribute("Enabled").Value);
|
||||
|
||||
ruleRecord.Actions = rule.Element("Actions").Elements().Select(action =>
|
||||
new ActionRecord {
|
||||
Type = action.Attribute("Type").Value,
|
||||
Category = action.Attribute("Category").Value,
|
||||
Position = int.Parse(action.Attribute("Position").Value),
|
||||
Parameters = action.Attribute("Parameters").Value,
|
||||
RuleRecord = ruleRecord
|
||||
}).ToList();
|
||||
|
||||
ruleRecord.Events = rule.Element("Events").Elements().Select(action =>
|
||||
new EventRecord {
|
||||
Type = action.Attribute("Type").Value,
|
||||
Category = action.Attribute("Category").Value,
|
||||
Parameters = action.Attribute("Parameters").Value,
|
||||
RuleRecord = ruleRecord
|
||||
}).ToList();
|
||||
|
||||
}
|
||||
|
||||
recipeContext.Executed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Orchard.Recipes.Models;
|
||||
using Orchard.Recipes.Services;
|
||||
using Orchard.Rules.Models;
|
||||
using Orchard.Rules.Services;
|
||||
|
||||
namespace Orchard.Rules.Recipes.Executors {
|
||||
public class RulesStep : RecipeExecutionStep {
|
||||
private readonly IRulesServices _rulesServices;
|
||||
|
||||
public RulesStep(IRulesServices rulesServices) {
|
||||
_rulesServices = rulesServices;
|
||||
}
|
||||
|
||||
public override string Name {
|
||||
get { return "Rules"; }
|
||||
}
|
||||
|
||||
public override void Execute(RecipeExecutionContext context) {
|
||||
foreach (var rule in context.RecipeStep.Step.Elements()) {
|
||||
|
||||
var ruleRecord = _rulesServices.CreateRule(rule.Attribute("Name").Value);
|
||||
ruleRecord.Enabled = bool.Parse(rule.Attribute("Enabled").Value);
|
||||
|
||||
ruleRecord.Actions = rule.Element("Actions").Elements().Select(action =>
|
||||
new ActionRecord {
|
||||
Type = action.Attribute("Type").Value,
|
||||
Category = action.Attribute("Category").Value,
|
||||
Position = int.Parse(action.Attribute("Position").Value),
|
||||
Parameters = action.Attribute("Parameters").Value,
|
||||
RuleRecord = ruleRecord
|
||||
}).ToList();
|
||||
|
||||
ruleRecord.Events = rule.Element("Events").Elements().Select(action =>
|
||||
new EventRecord {
|
||||
Type = action.Attribute("Type").Value,
|
||||
Category = action.Attribute("Category").Value,
|
||||
Parameters = action.Attribute("Parameters").Value,
|
||||
RuleRecord = ruleRecord
|
||||
}).ToList();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user