Adds ILayerEvaluationService and implementation.

The default implementation of this service is merely an abstraction of the logic that already existed in `WidgetFilter`
This commit is contained in:
paynecrl97
2015-06-08 10:47:16 +01:00
parent e810d885a0
commit ffcccf7ae1
5 changed files with 69 additions and 29 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().ToArray());
// 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

@@ -1,6 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using Orchard.Localization;
using Orchard.Scripting;
using Orchard.Widgets.Services;
@@ -21,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,51 @@
using System;
using System.Collections.Generic;
using Orchard.Localization;
using Orchard.Logging;
using Orchard.Widgets.Models;
using Orchard.ContentManagement;
namespace Orchard.Widgets.Services{
public class DefaultLayerEvaluationService : ILayerEvaluationService {
private readonly IRuleManager _ruleManager;
private readonly IOrchardServices _orchardServices;
public DefaultLayerEvaluationService(IRuleManager ruleManager, IOrchardServices orchardServices) {
_ruleManager = ruleManager;
_orchardServices = orchardServices;
Logger = NullLogger.Instance;
T = NullLocalizer.Instance;
}
public ILogger Logger { get; set; }
public Localizer T { get; private set; }
public IEnumerable<int> GetActiveLayerIds()
{
// 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 occured during layer evaluation on: {0}", activeLayer.Name).Text);
}
}
return activeLayerIds;
}
}
}

View File

@@ -0,0 +1,8 @@
using System.Collections.Generic;
namespace Orchard.Widgets.Services
{
public interface ILayerEvaluationService : IDependency {
IEnumerable<int> GetActiveLayerIds();
}
}