Added Workflow State token provider.

This token provider enables tokenized fields to access WorkflowContext state values.
Example: {Workflow.State:MyData.MyProperty.SubProperty}
This commit is contained in:
Sipke Schoorstra
2014-01-12 17:42:28 +01:00
parent dee5a2104a
commit 013d55357b
2 changed files with 39 additions and 0 deletions

View File

@@ -176,6 +176,7 @@
<Compile Include="Services\IWorkflowManager.cs" />
<Compile Include="Services\WorkflowManager.cs" />
<Compile Include="Tokens\SignalTokens.cs" />
<Compile Include="Tokens\StateTokens.cs" />
<Compile Include="ViewModels\AdminEditViewModel.cs" />
<Compile Include="ViewModels\AdminIndexViewModel.cs" />
<Compile Include="ViewModels\WorkflowDefinitionViewModel.cs" />

View File

@@ -0,0 +1,38 @@
using System;
using System.Linq;
using Orchard.Tokens;
using Orchard.Workflows.Models;
namespace Orchard.Workflows.Tokens {
public class StateTokens : Component, ITokenProvider {
public void Describe(DescribeContext context) {
context.For("Workflow", T("Workflow"), T("Workflow tokens."))
.Token("State:*", T("State:<workflowcontext path>"), T("The workflow context state to access. Workflow.State:MyData.MyProperty.SubProperty"))
;
}
public void Evaluate(EvaluateContext context) {
context.For<WorkflowContext>("Workflow")
.Token(token => token.StartsWith("State:", StringComparison.OrdinalIgnoreCase) ? token.Substring("State:".Length) : null, ParseState);
}
/// <summary>
/// Resolves the specified expression into an object stored in WorkflowContext.
/// </summary>
/// <param name="expression">The expression resolving to the state stored in WorkflowContext. E.g. "MyData.MyProperty.MySubProperty"</param>
private object ParseState(string expression, WorkflowContext workflowContext) {
var path = expression.Split(new[] {'.'}, StringSplitOptions.RemoveEmptyEntries);
var first = path.First();
var obj = (dynamic)workflowContext.GetState(first);
if (path.Length > 1) {
foreach (var property in path.Skip(1)) {
obj = obj[property];
}
}
return obj;
}
}
}