Skip to content

Commit

Permalink
Argument bindable.
Browse files Browse the repository at this point in the history
  • Loading branch information
SeriaWei committed Mar 16, 2023
1 parent 3650c33 commit 32b6523
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 46 deletions.
57 changes: 57 additions & 0 deletions src/ZKEACMS.EventAction/ActionExecutor/ActionContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
* Copyright (c) ZKEASOFT. All rights reserved.
* http://www.zkea.net/licenses */

using Easy.RepositoryPattern;
using Fluid;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -13,6 +16,8 @@ namespace ZKEACMS.EventAction.ActionExecutor
{
public class ActionContent
{
private ConcurrentDictionary<string, IFluidTemplate> _templates = new ConcurrentDictionary<string, IFluidTemplate>();

[YamlMember(Alias = "name")]
public string Name { get; set; }

Expand All @@ -24,5 +29,57 @@ public class ActionContent

[YamlMember(Alias = "with")]
public Dictionary<string, string> With { get; set; }

public Dictionary<string, string> GetRendedWith(object model)
{
var renderedArgs = new Dictionary<string, string>();
if (With == null) return renderedArgs;

foreach (var item in With)
{
var templateResult = ParseTemplate(item.Key, item.Value).Result;
if (templateResult == null)
{
renderedArgs[item.Key] = item.Value;
continue;
}

renderedArgs[item.Key] = templateResult.Render(model);
}
return renderedArgs;
}
public ServiceResult<IFluidTemplate> ParseTemplate(string key, string template)
{
var result = new ServiceResult<IFluidTemplate>();
try
{
var templateResult = _templates.GetOrAdd(key, key =>
{
if (!FluidTemplateHelper.TryParse(template, out var result, out var error)) throw new Exception(error);
return result;
});
result.Result = templateResult;
}
catch (Exception ex)
{
result.AddRuleViolation("With", ex.Message);
}
return result;
}
public ServiceResult ValidateWith()
{
var result = new ServiceResult();
if (With == null) return result;

foreach (var item in With)
{
var template = ParseTemplate(item.Key, item.Value);
if (!template.HasViolation) continue;

result.AddRuleViolation(template.ErrorMessage);
break;
}
return result;
}
}
}
3 changes: 1 addition & 2 deletions src/ZKEACMS.EventAction/ActionExecutor/EventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public void Handle(object entity, EventArg e)
var executor = _executorManager.CreateExecutor(parsedAction.Uses);
if (executor == null) continue;

var arg = _eventActionService.RenderArguments(parsedAction.With, entity);
executor.Execute(arg, entity, e);
executor.Execute(new Arguments(parsedAction.GetRendedWith(entity)), entity, e);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/ZKEACMS.EventAction/FluidTemplateHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Linq;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Unicode;
using System.Threading.Tasks;
using ZKEACMS.EventAction.TemplateEngine;

Expand All @@ -24,7 +25,7 @@ public static string Render(this IFluidTemplate template, object model)
var context = new TemplateContext(model, templateOptions);
var viewModel = new TemplateViewModel { Model = model };
context.SetValue("this", new ViewModelAccessor(JObject.FromObject(viewModel)));
return template.Render(context, HtmlEncoder.Default);
return template.Render(context, HtmlEncoder.Create(UnicodeRanges.All));
}

public static bool TryParse(string template, out IFluidTemplate fluidTemplate, out string error)
Expand Down
52 changes: 11 additions & 41 deletions src/ZKEACMS.EventAction/Service/EventActionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public sealed class EventActionService : ServiceBase<Models.EventAction>, IEvent
private readonly ICacheManager<Dictionary<string, List<EventActionContent>>> _cacheManager;
private readonly ILogger<EventActionService> _logger;
private readonly ILocalize _localize;
private static ConcurrentDictionary<string, IFluidTemplate> _templates = new ConcurrentDictionary<string, IFluidTemplate>();

public EventActionService(IApplicationContext applicationContext, CMSDbContext dbContext,
ICacheManager<Dictionary<string, List<EventActionContent>>> cacheManager, ILogger<EventActionService> logger, ILocalize localize)
Expand Down Expand Up @@ -96,9 +95,17 @@ public Dictionary<string, List<EventActionContent>> GetAllActivedActinosFromCach
if (eventAction.Actions == null) return result;
foreach (var action in eventAction.Actions)
{
if (ExecutorManager.IsExecutorRegisted(action.Uses)) continue;

result.AddRuleViolation("Actions", _localize.Get("{0} is not available").FormatWith(action.Uses));
if (!ExecutorManager.IsExecutorRegisted(action.Uses))
{
result.AddRuleViolation("Actions", _localize.Get("{0} is not available").FormatWith(action.Uses));
break;
}
var validateResult = action.ValidateWith();
if (validateResult.HasViolation)
{
result.AddRuleViolation("Actions", validateResult.ErrorMessage);
break;
}
}
}
catch (Exception ex)
Expand All @@ -107,42 +114,5 @@ public Dictionary<string, List<EventActionContent>> GetAllActivedActinosFromCach
}
return result;
}

public Arguments RenderArguments(Dictionary<string, string> arguments, object model)
{
var renderedArgs = new Dictionary<string, string>();
if (arguments == null) return new Arguments(renderedArgs);
foreach (var item in arguments)
{
var templateResult = ParseTemplate(item.Value);
if (templateResult.HasViolation)
{
renderedArgs[item.Key] = item.Value;
continue;
}
renderedArgs[item.Key] = templateResult.Result.Render(model);
}
return new Arguments(renderedArgs);
}

private ServiceResult<IFluidTemplate> ParseTemplate(string template)
{
var result = new ServiceResult<IFluidTemplate>();
try
{
var templateResult = _templates.GetOrAdd(template, key =>
{
if (!FluidTemplateHelper.TryParse(key, out var result, out var error)) throw new Exception(error);
return result;
});
result.Result = templateResult;
}
catch (Exception ex)
{
result.AddRuleViolation(ex.Message);
}

return result;
}
}
}
2 changes: 0 additions & 2 deletions src/ZKEACMS.EventAction/Service/IEventActionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,5 @@ namespace ZKEACMS.EventAction.Service
public interface IEventActionService : IService<Models.EventAction>
{
Dictionary<string, List<EventActionContent>> GetAllActivedActinosFromCache();

Arguments RenderArguments(Dictionary<string, string> arguments, object model);
}
}

0 comments on commit 32b6523

Please sign in to comment.