forked from aiska/BpmNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessManager.cs
More file actions
102 lines (88 loc) · 3.88 KB
/
Copy pathProcessManager.cs
File metadata and controls
102 lines (88 loc) · 3.88 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
using BPMNET.Bpmn;
using BPMNET.Exception;
using Ciloci.Flee;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
namespace BPMNET.EntityCore.Manager
{
public class ProcessManager
{
ProcessInstanceStore procInsStore;
BpmnProcessStore procStore;
ProcessItemDefinitionStore itemDefStore;
ProcessHistoryStore hisStore;
ProcessTaskStore taskStore;
public ProcessManager(DbContext context)
{
procInsStore = new ProcessInstanceStore(context);
procStore = new BpmnProcessStore(context);
itemDefStore = new ProcessItemDefinitionStore(context);
hisStore = new ProcessHistoryStore(context);
taskStore = new ProcessTaskStore(context);
}
public bool StartProcess(Guid processKey, string processInstanceName, string user)
{
bool result = false;
if (string.IsNullOrWhiteSpace(processInstanceName))
throw new BpmnVariableEmptyException("processInstanceName");
BpmnProcess proc = procStore.FindById(processKey);
if (proc == null)
throw new BpmnProcessNotFoundException(string.Format("Bpmn Process with key: '{0}' not found.", processKey.ToString()));
ProcessInstance pi = new ProcessInstance();
pi.ProcessKey = processKey;
pi.ProcessInstanceName = processInstanceName;
pi.Owner = user;
//Add to history
AddHistory(proc.Name, pi.ProcessInstanceName, "", string.Format("User: {0} Started Process '{1}'", user, proc.Name), user);
ProcessItemDefinition[] startedItem = itemDefStore.GetStartEvent(processKey).ToArray();
if (startedItem != null)
{
foreach (var started in startedItem)
{
Guid[] nextItems = itemDefStore.GetSequenceFlow(started.Key).ToArray();
if (nextItems != null)
{
foreach (var item in nextItems)
{
ProcessItemDefinition def = itemDefStore.FindById(item);
if (def != null && IsTask(def.ItemType))
{
ProcessTask t = new ProcessTask();
t.ProcessKey = proc.Key;
t.ProcessInstanceId = pi.ProcessInstanceId;
t.ProcessItemDefinitionId = def.Key;
t.ProcessTaskName = def.ItemName;
taskStore.Create(t);
//Add to history
AddHistory(proc.Name, pi.ProcessInstanceName, t.ProcessTaskName, string.Format("User: {0} Created Task '{1}'", user, proc.Name), user);
}
}
}
}
}
return result;
}
private bool IsTask(ProcessItemType type)
{
return type == ProcessItemType.UserTask ||
type == ProcessItemType.ManualTask;
}
private ProcessItemDefinition StartFlow(Guid processKey)
{
return itemDefStore.Entities.FirstOrDefault(t => t.ItemType.Equals(Bpmn.ProcessItemType.StartEvent) && t.ProcessKey.Equals(processKey));
}
private void AddHistory(string processName, string processInstanceName, string taskName, string description, string user)
{
ProcessHistory history = new ProcessHistory();
history.ProcessName = processName;
history.ProcessInstanceName = processInstanceName;
history.TaskName = taskName;
history.Description = description;
history.User = user;
hisStore.Create(history);
}
}
}