diff --git a/Recipes/Builders/SitemapStep.cs b/Recipes/Builders/SitemapStep.cs new file mode 100644 index 0000000..64364d1 --- /dev/null +++ b/Recipes/Builders/SitemapStep.cs @@ -0,0 +1,126 @@ +using System.Linq; +using System.Xml.Linq; +using Orchard.Data; +using Orchard.Localization; +using Orchard.Recipes.Services; +using WebAdvanced.Sitemap.Models; +using System.Collections.Generic; + +namespace WebAdvanced.Sitemap.Recipes.Builders +{ + public class SitemapStep : RecipeBuilderStep + { + private readonly IRepository _routeRepository; + private readonly IRepository _settingsRepository; + private readonly IRepository _customRouteRepository; + + public SitemapStep( + IRepository routeRepository, + IRepository settingsRepository, + IRepository customRouteRepository) + { + _routeRepository = routeRepository; + _settingsRepository = settingsRepository; + _customRouteRepository = customRouteRepository; + } + + public override string Name + { + get { return "WebAdvancedSitemap"; } + } + + public override LocalizedString DisplayName + { + get { return T("Web Advanced Sitemap"); } + } + + public override LocalizedString Description + { + get { return T("Exports Web Advanced sitemap definitions."); } + } + + public override void Build(BuildContext context) + { + var root = new XElement("WebAdvancedSitemap"); + + BuildRoutes(root); + BuildSettings(root); + BuildCustomRoutes(root); + + if(root.HasElements) + { + context.RecipeDocument.Element("Orchard").Add(root); + } + } + + private void BuildRoutes(XElement root) + { + var routeDefinitions = _routeRepository.Table.ToList(); + + if (!routeDefinitions.Any()) + { + return; + } + + var routes = new XElement("Routes"); + root.Add(routes); + + foreach (var routeDefinition in routeDefinitions.OrderBy(x => x.Slug)) + { + routes.Add(new XElement("Route", + new XAttribute("Slug", routeDefinition.Slug), + new XAttribute("DisplayLevels", routeDefinition.DisplayLevels), + new XAttribute("Active", routeDefinition.Active), + new XAttribute("DisplayColumn", routeDefinition.DisplayColumn), + new XAttribute("Weight", routeDefinition.Weight))); + } + } + + private void BuildSettings(XElement root) + { + var settingsDefinitions = _settingsRepository.Table.ToList(); + + if (!settingsDefinitions.Any()) + { + return; + } + + var settings = new XElement("Settings"); + root.Add(settings); + + foreach (var settingsDefinition in settingsDefinitions.OrderBy(x => x.ContentType)) + { + settings.Add(new XElement("Setting", + new XAttribute("ContentType", settingsDefinition.ContentType), + new XAttribute("IndexForDisplay", settingsDefinition.IndexForDisplay), + new XAttribute("IndexForXml", settingsDefinition.IndexForXml), + new XAttribute("UpdateFrequency", settingsDefinition.UpdateFrequency), + new XAttribute("Priority", settingsDefinition.Priority))); + } + } + + private void BuildCustomRoutes(XElement root) + { + var customRouteDefinitions = _customRouteRepository.Table.ToList(); + + if (!customRouteDefinitions.Any()) + { + return; + } + + var customRoutes = new XElement("CustomRoutes"); + root.Add(customRoutes); + + foreach (var customRouteDefinition in customRouteDefinitions.OrderBy(x => x.Url)) + { + customRoutes.Add(new XElement("CustomRoute", + new XAttribute("Url", customRouteDefinition.Url), + new XAttribute("IndexForDisplay", customRouteDefinition.IndexForDisplay), + new XAttribute("IndexForXml", customRouteDefinition.IndexForXml), + new XAttribute("UpdateFrequency", customRouteDefinition.UpdateFrequency), + new XAttribute("Priority", customRouteDefinition.Priority))); + } + } + } +} + diff --git a/Recipes/Executors/SitemapStep.cs b/Recipes/Executors/SitemapStep.cs new file mode 100644 index 0000000..ac0ea63 --- /dev/null +++ b/Recipes/Executors/SitemapStep.cs @@ -0,0 +1,175 @@ +using System; +using System.Collections.Generic; +using Orchard.Data; +using Orchard.Logging; +using Orchard.Recipes.Models; +using Orchard.Recipes.Services; +using WebAdvanced.Sitemap.Models; +using System.Xml.Linq; + +namespace WebAdvanced.Sitemap.Recipes.Executors +{ + public class SitemapStep : RecipeExecutionStep + { + private readonly IRepository _routeRepository; + private readonly IRepository _settingsRepository; + private readonly IRepository _customRouteRepository; + + public SitemapStep( + IRepository routeRepository, + IRepository settingsRepository, + IRepository customRouteRepository, + RecipeExecutionLogger logger) : base(logger) + { + _routeRepository = routeRepository; + _settingsRepository = settingsRepository; + _customRouteRepository = customRouteRepository; + } + + public override string Name { + get { return "WebAdvancedSitemap"; } + } + + public override void Execute(RecipeExecutionContext context) + { + ProcessRoutes(context.RecipeStep.Step); + ProcessSettings(context.RecipeStep.Step); + ProcessCustomRoutes(context.RecipeStep.Step); + } + + private void ProcessRoutes(XElement root) + { + var routeDefinitionsElement = root.Element("Routes"); + if (routeDefinitionsElement == null) + { + return; + } + + foreach (var routeDefinitionElement in routeDefinitionsElement.Elements()) + { + var routeSlug = routeDefinitionElement.Attribute("Slug").Value; + Logger.Information("Importing route '{0}'.", routeSlug); + + try + { + var routeDefinition = GetOrCreateRouteDefinition(routeSlug); + routeDefinition.DisplayLevels = int.Parse(routeDefinitionElement.Attribute("DisplayLevels").Value); + routeDefinition.Active = bool.Parse(routeDefinitionElement.Attribute("Active").Value); + routeDefinition.DisplayColumn = int.Parse(routeDefinitionElement.Attribute("DisplayColumn").Value); + routeDefinition.Weight = int.Parse(routeDefinitionElement.Attribute("Weight").Value); + } + catch (Exception ex) + { + Logger.Error(ex, "Error while importing route '{0}'.", routeSlug); + throw; + } + } + } + + private SitemapRouteRecord GetOrCreateRouteDefinition(string slug) + { + var routeDefinition = _routeRepository.Get(x => x.Slug == slug); + + if(routeDefinition == null) + { + routeDefinition = new SitemapRouteRecord + { + Slug = slug + }; + _routeRepository.Create(routeDefinition); + } + + return routeDefinition; + } + + private void ProcessSettings(XElement root) + { + var settingsDefinitionsElement = root.Element("Settings"); + if (settingsDefinitionsElement == null) + { + return; + } + + foreach (var settingDefinitionElement in settingsDefinitionsElement.Elements()) + { + var settingContentType = settingDefinitionElement.Attribute("ContentType").Value; + Logger.Information("Importing settings '{0}'.", settingContentType); + + try + { + var settingDefinition = GetOrCreateSettingDefinition(settingContentType); + settingDefinition.IndexForDisplay = bool.Parse(settingDefinitionElement.Attribute("IndexForDisplay").Value); + settingDefinition.IndexForXml = bool.Parse(settingDefinitionElement.Attribute("IndexForXml").Value); + settingDefinition.UpdateFrequency = settingDefinitionElement.Attribute("UpdateFrequency").Value; + settingDefinition.Priority = int.Parse(settingDefinitionElement.Attribute("Priority").Value); + } + catch (Exception ex) + { + Logger.Error(ex, "Error while importing setting '{0}'.", settingContentType); + throw; + } + } + } + + private SitemapSettingsRecord GetOrCreateSettingDefinition(string contentType) + { + var settingDefinition = _settingsRepository.Get(x => x.ContentType == contentType); + + if (settingDefinition == null) + { + settingDefinition = new SitemapSettingsRecord + { + ContentType = contentType + }; + _settingsRepository.Create(settingDefinition); + } + + return settingDefinition; + } + + private void ProcessCustomRoutes(XElement root) + { + var customRoutesDefinitionsElement = root.Element("CustomRoutes"); + if (customRoutesDefinitionsElement == null) + { + return; + } + + foreach (var customRouteDefinitionElement in customRoutesDefinitionsElement.Elements()) + { + var customRouteUrl = customRouteDefinitionElement.Attribute("Url").Value; + Logger.Information("Importing custom route '{0}'.", customRouteUrl); + + try + { + var customRouteDefinition = GetOrCreateCustomRouteDefinition(customRouteUrl); + customRouteDefinition.IndexForDisplay = bool.Parse(customRouteDefinitionElement.Attribute("IndexForDisplay").Value); + customRouteDefinition.IndexForXml = bool.Parse(customRouteDefinitionElement.Attribute("IndexForXml").Value); + customRouteDefinition.UpdateFrequency = customRouteDefinitionElement.Attribute("UpdateFrequency").Value; + customRouteDefinition.Priority = int.Parse(customRouteDefinitionElement.Attribute("Priority").Value); + } + catch (Exception ex) + { + Logger.Error(ex, "Error while importing custom route '{0}'.", customRouteUrl); + throw; + } + } + } + + private SitemapCustomRouteRecord GetOrCreateCustomRouteDefinition(string url) + { + var customRouteDefinition = _customRouteRepository.Get(x => x.Url == url); + + if (customRouteDefinition == null) + { + customRouteDefinition = new SitemapCustomRouteRecord + { + Url = url + }; + _customRouteRepository.Create(customRouteDefinition); + } + + return customRouteDefinition; + } + } +} diff --git a/Services/AdvancedSitemapService.cs b/Services/AdvancedSitemapService.cs index 5268d0f..f2d346b 100644 --- a/Services/AdvancedSitemapService.cs +++ b/Services/AdvancedSitemapService.cs @@ -1,13 +1,10 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Security.Policy; using System.Text.RegularExpressions; using System.Xml.Linq; -using JetBrains.Annotations; using Orchard.Caching; using Orchard.ContentManagement; -using Orchard.ContentManagement.Aspects; using Orchard.ContentManagement.MetaData; using Orchard.Data; using Orchard.Services; @@ -18,7 +15,6 @@ using WebAdvanced.Sitemap.Extensions; namespace WebAdvanced.Sitemap.Services { - [UsedImplicitly] public class AdvancedSitemapService : IAdvancedSitemapService { readonly IRepository _routeRepository; readonly IRepository _settingsRepository; diff --git a/WebAdvanced.Sitemap.csproj b/WebAdvanced.Sitemap.csproj index 5c87dc3..0a58f1c 100644 --- a/WebAdvanced.Sitemap.csproj +++ b/WebAdvanced.Sitemap.csproj @@ -1,5 +1,5 @@  - + Debug @@ -12,7 +12,7 @@ Properties WebAdvanced.Sitemap WebAdvanced.Sitemap - v4.0 + v4.5.2 false @@ -45,6 +45,7 @@ prompt 4 AllRules.ruleset + false pdbonly @@ -54,24 +55,45 @@ prompt 4 AllRules.ruleset + false + + ..\..\..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll + True + 3.5 - - False - ..\..\..\..\lib\aspnetmvc\System.Web.Mvc.dll + + ..\..\..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.Helpers.dll + True + + + ..\..\..\packages\Microsoft.AspNet.Mvc.5.2.3\lib\net45\System.Web.Mvc.dll + True - - False - ..\..\..\..\lib\aspnetmvc\System.Web.Razor.dll + + ..\..\..\packages\Microsoft.AspNet.Razor.3.2.3\lib\net45\System.Web.Razor.dll + True + + ..\..\..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.dll + True + + + ..\..\..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.Deployment.dll + True + + + ..\..\..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.Razor.dll + True + @@ -106,6 +128,8 @@ + + @@ -179,6 +203,10 @@ true + + + + 10.0 $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) diff --git a/app.config b/app.config new file mode 100644 index 0000000..6f233ad --- /dev/null +++ b/app.config @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/packages.config b/packages.config new file mode 100644 index 0000000..6729ced --- /dev/null +++ b/packages.config @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file