-
Notifications
You must be signed in to change notification settings - Fork 0
/
bpmn-history.js
242 lines (205 loc) · 6.82 KB
/
bpmn-history.js
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* global Bpmn:true */
import { Mongo } from 'meteor/mongo'
import { check, Match } from 'meteor/check'
import { Meteor } from 'meteor/meteor'
const _name = 'extensions:history'
const history = {}
history.name = _name
// //////////////////////////////////////////////////////////////////////////////////////
//
// Extend Events
//
// //////////////////////////////////////////////////////////////////////////////////////
/**
* @override Bpmn.Events
* @description Extends the default list of events by 'resume' and 'stop' which are added to the history on stop/resume.
* @type {String}
*/
Bpmn.Events = Object.assign(Bpmn.Events, {
resume: 'resume',
stop: 'stop',
})
// //////////////////////////////////////////////////////////////////////////////////////
//
// Define Collection
//
// //////////////////////////////////////////////////////////////////////////////////////
/**
* Optional schema, that can be attached to the collection for the history log.
* @type {{instanceId: String, processId: String, elementId: String, eventName: String, state: String}}
*/
const BpmnHistoryCollectionSchema = {
createdAt: Date,
createdBy: String,
instanceId: String,
processId: String,
elementId: String,
eventName: String,
stateName: String,
}
/**
* Default name of the collection for the history log.
* @type {string}
*/
const historyCollectionName = 'BpmnHistoryCollection'
/**
* The default collection for logging history of events.
* @type {Mongo.Collection | String}
*/
const BpmnHistoryCollection = new Mongo.Collection(historyCollectionName)
BpmnHistoryCollection.schema = BpmnHistoryCollectionSchema
BpmnHistoryCollection.name = historyCollectionName
history.collection = BpmnHistoryCollection
// //////////////////////////////////////////////////////////////////////////////////////
//
// addToHistory
//
// //////////////////////////////////////////////////////////////////////////////////////
history.methods = {}
/**
* Adds a history entry to the history collection by given parameters.
* @param eventName The name of the event that occurred. see {Bpmn.Events}
* @param elementId The id of the current process element (activity), involved in the event.
* @param processId The id of the current process, involved in the event.
* @param instanceId The id of the current Bpmn.Engine instance.
* @param stateName String of the current state, gathered by {Bpmn.Engine.prototype.getState().state}
* @param userId (optional) id of the user involved int the event.
* @returns {String} The id of the inserted document.
*/
function addToHistory ({eventName, elementId, processId, instanceId, stateName, userId}) {
check(eventName, String)
check(elementId, String)
check(processId, String)
check(instanceId, String)
check(stateName, String)
check(userId, Match.Maybe(String))
const createdBy = userId || this.userId
const insertDocId = BpmnHistoryCollection.insert({
instanceId,
processId,
stateName,
elementId,
eventName,
createdAt: new Date(),
createdBy,
})
if (!insertDocId || !BpmnHistoryCollection.findOne(insertDocId))
throw new Error('history document not inserted on event ' + eventName + ' instanceId=' + instanceId)
return insertDocId
}
/**
* @inheritDoc {addToHistory}
*/
history.add = addToHistory
// //////////////////////////////////////////////////////////////////////////////////////
//
// HOOKS
//
// //////////////////////////////////////////////////////////////////////////////////////
const historyHooks = {}
historyHooks.onExecuteBefore = function (engineFct, options) {
const engine = engineFct()
const preventEvents = options && options.prevent
// listen to engine's end unless prevented
const preventEnd = preventEvents && preventEvents.end === false
if (!preventEnd) {
engine.on('end', Meteor.bindEnvironment((process) => {
history.add({
eventName: engine.stopped ? 'stop' : 'end',
elementId: process.id,
instanceId: engine.instanceId || options.instanceId,
processId: process.id,
stateName: engine.getState().state,
})
}))
}
// listen to element events
const historyListener = Bpmn.createListeners((processElement, processInstance, event) => {
history.add({
eventName: event,
elementId: processElement.id,
instanceId: engine.instanceId,
processId: processInstance.id,
stateName: engine.getState().state,
})
}, preventEvents ? preventEvents : undefined)
options.listener = Bpmn.mergeListeners({
source: options.listener,
target: historyListener,
})
}
historyHooks.onResumeBefore = function (engineFct, options) {
const preventEvents = options && options.prevent
// also assign listener
// on each event on resume
// but with respect of prevent flags
const historyListener = Bpmn.createListeners((processElement, processInstance, event) => {
const engine = engineFct()
history.add({
eventName: event,
elementId: processElement.id,
instanceId: options.instanceId,
processId: processInstance.id,
stateName: engine && engine.getState().state,
})
}, preventEvents ? preventEvents : undefined)
options.listener = Bpmn.mergeListeners({
source: options.listener,
target: historyListener,
})
}
historyHooks.onResume = function (engineFct, options) {
// log resume event
const engine = engineFct()
const instanceId = engine.instanceId || options.instanceId
const state = engine.getState()
Meteor.defer(function () {
history.add({
eventName: 'resume',
elementId: 'undefined',
instanceId,
processId: 'undefined',
stateName: state.state,
})
})
}
historyHooks.onResumeAfter = Meteor.bindEnvironment(function (engineFct, options) {
const engine = engineFct()
const preventEvents = options && options.prevent
// listen to engine's end unless prevented
const preventEnd = preventEvents && preventEvents.end === false
if (!preventEnd) {
engine.on('end', Meteor.bindEnvironment((process) => {
history.add({
eventName: 'end',
elementId: process.id,
instanceId: engine.instanceId,
processId: process.id,
stateName: engine.getState().state,
})
}))
}
})
historyHooks.onStopBefore = Meteor.bindEnvironment(function (engineFct) {
const engine = engineFct()
history.add({
eventName: 'stop',
elementId: 'undefined',
instanceId: engine.instanceId,
processId: 'undefined',
stateName: engine.getState().state,
})
})
history.hooks = historyHooks
history.on = function on () {
Bpmn.hooks.add(history.name, historyHooks)
}
history.off = function off () {
Bpmn.hooks.remove(history.name)
}
// //////////////////////////////////////////////////////////////////////////////////////
//
// ASSIGN EXTENSION
//
// //////////////////////////////////////////////////////////////////////////////////////
Bpmn.history = history