forked from optimajet/WorkflowEngine.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTTXmlWorkflowGenerator.cs
More file actions
67 lines (54 loc) · 2.57 KB
/
TTXmlWorkflowGenerator.cs
File metadata and controls
67 lines (54 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Xml.Linq;
namespace OptimaJet.Workflow.Core.Generator
{
/// <summary>
/// Generate process scheme from TT template
/// </summary>
public class TTXmlWorkflowGenerator : IWorkflowGenerator<XElement>
{
protected IDictionary<string,Type> TemplateTypeMapping = new Dictionary<string, Type>();
/// <summary>
/// Generate not parsed process scheme
/// </summary>
/// <param name="schemeCode">Code of the scheme</param>
/// <param name="schemeId">Id of the scheme</param>
/// <param name="parameters">Parameters for creating scheme</param>
/// <returns>Not parsed process scheme</returns>
public XElement Generate(string schemeCode, Guid schemeId, IDictionary<string, object> parameters)
{
var processTemplateType = TemplateTypeMapping[schemeCode.ToLower()];
var sessionProperty = processTemplateType.GetTypeInfo().GetProperty("Session", typeof(IDictionary<string, object>));
var transformTextMethod = processTemplateType.GetTypeInfo().GetMethod("TransformText");
var initializeMethod = processTemplateType.GetTypeInfo().GetMethod("Initialize");
var obj = Activator.CreateInstance(processTemplateType, false);
var session = (IDictionary<string, object>)sessionProperty.GetGetMethod(false).Invoke(obj,new object[]{});
if (session == null)
{
session = new Dictionary<string, object>();
sessionProperty.GetSetMethod(false).Invoke(obj, new object[] {session});
}
session.Clear();
foreach (var parameter in parameters)
session.Add(parameter.Key,parameter.Value);
session.Add("SchemeId",schemeId);
initializeMethod.Invoke(obj, new object[] {});
var output = (string) transformTextMethod.Invoke(obj, new object[] {});
return XElement.Parse(output);
}
/// <summary>
/// Adds relationship between code of the scheme and TT template type
/// </summary>
/// <param name="schemeCode">Code of the scheme</param>
/// <param name="generatorSource">TT template type</param>
public void AddMapping(string schemeCode, object generatorSource)
{
var type = generatorSource as Type;
if (type == null)
throw new InvalidOperationException();
TemplateTypeMapping.Add(schemeCode.ToLower(), type);
}
}
}