#20404: Moving site settings export to individual drivers providing a legacy method

Work Item: 20404
This commit is contained in:
Stanley Goldman
2014-01-12 16:46:01 -05:00
committed by Sebastien Ros
parent a5210cad47
commit b2be9705c6
21 changed files with 293 additions and 58 deletions

View File

@@ -214,6 +214,7 @@
<Compile Include="Scheduling\Services\ScheduledTaskExecutor.cs" />
<Compile Include="Scheduling\Models\Task.cs" />
<Compile Include="Settings\Commands\SiteSettingsCommands.cs" />
<Compile Include="Settings\Drivers\SiteSettings2PartDriver.cs" />
<Compile Include="Settings\Models\SiteSettings2Part.cs" />
<Compile Include="Settings\Models\SiteSettings2PartRecord.cs" />
<Compile Include="Settings\ResourceManifest.cs" />

View File

@@ -0,0 +1,27 @@
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using Orchard.Core.Settings.Models;
using Orchard.Localization;
using Orchard.Logging;
using Orchard.Settings;
namespace Orchard.Core.Settings.Drivers
{
public class SiteSettings2PartDriver : ContentPartDriver<SiteSettings2Part> {
public SiteSettings2PartDriver() {
T = NullLocalizer.Instance;
Logger = NullLogger.Instance;
}
public Localizer T { get; set; }
public ILogger Logger { get; set; }
protected override void Exporting(SiteSettings2Part part, ExportContentContext context) {
DefaultSettingsPartImportExport.ExportSettingsPart(part, context);
}
protected override void Importing(SiteSettings2Part part, ImportContentContext context) {
DefaultSettingsPartImportExport.ImportSettingPart(part, context.Data.Element(part.PartDefinition.Name));
}
}
}

View File

@@ -3,6 +3,7 @@ using System.Net;
using JetBrains.Annotations;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using Orchard.Core.Settings.Models;
using Orchard.Core.Settings.ViewModels;
using Orchard.Localization.Services;
@@ -101,5 +102,13 @@ namespace Orchard.Core.Settings.Drivers {
return ContentShape("Parts_Settings_SiteSettingsPart",
() => shapeHelper.EditorTemplate(TemplateName: "Parts.Settings.SiteSettingsPart", Model: model, Prefix: Prefix));
}
protected override void Exporting(SiteSettingsPart part, ExportContentContext context) {
DefaultSettingsPartImportExport.ExportSettingsPart(part, context);
}
protected override void Importing(SiteSettingsPart part, ImportContentContext context) {
DefaultSettingsPartImportExport.ImportSettingPart(part, context.Data.Element(part.PartDefinition.Name));
}
}
}

View File

@@ -2,7 +2,9 @@
using Orchard.Comments.Models;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using Orchard.Localization;
using Orchard.Settings;
namespace Orchard.Comments.Drivers {
public class CommentSettingsPartDriver : ContentPartDriver<CommentSettingsPart> {
@@ -28,5 +30,13 @@ namespace Orchard.Comments.Drivers {
})
.OnGroup("comments");
}
protected override void Exporting(CommentSettingsPart part, ExportContentContext context) {
DefaultSettingsPartImportExport.ExportSettingsPart(part, context);
}
protected override void Importing(CommentSettingsPart part, ImportContentContext context) {
DefaultSettingsPartImportExport.ImportSettingPart(part, context.Data.Element(part.PartDefinition.Name));
}
}
}

View File

@@ -1,8 +1,10 @@
using System;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using Orchard.Email.Models;
using Orchard.Localization;
using Orchard.Settings;
namespace Orchard.Email.Drivers {
@@ -39,5 +41,13 @@ namespace Orchard.Email.Drivers {
})
.OnGroup("email");
}
protected override void Exporting(SmtpSettingsPart part, ExportContentContext context) {
DefaultSettingsPartImportExport.ExportSettingsPart(part, context);
}
protected override void Importing(SmtpSettingsPart part, ImportContentContext context) {
DefaultSettingsPartImportExport.ImportSettingPart(part, context.Data.Element(part.PartDefinition.Name));
}
}
}

View File

@@ -53,6 +53,8 @@
<HintPath>..\..\..\..\lib\aspnetmvc\System.Web.Mvc.dll</HintPath>
</Reference>
<Reference Include="System.Web" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<ItemGroup>
<Compile Include="Activities\MailActivity.cs" />

View File

@@ -139,31 +139,8 @@ namespace Orchard.ImportExport.Services {
}
private XElement ExportSiteSettings() {
var settings = new XElement("Settings");
var hasSetting = false;
foreach (var sitePart in _orchardServices.WorkContext.CurrentSite.ContentItem.Parts) {
var setting = new XElement(sitePart.PartDefinition.Name);
foreach (var property in sitePart.GetType().GetProperties()) {
var propertyType = property.PropertyType;
// Supported types (we also know they are not indexed properties).
if (propertyType == typeof(string) || propertyType == typeof(bool) || propertyType == typeof(int)) {
// Exclude read-only properties.
if (property.GetSetMethod() != null) {
setting.SetAttributeValue(property.Name, property.GetValue(sitePart, null));
hasSetting = true;
}
}
}
if (hasSetting) {
settings.Add(setting);
hasSetting = false;
}
}
return settings;
var exportContentItem = ExportContentItem(_orchardServices.WorkContext.CurrentSite.ContentItem);
return new XElement("Settings", exportContentItem.Elements());
}
private XElement ExportData(IEnumerable<string> contentTypes, IEnumerable<ContentItem> contentItems, int? batchSize) {

View File

@@ -0,0 +1,28 @@
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using Orchard.Localization;
using Orchard.Logging;
using Orchard.MediaLibrary.Models;
using Orchard.Settings;
namespace Orchard.MediaLibrary.Drivers
{
public class WebSearchSettingsPartDriver : ContentPartDriver<WebSearchSettingsPart> {
public WebSearchSettingsPartDriver()
{
T = NullLocalizer.Instance;
Logger = NullLogger.Instance;
}
public Localizer T { get; set; }
public ILogger Logger { get; set; }
protected override void Exporting(WebSearchSettingsPart part, ExportContentContext context) {
DefaultSettingsPartImportExport.ExportSettingsPart(part, context);
}
protected override void Importing(WebSearchSettingsPart part, ImportContentContext context) {
DefaultSettingsPartImportExport.ImportSettingPart(part, context.Data.Element(part.PartDefinition.Name));
}
}
}

View File

@@ -98,6 +98,7 @@
<Content Include="Views\Web.config" />
<Content Include="Scripts\Web.config" />
<Content Include="Styles\Web.config" />
<Compile Include="Drivers\WebSearchSettingsPartDriver.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Content Include="Module.txt" />
</ItemGroup>

View File

@@ -1,10 +1,12 @@
using JetBrains.Annotations;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using Orchard.Localization;
using Orchard.Messaging.Models;
using Orchard.Messaging.Services;
using Orchard.Messaging.ViewModels;
using Orchard.Settings;
namespace Orchard.Messaging.Drivers {
[UsedImplicitly]
@@ -43,5 +45,13 @@ namespace Orchard.Messaging.Drivers {
return ContentShape("Parts_MessageSettings_Edit", () => shapeHelper.EditorTemplate(TemplateName: TemplateName, Model: model, Prefix: Prefix));
}
protected override void Exporting(MessageSettingsPart part, ExportContentContext context) {
DefaultSettingsPartImportExport.ExportSettingsPart(part, context);
}
protected override void Importing(MessageSettingsPart part, ImportContentContext context) {
DefaultSettingsPartImportExport.ImportSettingPart(part, context.Data.Element(part.PartDefinition.Name));
}
}
}

View File

@@ -0,0 +1,28 @@
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using Orchard.Localization;
using Orchard.Logging;
using Orchard.OutputCache.Models;
using Orchard.Settings;
namespace Orchard.OutputCache.Drivers
{
public class CacheSettingsPartDriver : ContentPartDriver<CacheSettingsPart> {
public CacheSettingsPartDriver()
{
T = NullLocalizer.Instance;
Logger = NullLogger.Instance;
}
public Localizer T { get; set; }
public ILogger Logger { get; set; }
protected override void Exporting(CacheSettingsPart part, ExportContentContext context) {
DefaultSettingsPartImportExport.ExportSettingsPart(part, context);
}
protected override void Importing(CacheSettingsPart part, ImportContentContext context) {
DefaultSettingsPartImportExport.ImportSettingPart(part, context.Data.Element(part.PartDefinition.Name));
}
}
}

View File

@@ -90,6 +90,7 @@
<Compile Include="AdminMenu.cs" />
<Compile Include="Controllers\AdminController.cs" />
<Compile Include="Controllers\StatisticsController.cs" />
<Compile Include="Drivers\CacheSettingsPartDriver.cs" />
<Compile Include="Filters\OutputCacheFilter.cs" />
<Compile Include="Handlers\CacheSettingsPartHandler.cs" />
<Compile Include="Handlers\DisplayedContentItemHandler.cs" />

View File

@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Handlers;
using Orchard.Localization;
using Orchard.Logging;
using Orchard.Recipes.Models;
@@ -11,15 +13,20 @@ using Orchard.Settings;
namespace Orchard.Recipes.RecipeHandlers {
public class SettingsRecipeHandler : IRecipeHandler {
private readonly ISiteService _siteService;
private readonly IContentManager _contentManager;
private readonly Lazy<IEnumerable<IContentHandler>> _handlers;
public SettingsRecipeHandler(ISiteService siteService) {
public SettingsRecipeHandler(ISiteService siteService, IContentManager contentManager, Lazy<IEnumerable<IContentHandler>> handlers) {
_siteService = siteService;
_contentManager = contentManager;
_handlers = handlers;
Logger = NullLogger.Instance;
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
public ILogger Logger { get; set; }
private IEnumerable<IContentHandler> Handlers { get { return _handlers.Value; } }
/*
<Settings>
@@ -34,41 +41,19 @@ namespace Orchard.Recipes.RecipeHandlers {
}
var site = _siteService.GetSiteSettings();
foreach (var element in recipeContext.RecipeStep.Step.Elements()) {
var partName = XmlConvert.DecodeName(element.Name.LocalName);
foreach (var contentPart in site.ContentItem.Parts) {
if (!String.Equals(contentPart.PartDefinition.Name, partName, StringComparison.OrdinalIgnoreCase)) {
continue;
}
foreach (var attribute in element.Attributes()) {
SetSetting(attribute, contentPart);
}
}
var importContentSession = new ImportContentSession(_contentManager);
var context = new ImportContentContext(site.ContentItem, recipeContext.RecipeStep.Step, importContentSession);
foreach (var contentHandler in Handlers) {
contentHandler.Importing(context);
}
foreach (var contentHandler in Handlers) {
contentHandler.Imported(context);
}
recipeContext.Executed = true;
}
private static void SetSetting(XAttribute attribute, ContentPart contentPart) {
var attributeName = attribute.Name.LocalName;
var attributeValue = attribute.Value;
var property = contentPart.GetType().GetProperty(attributeName);
if (property == null) {
throw new InvalidOperationException(string.Format("Could set setting {0} for part {1} because it was not found.", attributeName, contentPart.PartDefinition.Name));
}
var propertyType = property.PropertyType;
if (propertyType == typeof(string)) {
property.SetValue(contentPart, attributeValue, null);
}
else if (propertyType == typeof(bool)) {
property.SetValue(contentPart, Boolean.Parse(attributeValue), null);
}
else if (propertyType == typeof(int)) {
property.SetValue(contentPart, Int32.Parse(attributeValue), null);
}
else {
throw new InvalidOperationException(string.Format("Could set setting {0} for part {1} because its type is not supported. Settings should be integer,boolean or string.", attributeName, contentPart.PartDefinition.Name));
}
}
}
}

View File

@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using Orchard.Indexing;
using Orchard.Localization;
using Orchard.Search.Models;
@@ -61,5 +63,20 @@ namespace Orchard.Search.Drivers {
return shapeHelper.EditorTemplate(TemplateName: "Parts/Search.SiteSettings", Model: model, Prefix: Prefix);
}).OnGroup("search");
}
protected override void Exporting(SearchSettingsPart part, ExportContentContext context) {
DefaultSettingsPartImportExport.ExportSettingsPart(part, context);
context.Element(part.PartDefinition.Name).Add(new XAttribute("SearchedFields", string.Join(",", part.SearchedFields)));
}
protected override void Importing(SearchSettingsPart part, ImportContentContext context) {
var xElement = context.Data.Element(part.PartDefinition.Name);
if (xElement == null) return;
DefaultSettingsPartImportExport.ImportSettingPart(part, xElement);
var searchedFields = xElement.Attribute("SearchedFields");
part.SearchedFields = searchedFields.Value.Split(new[] {","}, StringSplitOptions.RemoveEmptyEntries);
}
}
}

View File

@@ -55,6 +55,8 @@
<HintPath>..\..\..\..\lib\aspnetmvc\System.Web.Mvc.dll</HintPath>
</Reference>
<Reference Include="System.Web" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<ItemGroup>
<Compile Include="Commands\SearchCommands.cs" />

View File

@@ -0,0 +1,28 @@
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using Orchard.Localization;
using Orchard.Logging;
using Orchard.Settings;
using Orchard.Themes.Models;
namespace Orchard.Themes.Drivers
{
public class ThemeSiteSettingsPartDriver : ContentPartDriver<ThemeSiteSettingsPart> {
public ThemeSiteSettingsPartDriver()
{
T = NullLocalizer.Instance;
Logger = NullLogger.Instance;
}
public Localizer T { get; set; }
public ILogger Logger { get; set; }
protected override void Exporting(ThemeSiteSettingsPart part, ExportContentContext context) {
DefaultSettingsPartImportExport.ExportSettingsPart(part, context);
}
protected override void Importing(ThemeSiteSettingsPart part, ImportContentContext context) {
DefaultSettingsPartImportExport.ImportSettingPart(part, context.Data.Element(part.PartDefinition.Name));
}
}
}

View File

@@ -20,6 +20,11 @@
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<TargetFrameworkProfile />
<UseIISExpress>false</UseIISExpress>
<IISExpressSSLPort />
<IISExpressAnonymousAuthentication />
<IISExpressWindowsAuthentication />
<IISExpressUseClassicPipelineMode />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@@ -48,10 +53,13 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\lib\aspnetmvc\System.Web.Mvc.dll</HintPath>
</Reference>
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<ItemGroup>
<Compile Include="AdminMenu.cs" />
<Compile Include="Commands\ThemeCommands.cs" />
<Compile Include="Drivers\ThemeSiteSettingsPartDriver.cs" />
<Compile Include="Events\IExtensionDisplayEventHandler.cs" />
<Compile Include="Migrations.cs" />
<Compile Include="Models\ThemeEntry.cs" />

View File

@@ -0,0 +1,24 @@
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using Orchard.Localization;
using Orchard.Logging;
using Orchard.Settings;
using Orchard.Users.Models;
namespace Orchard.Users.Drivers
{
public class RegistrationSettingsPartDriver : ContentPartDriver<RegistrationSettingsPart> {
public RegistrationSettingsPartDriver()
{
T = NullLocalizer.Instance;
Logger = NullLogger.Instance;
}
public Localizer T { get; set; }
public ILogger Logger { get; set; }
protected override void Exporting(RegistrationSettingsPart part, ExportContentContext context) {
DefaultSettingsPartImportExport.ExportSettingsPart(part, context);
}
}
}

View File

@@ -64,6 +64,7 @@
<Compile Include="Commands\UserCommands.cs" />
<Compile Include="Controllers\AccountController.cs" />
<Compile Include="Controllers\AdminController.cs" />
<Compile Include="Drivers\RegistrationSettingsPartDriver.cs" />
<Compile Include="Drivers\UserPartDriver.cs" />
<Compile Include="Migrations.cs" />
<Compile Include="Events\UserContext.cs" />

View File

@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using Orchard.ContentManagement.Handlers;
using Orchard.Recipes.Models;
using Orchard.Settings;
namespace Orchard.ContentManagement.Drivers
{
public class DefaultSettingsPartImportExport
{
public static void ExportSettingsPart(ContentPart sitePart, ExportContentContext context) {
var xAttributes = new List<object>();
foreach (var property in sitePart.GetType().GetProperties()) {
var propertyType = property.PropertyType;
// Supported types (we also know they are not indexed properties).
if (propertyType == typeof (string) || propertyType == typeof (bool) || propertyType == typeof (int)) {
// Exclude read-only properties.
if (property.GetSetMethod() != null) {
var value = property.GetValue(sitePart, null);
if (value == null)
continue;
xAttributes.Add(new XAttribute(property.Name, value));
}
}
}
if(xAttributes.Any()) {
context.Element(sitePart.PartDefinition.Name).Add(xAttributes.ToArray());
}
}
public static void ImportSettingPart(ContentPart sitePart, XElement element) {
if(element == null)
return;
foreach (var attribute in element.Attributes()) {
var attributeName = attribute.Name.LocalName;
var attributeValue = attribute.Value;
var property = sitePart.GetType().GetProperty(attributeName);
if (property == null) {
throw new InvalidOperationException(string.Format("Could set setting {0} for part {1} because it was not found.", attributeName, sitePart.PartDefinition.Name));
}
var propertyType = property.PropertyType;
if (propertyType == typeof(string)) {
property.SetValue(sitePart, attributeValue, null);
}
else if (propertyType == typeof(bool)) {
property.SetValue(sitePart, Boolean.Parse(attributeValue), null);
}
else if (propertyType == typeof(int)) {
property.SetValue(sitePart, Int32.Parse(attributeValue), null);
}
}
}
}
}

View File

@@ -302,6 +302,7 @@
<Compile Include="Services\DefaultJsonConverter.cs" />
<Compile Include="Services\IJsonConverter.cs" />
<Compile Include="Settings\CurrentSiteWorkContext.cs" />
<Compile Include="ContentManagement\Drivers\DefaultSettingsPartImportExport.cs" />
<Compile Include="Settings\ResourceDebugMode.cs" />
<Compile Include="Themes\CurrentThemeWorkContext.cs" />
<Compile Include="Themes\ThemeManager.cs" />