From 586ad9be84217bbf95aa73314502ff49b073a4bf Mon Sep 17 00:00:00 2001 From: mahsaro Date: Thu, 23 Jul 2015 14:19:24 +0100 Subject: [PATCH] Alias step builder and executer --- .../Orchard.Alias/Orchard.Alias.csproj | 18 +-- .../Recipes/Builders/AliasStep.cs | 129 +++++++++++++++++ .../Recipes/Executors/AliasStep.cs | 132 ++++++++++++++++++ 3 files changed, 271 insertions(+), 8 deletions(-) create mode 100644 src/Orchard.Web/Modules/Orchard.Alias/Recipes/Builders/AliasStep.cs create mode 100644 src/Orchard.Web/Modules/Orchard.Alias/Recipes/Executors/AliasStep.cs diff --git a/src/Orchard.Web/Modules/Orchard.Alias/Orchard.Alias.csproj b/src/Orchard.Web/Modules/Orchard.Alias/Orchard.Alias.csproj index 5f56dcfe8..75ee4a0d0 100644 --- a/src/Orchard.Web/Modules/Orchard.Alias/Orchard.Alias.csproj +++ b/src/Orchard.Web/Modules/Orchard.Alias/Orchard.Alias.csproj @@ -110,6 +110,8 @@ + + @@ -123,20 +125,20 @@ - $(ProjectDir)\..\Manifests - - diff --git a/src/Orchard.Web/Modules/Orchard.Alias/Recipes/Builders/AliasStep.cs b/src/Orchard.Web/Modules/Orchard.Alias/Recipes/Builders/AliasStep.cs new file mode 100644 index 000000000..9ec528ec1 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Alias/Recipes/Builders/AliasStep.cs @@ -0,0 +1,129 @@ +using System.Linq; +using System.Xml.Linq; +using Orchard.Data; +using Orchard.Localization; +using Orchard.Recipes.Services; +using Orchard.Alias.Records; +using Orchard.Alias.Implementation.Holder; + +namespace Orchard.Roles.Recipes.Builders { + public class RolesStep : RecipeBuilderStep { + private readonly IRepository _aliasRecordepository; + private readonly IAliasHolder _aliasHolder; + + public RolesStep(IRepository aliasRecordRepository, IAliasHolder aliasHolder) + { + _aliasRecordepository = aliasRecordRepository; + _aliasHolder = aliasHolder; + } + + public override string Name { + get { return "Alias"; } + } + + public override LocalizedString DisplayName { + get { return T("Alias"); } + } + + public override LocalizedString Description { + get { return T("Exports the aliases."); } + } + + public override void Build(BuildContext context) { + //var aliases = _aliasRecordepository.Table.ToList(); + + var aliases = _aliasHolder.GetMaps().SelectMany(m => m.GetAliases()).ToList(); + + if (!aliases.Any()) + return; + + var root = new XElement("Aliases"); + context.RecipeDocument.Element("Orchard").Add(root); + + foreach (var alias in aliases.OrderBy(x => x.Path)) + { + var aliasElement = new XElement("Alias", new XAttribute("Path", alias.Path)); + //root.Add( + // new XElement("Alias", + // new XAttribute("Path", alias.Path))); + var routeValuesElement = new XElement("RouteValues"); + foreach (var routeValue in alias.RouteValues) + { + routeValuesElement.Add(new XElement("Add", new XAttribute("Key", routeValue.Key), new XAttribute("Value", routeValue.Value))); + } + + aliasElement.Add(routeValuesElement); + root.Add(aliasElement); + } + + //add a collection of all the alias paths in this site so that the importing site can remove aliases that don't exist remotely + var pathsElement = new XElement("Paths"); + foreach (var aliasInfo in aliases) + { + pathsElement.Add(new XElement("Add", new XAttribute("Path", aliasInfo.Path))); + } + + root.Add(pathsElement); + + //var rootElement = context.Document.Descendants("Orchard").FirstOrDefault(); + + //if (rootElement == null) + //{ + // var ex = new OrchardException(T("Could not export this site's Aliases because the document passed via the Export Context did not contain a node called 'Orchard'. The document was malformed.")); + // Logger.Error(ex, ex.Message); + // throw ex; + //} + + //rootElement.Add(xmlElement); + } + + //public void Exporting(ExportContext context) + //{ + // if (!context.ExportOptions.CustomSteps.Contains("Aliases")) { return; } + + // var xmlElement = new XElement("Aliases"); + + // var autoroutePaths = _contentManager.Query().List().Select(p => p.Path); + // var allAliasInfos = _aliasHolder.GetMaps().SelectMany(m => m.GetAliases()).ToList(); + + // //we need to remove any aliases that are autoroutes because the remote conent id may not sync up with the local content id. the autoroutes will be imported as part of the content import + // var aliasInfosToExport = allAliasInfos.Where(ai => !autoroutePaths.Contains(ai.Path)); + + // foreach (var aliasInfo in aliasInfosToExport) + // { + // var aliasElement = new XElement("Alias", new XAttribute("Path", aliasInfo.Path)); + + // var routeValuesElement = new XElement("RouteValues"); + // foreach (var routeValue in aliasInfo.RouteValues) + // { + // routeValuesElement.Add(new XElement("Add", new XAttribute("Key", routeValue.Key), new XAttribute("Value", routeValue.Value))); + // } + + // aliasElement.Add(routeValuesElement); + // xmlElement.Add(aliasElement); + // } + + // //add a collection of all the alias paths in this site so that the importing site can remove aliases that don't exist remotely + // var pathsElement = new XElement("Paths"); + // foreach (var aliasInfo in allAliasInfos) + // { + // pathsElement.Add(new XElement("Add", new XAttribute("Path", aliasInfo.Path))); + // } + + // xmlElement.Add(pathsElement); + + // var rootElement = context.Document.Descendants("Orchard").FirstOrDefault(); + + // if (rootElement == null) + // { + // var ex = new OrchardException(T("Could not export this site's Aliases because the document passed via the Export Context did not contain a node called 'Orchard'. The document was malformed.")); + // Logger.Error(ex, ex.Message); + // throw ex; + // } + + // rootElement.Add(xmlElement); + //} + + + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Alias/Recipes/Executors/AliasStep.cs b/src/Orchard.Web/Modules/Orchard.Alias/Recipes/Executors/AliasStep.cs new file mode 100644 index 000000000..67b2ba56e --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Alias/Recipes/Executors/AliasStep.cs @@ -0,0 +1,132 @@ +using System; +using System.Web; +using System.Web.Routing; +using System.Linq; +using Orchard.Alias; +using Orchard.Logging; +using Orchard.Recipes.Models; +using Orchard.Recipes.Services; + + +namespace Orchard.Alias.Recipes.Executors { + public class AliasStep : RecipeExecutionStep { + private readonly IAliasService _aliasService; + + public AliasStep(IAliasService aliasService) + { + _aliasService = aliasService; + } + + public override string Name { + get { return "Alias"; } + } + + public override void Execute(RecipeExecutionContext context) { + //var installedPermissions = _roleService.GetInstalledPermissions().SelectMany(p => p.Value).ToList(); + + foreach (var aliasElement in context.RecipeStep.Step.Elements()) { + var aliasPath = aliasElement.Attribute("Path").Value; + + Logger.Information("Processing alias '{0}'.", aliasPath); + + //var path = aliasElement.Attribute("Path").Value; + var rvd = new RouteValueDictionary(); + + var routeValuesElement = aliasElement.Descendants("RouteValues").FirstOrDefault(); + + if (routeValuesElement != null) + { + foreach (var routeValue in routeValuesElement.Descendants("Add")) + { + rvd.Add(routeValue.Attribute("Key").Value, routeValue.Attribute("Value").Value); + } + } + + _aliasService.Set(aliasPath, rvd, "Custom"); + + try { + _aliasService.Set(aliasPath, rvd, "Custom"); + //var role = _roleService.GetRoleByName(roleName); + //if (role == null) { + // _roleService.CreateRole(roleName); + // role = _roleService.GetRoleByName(roleName); + } + + // var permissions = roleElement.Attribute("Permissions").Value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); + // // Only import permissions for currenlty installed modules. + // var permissionsValid = permissions.Where(permission => installedPermissions.Any(x => x.Name == permission)).ToList(); + + // // Union to keep existing permissions. + // _roleService.UpdateRole(role.Id, role.Name, permissionsValid.Union(role.RolesPermissions.Select(p => p.Permission.Name))); + //} + catch (Exception ex) { + Logger.Error(ex, "Error while processing alias '{0}'.", aliasPath); + throw; + } + + ////remove all local pathys that are not present in the remote export + //var allRemotePaths = recipeContext.RecipeStep.Step.XPathSelectElements("Paths/Add").Select(e => e.Attribute("Path").Value); + //var allLocalPaths = _aliasService.List().Select(t => t.Item1).ToList(); + + //foreach (var path in allLocalPaths.Where(p => !allRemotePaths.Contains(p))) + //{ + // _aliasService.Delete(path); + //} + + + //recipeContext.Executed = true; + } + } + + /* + + + + + + + + + */ + //Enable any features that are in the list, disable features that aren't in the list + //public override void Execute(RecipeExecutionContext context) + //{ + // if (!String.Equals(context.RecipeStep.Name, "Aliases", StringComparison.OrdinalIgnoreCase)) + // { + // return; + // } + + // var aliasElements = context.RecipeStep.Step.Descendants("Alias"); + + // foreach (var aliasElement in aliasElements) + // { + // var path = aliasElement.Attribute("Path").Value; + // var rvd = new RouteValueDictionary(); + + // var routeValuesElement = aliasElement.Descendants("RouteValues").FirstOrDefault(); + + // if (routeValuesElement != null) + // { + // foreach (var routeValue in routeValuesElement.Descendants("Add")) + // { + // rvd.Add(routeValue.Attribute("Key").Value, routeValue.Attribute("Value").Value); + // } + // } + + // _aliasService.Set(path, rvd, "Custom"); + // } + + // //remove all local pathys that are not present in the remote export + // var allRemotePaths = recipeContext.RecipeStep.Step.XPathSelectElements("Paths/Add").Select(e => e.Attribute("Path").Value); + // var allLocalPaths = _aliasService.List().Select(t => t.Item1).ToList(); + + // foreach (var path in allLocalPaths.Where(p => !allRemotePaths.Contains(p))) + // { + // _aliasService.Delete(path); + // } + + + // recipeContext.Executed = true; + //} + } +} \ No newline at end of file