-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathExecutionInstanceRepository.php
More file actions
350 lines (302 loc) · 11.1 KB
/
Copy pathExecutionInstanceRepository.php
File metadata and controls
350 lines (302 loc) · 11.1 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
namespace ProcessMaker\Repositories;
use Carbon\Carbon;
use ProcessMaker\Jobs\CaseStore;
use ProcessMaker\Jobs\CaseUpdateStatus;
use ProcessMaker\Models\AnonymousUser;
use ProcessMaker\Models\ProcessCollaboration;
use ProcessMaker\Models\ProcessRequest;
use ProcessMaker\Models\User;
use ProcessMaker\Nayra\Contracts\Bpmn\ParticipantInterface;
use ProcessMaker\Nayra\Contracts\Engine\ExecutionInstanceInterface;
use ProcessMaker\Nayra\Contracts\Repositories\ExecutionInstanceRepositoryInterface;
use ProcessMaker\Nayra\Contracts\Repositories\StorageInterface;
use ProcessMaker\Nayra\RepositoryTrait;
use ProcessMaker\SanitizeHelper;
/**
* Execution Instance Repository.
*/
class ExecutionInstanceRepository implements ExecutionInstanceRepositoryInterface
{
use RepositoryTrait;
private bool $abortIfInstanceNotFound = true;
private bool $loadTokens = true;
/**
* Set not found flag
*
* @param bool $abortIfInstanceNotFound
*/
public function setAbortIfInstanceNotFound(bool $abortIfInstanceNotFound)
{
$this->abortIfInstanceNotFound = $abortIfInstanceNotFound;
}
/**
* Set load tokens flag
*
* @param bool $loadTokens
*/
public function setLoadTokens(bool $loadTokens)
{
$this->loadTokens = $loadTokens;
}
/**
* Create an execution instance.
*
* @return ExecutionInstanceInterface
*/
public function createExecutionInstance(): ExecutionInstanceInterface
{
$instance = new ProcessRequest();
$instance->setId(uniqid('request', true));
return $instance;
}
/**
* Load an execution instance from a persistent storage.
*
* @param string $instanceId
* @param StorageInterface $storage
*
* @return ExecutionInstanceInterface|null
*/
public function loadExecutionInstanceByUid($instanceId, StorageInterface $storage): ?ExecutionInstanceInterface
{
// Get process request
if (is_numeric($instanceId)) {
$instance = ProcessRequest::find($instanceId);
} else {
$instance = ProcessRequest::where('uuid', $instanceId)->first();
}
// Finish if process request not exists
if (!$instance && $this->abortIfInstanceNotFound) {
abort(404, 'Instance not found');
} elseif (!$instance) {
return null;
}
// Get process
$callableId = $instance->callable_id;
$process = $storage->getProcess($callableId);
// Set data store
$dataStore = $storage->getFactory()->createDataStore();
$dataStore->setData($instance->data);
// Set process request properties
$instance->setId($instance->getKey());
$instance->setProcess($process);
$instance->setDataStore($dataStore);
// Get transitions
$process->getTransitions($storage->getFactory());
// Finish if is not necessary load the tokens
if (!$this->loadTokens) {
return $instance;
}
// Load tokens
$tokens = $instance->tokens()->where('status', '!=', 'CLOSED')->get();
foreach ($tokens as $token) {
$tokenInfo = [
'id' => $token->getKey(),
'status' => $token->status,
'index' => $token->element_index,
'element_ref' => $token->element_id,
];
$token->setProperties(array_merge($token->token_properties ?: [], $tokenInfo));
$element = $storage->getElementInstanceById($tokenInfo['element_ref']);
$element->addToken($instance, $token);
}
return $instance;
}
/**
* Create or update an execution instance to a persistent storage.
*
* @param ExecutionInstanceInterface $instance
*/
public function storeExecutionInstance(ExecutionInstanceInterface $instance)
{
// TODO: Implement store() method or Remove from Interface
}
/**
* Persists instance data related to the event Process Instance Created
*
* @param ExecutionInstanceInterface $instance
*
* @return mixed
*/
public function persistInstanceCreated(ExecutionInstanceInterface $instance)
{
// Get instance data
$data = $instance->getDataStore()->getData();
// Get the process
$process = $instance->getProcess();
// Get process definition
$definition = $process->getOwnerDocument()->getModel();
$version = $process->getOwnerDocument()->getProcessVersion();
// Do nothing if is not persistent
if ($process->isNonPersistent()) {
return;
}
// Check if instance is a subprocess
$parent = $data['_parent'] ?? null;
if (!empty($parent) && is_numeric($parent['request_id'])) {
$instance->parent_request_id = $parent['request_id'];
}
// Save process request
$instance->callable_id = $process->getId();
$instance->collaboration_uuid = $instance->getProperty('collaboration_uuid', null);
$instance->process_id = $definition->getKey();
$instance->process_version_id = $version?->getKey()
?: $definition->getLatestVersion()->getKey();
if ($instance->collaboration_uuid && !$instance->process_collaboration_id) {
$collaboration = ProcessCollaboration::firstOrCreate([
'uuid' => $instance->collaboration_uuid,
'process_id' => $instance->process_id,
]);
if ($collaboration) {
$instance->process_collaboration_id = $collaboration->id;
}
}
$instance->user_id = pmUser() ? pmUser()->getKey() : app(AnonymousUser::class)->id;
$instance->name = $definition->name;
$instance->status = 'ACTIVE';
$instance->initiated_at = Carbon::now();
$instance->do_not_sanitize = SanitizeHelper::getDoNotSanitizeFields($definition);
$instance->data = $data;
$instance->saveOrFail();
// Set id
$instance->setId($instance->getKey());
CaseStore::dispatchSync($instance);
// Persists collaboration
$this->persistCollaboration($instance);
}
/**
* Persists instance when an error occurs
*
* @param ExecutionInstanceInterface $instance
* @return mixed
*/
public function persistInstanceError(ExecutionInstanceInterface $instance)
{
// Get process
$process = $instance->getProcess();
// Do nothing if is not persistent
if ($process->isNonPersistent()) {
return;
}
// Save instance with error
$instance->status = 'ERROR';
$instance->mergeLatestStoredData();
$instance->saveOrFail();
CaseUpdateStatus::dispatchSync($instance);
}
/**
* Persists instance data related to the event Process Instance Completed
*
* @param ExecutionInstanceInterface $instance
* @return mixed
*/
public function persistInstanceUpdated(ExecutionInstanceInterface $instance)
{
// Get process
$process = $instance->getProcess();
// Do nothing if is not persistent
if ($process->isNonPersistent()) {
return;
}
// Save updated instance
if (!$instance->status) {
$instance->status = 'ACTIVE';
}
$instance->mergeLatestStoredData();
$instance->saveOrFail();
CaseUpdateStatus::dispatchSync($instance);
}
/**
* Persists instance data related to the event Process Instance Completed
*
* @param ExecutionInstanceInterface $instance
* @return mixed
*/
public function persistInstanceCompleted(ExecutionInstanceInterface $instance)
{
// Get process
$process = $instance->getProcess();
// Do nothing if is not persistent
if ($process->isNonPersistent()) {
return;
}
// Save completed instance
$instance->status = 'COMPLETED';
$instance->completed_at = Carbon::now();
$instance->mergeLatestStoredData();
$instance->saveOrFail();
CaseUpdateStatus::dispatchSync($instance);
}
/**
* Persists collaboration between two instances.
*
* @param ExecutionInstanceInterface $instance Target instance
* @param ParticipantInterface $participant Participant related to the target instance
* @param ExecutionInstanceInterface $source Source instance
* @param ParticipantInterface $sourceParticipant Source participant
*/
public function persistInstanceCollaboration(ExecutionInstanceInterface $instance, ParticipantInterface $participant = null, ExecutionInstanceInterface $source, ParticipantInterface $sourceParticipant = null)
{
// Get process
$process = $instance->getProcess();
// Do nothing if is not persistent
if ($process->isNonPersistent()) {
return;
}
// Get collaboration id if not exists
if ($source->process_collaboration_id === null) {
$collaboration = new ProcessCollaboration();
$collaboration->process_id = $instance->process->getKey();
$collaboration->saveOrFail();
$source->process_collaboration_id = $collaboration->getKey();
$source->saveOrFail();
}
// Save collaboration
$instance->process_collaboration_id = $source->process_collaboration_id;
$instance->participant_id = $participant ? $participant->getId() : null;
$instance->saveOrFail();
}
/**
* Persist current collaboration.
*
* @param ProcessRequest $request
*/
private function persistCollaboration(ProcessRequest $request)
{
// Get valid engine
$engine = $request->getProcess()->getEngine();
if ($engine) {
if (count($engine->getExecutionInstances()) <= 1) {
return;
}
// Get current collaboration
$collaboration = null;
foreach ($engine->getExecutionInstances() as $instance) {
if ($instance->collaboration) {
$collaboration = $instance->collaboration;
break;
}
}
// If not exists a collaboration, create a new one
if (!$collaboration) {
$collaboration = new ProcessCollaboration();
$collaboration->process_id = $request->process->getKey();
$collaboration->saveOrFail();
}
// Update collaboration id
$request->process_collaboration_id = $collaboration->id;
$request->saveOrFail();
} elseif ($request->collaboration_uuid) {
// find by uuid or create
$collaboration = ProcessCollaboration::firstOrCreate([
'uuid' => $request->collaboration_uuid,
'process_id' => $request->process_id,
]);
if ($collaboration) {
$request->process_collaboration_id = $collaboration->id;
$request->saveOrFail();
}
}
}
}