#18930: Isolating fields in content handlers

Work Item: 18930

--HG--
branch : 1.x
This commit is contained in:
Sebastien Ros
2012-09-28 17:49:37 -07:00
parent 5c1cc397ce
commit 88cd865396
8 changed files with 66 additions and 21 deletions

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using JetBrains.Annotations;
@@ -37,10 +38,8 @@ namespace Orchard.Core.Common.Drivers {
return ContentShape("Fields_Common_Text", GetDifferentiator(field, part),
() => {
var settings = field.PartFieldDefinition.Settings.GetModel<TextFieldSettings>();
object fieldValue = field.Value;
fieldValue = new HtmlString(_htmlFilters.Aggregate(field.Value, (text, filter) => filter.ProcessContent(text, settings.Flavor)));
object fieldValue = new HtmlString(_htmlFilters.Aggregate(field.Value, (text, filter) => filter.ProcessContent(text, settings.Flavor)));
return shapeHelper.Fields_Common_Text(Name: field.Name, Value: fieldValue);
});
}
@@ -59,6 +58,11 @@ namespace Orchard.Core.Common.Drivers {
}
protected override DriverResult Editor(ContentPart part, TextField field, IUpdateModel updater, dynamic shapeHelper) {
if(field.Name == "Note") {
throw new ArgumentException();
}
var viewModel = new TextFieldDriverViewModel {
Field = field,
Text = field.Value,

View File

@@ -5,14 +5,15 @@ using Orchard.ContentManagement.Handlers;
using Orchard.ContentManagement.MetaData;
using Orchard.DisplayManagement;
using Orchard.DisplayManagement.Shapes;
using Orchard.Logging;
namespace Orchard.ContentManagement.Drivers {
public abstract class ContentFieldDriver<TField> : IContentFieldDriver where TField : ContentField, new() {
protected virtual string Prefix { get { return ""; } }
protected virtual string Zone { get { return "Content"; } }
void IContentFieldDriver.GetContentItemMetadata(GetContentItemMetadataContext context) {
Process(context.ContentItem, (part, field) => GetContentItemMetadata(part, field, context.Metadata));
void IContentFieldDriver.GetContentItemMetadata(GetContentItemMetadataContext context) {
Process(context.ContentItem, (part, field) => GetContentItemMetadata(part, field, context.Metadata), context.Logger);
}
DriverResult IContentFieldDriver.BuildDisplayShape(BuildDisplayContext context) {
@@ -25,7 +26,7 @@ namespace Orchard.ContentManagement.Drivers {
}
return result;
});
}, context.Logger);
}
DriverResult IContentFieldDriver.BuildEditorShape(BuildEditorContext context) {
@@ -38,7 +39,7 @@ namespace Orchard.ContentManagement.Drivers {
}
return result;
});
}, context.Logger);
}
DriverResult IContentFieldDriver.UpdateEditorShape(UpdateEditorContext context) {
@@ -51,40 +52,39 @@ namespace Orchard.ContentManagement.Drivers {
}
return result;
});
}, context.Logger);
}
void IContentFieldDriver.Importing(ImportContentContext context) {
Process(context.ContentItem, (part, field) => Importing(part, field, context));
Process(context.ContentItem, (part, field) => Importing(part, field, context), context.Logger);
}
void IContentFieldDriver.Imported(ImportContentContext context) {
Process(context.ContentItem, (part, field) => Imported(part, field, context));
Process(context.ContentItem, (part, field) => Imported(part, field, context), context.Logger);
}
void IContentFieldDriver.Exporting(ExportContentContext context) {
Process(context.ContentItem, (part, field) => Exporting(part, field, context));
Process(context.ContentItem, (part, field) => Exporting(part, field, context), context.Logger);
}
void IContentFieldDriver.Exported(ExportContentContext context) {
Process(context.ContentItem, (part, field) => Exported(part, field, context));
Process(context.ContentItem, (part, field) => Exported(part, field, context), context.Logger);
}
void IContentFieldDriver.Describe(DescribeMembersContext context) {
Describe(context);
}
void Process(ContentItem item, Action<ContentPart, TField> effort) {
void Process(ContentItem item, Action<ContentPart, TField> effort, ILogger logger) {
var occurences = item.Parts.SelectMany(part => part.Fields.OfType<TField>().Select(field => new { part, field }));
foreach (var occurence in occurences) {
effort(occurence.part, occurence.field);
}
occurences.Invoke(pf => effort(pf.part, pf.field), logger);
}
DriverResult Process(ContentItem item, Func<ContentPart, TField, DriverResult> effort) {
DriverResult Process(ContentItem item, Func<ContentPart, TField, DriverResult> effort, ILogger logger) {
var results = item.Parts
.SelectMany(part => part.Fields.OfType<TField>().Select(field => new { part, field }))
.Select(pf => effort(pf.part, pf.field));
.SelectMany(part => part.Fields.OfType<TField>().Select(field => new { part, field }))
.Invoke(pf => effort(pf.part, pf.field), logger);
return Combined(results.ToArray());
}

View File

@@ -25,7 +25,7 @@ namespace Orchard.ContentManagement.Drivers.Coordinators {
public ILogger Logger { get; set; }
public override void Initializing(InitializingContentContext context) {
var fieldInfos = _drivers.SelectMany(x => x.GetFieldInfo());
var fieldInfos = _drivers.SelectMany(x => x.GetFieldInfo()).ToArray();
var parts = context.ContentItem.Parts;
foreach (var contentPart in parts) {
foreach (var partFieldDefinition in contentPart.PartDefinition.Fields) {
@@ -46,11 +46,13 @@ namespace Orchard.ContentManagement.Drivers.Coordinators {
}
public override void GetContentItemMetadata(GetContentItemMetadataContext context) {
context.Logger = Logger;
_drivers.Invoke(driver => driver.GetContentItemMetadata(context), Logger);
}
public override void BuildDisplay(BuildDisplayContext context) {
_drivers.Invoke(driver => {
context.Logger = Logger;
var result = driver.BuildDisplayShape(context);
if (result != null)
result.Apply(context);
@@ -59,6 +61,7 @@ namespace Orchard.ContentManagement.Drivers.Coordinators {
public override void BuildEditor(BuildEditorContext context) {
_drivers.Invoke(driver => {
context.Logger = Logger;
var result = driver.BuildEditorShape(context);
if (result != null)
result.Apply(context);
@@ -67,6 +70,7 @@ namespace Orchard.ContentManagement.Drivers.Coordinators {
public override void UpdateEditor(UpdateEditorContext context) {
_drivers.Invoke(driver => {
context.Logger = Logger;
var result = driver.UpdateEditorShape(context);
if (result != null)
result.Apply(context);
@@ -74,24 +78,28 @@ namespace Orchard.ContentManagement.Drivers.Coordinators {
}
public override void Importing(ImportContentContext context) {
context.Logger = Logger;
foreach (var contentFieldDriver in _drivers) {
contentFieldDriver.Importing(context);
}
}
public override void Imported(ImportContentContext context) {
context.Logger = Logger;
foreach (var contentFieldDriver in _drivers) {
contentFieldDriver.Imported(context);
}
}
public override void Exporting(ExportContentContext context) {
context.Logger = Logger;
foreach (var contentFieldDriver in _drivers) {
contentFieldDriver.Exporting(context);
}
}
public override void Exported(ExportContentContext context) {
context.Logger = Logger;
foreach (var contentFieldDriver in _drivers) {
contentFieldDriver.Exported(context);
}

View File

@@ -1,6 +1,7 @@
using System;
using Orchard.DisplayManagement;
using Orchard.DisplayManagement.Descriptors;
using Orchard.Logging;
namespace Orchard.ContentManagement.Handlers {
public class BuildShapeContext {
@@ -20,6 +21,7 @@ namespace Orchard.ContentManagement.Handlers {
public IShape Layout { get; set; }
public string GroupId { get; private set; }
public ContentPart ContentPart { get; set; }
public ILogger Logger { get; set; }
public Func<string, string, string, PlacementInfo> FindPlacement { get; set; }
}

View File

@@ -1,4 +1,5 @@
using Orchard.ContentManagement.Records;
using Orchard.Logging;
namespace Orchard.ContentManagement.Handlers {
public class ContentContextBase {
@@ -15,5 +16,6 @@ namespace Orchard.ContentManagement.Handlers {
public ContentItem ContentItem { get; private set; }
public ContentItemRecord ContentItemRecord { get; private set; }
public IContentManager ContentManager { get; private set; }
public ILogger Logger { get; set; }
}
}

View File

@@ -1,6 +1,9 @@
using Orchard.Logging;
namespace Orchard.ContentManagement.Handlers {
public class GetContentItemMetadataContext {
public ContentItem ContentItem { get; set; }
public ContentItemMetadata Metadata { get; set; }
public ILogger Logger { get; set; }
}
}

View File

@@ -6,6 +6,7 @@ namespace Orchard.ContentManagement.Handlers {
public XElement Data { get; set; }
private ImportContentSession Session { get; set; }
public ImportContentContext(ContentItem contentItem, XElement data, ImportContentSession importContentSession)
: base(contentItem) {
Data = data;

View File

@@ -31,6 +31,31 @@ namespace Orchard {
}
}
public static IEnumerable<TResult> Invoke<TEvents, TResult>(this IEnumerable<TEvents> events, Func<TEvents, TResult> dispatch, ILogger logger) {
foreach (var sink in events) {
TResult result = default(TResult);
try {
result = dispatch(sink);
}
catch (Exception ex) {
if (IsLogged(ex)) {
logger.Error(ex, "{2} thrown from {0} by {1}",
typeof(TEvents).Name,
sink.GetType().FullName,
ex.GetType().Name);
}
if (ex.IsFatal()) {
throw;
}
}
yield return result;
}
}
private static bool IsLogged(Exception ex) {
return ex is OrchardSecurityException || !ex.IsFatal();
}