Total Complexity | 183 |
Total Lines | 1345 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like DefaultHistoryEventProducer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DefaultHistoryEventProducer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
82 | class DefaultHistoryEventProducer implements HistoryEventProducerInterface |
||
83 | { |
||
84 | //protected final static ConfigurationLogger LOG = ProcessEngineLogger.CONFIG_LOGGER; |
||
85 | |||
86 | protected function initActivityInstanceEvent( |
||
87 | HistoricActivityInstanceEventEntity $evt, |
||
88 | ExecutionEntity $instance, |
||
89 | HistoryEventTypeInterface $eventType |
||
90 | ): void { |
||
91 | $eventSource = $instance->getActivity(); |
||
92 | if ($eventSource == null) { |
||
93 | $eventSource = $instance->getEventSource(); |
||
94 | } |
||
95 | $activityInstanceId = $instance->getActivityInstanceId(); |
||
96 | |||
97 | $parentActivityInstanceId = null; |
||
98 | $parentExecution = $instance->getParent(); |
||
99 | |||
100 | if ($parentExecution !== null && CompensationBehavior::isCompensationThrowing($parentExecution) && $instance->getActivity() !== null) { |
||
101 | $parentActivityInstanceId = CompensationBehavior::getParentActivityInstanceId($instance); |
||
102 | } else { |
||
103 | $parentActivityInstanceId = $instance->getParentActivityInstanceId(); |
||
104 | } |
||
105 | |||
106 | $this->initActivityInstanceEventWithIds( |
||
107 | $evt, |
||
108 | $instance, |
||
109 | $eventSource, |
||
110 | $activityInstanceId, |
||
|
|||
111 | $parentActivityInstanceId, |
||
112 | $eventType |
||
113 | ); |
||
114 | } |
||
115 | |||
116 | protected function initActivityInstanceEventWithMigration( |
||
117 | HistoricActivityInstanceEventEntity $evt, |
||
118 | MigratingActivityInstance $migratingActivityInstance, |
||
119 | HistoryEventTypeInterface $eventType |
||
120 | ): void { |
||
121 | $eventSource = $migratingActivityInstance->getTargetScope(); |
||
122 | $activityInstanceId = $migratingActivityInstance->getActivityInstanceId(); |
||
123 | |||
124 | $parentInstance = $migratingActivityInstance->getParent(); |
||
125 | $parentActivityInstanceId = null; |
||
126 | if ($parentInstance !== null) { |
||
127 | $parentActivityInstanceId = $parentInstance->getActivityInstanceId(); |
||
128 | } |
||
129 | |||
130 | $execution = $migratingActivityInstance->resolveRepresentativeExecution(); |
||
131 | |||
132 | $this->initActivityInstanceEventWithIds( |
||
133 | $evt, |
||
134 | $execution, |
||
135 | $eventSource, |
||
136 | $activityInstanceId, |
||
137 | $parentActivityInstanceId, |
||
138 | $eventType |
||
139 | ); |
||
140 | } |
||
141 | |||
142 | protected function initActivityInstanceEventWithIds( |
||
143 | HistoricActivityInstanceEventEntity $evt, |
||
144 | ExecutionEntity $execution, |
||
145 | PvmScopeInterface $eventSource, |
||
146 | string $activityInstanceId, |
||
147 | string $parentActivityInstanceId, |
||
148 | HistoryEventTypeInterface $eventType |
||
149 | ): void { |
||
150 | |||
151 | $evt->setId($activityInstanceId); |
||
152 | $evt->setEventType($eventType->getEventName()); |
||
153 | $evt->setActivityInstanceId($activityInstanceId); |
||
154 | $evt->setParentActivityInstanceId($parentActivityInstanceId); |
||
155 | $evt->setProcessDefinitionId($execution->getProcessDefinitionId()); |
||
156 | $evt->setProcessInstanceId($execution->getProcessInstanceId()); |
||
157 | $evt->setExecutionId($execution->getId()); |
||
158 | $evt->setTenantId($execution->getTenantId()); |
||
159 | $evt->setRootProcessInstanceId($execution->getRootProcessInstanceId()); |
||
160 | |||
161 | if ($this->isHistoryRemovalTimeStrategyStart()) { |
||
162 | $this->provideRemovalTime($evt); |
||
163 | } |
||
164 | |||
165 | $definition = $execution->getProcessDefinition(); |
||
166 | if ($definition !== null) { |
||
167 | $evt->setProcessDefinitionKey($definition->getKey()); |
||
168 | } |
||
169 | |||
170 | $evt->setActivityId($eventSource->getId()); |
||
171 | $evt->setActivityName($eventSource->getProperty("name")); |
||
172 | $evt->setActivityType($eventSource->getProperty("type")); |
||
173 | |||
174 | // update sub process reference |
||
175 | $subProcessInstance = $execution->getSubProcessInstance(); |
||
176 | if ($subProcessInstance !== null) { |
||
177 | $evt->setCalledProcessInstanceId($subProcessInstance->getId()); |
||
178 | } |
||
179 | |||
180 | // update sub case reference |
||
181 | /*CaseExecutionEntity subCaseInstance = $execution->getSubCaseInstance(); |
||
182 | if (subCaseInstance !== null) { |
||
183 | $evt->setCalledCaseInstanceId(subCaseInstance->getId()); |
||
184 | }*/ |
||
185 | } |
||
186 | |||
187 | protected function initProcessInstanceEvent( |
||
188 | HistoricProcessInstanceEventEntity $evt, |
||
189 | ExecutionEntity $execution, |
||
190 | HistoryEventTypeInterface $eventType |
||
191 | ): void { |
||
192 | $processDefinitionId = $execution->getProcessDefinitionId(); |
||
193 | $processInstanceId = $execution->getProcessInstanceId(); |
||
194 | $executionId = $execution->getId(); |
||
195 | // the given execution is the process instance! |
||
196 | //String caseInstanceId = $execution->getCaseInstanceId(); |
||
197 | $tenantId = $execution->getTenantId(); |
||
198 | |||
199 | $definition = $execution->getProcessDefinition(); |
||
200 | $processDefinitionKey = null; |
||
201 | if ($definition !== null) { |
||
202 | $processDefinitionKey = $definition->getKey(); |
||
203 | } |
||
204 | |||
205 | $evt->setId($processInstanceId); |
||
206 | $evt->setEventType($eventType->getEventName()); |
||
207 | $evt->setProcessDefinitionKey($processDefinitionKey); |
||
208 | $evt->setProcessDefinitionId($processDefinitionId); |
||
209 | $evt->setProcessInstanceId($processInstanceId); |
||
210 | $evt->setExecutionId($executionId); |
||
211 | $evt->setBusinessKey($execution->getProcessBusinessKey()); |
||
212 | //$evt->setCaseInstanceId(caseInstanceId); |
||
213 | $evt->setTenantId($tenantId); |
||
214 | $evt->setRootProcessInstanceId($execution->getRootProcessInstanceId()); |
||
215 | |||
216 | /*if ($execution->getSuperCaseExecution() !== null) { |
||
217 | $evt->setSuperCaseInstanceId($execution->getSuperCaseExecution()->getCaseInstanceId()); |
||
218 | }*/ |
||
219 | if ($execution->getSuperExecution() !== null) { |
||
220 | $evt->setSuperProcessInstanceId($execution->getSuperExecution()->getProcessInstanceId()); |
||
221 | } |
||
222 | } |
||
223 | |||
224 | protected function initTaskInstanceEvent( |
||
225 | HistoricTaskInstanceEventEntity $evt, |
||
226 | TaskEntity $taskEntity, |
||
227 | HistoryEventTypeInterface $eventType |
||
228 | ): void { |
||
229 | $processDefinitionKey = null; |
||
230 | $definition = $taskEntity->getProcessDefinition(); |
||
231 | if ($definition !== null) { |
||
232 | $processDefinitionKey = $definition->getKey(); |
||
233 | } |
||
234 | |||
235 | $processDefinitionId = $taskEntity->getProcessDefinitionId(); |
||
236 | $processInstanceId = $taskEntity->getProcessInstanceId(); |
||
237 | $executionId = $taskEntity->getExecutionId(); |
||
238 | |||
239 | /*String caseDefinitionKey = null; |
||
240 | CaseDefinitionEntity caseDefinition = $taskEntity->getCaseDefinition(); |
||
241 | if (caseDefinition !== null) { |
||
242 | caseDefinitionKey = caseDefinition->getKey(); |
||
243 | } |
||
244 | |||
245 | String caseDefinitionId = $taskEntity->getCaseDefinitionId(); |
||
246 | String caseExecutionId = $taskEntity->getCaseExecutionId(); |
||
247 | String caseInstanceId = $taskEntity->getCaseInstanceId();*/ |
||
248 | $tenantId = $taskEntity->getTenantId(); |
||
249 | |||
250 | $evt->setId($taskEntity->getId()); |
||
251 | $evt->setEventType($eventType->getEventName()); |
||
252 | $evt->setTaskId($taskEntity->getId()); |
||
253 | |||
254 | $evt->setProcessDefinitionKey($processDefinitionKey); |
||
255 | $evt->setProcessDefinitionId($processDefinitionId); |
||
256 | $evt->setProcessInstanceId($processInstanceId); |
||
257 | $evt->setExecutionId($executionId); |
||
258 | |||
259 | /*$evt->setCaseDefinitionKey(caseDefinitionKey); |
||
260 | $evt->setCaseDefinitionId(caseDefinitionId); |
||
261 | $evt->setCaseExecutionId(caseExecutionId); |
||
262 | $evt->setCaseInstanceId(caseInstanceId);*/ |
||
263 | |||
264 | $evt->setAssignee($taskEntity->getAssignee()); |
||
265 | $evt->setDescription($taskEntity->getDescription()); |
||
266 | $evt->setDueDate($taskEntity->getDueDate()); |
||
267 | $evt->setFollowUpDate($taskEntity->getFollowUpDate()); |
||
268 | $evt->setName($taskEntity->getName()); |
||
269 | $evt->setOwner($taskEntity->getOwner()); |
||
270 | $evt->setParentTaskId($taskEntity->getParentTaskId()); |
||
271 | $evt->setPriority($taskEntity->getPriority()); |
||
272 | $evt->setTaskDefinitionKey($taskEntity->getTaskDefinitionKey()); |
||
273 | $evt->setTenantId($tenantId); |
||
274 | |||
275 | $execution = $taskEntity->getExecution(); |
||
276 | if ($execution !== null) { |
||
277 | $evt->setActivityInstanceId($execution->getActivityInstanceId()); |
||
278 | $evt->setRootProcessInstanceId($execution->getRootProcessInstanceId()); |
||
279 | |||
280 | if ($this->isHistoryRemovalTimeStrategyStart()) { |
||
281 | $this->provideRemovalTime($evt); |
||
282 | } |
||
283 | } |
||
284 | } |
||
285 | |||
286 | protected function initHistoricVariableUpdateEvt( |
||
287 | HistoricVariableUpdateEventEntity $evt, |
||
288 | VariableInstanceEntity $variableInstance, |
||
289 | HistoryEventTypeInterface $eventType |
||
290 | ): void { |
||
291 | |||
292 | // init properties |
||
293 | $evt->setEventType($eventType->getEventName()); |
||
294 | $evt->setTimestamp(ClockUtil::getCurrentTime()->format('c')); |
||
295 | $evt->setVariableInstanceId($variableInstance->getId()); |
||
296 | $evt->setProcessInstanceId($variableInstance->getProcessInstanceId()); |
||
297 | $evt->setExecutionId($variableInstance->getExecutionId()); |
||
298 | //$evt->setCaseInstanceId($variableInstance->getCaseInstanceId()); |
||
299 | //$evt->setCaseExecutionId($variableInstance->getCaseExecutionId()); |
||
300 | $evt->setTaskId($variableInstance->getTaskId()); |
||
301 | $evt->setRevision($variableInstance->getRevision()); |
||
302 | $evt->setVariableName($variableInstance->getName()); |
||
303 | $evt->setSerializerName($variableInstance->getSerializerName()); |
||
304 | $evt->setTenantId($variableInstance->getTenantId()); |
||
305 | $evt->setUserOperationId(Context::getCommandContext()->getOperationId()); |
||
306 | |||
307 | $execution = $variableInstance->getExecution(); |
||
308 | if ($execution !== null) { |
||
309 | $definition = $execution->getProcessDefinition(); |
||
310 | if ($definition !== null) { |
||
311 | $evt->setProcessDefinitionId($definition->getId()); |
||
312 | $evt->setProcessDefinitionKey($definition->getKey()); |
||
313 | } |
||
314 | $evt->setRootProcessInstanceId($execution->getRootProcessInstanceId()); |
||
315 | |||
316 | if ($this->isHistoryRemovalTimeStrategyStart()) { |
||
317 | $this->provideRemovalTime($evt); |
||
318 | } |
||
319 | } |
||
320 | |||
321 | /*CaseExecutionEntity caseExecution = variableInstance->getCaseExecution(); |
||
322 | if (caseExecution !== null) { |
||
323 | CaseDefinitionEntity definition = (CaseDefinitionEntity) caseExecution->getCaseDefinition(); |
||
324 | if (definition !== null) { |
||
325 | $evt->setCaseDefinitionId(definition->getId()); |
||
326 | $evt->setCaseDefinitionKey(definition->getKey()); |
||
327 | } |
||
328 | }*/ |
||
329 | |||
330 | // copy value |
||
331 | $evt->setTextValue($variableInstance->getTextValue()); |
||
332 | $evt->setTextValue2($variableInstance->getTextValue2()); |
||
333 | $evt->setDoubleValue($variableInstance->getDoubleValue()); |
||
334 | $evt->setLongValue($variableInstance->getLongValue()); |
||
335 | if ($variableInstance->getByteArrayValueId() !== null) { |
||
336 | $evt->setByteValue($variableInstance->getByteArrayValue()); |
||
337 | } |
||
338 | } |
||
339 | |||
340 | protected function initUserOperationLogEvent( |
||
341 | UserOperationLogEntryEventEntity $evt, |
||
342 | UserOperationLogContext $context, |
||
343 | UserOperationLogContextEntry $contextEntry, |
||
344 | PropertyChange $propertyChange |
||
345 | ): void { |
||
346 | // init properties |
||
347 | $evt->setDeploymentId($contextEntry->getDeploymentId()); |
||
348 | $evt->setEntityType($contextEntry->getEntityType()); |
||
349 | $evt->setOperationType($contextEntry->getOperationType()); |
||
350 | $evt->setOperationId($context->getOperationId()); |
||
351 | $evt->setUserId($context->getUserId()); |
||
352 | $evt->setProcessDefinitionId($contextEntry->getProcessDefinitionId()); |
||
353 | $evt->setProcessDefinitionKey($contextEntry->getProcessDefinitionKey()); |
||
354 | $evt->setProcessInstanceId($contextEntry->getProcessInstanceId()); |
||
355 | $evt->setExecutionId($contextEntry->getExecutionId()); |
||
356 | $evt->setCaseDefinitionId($contextEntry->getCaseDefinitionId()); |
||
357 | //$evt->setCaseInstanceId($contextEntry->getCaseInstanceId()); |
||
358 | $evt->setCaseExecutionId($contextEntry->getCaseExecutionId()); |
||
359 | $evt->setTaskId($contextEntry->getTaskId()); |
||
360 | $evt->setJobId($contextEntry->getJobId()); |
||
361 | $evt->setJobDefinitionId($contextEntry->getJobDefinitionId()); |
||
362 | $evt->setBatchId($contextEntry->getBatchId()); |
||
363 | $evt->setCategory($contextEntry->getCategory()); |
||
364 | $evt->setTimestamp(ClockUtil::getCurrentTime()->format('c')); |
||
365 | $evt->setRootProcessInstanceId($contextEntry->getRootProcessInstanceId()); |
||
366 | $evt->setExternalTaskId($contextEntry->getExternalTaskId()); |
||
367 | $evt->setAnnotation($contextEntry->getAnnotation()); |
||
368 | |||
369 | if ($this->isHistoryRemovalTimeStrategyStart()) { |
||
370 | $this->provideRemovalTime($evt); |
||
371 | } |
||
372 | |||
373 | // init property value |
||
374 | $evt->setProperty($propertyChange->getPropertyName()); |
||
375 | $evt->setOrgValue($propertyChange->getOrgValueString()); |
||
376 | $evt->setNewValue($propertyChange->getNewValueString()); |
||
377 | } |
||
378 | |||
379 | protected function initHistoricIncidentEvent( |
||
380 | HistoricIncidentEventEntity $evt, |
||
381 | IncidentInterface $incident, |
||
382 | HistoryEventTypeInterface $eventType |
||
383 | ): void { |
||
384 | // init properties |
||
385 | $evt->setId($incident->getId()); |
||
386 | $evt->setProcessDefinitionId($incident->getProcessDefinitionId()); |
||
387 | $evt->setProcessInstanceId($incident->getProcessInstanceId()); |
||
388 | $evt->setExecutionId($incident->getExecutionId()); |
||
389 | $evt->setCreateTime($incident->getIncidentTimestamp()); |
||
390 | $evt->setIncidentType($incident->getIncidentType()); |
||
391 | $evt->setActivityId($incident->getActivityId()); |
||
392 | $evt->setCauseIncidentId($incident->getCauseIncidentId()); |
||
393 | $evt->setRootCauseIncidentId($incident->getRootCauseIncidentId()); |
||
394 | $evt->setConfiguration($incident->getConfiguration()); |
||
395 | $evt->setIncidentMessage($incident->getIncidentMessage()); |
||
396 | $evt->setTenantId($incident->getTenantId()); |
||
397 | $evt->setJobDefinitionId($incident->getJobDefinitionId()); |
||
398 | $evt->setHistoryConfiguration($incident->getHistoryConfiguration()); |
||
399 | $evt->setFailedActivityId($incident->getFailedActivityId()); |
||
400 | $evt->setAnnotation($incident->getAnnotation()); |
||
401 | |||
402 | $jobId = $incident->getConfiguration(); |
||
403 | if ($jobId !== null && $this->isHistoryRemovalTimeStrategyStart()) { |
||
404 | $historicBatch = $this->getHistoricBatchByJobId($jobId); |
||
405 | if ($historicBatch !== null) { |
||
406 | $evt->setRemovalTime($historicBatch->getRemovalTime()); |
||
407 | } |
||
408 | } |
||
409 | |||
410 | $incidentEntity = $incident; |
||
411 | $definition = $incidentEntity->getProcessDefinition(); |
||
412 | if ($definition !== null) { |
||
413 | $evt->setProcessDefinitionKey($definition->getKey()); |
||
414 | } |
||
415 | |||
416 | $execution = $incidentEntity->getExecution(); |
||
417 | if ($execution !== null) { |
||
418 | $evt->setRootProcessInstanceId($execution->getRootProcessInstanceId()); |
||
419 | |||
420 | if ($this->isHistoryRemovalTimeStrategyStart()) { |
||
421 | $this->provideRemovalTime($evt); |
||
422 | } |
||
423 | } |
||
424 | |||
425 | // init event type |
||
426 | $evt->setEventType($eventType->getEventName()); |
||
427 | |||
428 | // init state |
||
429 | $incidentState = IncidentStateImpl::default(); |
||
430 | if (HistoryEventTypes::incidentDelete()->equals($eventType)) { |
||
431 | $incidentState = IncidentStateImpl::deleted(); |
||
432 | } elseif (HistoryEventTypes::incidentResolve()->equals($eventType)) { |
||
433 | $incidentState = IncidentStateImpl::resolved(); |
||
434 | } |
||
435 | $evt->setIncidentState($incidentState->getStateCode()); |
||
436 | } |
||
437 | |||
438 | protected function createHistoricVariableEvent( |
||
439 | VariableInstanceEntity $variableInstance, |
||
440 | VariableScopeInterface $sourceVariableScope, |
||
441 | HistoryEventTypeInterface $eventType |
||
442 | ): HistoryEvent { |
||
443 | $scopeActivityInstanceId = null; |
||
444 | $sourceActivityInstanceId = null; |
||
445 | |||
446 | if ($variableInstance->getExecutionId() !== null) { |
||
447 | $scopeExecution = Context::getCommandContext() |
||
448 | ->getDbEntityManager() |
||
449 | ->selectById(ExecutionEntity::class, $variableInstance->getExecutionId()); |
||
450 | |||
451 | if ( |
||
452 | $variableInstance->getTaskId() == null |
||
453 | && !$variableInstance->isConcurrentLocal() |
||
454 | ) { |
||
455 | $scopeActivityInstanceId = $scopeExecution->getParentActivityInstanceId(); |
||
456 | } else { |
||
457 | $scopeActivityInstanceId = $scopeExecution->getActivityInstanceId(); |
||
458 | } |
||
459 | } elseif ($variableInstance->getCaseExecutionId() !== null) { |
||
460 | $scopeActivityInstanceId = $variableInstance->getCaseExecutionId(); |
||
461 | } |
||
462 | |||
463 | $sourceExecution = null; |
||
464 | //CaseExecutionEntity sourceCaseExecution = null; |
||
465 | if ($sourceVariableScope instanceof ExecutionEntity) { |
||
466 | $sourceExecution = $sourceVariableScope; |
||
467 | $sourceActivityInstanceId = $sourceExecution->getActivityInstanceId(); |
||
468 | } elseif ($sourceVariableScope instanceof TaskEntity) { |
||
469 | $sourceExecution = $sourceVariableScope->getExecution(); |
||
470 | if ($sourceExecution !== null) { |
||
471 | $sourceActivityInstanceId = $sourceExecution->getActivityInstanceId(); |
||
472 | }/* else { |
||
473 | sourceCaseExecution = ((TaskEntity) sourceVariableScope)->getCaseExecution(); |
||
474 | if (sourceCaseExecution !== null) { |
||
475 | sourceActivityInstanceId = sourceCaseExecution->getId(); |
||
476 | } |
||
477 | }*/ |
||
478 | } /*elseif (sourceVariableScopeInterface $instanceof CaseExecutionEntity) { |
||
479 | sourceCaseExecution = (CaseExecutionEntity) sourceVariableScope; |
||
480 | sourceActivityInstanceId = sourceCaseExecution->getId(); |
||
481 | }*/ |
||
482 | |||
483 | // create event |
||
484 | $evt = $this->newVariableUpdateEventEntity($sourceExecution); |
||
485 | // initialize |
||
486 | $this->initHistoricVariableUpdateEvt($evt, $variableInstance, $eventType); |
||
487 | // initialize sequence counter |
||
488 | $this->initSequenceCounter($variableInstance, $evt); |
||
489 | |||
490 | // set scope activity instance id |
||
491 | $evt->setScopeActivityInstanceId($scopeActivityInstanceId); |
||
492 | |||
493 | // set source activity instance id |
||
494 | $evt->setActivityInstanceId($sourceActivityInstanceId); |
||
495 | |||
496 | // mark initial variables on process start |
||
497 | if ( |
||
498 | $sourceExecution !== null |
||
499 | && $sourceExecution->isProcessInstanceStarting() |
||
500 | && HistoryEventTypes::variabelInstanceCreate()->equals($eventType) |
||
501 | ) { |
||
502 | if ($variableInstance->getSequenceCounter() == 1) { |
||
503 | $evt->setInitial(true); |
||
504 | } |
||
505 | |||
506 | if ($sourceActivityInstanceId == null && $sourceExecution->getActivity() !== null && $sourceExecution->getTransition() == null) { |
||
507 | $evt->setActivityInstanceId($sourceExecution->getProcessInstanceId()); |
||
508 | } |
||
509 | } |
||
510 | |||
511 | return $evt; |
||
512 | } |
||
513 | |||
514 | // event instance factory //////////////////////// |
||
515 | |||
516 | protected function newProcessInstanceEventEntity(ExecutionEntity $execution): HistoricProcessInstanceEventEntity |
||
517 | { |
||
518 | return new HistoricProcessInstanceEventEntity(); |
||
519 | } |
||
520 | |||
521 | protected function newActivityInstanceEventEntity(ExecutionEntity $execution): HistoricActivityInstanceEventEntity |
||
522 | { |
||
523 | return new HistoricActivityInstanceEventEntity(); |
||
524 | } |
||
525 | |||
526 | protected function newTaskInstanceEventEntity(DelegateTaskInterface $task): HistoricTaskInstanceEventEntity |
||
527 | { |
||
528 | return new HistoricTaskInstanceEventEntity(); |
||
529 | } |
||
530 | |||
531 | protected function newVariableUpdateEventEntity(ExecutionEntity $execution): HistoricVariableUpdateEventEntity |
||
532 | { |
||
533 | return new HistoricVariableUpdateEventEntity(); |
||
534 | } |
||
535 | |||
536 | protected function newHistoricFormPropertyEvent(): HistoricFormPropertyEventEntity |
||
537 | { |
||
538 | return new HistoricFormPropertyEventEntity(); |
||
539 | } |
||
540 | |||
541 | protected function newIncidentEventEntity(IncidentInterface $incident): HistoricIncidentEventEntity |
||
542 | { |
||
543 | return new HistoricIncidentEventEntity(); |
||
544 | } |
||
545 | |||
546 | protected function newHistoricJobLogEntity(JobInterface $job): HistoricJobLogEventEntity |
||
547 | { |
||
548 | return new HistoricJobLogEventEntity(); |
||
549 | } |
||
550 | |||
551 | protected function newBatchEventEntity(BatchEntity $batch): HistoricBatchEntity |
||
552 | { |
||
553 | return new HistoricBatchEntity(); |
||
554 | } |
||
555 | |||
556 | protected function loadProcessInstanceEventEntity(ExecutionEntity $execution): HistoricProcessInstanceEventEntity |
||
557 | { |
||
558 | return $this->newProcessInstanceEventEntity($execution); |
||
559 | } |
||
560 | |||
561 | protected function loadActivityInstanceEventEntity(ExecutionEntity $execution): HistoricActivityInstanceEventEntity |
||
562 | { |
||
563 | return $this->newActivityInstanceEventEntity($execution); |
||
564 | } |
||
565 | |||
566 | protected function loadTaskInstanceEvent(DelegateTaskInterface $task): HistoricTaskInstanceEventEntity |
||
567 | { |
||
568 | return newTaskInstanceEventEntity($task); |
||
569 | } |
||
570 | |||
571 | protected function loadIncidentEvent(IncidentInterface $incident): HistoricIncidentEventEntity |
||
572 | { |
||
573 | return $this->newIncidentEventEntity($incident); |
||
574 | } |
||
575 | |||
576 | protected function loadBatchEntity(BatchEntity $batch): HistoricBatchEntity |
||
577 | { |
||
578 | return newBatchEventEntity($batch); |
||
579 | } |
||
580 | |||
581 | // Implementation //////////////////////////////// |
||
582 | |||
583 | public function createProcessInstanceStartEvt(DelegateExecutionInterface $execution): HistoryEvent |
||
584 | { |
||
585 | $executionEntity = $execution; |
||
586 | |||
587 | // create event instance |
||
588 | $evt = $this->newProcessInstanceEventEntity($executionEntity); |
||
589 | |||
590 | // initialize event |
||
591 | $this->initProcessInstanceEvent($evt, $executionEntity, HistoryEventTypes::processInstanceStart()); |
||
592 | |||
593 | $evt->setStartActivityId($executionEntity->getActivityId()); |
||
594 | $evt->setStartTime(ClockUtil::getCurrentTime()->format('c')); |
||
595 | |||
596 | // set super process instance id |
||
597 | $superExecution = $executionEntity->getSuperExecution(); |
||
598 | if ($superExecution !== null) { |
||
599 | $evt->setSuperProcessInstanceId($superExecution->getProcessInstanceId()); |
||
600 | } |
||
601 | |||
602 | //state |
||
603 | $evt->setState(HistoricProcessInstanceInterface::STATE_ACTIVE); |
||
604 | |||
605 | // set start user Id |
||
606 | $evt->setStartUserId(Context::getCommandContext()->getAuthenticatedUserId()); |
||
607 | |||
608 | if ($this->isHistoryRemovalTimeStrategyStart()) { |
||
609 | if ($this->isRootProcessInstance($evt)) { |
||
610 | $removalTime = $this->calculateRemovalTime($evt); |
||
611 | $evt->setRemovalTime($removalTime); |
||
612 | } else { |
||
613 | $this->provideRemovalTime($evt); |
||
614 | } |
||
615 | } |
||
616 | |||
617 | return $evt; |
||
618 | } |
||
619 | |||
620 | public function createProcessInstanceUpdateEvt(DelegateExecutionInterface $execution): HistoryEvent |
||
621 | { |
||
622 | $executionEntity = $execution; |
||
623 | |||
624 | // create event instance |
||
625 | $evt = $this->loadProcessInstanceEventEntity($executionEntity); |
||
626 | |||
627 | // initialize event |
||
628 | $this->initProcessInstanceEvent($evt, $executionEntity, HistoryEventTypes::processInstanceUpdate()); |
||
629 | |||
630 | if ($executionEntity->isSuspended()) { |
||
631 | $evt->setState(HistoricProcessInstanceInterface::STATE_SUSPENDED); |
||
632 | } else { |
||
633 | $evt->setState(HistoricProcessInstanceInterface::STATE_ACTIVE); |
||
634 | } |
||
635 | |||
636 | return $evt; |
||
637 | } |
||
638 | |||
639 | public function createProcessInstanceMigrateEvt(DelegateExecutionInterface $execution): HistoryEvent |
||
640 | { |
||
641 | $executionEntity = $execution; |
||
642 | |||
643 | // create event instance |
||
644 | $evt = $this->newProcessInstanceEventEntity($executionEntity); |
||
645 | |||
646 | // initialize event |
||
647 | $this->initProcessInstanceEvent($evt, $executionEntity, HistoryEventTypes::processInstanceMigrate()); |
||
648 | |||
649 | if ($executionEntity->isSuspended()) { |
||
650 | $evt->setState(HistoricProcessInstanceInterface::STATE_SUSPENDED); |
||
651 | } else { |
||
652 | $evt->setState(HistoricProcessInstanceInterface::STATE_ACTIVE); |
||
653 | } |
||
654 | |||
655 | return $evt; |
||
656 | } |
||
657 | |||
658 | public function createProcessInstanceEndEvt(DelegateExecutionInterface $execution): HistoryEvent |
||
659 | { |
||
660 | $executionEntity = $execution; |
||
661 | |||
662 | // create event instance |
||
663 | $evt = $this->loadProcessInstanceEventEntity($executionEntity); |
||
664 | |||
665 | // initialize event |
||
666 | $this->initProcessInstanceEvent($evt, $executionEntity, HistoryEventTypes::processInstanceEnd()); |
||
667 | |||
668 | $this->determineEndState($executionEntity, $evt); |
||
669 | |||
670 | // set end activity id |
||
671 | $evt->setEndActivityId($executionEntity->getActivityId()); |
||
672 | $evt->setEndTime(ClockUtil::getCurrentTime()->format('c')); |
||
673 | |||
674 | if ($evt->getStartTime() !== null) { |
||
675 | $et = new \DateTime($evt->getEndTime()); |
||
676 | $endTimeUt = $et->getTimestamp(); |
||
677 | |||
678 | $st = new \DateTime($evt->getStartTime()); |
||
679 | $startTimeUt = $st->getTimestamp(); |
||
680 | |||
681 | $evt->setDurationInMillis($endTimeUt * 1000 - $startTimeUt * 1000); |
||
682 | } |
||
683 | |||
684 | if ($this->isRootProcessInstance($evt) && $this->isHistoryRemovalTimeStrategyEnd()) { |
||
685 | $removalTime = $this->calculateRemovalTime($evt); |
||
686 | |||
687 | if ($removalTime !== null) { |
||
688 | $this->addRemovalTimeToHistoricProcessInstances($evt->getRootProcessInstanceId(), $removalTime); |
||
689 | /*if ($this->isDmnEnabled()) { |
||
690 | $this->addRemovalTimeToHistoricDecisions($evt->getRootProcessInstanceId(), $removalTime); |
||
691 | }*/ |
||
692 | } |
||
693 | } |
||
694 | |||
695 | // set delete reason (if applicable). |
||
696 | if ($executionEntity->getDeleteReason() !== null) { |
||
697 | $evt->setDeleteReason($executionEntity->getDeleteReason()); |
||
698 | } |
||
699 | |||
700 | return $evt; |
||
701 | } |
||
702 | |||
703 | protected function addRemovalTimeToHistoricDecisions(string $rootProcessInstanceId, string $removalTime): void |
||
704 | { |
||
705 | Context::getCommandContext() |
||
706 | ->getHistoricDecisionInstanceManager() |
||
707 | ->addRemovalTimeToDecisionsByRootProcessInstanceId($rootProcessInstanceId, $removalTime); |
||
708 | } |
||
709 | |||
710 | protected function addRemovalTimeToHistoricProcessInstances(string $rootProcessInstanceId, string $removalTime): void |
||
711 | { |
||
712 | Context::getCommandContext() |
||
713 | ->getHistoricProcessInstanceManager() |
||
714 | ->addRemovalTimeToProcessInstancesByRootProcessInstanceId($rootProcessInstanceId, $removalTime); |
||
715 | } |
||
716 | |||
717 | /*protected boolean isDmnEnabled() { |
||
718 | return Context::getCommandContext() |
||
719 | ->getProcessEngineConfiguration() |
||
720 | ->isDmnEnabled(); |
||
721 | }*/ |
||
722 | |||
723 | protected function determineEndState(ExecutionEntity $executionEntity, HistoricProcessInstanceEventEntity $evt): void |
||
724 | { |
||
725 | //determine state |
||
726 | if ($executionEntity->getActivity() !== null) { |
||
727 | $evt->setState(HistoricProcessInstanceInterface::STATE_COMPLETED); |
||
728 | } else { |
||
729 | if ($executionEntity->isExternallyTerminated()) { |
||
730 | $evt->setState(HistoricProcessInstanceInterface::STATE_EXTERNALLY_TERMINATED); |
||
731 | } elseif (!$executionEntity->isExternallyTerminated()) { |
||
732 | $evt->setState(HistoricProcessInstanceInterface::STATE_INTERNALLY_TERMINATED); |
||
733 | } |
||
734 | } |
||
735 | } |
||
736 | |||
737 | public function createActivityInstanceStartEvt(DelegateExecutionInterface $execution): HistoryEvent |
||
738 | { |
||
739 | $executionEntity = $execution; |
||
740 | |||
741 | // create event instance |
||
742 | $evt = $this->newActivityInstanceEventEntity($executionEntity); |
||
743 | |||
744 | // initialize event |
||
745 | $this->initActivityInstanceEvent($evt, $executionEntity, HistoryEventTypes::activityInstanceStart()); |
||
746 | |||
747 | // initialize sequence counter |
||
748 | $this->initSequenceCounter($executionEntity, $evt); |
||
749 | |||
750 | $evt->setStartTime(ClockUtil::getCurrentTime()->format('c')); |
||
751 | |||
752 | return $evt; |
||
753 | } |
||
754 | |||
755 | public function createActivityInstanceUpdateEvt(DelegateExecutionInterface $execution, ?DelegateTaskInterface $task = null): HistoryEvent |
||
756 | { |
||
757 | $executionEntity = $execution; |
||
758 | |||
759 | // create event instance |
||
760 | $evt = $this->loadActivityInstanceEventEntity($executionEntity); |
||
761 | |||
762 | // initialize event |
||
763 | $this->initActivityInstanceEvent($evt, $executionEntity, HistoryEventTypes::activityInstanceUpdate()); |
||
764 | |||
765 | // update task assignment |
||
766 | if ($task !== null) { |
||
767 | $evt->setTaskId($task->getId()); |
||
768 | $evt->setTaskAssignee($task->getAssignee()); |
||
769 | } |
||
770 | |||
771 | return $evt; |
||
772 | } |
||
773 | |||
774 | public function createActivityInstanceMigrateEvt(MigratingActivityInstance $actInstance): HistoryEvent |
||
775 | { |
||
776 | |||
777 | // create event instance |
||
778 | $evt = $this->loadActivityInstanceEventEntity($actInstance->resolveRepresentativeExecution()); |
||
779 | |||
780 | // initialize event |
||
781 | $this->initActivityInstanceEvent($evt, $actInstance, HistoryEventTypes::activityInstanceMigrate()); |
||
782 | |||
783 | return $evt; |
||
784 | } |
||
785 | |||
786 | public function createActivityInstanceEndEvt(DelegateExecutionInterface $execution): HistoryEvent |
||
787 | { |
||
788 | $executionEntity = $execution; |
||
789 | |||
790 | // create event instance |
||
791 | $evt = $this->loadActivityInstanceEventEntity($executionEntity); |
||
792 | $evt->setActivityInstanceState($executionEntity->getActivityInstanceState()); |
||
793 | |||
794 | // initialize event |
||
795 | $this->initActivityInstanceEvent($evt, $execution, HistoryEventTypes::activityInstanceEnd()); |
||
796 | |||
797 | $evt->setEndTime(ClockUtil::getCurrentTime()->format('c')); |
||
798 | if ($evt->getStartTime() !== null) { |
||
799 | $evt->setDurationInMillis((new \DateTime($evt->getEndTime()))->getTimestamp() * 1000 - (new \DateTime($evt->getStartTime()))->getTimestamp() * 1000); |
||
800 | } |
||
801 | |||
802 | return $evt; |
||
803 | } |
||
804 | |||
805 | public function createTaskInstanceCreateEvt(DelegateTaskInterface $task): HistoryEvent |
||
806 | { |
||
807 | |||
808 | // create event instance |
||
809 | $evt = newTaskInstanceEventEntity($task); |
||
810 | |||
811 | // initialize event |
||
812 | $this->initTaskInstanceEvent($evt, $task, HistoryEventTypes::taskInstanceCreate()); |
||
813 | |||
814 | $evt->setStartTime(ClockUtil::getCurrentTime()->format('c')); |
||
815 | |||
816 | return $evt; |
||
817 | } |
||
818 | |||
819 | public function createTaskInstanceUpdateEvt(DelegateTaskInterface $task): HistoryEvent |
||
820 | { |
||
821 | |||
822 | // create event instance |
||
823 | $evt = $this->loadTaskInstanceEvent($task); |
||
824 | |||
825 | // initialize event |
||
826 | $this->initTaskInstanceEvent($evt, $task, HistoryEventTypes::taskInstanceUpdate()); |
||
827 | |||
828 | return $evt; |
||
829 | } |
||
830 | |||
831 | public function createTaskInstanceMigrateEvt(DelegateTaskInterface $task): HistoryEvent |
||
832 | { |
||
833 | // create event instance |
||
834 | $evt = $this->loadTaskInstanceEvent($task); |
||
835 | |||
836 | // initialize event |
||
837 | $this->initTaskInstanceEvent($evt, $task, HistoryEventTypes::taskInstanceMigrate()); |
||
838 | |||
839 | return $evt; |
||
840 | } |
||
841 | |||
842 | public function createTaskInstanceCompleteEvt(DelegateTaskInterface $task, string $deleteReason): HistoryEvent |
||
843 | { |
||
844 | // create event instance |
||
845 | $evt = $this->loadTaskInstanceEvent($task); |
||
846 | |||
847 | // initialize event |
||
848 | $this->initTaskInstanceEvent($evt, $task, HistoryEventTypes::taskInstanceComplete()); |
||
849 | |||
850 | // set end time |
||
851 | $evt->setEndTime(ClockUtil::getCurrentTime()->format('c')); |
||
852 | if ($evt->getStartTime() !== null) { |
||
853 | $evt->setDurationInMillis((new \DateTime($evt->getEndTime()))->getTimestamp() * 1000 - (new \DateTime($evt->getStartTime()))->getTimestamp() * 1000); |
||
854 | } |
||
855 | |||
856 | // set delete reason |
||
857 | $evt->setDeleteReason($deleteReason); |
||
858 | |||
859 | return $evt; |
||
860 | } |
||
861 | |||
862 | // User Operation Logs /////////////////////////// |
||
863 | |||
864 | public function createUserOperationLogEvents(UserOperationLogContext $context): array |
||
865 | { |
||
866 | $historyEvents = []; |
||
867 | |||
868 | $operationId = Context::getCommandContext()->getOperationId(); |
||
869 | $context->setOperationId($operationId); |
||
870 | |||
871 | foreach ($context->getEntries() as $entry) { |
||
872 | foreach ($entry->getPropertyChanges() as $propertyChange) { |
||
873 | $evt = new UserOperationLogEntryEventEntity(); |
||
874 | |||
875 | $this->initUserOperationLogEvent($evt, $context, $entry, $propertyChange); |
||
876 | |||
877 | $historyEvents[] = $evt; |
||
878 | } |
||
879 | } |
||
880 | |||
881 | return $historyEvents; |
||
882 | } |
||
883 | |||
884 | // variables ///////////////////////////////// |
||
885 | |||
886 | public function createHistoricVariableCreateEvt(VariableInstanceEntity $variableInstance, VariableScopeInterface $sourceVariableScope): HistoryEvent |
||
887 | { |
||
888 | return $this->createHistoricVariableEvent($variableInstance, $sourceVariableScope, HistoryEventTypes::variableInstanceCreate()); |
||
889 | } |
||
890 | |||
891 | public function createHistoricVariableDeleteEvt(VariableInstanceEntity $variableInstance, VariableScopeInterface $sourceVariableScope): HistoryEvent |
||
892 | { |
||
893 | return $this->createHistoricVariableEvent($variableInstance, $sourceVariableScope, HistoryEventTypes::variableInstanceDelete()); |
||
894 | } |
||
895 | |||
896 | public function createHistoricVariableUpdateEvt(VariableInstanceEntity $variableInstance, VariableScopeInterface $sourceVariableScope): HistoryEvent |
||
897 | { |
||
898 | return $this->createHistoricVariableEvent($variableInstance, $sourceVariableScope, HistoryEventTypes::variableInstanceUpdate()); |
||
899 | } |
||
900 | |||
901 | public function createHistoricVariableMigrateEvt(VariableInstanceEntity $variableInstance): HistoryEvent |
||
902 | { |
||
903 | return $this->createHistoricVariableEvent($variableInstance, null, HistoryEventTypes::variableInstanceMigrate()); |
||
904 | } |
||
905 | |||
906 | // form Properties /////////////////////////// |
||
907 | |||
908 | public function createFormPropertyUpdateEvt(ExecutionEntity $execution, string $propertyId, string $propertyValue, string $taskId): HistoryEvent |
||
909 | { |
||
910 | $idGenerator = Context::getProcessEngineConfiguration()->getIdGenerator(); |
||
911 | |||
912 | $historicFormPropertyEntity = newHistoricFormPropertyEvent(); |
||
913 | |||
914 | $historicFormPropertyEntity->setId($idGenerator->getNextId()); |
||
915 | $historicFormPropertyEntity->setEventType(HistoryEventTypes::formPropertyUpdate()->getEventName()); |
||
916 | $historicFormPropertyEntity->setTimestamp(ClockUtil::getCurrentTime()->format('c')); |
||
917 | $historicFormPropertyEntity->setExecutionId($execution->getId()); |
||
918 | $historicFormPropertyEntity->setProcessDefinitionId($execution->getProcessDefinitionId()); |
||
919 | $historicFormPropertyEntity->setProcessInstanceId($execution->getProcessInstanceId()); |
||
920 | $historicFormPropertyEntity->setPropertyId($propertyId); |
||
921 | $historicFormPropertyEntity->setPropertyValue($propertyValue); |
||
922 | $historicFormPropertyEntity->setTaskId($taskId); |
||
923 | $historicFormPropertyEntity->setTenantId($execution->getTenantId()); |
||
924 | $historicFormPropertyEntity->setUserOperationId(Context::getCommandContext()->getOperationId()); |
||
925 | $historicFormPropertyEntity->setRootProcessInstanceId($execution->getRootProcessInstanceId()); |
||
926 | |||
927 | if ($this->isHistoryRemovalTimeStrategyStart()) { |
||
928 | $this->provideRemovalTime($historicFormPropertyEntity); |
||
929 | } |
||
930 | |||
931 | $definition = $execution->getProcessDefinition(); |
||
932 | if ($definition !== null) { |
||
933 | $historicFormPropertyEntity->setProcessDefinitionKey($definition->getKey()); |
||
934 | } |
||
935 | |||
936 | // initialize sequence counter |
||
937 | $this->initSequenceCounter($execution, $historicFormPropertyEntity); |
||
938 | |||
939 | if ($execution->isProcessInstanceStarting()) { |
||
940 | // instantiate activity instance id as process instance id when starting a process instance |
||
941 | // via a form |
||
942 | $historicFormPropertyEntity->setActivityInstanceId($execution->getProcessInstanceId()); |
||
943 | } else { |
||
944 | $historicFormPropertyEntity->setActivityInstanceId($execution->getActivityInstanceId()); |
||
945 | } |
||
946 | |||
947 | return $historicFormPropertyEntity; |
||
948 | } |
||
949 | |||
950 | // Incidents ////////////////////////////////// |
||
951 | |||
952 | public function createHistoricIncidentCreateEvt(IncidentInterface $incident): HistoryEvent |
||
953 | { |
||
954 | return $this->createHistoricIncidentEvt($incident, HistoryEventTypes::incidentCreate()); |
||
955 | } |
||
956 | |||
957 | public function createHistoricIncidentUpdateEvt(IncidentInterface $incident): HistoryEvent |
||
958 | { |
||
959 | return $this->createHistoricIncidentEvt($incident, HistoryEventTypes::incidentUpdate()); |
||
960 | } |
||
961 | |||
962 | public function createHistoricIncidentResolveEvt(IncidentInterface $incident): HistoryEvent |
||
963 | { |
||
964 | return $this->createHistoricIncidentEvt($incident, HistoryEventTypes::incidentResolve()); |
||
965 | } |
||
966 | |||
967 | public function createHistoricIncidentDeleteEvt(IncidentInterface $incident): HistoryEvent |
||
968 | { |
||
969 | return $this->createHistoricIncidentEvt($incident, HistoryEventTypes::incidentDelete()); |
||
970 | } |
||
971 | |||
972 | public function createHistoricIncidentMigrateEvt(IncidentInterface $incident): HistoryEvent |
||
973 | { |
||
974 | return $this->createHistoricIncidentEvt($incident, HistoryEventTypes::incidentMigrate()); |
||
975 | } |
||
976 | |||
977 | protected function createHistoricIncidentEvt(IncidentInterface $incident, HistoryEventTypes $eventType): HistoryEvent |
||
978 | { |
||
979 | // create event |
||
980 | $evt = $this->loadIncidentEvent($incident); |
||
981 | // initialize |
||
982 | $this->initHistoricIncidentEvent($evt, $incident, $eventType); |
||
983 | |||
984 | if (HistoryEventTypes::incidentResolve()->equals($eventType) || HistoryEventTypes::incidentDelete()->equals($eventType)) { |
||
985 | $evt->setEndTime(ClockUtil::getCurrentTime()->format('c')); |
||
986 | } |
||
987 | |||
988 | return $evt; |
||
989 | } |
||
990 | |||
991 | public function createHistoricIdentityLinkAddEvent(IdentityLinkInterface $identityLink): HistoryEvent |
||
992 | { |
||
993 | return $this->createHistoricIdentityLinkEvt($identityLink, HistoryEventTypes::identityLinkAdd()); |
||
994 | } |
||
995 | |||
996 | public function createHistoricIdentityLinkDeleteEvent(IdentityLinkInterface $identityLink): HistoryEvent |
||
997 | { |
||
998 | return $this->createHistoricIdentityLinkEvt($identityLink, HistoryEventTypes::identityLinkDelete()); |
||
999 | } |
||
1000 | |||
1001 | protected function createHistoricIdentityLinkEvt(IdentityLinkInterface $identityLink, HistoryEventTypes $eventType): HistoryEvent |
||
1002 | { |
||
1003 | // create historic identity link event |
||
1004 | $evt = newIdentityLinkEventEntity(); |
||
1005 | // Mapping all the values of identity link to HistoricIdentityLinkEvent |
||
1006 | $this->initHistoricIdentityLinkEvent($evt, $identityLink, $eventType); |
||
1007 | return $evt; |
||
1008 | } |
||
1009 | |||
1010 | protected function newIdentityLinkEventEntity(): HistoricIdentityLinkLogEventEntity |
||
1013 | } |
||
1014 | |||
1015 | protected function initHistoricIdentityLinkEvent(HistoricIdentityLinkLogEventEntity $evt, IdentityLinkInterface $identityLink, HistoryEventTypeInterface $eventType): void |
||
1016 | { |
||
1017 | if ($identityLink->getTaskId() !== null) { |
||
1018 | $task = Context::getCommandContext() |
||
1019 | ->getTaskManager() |
||
1020 | ->findTaskById($identityLink->getTaskId()); |
||
1021 | |||
1022 | $evt->setProcessDefinitionId($task->getProcessDefinitionId()); |
||
1023 | |||
1024 | if ($task->getProcessDefinition() !== null) { |
||
1025 | $evt->setProcessDefinitionKey($task->getProcessDefinition()->getKey()); |
||
1026 | } |
||
1027 | |||
1028 | $execution = $task->getExecution(); |
||
1029 | if ($execution !== null) { |
||
1030 | $evt->setRootProcessInstanceId($execution->getRootProcessInstanceId()); |
||
1031 | |||
1032 | if ($this->isHistoryRemovalTimeStrategyStart()) { |
||
1033 | $this->provideRemovalTime($evt); |
||
1034 | } |
||
1035 | } |
||
1036 | } |
||
1037 | |||
1038 | if ($identityLink->getProcessDefId() !== null) { |
||
1039 | $evt->setProcessDefinitionId($identityLink->getProcessDefId()); |
||
1040 | |||
1041 | $definition = Context::getProcessEngineConfiguration() |
||
1042 | ->getDeploymentCache() |
||
1043 | ->findProcessDefinitionFromCache($identityLink->getProcessDefId()); |
||
1044 | $evt->setProcessDefinitionKey($definition->getKey()); |
||
1045 | } |
||
1046 | |||
1047 | $evt->setTime(ClockUtil::getCurrentTime()->format('c')); |
||
1048 | $evt->setType($identityLink->getType()); |
||
1049 | $evt->setUserId($identityLink->getUserId()); |
||
1050 | $evt->setGroupId($identityLink->getGroupId()); |
||
1051 | $evt->setTaskId($identityLink->getTaskId()); |
||
1052 | $evt->setTenantId($identityLink->getTenantId()); |
||
1053 | // There is a conflict in HistoryEventTypes for 'delete' keyword, |
||
1054 | // So HistoryEventTypes.IDENTITY_LINK_ADD / |
||
1055 | // HistoryEventTypes.IDENTITY_LINK_DELETE is provided with the event name |
||
1056 | // 'add-identity-link' /'delete-identity-link' |
||
1057 | // and changed to 'add'/'delete' (While inserting it into the database) on |
||
1058 | // Historic identity link add / delete event |
||
1059 | $operationType = "add"; |
||
1060 | if ($eventType->getEventName() == HistoryEventTypes::identityLinkDelete()->getEventName()) { |
||
1061 | $operationType = "delete"; |
||
1062 | } |
||
1063 | |||
1064 | $evt->setOperationType($operationType); |
||
1065 | $evt->setEventType($eventType->getEventName()); |
||
1066 | $evt->setAssignerId(Context::getCommandContext()->getAuthenticatedUserId()); |
||
1067 | } |
||
1068 | // Batch |
||
1069 | |||
1070 | public function createBatchStartEvent(BatchInterface $batch): HistoryEvent |
||
1071 | { |
||
1072 | $historicBatch = $this->createBatchEvent($batch, HistoryEventTypes::batchStart()); |
||
1073 | |||
1074 | if ($this->isHistoryRemovalTimeStrategyStart()) { |
||
1075 | $this->provideRemovalTime($historicBatch); |
||
1076 | } |
||
1077 | |||
1078 | return $historicBatch; |
||
1079 | } |
||
1080 | |||
1081 | public function createBatchEndEvent(BatchInterface $batch): HistoryEvent |
||
1082 | { |
||
1083 | $historicBatch = $this->createBatchEvent($batch, HistoryEventTypes::batchEnd()); |
||
1084 | |||
1085 | if ($this->isHistoryRemovalTimeStrategyEnd()) { |
||
1086 | $this->provideRemovalTime($historicBatch); |
||
1087 | |||
1088 | $this->addRemovalTimeToHistoricJobLog($historicBatch); |
||
1089 | $this->addRemovalTimeToHistoricIncidents($historicBatch); |
||
1090 | } |
||
1091 | |||
1092 | return $historicBatch; |
||
1093 | } |
||
1094 | |||
1095 | protected function createBatchEvent(BatchEntity $batch, HistoryEventTypes $eventType): HistoryEvent |
||
1096 | { |
||
1097 | $event = $this->loadBatchEntity($batch); |
||
1098 | |||
1099 | $event->setId($batch->getId()); |
||
1100 | $event->setType($batch->getType()); |
||
1101 | $event->setTotalJobs($batch->getTotalJobs()); |
||
1102 | $event->setBatchJobsPerSeed($batch->getBatchJobsPerSeed()); |
||
1103 | $event->setInvocationsPerBatchJob($batch->getInvocationsPerBatchJob()); |
||
1104 | $event->setSeedJobDefinitionId($batch->getSeedJobDefinitionId()); |
||
1105 | $event->setMonitorJobDefinitionId($batch->getMonitorJobDefinitionId()); |
||
1106 | $event->setBatchJobDefinitionId($batch->getBatchJobDefinitionId()); |
||
1107 | $event->setTenantId($batch->getTenantId()); |
||
1108 | $event->setEventType($eventType->getEventName()); |
||
1109 | |||
1110 | if (HistoryEventTypes::batchStart()->equals($eventType)) { |
||
1111 | $event->setStartTime(ClockUtil::getCurrentTime()->format('c')); |
||
1112 | $event->setCreateUserId(Context::getCommandContext()->getAuthenticatedUserId()); |
||
1113 | } |
||
1114 | |||
1115 | if (HistoryEventTypes::batchEnd()->equals($eventType)) { |
||
1116 | $event->setEndTime(ClockUtil::getCurrentTime()->format('c')); |
||
1117 | } |
||
1118 | |||
1119 | return $event; |
||
1120 | } |
||
1121 | |||
1122 | // Job Log |
||
1123 | |||
1124 | public function createHistoricJobLogCreateEvt(JobInterface $job): HistoryEvent |
||
1125 | { |
||
1126 | return $this->createHistoricJobLogEvt($job, HistoryEventTypes::jobCreate()); |
||
1127 | } |
||
1128 | |||
1129 | public function createHistoricJobLogFailedEvt(JobInterface $job, ?\Throwable $exception = null): HistoryEvent |
||
1130 | { |
||
1131 | $event = $this->createHistoricJobLogEvt($job, HistoryEventTypes::jobFail()); |
||
1132 | |||
1133 | if ($exception !== null) { |
||
1134 | // exception message |
||
1135 | $event->setJobExceptionMessage($exception->getMessage()); |
||
1136 | |||
1137 | // stacktrace |
||
1138 | $exceptionStacktrace = ExceptionUtil::getExceptionStacktrace($exception); |
||
1139 | $exceptionBytes = StringUtil::toByteArray($exceptionStacktrace); |
||
1140 | |||
1141 | $byteArray = ExceptionUtil::createJobExceptionByteArray($exceptionBytes, ResourceTypes::history()); |
||
1142 | $byteArray->setRootProcessInstanceId($event->getRootProcessInstanceId()); |
||
1143 | |||
1144 | if ($this->isHistoryRemovalTimeStrategyStart()) { |
||
1145 | $byteArray->setRemovalTime($event->getRemovalTime()); |
||
1146 | } |
||
1147 | |||
1148 | $event->setExceptionByteArrayId($byteArray->getId()); |
||
1149 | } |
||
1150 | |||
1151 | return $event; |
||
1152 | } |
||
1153 | |||
1154 | public function createHistoricJobLogSuccessfulEvt(JobInterface $job): HistoryEvent |
||
1155 | { |
||
1156 | return $this->createHistoricJobLogEvt($job, HistoryEventTypes::jobSuccess()); |
||
1157 | } |
||
1158 | |||
1159 | public function createHistoricJobLogDeleteEvt(JobInterface $job): HistoryEvent |
||
1160 | { |
||
1161 | return $this->createHistoricJobLogEvt($job, HistoryEventTypes::jobDelete()); |
||
1162 | } |
||
1163 | |||
1164 | protected function createHistoricJobLogEvt(JobInterface $job, HistoryEventTypeInterface $eventType): HistoryEvent |
||
1165 | { |
||
1166 | $event = newHistoricJobLogEntity($job); |
||
1167 | $this->initHistoricJobLogEvent($event, $job, $eventType); |
||
1168 | return $event; |
||
1169 | } |
||
1170 | |||
1171 | protected function initHistoricJobLogEvent(HistoricJobLogEventEntity $evt, JobInterface $job, HistoryEventTypeInterface $eventType): void |
||
1172 | { |
||
1173 | $currentTime = ClockUtil::getCurrentTime()->format('c'); |
||
1174 | $evt->setTimestamp($currentTime); |
||
1175 | |||
1176 | $jobEntity = $job; |
||
1177 | $evt->setJobId($jobEntity->getId()); |
||
1178 | $evt->setJobDueDate($jobEntity->getDuedate()); |
||
1179 | $evt->setJobRetries($jobEntity->getRetries()); |
||
1180 | $evt->setJobPriority($jobEntity->getPriority()); |
||
1181 | |||
1182 | $hostName = Context::getCommandContext()->getProcessEngineConfiguration()->getHostname(); |
||
1183 | $evt->setHostname($hostName); |
||
1184 | |||
1185 | if (HistoryCleanupJobHandler::TYPE == $jobEntity->getJobHandlerType()) { |
||
1186 | $timeToLive = Context::getProcessEngineConfiguration()->getHistoryCleanupJobLogTimeToLive(); |
||
1187 | if ($timeToLive !== null) { |
||
1188 | try { |
||
1189 | $timeToLiveDays = ParseUtil::parseHistoryTimeToLive($timeToLive); |
||
1190 | $removalTime = DefaultHistoryRemovalTimeProvider::determineRemovalTime($currentTime, $timeToLiveDays); |
||
1191 | $evt->setRemovalTime($removalTime); |
||
1192 | } catch (ProcessEngineException $e) { |
||
1193 | //$wrappedException = LOG.invalidPropertyValue("historyCleanupJobLogTimeToLive", timeToLive, e); |
||
1194 | //LOG.invalidPropertyValue(wrappedException); |
||
1195 | throw $e; |
||
1196 | } |
||
1197 | } |
||
1198 | } |
||
1199 | |||
1200 | $jobDefinition = $jobEntity->getJobDefinition(); |
||
1201 | if ($jobDefinition !== null) { |
||
1202 | $evt->setJobDefinitionId($jobDefinition->getId()); |
||
1203 | $evt->setJobDefinitionType($jobDefinition->getJobType()); |
||
1204 | $evt->setJobDefinitionConfiguration($jobDefinition->getJobConfiguration()); |
||
1205 | |||
1206 | $historicBatchId = $jobDefinition->getJobConfiguration(); |
||
1207 | if ($historicBatchId !== null && $this->isHistoryRemovalTimeStrategyStart()) { |
||
1208 | $historicBatch = $this->getHistoricBatchById($historicBatchId); |
||
1209 | if ($historicBatch !== null) { |
||
1210 | $evt->setRemovalTime($historicBatch->getRemovalTime()); |
||
1211 | } |
||
1212 | } |
||
1213 | } else { |
||
1214 | // in case of async signal there does not exist a job definition |
||
1215 | // but we use the jobHandlerType as jobDefinitionType |
||
1216 | $evt->setJobDefinitionType($jobEntity->getJobHandlerType()); |
||
1217 | } |
||
1218 | |||
1219 | $evt->setActivityId($jobEntity->getActivityId()); |
||
1220 | $evt->setFailedActivityId($jobEntity->getFailedActivityId()); |
||
1221 | $evt->setExecutionId($jobEntity->getExecutionId()); |
||
1222 | $evt->setProcessInstanceId($jobEntity->getProcessInstanceId()); |
||
1223 | $evt->setProcessDefinitionId($jobEntity->getProcessDefinitionId()); |
||
1224 | $evt->setProcessDefinitionKey($jobEntity->getProcessDefinitionKey()); |
||
1225 | $evt->setDeploymentId($jobEntity->getDeploymentId()); |
||
1226 | $evt->setTenantId($jobEntity->getTenantId()); |
||
1227 | |||
1228 | $execution = $jobEntity->getExecution(); |
||
1229 | if ($execution !== null) { |
||
1230 | $evt->setRootProcessInstanceId($execution->getRootProcessInstanceId()); |
||
1231 | |||
1232 | if ($this->isHistoryRemovalTimeStrategyStart()) { |
||
1233 | $this->provideRemovalTime($evt); |
||
1234 | } |
||
1235 | } |
||
1236 | |||
1237 | // initialize sequence counter |
||
1238 | $this->initSequenceCounter($jobEntity, $evt); |
||
1239 | |||
1240 | $state = null; |
||
1241 | if (HistoryEventTypes::jobCreate()->equals($eventType)) { |
||
1242 | $state = JobStateImpl::created(); |
||
1243 | } elseif (HistoryEventTypes::jobFail()->equals($eventType)) { |
||
1244 | $state = JobStateImpl::failed(); |
||
1245 | } elseif (HistoryEventTypes::jobSuccess()->equals($eventType)) { |
||
1246 | $state = JobStateImpl::successful(); |
||
1247 | } elseif (HistoryEventTypes::jobDelete()->equals($eventType)) { |
||
1248 | $state = JobStateImpl::deleted(); |
||
1249 | } |
||
1250 | $evt->setState($state->getStateCode()); |
||
1251 | } |
||
1252 | |||
1253 | public function createHistoricExternalTaskLogCreatedEvt(ExternalTaskInterface $task): HistoryEvent |
||
1254 | { |
||
1255 | return initHistoricExternalTaskLog($task, ExternalTaskStateImpl::created()); |
||
1256 | } |
||
1257 | |||
1258 | public function createHistoricExternalTaskLogFailedEvt(ExternalTaskInterface $task): HistoryEvent |
||
1259 | { |
||
1260 | $event = $this->initHistoricExternalTaskLog($task, ExternalTaskStateImpl::failed()); |
||
1261 | $event->setErrorMessage($task->getErrorMessage()); |
||
1262 | $errorDetails = $task->getErrorDetails(); |
||
1263 | if ($errorDetails !== null) { |
||
1264 | $event->setErrorDetails($errorDetails); |
||
1265 | } |
||
1266 | return $event; |
||
1267 | } |
||
1268 | |||
1269 | public function createHistoricExternalTaskLogSuccessfulEvt(ExternalTaskInterface $task): HistoryEvent |
||
1270 | { |
||
1271 | return $this->initHistoricExternalTaskLog($task, ExternalTaskStateImpl::successful()); |
||
1272 | } |
||
1273 | |||
1274 | public function createHistoricExternalTaskLogDeletedEvt(ExternalTaskInterface $task): HistoryEvent |
||
1275 | { |
||
1276 | return $this->initHistoricExternalTaskLog($task, ExternalTaskStateImpl::deleted()); |
||
1277 | } |
||
1278 | |||
1279 | protected function initHistoricExternalTaskLog(ExternalTaskEntity $entity, ExternalTaskStateInterface $state): HistoricExternalTaskLogEntity |
||
1280 | { |
||
1281 | $event = new HistoricExternalTaskLogEntity(); |
||
1282 | $event->setTimestamp(ClockUtil::getCurrentTime()->format('c')); |
||
1283 | $event->setExternalTaskId($entity->getId()); |
||
1284 | $event->setTopicName($entity->getTopicName()); |
||
1285 | $event->setWorkerId($entity->getWorkerId()); |
||
1286 | |||
1287 | $event->setPriority($entity->getPriority()); |
||
1288 | $event->setRetries($entity->getRetries()); |
||
1289 | |||
1290 | $event->setActivityId($entity->getActivityId()); |
||
1291 | $event->setActivityInstanceId($entity->getActivityInstanceId()); |
||
1292 | $event->setExecutionId($entity->getExecutionId()); |
||
1293 | |||
1294 | $event->setProcessInstanceId($entity->getProcessInstanceId()); |
||
1295 | $event->setProcessDefinitionId($entity->getProcessDefinitionId()); |
||
1296 | $event->setProcessDefinitionKey($entity->getProcessDefinitionKey()); |
||
1297 | $event->setTenantId($entity->getTenantId()); |
||
1298 | $event->setState($state->getStateCode()); |
||
1299 | |||
1300 | $execution = $entity->getExecution(); |
||
1301 | if ($execution !== null) { |
||
1302 | $event->setRootProcessInstanceId($execution->getRootProcessInstanceId()); |
||
1303 | |||
1304 | if ($this->isHistoryRemovalTimeStrategyStart()) { |
||
1305 | $this->provideRemovalTime($event); |
||
1306 | } |
||
1307 | } |
||
1308 | |||
1309 | return $event; |
||
1310 | } |
||
1311 | |||
1312 | protected function isRootProcessInstance(HistoricProcessInstanceEventEntity $evt): bool |
||
1313 | { |
||
1314 | return $evt->getProcessInstanceId() == $evt->getRootProcessInstanceId(); |
||
1315 | } |
||
1316 | |||
1317 | protected function isHistoryRemovalTimeStrategyStart(): bool |
||
1318 | { |
||
1319 | return ProcessEngineConfiguration::HISTORY_REMOVAL_TIME_STRATEGY_START == $this->getHistoryRemovalTimeStrategy(); |
||
1320 | } |
||
1321 | |||
1322 | protected function isHistoryRemovalTimeStrategyEnd(): bool |
||
1323 | { |
||
1324 | return ProcessEngineConfiguration::HISTORY_REMOVAL_TIME_STRATEGY_END == $this->getHistoryRemovalTimeStrategy(); |
||
1325 | } |
||
1326 | |||
1327 | protected function getHistoryRemovalTimeStrategy(): string |
||
1328 | { |
||
1329 | return Context::getProcessEngineConfiguration() |
||
1330 | ->getHistoryRemovalTimeStrategy(); |
||
1331 | } |
||
1332 | |||
1333 | protected function calculateRemovalTime($event): ?string |
||
1334 | { |
||
1335 | if ($event instanceof HistoryEvent) { |
||
1336 | $processDefinitionId = $event->getProcessDefinitionId(); |
||
1337 | $processDefinition = findProcessDefinitionById($processDefinitionId); |
||
1338 | |||
1339 | return Context::getProcessEngineConfiguration() |
||
1340 | ->getHistoryRemovalTimeProvider() |
||
1341 | ->calculateRemovalTime($event, $processDefinition); |
||
1342 | } elseif ($event instanceof HistoricBatchEntity) { |
||
1343 | return Context::getProcessEngineConfiguration() |
||
1344 | ->getHistoryRemovalTimeProvider() |
||
1345 | ->calculateRemovalTime($event); |
||
1346 | } |
||
1347 | return null; |
||
1348 | } |
||
1349 | |||
1350 | protected function provideRemovalTime($event): void |
||
1351 | { |
||
1352 | if ($event instanceof HistoryEvent) { |
||
1353 | $rootProcessInstanceId = $event->getRootProcessInstanceId(); |
||
1354 | if ($rootProcessInstanceId !== null) { |
||
1355 | $historicRootProcessInstance = $this->getHistoricRootProcessInstance($rootProcessInstanceId); |
||
1356 | |||
1357 | if ($historicRootProcessInstance !== null) { |
||
1358 | $removalTime = $historicRootProcessInstance->getRemovalTime(); |
||
1359 | $event->setRemovalTime($removalTime); |
||
1360 | } |
||
1361 | } |
||
1362 | } elseif ($event instanceof HistoricBatchEntity) { |
||
1363 | $removalTime = $this->calculateRemovalTime($event); |
||
1364 | if ($removalTime !== null) { |
||
1365 | $event->setRemovalTime($removalTime); |
||
1366 | } |
||
1367 | } |
||
1368 | } |
||
1369 | |||
1370 | protected function getHistoricRootProcessInstance(string $rootProcessInstanceId): ?HistoricProcessInstanceEventEntity |
||
1371 | { |
||
1372 | return Context::getCommandContext() |
||
1373 | ->getDbEntityManager() |
||
1374 | ->selectById(HistoricProcessInstanceEventEntity::class, $rootProcessInstanceId); |
||
1375 | } |
||
1376 | |||
1377 | protected function findProcessDefinitionById(string $processDefinitionId): ?ProcessDefinitionInterface |
||
1378 | { |
||
1379 | return Context::getCommandContext() |
||
1380 | ->getProcessEngineConfiguration() |
||
1381 | ->getDeploymentCache() |
||
1382 | ->findDeployedProcessDefinitionById($processDefinitionId); |
||
1383 | } |
||
1384 | |||
1385 | protected function getHistoricBatchById(string $batchId): ?HistoricBatchEntity |
||
1386 | { |
||
1387 | return Context::getCommandContext() |
||
1388 | ->getHistoricBatchManager() |
||
1389 | ->findHistoricBatchById($batchId); |
||
1390 | } |
||
1391 | |||
1392 | protected function getHistoricBatchByJobId(string $jobId): ?HistoricBatchEntity |
||
1397 | } |
||
1398 | |||
1399 | protected function addRemovalTimeToHistoricJobLog(HistoricBatchEntity $historicBatch): void |
||
1400 | { |
||
1401 | $removalTime = $historicBatch->getRemovalTime(); |
||
1402 | if ($removalTime !== null) { |
||
1403 | Context::getCommandContext() |
||
1404 | ->getHistoricJobLogManager() |
||
1405 | ->addRemovalTimeToJobLogByBatchId($historicBatch->getId(), $removalTime); |
||
1406 | } |
||
1407 | } |
||
1408 | |||
1409 | protected function addRemovalTimeToHistoricIncidents(HistoricBatchEntity $historicBatch): void |
||
1410 | { |
||
1411 | $removalTime = $historicBatch->getRemovalTime(); |
||
1412 | if ($removalTime !== null) { |
||
1413 | Context::getCommandContext() |
||
1414 | ->getHistoricIncidentManager() |
||
1415 | ->addRemovalTimeToHistoricIncidentsByBatchId($historicBatch->getId(), $removalTime); |
||
1416 | } |
||
1417 | } |
||
1418 | |||
1419 | // sequence counter ////////////////////////////////////////////////////// |
||
1420 | |||
1421 | protected function initSequenceCounter($obj, HistoryEvent $event): void |
||
1422 | { |
||
1423 | if (is_int($obj)) { |
||
1424 | $event->setSequenceCounter($obj); |
||
1427 | } |
||
1428 | } |
||
1429 | } |
||
1430 |