|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jabe\Engine\Impl\Batch\RemovalTime; |
|
4
|
|
|
|
|
5
|
|
|
use Jabe\Engine\ProcessEngineConfiguration; |
|
6
|
|
|
use Jabe\Engine\Batch\BatchInterface; |
|
7
|
|
|
use Jabe\Engine\Impl\Batch\{ |
|
8
|
|
|
AbstractBatchJobHandler, |
|
9
|
|
|
BatchJobConfiguration, |
|
10
|
|
|
BatchJobContext, |
|
11
|
|
|
BatchJobDeclaration |
|
12
|
|
|
}; |
|
13
|
|
|
use Jabe\Engine\Impl\Interceptor\CommandContext; |
|
14
|
|
|
use Jabe\Engine\Impl\JobExecutor\JobDeclaration; |
|
15
|
|
|
use Jabe\Engine\Impl\Json\JsonObjectConverter; |
|
16
|
|
|
use Jabe\Engine\Impl\Persistence\Entity\{ |
|
17
|
|
|
ByteArrayEntity, |
|
18
|
|
|
ExecutionEntity, |
|
19
|
|
|
HistoricProcessInstanceEntity, |
|
20
|
|
|
MessageEntity |
|
21
|
|
|
}; |
|
22
|
|
|
use Jabe\Engine\Repository\ProcessDefinitionInterface; |
|
23
|
|
|
|
|
24
|
|
|
class ProcessSetRemovalTimeJobHandler extends AbstractBatchJobHandler |
|
25
|
|
|
{ |
|
26
|
|
|
public static $JOB_DECLARATION; |
|
27
|
|
|
|
|
28
|
|
|
public function execute(BatchJobConfiguration $configuration, ExecutionEntity $execution, CommandContext $commandContext, string $tenantId = null) |
|
29
|
|
|
{ |
|
30
|
|
|
$byteArrayId = $configuration->getConfigurationByteArrayId(); |
|
31
|
|
|
$configurationByteArray = $this->findByteArrayById($byteArrayId, $commandContext); |
|
32
|
|
|
|
|
33
|
|
|
$batchConfiguration = $this->readConfiguration($configurationByteArray); |
|
34
|
|
|
|
|
35
|
|
|
foreach ($batchConfiguration->getIds() as $instanceId) { |
|
36
|
|
|
$instance = $this->findProcessInstanceById($instanceId, $commandContext); |
|
37
|
|
|
|
|
38
|
|
|
if ($instance != null) { |
|
39
|
|
|
if ($batchConfiguration->isHierarchical() && $this->hasHierarchy($instance)) { |
|
|
|
|
|
|
40
|
|
|
$rootProcessInstanceId = $instance->getRootProcessInstanceId(); |
|
41
|
|
|
$rootInstance = $this->findProcessInstanceById($rootProcessInstanceId, $commandContext); |
|
42
|
|
|
$removalTime = $this->getOrCalculateRemovalTime($batchConfiguration, $rootInstance, $commandContext); |
|
|
|
|
|
|
43
|
|
|
$this->addRemovalTimeToHierarchy($rootProcessInstanceId, $removalTime, $commandContext); |
|
|
|
|
|
|
44
|
|
|
} else { |
|
45
|
|
|
$removalTime = $this->getOrCalculateRemovalTime($batchConfiguration, $instance, $commandContext); |
|
46
|
|
|
if ($removalTime != $instance->getRemovalTime()) { |
|
47
|
|
|
$this->addRemovalTime($instanceId, $removalTime, $commandContext); |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
protected function getOrCalculateRemovalTime(SetRemovalTimeBatchConfiguration $batchConfiguration, HistoricProcessInstanceEntity $instance, CommandContext $commandContext): ?string |
|
55
|
|
|
{ |
|
56
|
|
|
if ($batchConfiguration->hasRemovalTime()) { |
|
57
|
|
|
return $batchConfiguration->getRemovalTime(); |
|
58
|
|
|
} elseif ($this->hasBaseTime($instance, $commandContext)) { |
|
59
|
|
|
return $this->calculateRemovalTime($instance, $commandContext); |
|
60
|
|
|
} else { |
|
61
|
|
|
return null; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
protected function addRemovalTimeToHierarchy(string $rootProcessInstanceId, string $removalTime, CommandContext $commandContext): void |
|
66
|
|
|
{ |
|
67
|
|
|
$commandContext->getHistoricProcessInstanceManager() |
|
68
|
|
|
->addRemovalTimeToProcessInstancesByRootProcessInstanceId($rootProcessInstanceId, $removalTime); |
|
69
|
|
|
|
|
70
|
|
|
/*if (isDmnEnabled(commandContext)) { |
|
71
|
|
|
$commandContext->getHistoricDecisionInstanceManager() |
|
72
|
|
|
.addRemovalTimeToDecisionsByRootProcessInstanceId(rootProcessInstanceId, removalTime); |
|
73
|
|
|
}*/ |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
protected function addRemovalTime(string $instanceId, string $removalTime, CommandContext $commandContext): void |
|
77
|
|
|
{ |
|
78
|
|
|
$commandContext->getHistoricProcessInstanceManager() |
|
79
|
|
|
->addRemovalTimeById($instanceId, $removalTime); |
|
80
|
|
|
|
|
81
|
|
|
/*if (isDmnEnabled(commandContext)) { |
|
82
|
|
|
$commandContext->getHistoricDecisionInstanceManager() |
|
83
|
|
|
.addRemovalTimeToDecisionsByProcessInstanceId(instanceId, removalTime); |
|
84
|
|
|
}*/ |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected function hasBaseTime(HistoricProcessInstanceEntity $instance, CommandContext $commandContext): bool |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->isStrategyStart($commandContext) || ($this->isStrategyEnd($commandContext) && $this->isEnded($instance)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected function isEnded(HistoricProcessInstanceEntity $instance): bool |
|
93
|
|
|
{ |
|
94
|
|
|
return $instance->getEndTime() != null; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
protected function isStrategyStart(CommandContext $commandContext): bool |
|
98
|
|
|
{ |
|
99
|
|
|
return ProcessEngineConfiguration::HISTORY_REMOVAL_TIME_STRATEGY_START == $this->getHistoryRemovalTimeStrategy($commandContext); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
protected function isStrategyEnd(CommandContext $commandContext): bool |
|
103
|
|
|
{ |
|
104
|
|
|
return ProcessEngineConfiguration::HISTORY_REMOVAL_TIME_STRATEGY_END == $this->getHistoryRemovalTimeStrategy($commandContext); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
protected function hasHierarchy(HistoricProcessInstanceEntity $instance): bool |
|
108
|
|
|
{ |
|
109
|
|
|
return $instance->getRootProcessInstanceId() != null; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
protected function getHistoryRemovalTimeStrategy(CommandContext $commandContext): string |
|
113
|
|
|
{ |
|
114
|
|
|
return $commandContext->getProcessEngineConfiguration() |
|
115
|
|
|
->getHistoryRemovalTimeStrategy(); |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
protected function findProcessDefinitionById(string $processDefinitionId, CommandContext $commandContext): ?ProcessDefinitionInterface |
|
119
|
|
|
{ |
|
120
|
|
|
return $commandContext->getProcessEngineConfiguration() |
|
121
|
|
|
->getDeploymentCache() |
|
|
|
|
|
|
122
|
|
|
->findDeployedProcessDefinitionById($processDefinitionId); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/*protected boolean isDmnEnabled(CommandContext commandContext) { |
|
126
|
|
|
return $commandContext->getProcessEngineConfiguration().isDmnEnabled(); |
|
127
|
|
|
}*/ |
|
128
|
|
|
|
|
129
|
|
|
protected function calculateRemovalTime(HistoricProcessInstanceEntity $processInstance, CommandContext $commandContext): string |
|
130
|
|
|
{ |
|
131
|
|
|
$processDefinition = $this->findProcessDefinitionById($processInstance->getProcessDefinitionId(), $commandContext); |
|
132
|
|
|
|
|
133
|
|
|
return $commandContext->getProcessEngineConfiguration() |
|
134
|
|
|
->getHistoryRemovalTimeProvider() |
|
|
|
|
|
|
135
|
|
|
->calculateRemovalTime($processInstance, $processDefinition); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
protected function findByteArrayById(string $byteArrayId, CommandContext $commandContext): ByteArrayEntity |
|
139
|
|
|
{ |
|
140
|
|
|
return $commandContext->getDbEntityManager() |
|
141
|
|
|
->selectById(ByteArrayEntity::class, $byteArrayId); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
protected function findProcessInstanceById(string $instanceId, CommandContext $commandContext): ?HistoricProcessInstanceEntity |
|
145
|
|
|
{ |
|
146
|
|
|
return $commandContext->getHistoricProcessInstanceManager() |
|
147
|
|
|
->findHistoricProcessInstance($instanceId); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function getJobDeclaration(): JobDeclaration |
|
151
|
|
|
{ |
|
152
|
|
|
if (self::$JOB_DECLARATION === null) { |
|
153
|
|
|
self::$JOB_DECLARATION = new BatchJobDeclaration(BatchInterface::TYPE_PROCESS_SET_REMOVAL_TIME); |
|
154
|
|
|
} |
|
155
|
|
|
return self::$JOB_DECLARATION; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
protected function createJobConfiguration(BatchConfiguration $configuration, array $processInstanceIds): BatchConfiguration |
|
159
|
|
|
{ |
|
160
|
|
|
return (new SetRemovalTimeBatchConfiguration($processInstanceIds)) |
|
|
|
|
|
|
161
|
|
|
->setRemovalTime($configuration->getRemovalTime()) |
|
162
|
|
|
->setHasRemovalTime($configuration->hasRemovalTime()) |
|
163
|
|
|
->setHierarchical($configuration->isHierarchical()); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
protected function getJsonConverterInstance(): JsonObjectConverter |
|
167
|
|
|
{ |
|
168
|
|
|
return SetRemovalTimeJsonConverter::instance(); |
|
|
|
|
|
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
public function getType(): string |
|
172
|
|
|
{ |
|
173
|
|
|
return BatchInteface::TYPE_PROCESS_SET_REMOVAL_TIME; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|