Refactored LayerPart validation

- Log syntax errors on Warning level
- Move the validation to the Driver

--HG--
branch : dev
This commit is contained in:
Sebastien Ros
2010-10-20 14:30:31 -07:00
parent c2f46861bc
commit 0c17e162c9
3 changed files with 40 additions and 35 deletions

View File

@@ -7,7 +7,6 @@ using Orchard.Core.Contents.Controllers;
using Orchard.Localization;
using Orchard.UI.Admin;
using Orchard.UI.Notify;
using Orchard.UI.Widgets;
using Orchard.Widgets.Models;
using Orchard.Widgets.Services;
using Orchard.Widgets.ViewModels;
@@ -20,16 +19,13 @@ namespace Orchard.Widgets.Controllers {
private const string NotAuthorizedManageWidgetsLabel = "Not authorized to manage widgets";
private readonly IWidgetsService _widgetsService;
private readonly IRuleManager _ruleManager;
public AdminController(
IOrchardServices services,
IWidgetsService widgetsService,
IRuleManager ruleManager) {
IWidgetsService widgetsService) {
Services = services;
_widgetsService = widgetsService;
_ruleManager = ruleManager;
T = NullLocalizer.Instance;
}
@@ -179,8 +175,6 @@ namespace Orchard.Widgets.Controllers {
var model = Services.ContentManager.UpdateEditor(layerPart, this);
ValidateLayer(layerPart);
if (!ModelState.IsValid) {
Services.TransactionManager.Cancel();
return View(model);
@@ -227,8 +221,6 @@ namespace Orchard.Widgets.Controllers {
var model = Services.ContentManager.UpdateEditor(layerPart, this);
ValidateLayer(layerPart);
if (!ModelState.IsValid) {
Services.TransactionManager.Cancel();
return View(model);
@@ -339,27 +331,6 @@ namespace Orchard.Widgets.Controllers {
RedirectToAction("Index");
}
public bool ValidateLayer(LayerPart layer) {
if ( String.IsNullOrWhiteSpace(layer.LayerRule) ) {
layer.LayerRule = "true";
}
if(_widgetsService.GetLayers().Count(l => String.Equals(l.Name, layer.Name, StringComparison.InvariantCultureIgnoreCase)) > 1) { // the current layer counts for 1
ModelState.AddModelError("Name", T("A Layer with the same name already exists").Text);
return false;
}
try {
_ruleManager.Matches(layer.LayerRule);
}
catch ( Exception e ) {
ModelState.AddModelError("Description", T("The rule is not valid: {0}", e.Message).Text);
return false;
}
return true;
}
bool IUpdateModel.TryUpdateModel<TModel>(TModel model, string prefix, string[] includeProperties, string[] excludeProperties) {
return base.TryUpdateModel(model, prefix, includeProperties, excludeProperties);
}

View File

@@ -1,12 +1,31 @@
using JetBrains.Annotations;
using System;
using System.Linq;
using JetBrains.Annotations;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.Localization;
using Orchard.UI.Widgets;
using Orchard.Widgets.Models;
using Orchard.Widgets.Services;
namespace Orchard.Widgets.Drivers {
[UsedImplicitly]
public class LayerPartDriver : ContentPartDriver<LayerPart> {
private readonly IRuleManager _ruleManager;
private readonly IWidgetsService _widgetsService;
public LayerPartDriver(
IRuleManager ruleManager,
IWidgetsService widgetsService) {
_ruleManager = ruleManager;
_widgetsService = widgetsService;
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
protected override DriverResult Editor(LayerPart layerPart, dynamic shapeHelper) {
return ContentShape("Parts_Widgets_LayerPart",
@@ -14,7 +33,23 @@ namespace Orchard.Widgets.Drivers {
}
protected override DriverResult Editor(LayerPart layerPart, IUpdateModel updater, dynamic shapeHelper) {
updater.TryUpdateModel(layerPart, Prefix, null, null);
if(updater.TryUpdateModel(layerPart, Prefix, null, null)) {
if ( String.IsNullOrWhiteSpace(layerPart.LayerRule) ) {
layerPart.LayerRule = "true";
}
if ( _widgetsService.GetLayers().Any(l => String.Equals(l.Name, layerPart.Name, StringComparison.InvariantCultureIgnoreCase))) {
updater.AddModelError("Name", T("A Layer with the same name already exists"));
}
try {
_ruleManager.Matches(layerPart.LayerRule);
}
catch ( Exception e ) {
updater.AddModelError("Description", T("The rule is not valid: {0}", e.Message));
}
}
return Editor(layerPart, shapeHelper);
}
}

View File

@@ -47,9 +47,8 @@ namespace Orchard.Widgets.Filters {
IEnumerable<WidgetPart> widgetParts = _contentManager.Query<WidgetPart, WidgetPartRecord>().List();
IEnumerable<LayerPart> activeLayers = _contentManager.Query<LayerPart, LayerPartRecord>().List();
List<int> activeLayerIds = new List<int>();
var activeLayerIds = new List<int>();
foreach (var activeLayer in activeLayers) {
var context = workContext.HttpContext;
// ignore the rule if it fails to execute
try {
if (_ruleManager.Matches(activeLayer.Record.LayerRule)) {
@@ -57,7 +56,7 @@ namespace Orchard.Widgets.Filters {
}
}
catch(Exception e) {
Logger.Debug(e, T("An error occured during layer evaluation on: {0}", activeLayer.Name).Text);
Logger.Warning(e, T("An error occured during layer evaluation on: {0}", activeLayer.Name).Text);
}
}