forked from optimajet/WorkflowEngine.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkflowInit.cs
More file actions
154 lines (129 loc) · 5.4 KB
/
WorkflowInit.cs
File metadata and controls
154 lines (129 loc) · 5.4 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
using System.Configuration;
using System.Reflection;
using System.Xml.Linq;
using OptimaJet.Workflow.Core.Builder;
using OptimaJet.Workflow.Core.Bus;
using OptimaJet.Workflow.Core.Parser;
using OptimaJet.Workflow.Core.Runtime;
using OptimaJet.Workflow.Core.Persistence;
using WF.Sample.Business.Helpers;
namespace WF.Sample.Business.Workflow
{
public static class WorkflowInit
{
private static volatile WorkflowRuntime _runtime;
private static readonly object _sync = new object();
public static WorkflowRuntime Runtime
{
get
{
if (_runtime == null)
{
lock (_sync)
{
if (_runtime == null)
{
var connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
var provider = new OptimaJet.Workflow.DbPersistence.MSSQLProvider(connectionString);
var builder = new WorkflowBuilder<XElement>(provider,
new XmlWorkflowParser(),
provider
).WithDefaultCache();
_runtime = new WorkflowRuntime()
.WithBuilder(builder)
.WithActionProvider(new WorkflowActions())
.WithRuleProvider(new WorkflowRule())
.WithPersistenceProvider(provider)
.WithTimerManager(new TimerManager())
.WithBus(new NullBus())
.SwitchAutoUpdateSchemeBeforeGetAvailableCommandsOn()
.RegisterAssemblyForCodeActions(Assembly.GetExecutingAssembly())
.RegisterAssemblyForCodeActions(Assembly.GetAssembly(typeof(System.Data.Linq.DataContext)))
.Start();
_runtime.ProcessStatusChanged += _runtime_ProcessStatusChanged;
}
}
}
return _runtime;
}
}
static void _runtime_ProcessStatusChanged(object sender, ProcessStatusChangedEventArgs e)
{
if (e.NewStatus != ProcessStatus.Idled && e.NewStatus != ProcessStatus.Finalized)
return;
if (string.IsNullOrEmpty(e.SchemeCode))
return;
WorkflowActions.DeleteEmptyPreHistory(e.ProcessId);
_runtime.PreExecuteFromCurrentActivity(e.ProcessId);
//Inbox
using (var context = new DataModelDataContext())
{
context.DropWorkflowInbox(e.ProcessId);
context.SubmitChanges();
}
if (e.NewStatus != ProcessStatus.Finalized)
{
var d = new Action<ProcessStatusChangedEventArgs>(PreExecuteAndFillInbox);
d.BeginInvoke(e, FillInboxCallback, null);
}
//Change state name
var nextState = WorkflowInit.Runtime.GetLocalizedStateName(e.ProcessId, e.ProcessInstance.CurrentState);
using (var context = new DataModelDataContext())
{
var document = DocumentHelper.Get(e.ProcessId, context);
if (document == null)
return;
document.StateName = nextState;
context.SubmitChanges();
}
}
#region Inbox
private static void FillInboxCallback(IAsyncResult ar)
{
}
private static void PreExecuteAndFillInbox(ProcessStatusChangedEventArgs e)
{
var processId = e.ProcessId;
using (var context = new DataModelDataContext())
{
FillInbox(processId, context);
context.SubmitChanges();
}
}
private static void FillInbox(Guid processId, DataModelDataContext context)
{
var newActors = Runtime.GetAllActorsForDirectCommandTransitions(processId);
foreach (var newActor in newActors)
{
var newInboxItem = new WorkflowInbox() { Id = Guid.NewGuid(), IdentityId = new Guid(newActor), ProcessId = processId };
context.WorkflowInboxes.InsertOnSubmit(newInboxItem);
}
}
public static void RecalcInbox()
{
using (var context = new DataModelDataContext())
{
foreach (var d in WF.Sample.Business.Helpers.DocumentHelper.GetAll())
{
Guid id = d.Id;
try
{
if (_runtime.IsProcessExists(id))
{
_runtime.UpdateSchemeIfObsolete(id);
context.DropWorkflowInbox(id);
FillInbox(id, context);
context.SubmitChanges();
}
}
catch (Exception ex)
{
throw new Exception(string.Format("Unable to calculate the inbox for process Id = {0}", id), ex);
}
}
}
}
#endregion
}
}