Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Orchard 1.10 compatibility and Import/Export #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions Recipes/Builders/SitemapStep.cs
Original file line number Diff line number Diff line change
@@ -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<SitemapRouteRecord> _routeRepository;
private readonly IRepository<SitemapSettingsRecord> _settingsRepository;
private readonly IRepository<SitemapCustomRouteRecord> _customRouteRepository;

public SitemapStep(
IRepository<SitemapRouteRecord> routeRepository,
IRepository<SitemapSettingsRecord> settingsRepository,
IRepository<SitemapCustomRouteRecord> 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)));
}
}
}
}

175 changes: 175 additions & 0 deletions Recipes/Executors/SitemapStep.cs
Original file line number Diff line number Diff line change
@@ -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<SitemapRouteRecord> _routeRepository;
private readonly IRepository<SitemapSettingsRecord> _settingsRepository;
private readonly IRepository<SitemapCustomRouteRecord> _customRouteRepository;

public SitemapStep(
IRepository<SitemapRouteRecord> routeRepository,
IRepository<SitemapSettingsRecord> settingsRepository,
IRepository<SitemapCustomRouteRecord> 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;
}
}
}
4 changes: 0 additions & 4 deletions Services/AdvancedSitemapService.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,7 +15,6 @@
using WebAdvanced.Sitemap.Extensions;

namespace WebAdvanced.Sitemap.Services {
[UsedImplicitly]
public class AdvancedSitemapService : IAdvancedSitemapService {
readonly IRepository<SitemapRouteRecord> _routeRepository;
readonly IRepository<SitemapSettingsRecord> _settingsRepository;
Expand Down
Loading