|
12 | 12 | */ |
13 | 13 | package org.flowable.cmmn.engine.impl.history; |
14 | 14 |
|
| 15 | +import java.util.List; |
| 16 | + |
15 | 17 | import org.apache.commons.lang3.StringUtils; |
16 | 18 | import org.flowable.cmmn.api.repository.CaseDefinition; |
17 | 19 | import org.flowable.cmmn.engine.CmmnEngineConfiguration; |
|
25 | 27 | import org.flowable.cmmn.model.ExtensionElement; |
26 | 28 | import org.flowable.cmmn.model.PlanItem; |
27 | 29 | import org.flowable.cmmn.model.PlanItemDefinition; |
| 30 | +import org.flowable.common.engine.api.constant.ReferenceTypes; |
28 | 31 | import org.flowable.common.engine.api.scope.ScopeTypes; |
29 | 32 | import org.flowable.common.engine.impl.history.HistoryLevel; |
30 | 33 | import org.flowable.entitylink.service.impl.persistence.entity.EntityLinkEntity; |
@@ -218,12 +221,41 @@ protected boolean hasTaskHistoryLevel(HistoryLevel historyLevel) { |
218 | 221 | public boolean isHistoryEnabledForVariableInstance(VariableInstanceEntity variableInstanceEntity) { |
219 | 222 | String caseDefinitionId = null; |
220 | 223 | if (isEnableCaseDefinitionHistoryLevel() && variableInstanceEntity.getScopeId() != null) { |
221 | | - CaseInstanceEntity caseInstance = cmmnEngineConfiguration.getCaseInstanceEntityManager().findById(variableInstanceEntity.getScopeId()); |
222 | | - caseDefinitionId = caseInstance.getCaseDefinitionId(); |
| 224 | + caseDefinitionId = getCaseDefinitionId(variableInstanceEntity); |
223 | 225 | } |
224 | 226 | return isHistoryLevelAtLeast(HistoryLevel.AUDIT, caseDefinitionId); |
225 | 227 | } |
226 | 228 |
|
| 229 | + protected String getCaseDefinitionId(VariableInstanceEntity variableInstanceEntity) { |
| 230 | + String scopeId = variableInstanceEntity.getScopeId(); |
| 231 | + |
| 232 | + // A case (or case sub-scope) variable stores the case instance id as its scopeId. |
| 233 | + CaseInstanceEntity caseInstance = cmmnEngineConfiguration.getCaseInstanceEntityManager().findById(scopeId); |
| 234 | + if (caseInstance != null) { |
| 235 | + return caseInstance.getCaseDefinitionId(); |
| 236 | + } |
| 237 | + |
| 238 | + // The scopeId is not a case instance id. This happens for a variable that belongs to a process running |
| 239 | + // under a case, e.g. a BPMN multi-instance variable-aggregation variable whose scopeId is the process |
| 240 | + // instance id (see BpmnAggregation). The process may itself be nested under the case through one or more |
| 241 | + // BPMN call activities, so resolve the root process instance of the call hierarchy - that is the process |
| 242 | + // a CMMN process task started directly - and use the plan item instance that started it (its referenceId |
| 243 | + // is that root process instance id) to determine the owning case definition. |
| 244 | + ProcessInstanceService processInstanceService = cmmnEngineConfiguration.getProcessInstanceService(); |
| 245 | + if (processInstanceService != null) { |
| 246 | + String rootProcessInstanceId = processInstanceService.getRootProcessInstanceId(scopeId); |
| 247 | + List<PlanItemInstanceEntity> planItemInstances = cmmnEngineConfiguration.getPlanItemInstanceEntityManager() |
| 248 | + .findByReferenceId(rootProcessInstanceId); |
| 249 | + for (PlanItemInstanceEntity planItemInstance : planItemInstances) { |
| 250 | + if (ReferenceTypes.PLAN_ITEM_CHILD_PROCESS.equals(planItemInstance.getReferenceType())) { |
| 251 | + return planItemInstance.getCaseDefinitionId(); |
| 252 | + } |
| 253 | + } |
| 254 | + } |
| 255 | + |
| 256 | + return null; |
| 257 | + } |
| 258 | + |
227 | 259 | @Override |
228 | 260 | public boolean isHistoryEnabledForIdentityLink(IdentityLinkEntity identityLinkEntity) { |
229 | 261 | String caseDefinitionId = getCaseDefinitionId(identityLinkEntity); |
|
0 commit comments