Merge pull request #5392 from paynecrl97/5356/abstract_active_layer_evaluation

Abstract active layer evaluation away from the Widget Filter
This commit is contained in:
Sébastien Ros
2015-06-18 12:53:21 -07:00
5 changed files with 78 additions and 28 deletions

View File

@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Aspects;
@@ -9,25 +8,24 @@ using Orchard.Logging;
using Orchard.Mvc.Filters;
using Orchard.Themes;
using Orchard.UI.Admin;
using Orchard.Widgets.Models;
using Orchard.Widgets.Services;
namespace Orchard.Widgets.Filters {
public class WidgetFilter : FilterProvider, IResultFilter {
private readonly IWorkContextAccessor _workContextAccessor;
private readonly IRuleManager _ruleManager;
private readonly IWidgetsService _widgetsService;
private readonly IOrchardServices _orchardServices;
private readonly ILayerEvaluationService _layerEvaluationService;
public WidgetFilter(
IWorkContextAccessor workContextAccessor,
IRuleManager ruleManager,
IWorkContextAccessor workContextAccessor,
IWidgetsService widgetsService,
IOrchardServices orchardServices) {
IOrchardServices orchardServices,
ILayerEvaluationService layerEvaluationService) {
_workContextAccessor = workContextAccessor;
_ruleManager = ruleManager;
_widgetsService = widgetsService;
_orchardServices = orchardServices;
_layerEvaluationService = layerEvaluationService;
Logger = NullLogger.Instance;
T = NullLocalizer.Instance;
}
@@ -51,25 +49,7 @@ namespace Orchard.Widgets.Filters {
return;
}
// Once the Rule Engine is done:
// Get Layers and filter by zone and rule
// NOTE: .ForType("Layer") is faster than .Query<LayerPart, LayerPartRecord>()
IEnumerable<LayerPart> activeLayers = _orchardServices.ContentManager.Query<LayerPart>().ForType("Layer").List();
var activeLayerIds = new List<int>();
foreach (var activeLayer in activeLayers) {
// ignore the rule if it fails to execute
try {
if (_ruleManager.Matches(activeLayer.LayerRule)) {
activeLayerIds.Add(activeLayer.ContentItem.Id);
}
}
catch(Exception e) {
Logger.Warning(e, T("An error occured during layer evaluation on: {0}", activeLayer.Name).Text);
}
}
IEnumerable<WidgetPart> widgetParts = _widgetsService.GetWidgets(layerIds: activeLayerIds.ToArray());
var widgetParts = _widgetsService.GetWidgets(_layerEvaluationService.GetActiveLayerIds());
// Build and add shape to zone.
var zones = workContext.Layout.Zones;

View File

@@ -90,6 +90,8 @@
<Compile Include="RuleEngine\ContentDisplayedRuleProvider.cs" />
<Compile Include="RuleEngine\RuleManager.cs" />
<Compile Include="RuleEngine\UrlRuleProvider.cs" />
<Compile Include="Services\DefaultLayerEvaluationService.cs" />
<Compile Include="Services\ILayerEvaluationService.cs" />
<Compile Include="Services\IRuleManager.cs" />
<Compile Include="Services\IRuleProvider.cs" />
<Compile Include="Services\IWidgetsService.cs" />

View File

@@ -20,7 +20,7 @@ namespace Orchard.Widgets.RuleEngine {
public bool Matches(string expression) {
var evaluator = _evaluators.FirstOrDefault();
if (evaluator == null) {
throw new OrchardException(T("There are currently not scripting engine enabled"));
throw new OrchardException(T("There are currently no scripting engines enabled"));
}
var result = evaluator.Evaluate(expression, new List<IGlobalMethodProvider> { new GlobalMethodProvider(this) });

View File

@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using Orchard.Localization;
using Orchard.Logging;
using Orchard.Widgets.Models;
using Orchard.ContentManagement;
using Orchard.Core.Common.Utilities;
namespace Orchard.Widgets.Services{
public class DefaultLayerEvaluationService : ILayerEvaluationService {
private readonly IRuleManager _ruleManager;
private readonly IOrchardServices _orchardServices;
private readonly LazyField<int[]> _activeLayerIDs;
public DefaultLayerEvaluationService(IRuleManager ruleManager, IOrchardServices orchardServices) {
_ruleManager = ruleManager;
_orchardServices = orchardServices;
Logger = NullLogger.Instance;
T = NullLocalizer.Instance;
_activeLayerIDs = new LazyField<int[]>();
_activeLayerIDs.Loader(PopulateActiveLayers);
}
public ILogger Logger { get; set; }
public Localizer T { get; set; }
/// <summary>
/// Retrieves every Layer from the Content Manager and evaluates each one.
/// </summary>
/// <returns>
/// A collection of integers that represents the Ids of each active Layer
/// </returns>
public int[] GetActiveLayerIds() {
return _activeLayerIDs.Value;
}
private int[] PopulateActiveLayers() {
// Once the Rule Engine is done:
// Get Layers and filter by zone and rule
// NOTE: .ForType("Layer") is faster than .Query<LayerPart, LayerPartRecord>()
var activeLayers = _orchardServices.ContentManager.Query<LayerPart>().ForType("Layer").List();
var activeLayerIds = new List<int>();
foreach (var activeLayer in activeLayers) {
// ignore the rule if it fails to execute
try {
if (_ruleManager.Matches(activeLayer.LayerRule)) {
activeLayerIds.Add(activeLayer.ContentItem.Id);
}
}
catch (Exception e) {
Logger.Warning(e, T("An error occurred during layer evaluation on: {0}", activeLayer.Name).Text);
}
}
return activeLayerIds.ToArray();
}
}
}

View File

@@ -0,0 +1,6 @@
namespace Orchard.Widgets.Services
{
public interface ILayerEvaluationService : IDependency {
int[] GetActiveLayerIds();
}
}