From 8fdd57f982f3643bddac25a0194f94cb3a5b5c06 Mon Sep 17 00:00:00 2001 From: Romain Bioteau Date: Thu, 14 Mar 2024 09:58:17 +0100 Subject: [PATCH] fix(api): prevent delete process def. if archived children (#2883) When deleting a process definition, look for archived process instances in all states, including non root instances, and prevent deletion in that case. Closes [RUNTIME-1828](https://bonitasoft.atlassian.net/browse/RUNTIME-1828) --- .../bonitasoft/engine/test/APITestUtil.java | 34 +++++++++++-------- .../engine/api/impl/ProcessAPIImpl.java | 3 +- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/bonita-integration-tests/bonita-test-utils/src/main/java/org/bonitasoft/engine/test/APITestUtil.java b/bonita-integration-tests/bonita-test-utils/src/main/java/org/bonitasoft/engine/test/APITestUtil.java index 4f070e896a2..b805407372d 100644 --- a/bonita-integration-tests/bonita-test-utils/src/main/java/org/bonitasoft/engine/test/APITestUtil.java +++ b/bonita-integration-tests/bonita-test-utils/src/main/java/org/bonitasoft/engine/test/APITestUtil.java @@ -76,18 +76,7 @@ import org.bonitasoft.engine.bpm.flownode.FlowNodeInstanceNotFoundException; import org.bonitasoft.engine.bpm.flownode.GatewayInstance; import org.bonitasoft.engine.bpm.flownode.HumanTaskInstance; -import org.bonitasoft.engine.bpm.process.ActivationState; -import org.bonitasoft.engine.bpm.process.ArchivedProcessInstance; -import org.bonitasoft.engine.bpm.process.DesignProcessDefinition; -import org.bonitasoft.engine.bpm.process.Problem; -import org.bonitasoft.engine.bpm.process.ProcessDefinition; -import org.bonitasoft.engine.bpm.process.ProcessDefinitionNotFoundException; -import org.bonitasoft.engine.bpm.process.ProcessDeploymentInfo; -import org.bonitasoft.engine.bpm.process.ProcessDeploymentInfoCriterion; -import org.bonitasoft.engine.bpm.process.ProcessEnablementException; -import org.bonitasoft.engine.bpm.process.ProcessInstance; -import org.bonitasoft.engine.bpm.process.ProcessInstanceCriterion; -import org.bonitasoft.engine.bpm.process.ProcessInstanceState; +import org.bonitasoft.engine.bpm.process.*; import org.bonitasoft.engine.bpm.process.impl.ProcessDefinitionBuilder; import org.bonitasoft.engine.bpm.supervisor.ProcessSupervisor; import org.bonitasoft.engine.bpm.supervisor.ProcessSupervisorSearchDescriptor; @@ -702,18 +691,33 @@ public void disableAndDeleteProcess(final ProcessDefinition processDefinition) t } public void disableAndDeleteProcess(final long processDefinitionId) throws BonitaException { - getProcessAPI().disableProcess(processDefinitionId); + var processApi = getProcessAPI(); + processApi.disableProcess(processDefinitionId); // Delete all process instances long nbDeletedProcessInstances; do { - nbDeletedProcessInstances = getProcessAPI().deleteProcessInstances(processDefinitionId, 0, 100); + nbDeletedProcessInstances = processApi.deleteProcessInstances(processDefinitionId, 0, 100); } while (nbDeletedProcessInstances > 0); + // Search all root archived process instances ids for the given processDefinition + var rootArchivedProcessInstancesIds = processApi + .searchArchivedProcessInstancesInAllStates(new SearchOptionsBuilder(0, Integer.MAX_VALUE) + .filter(ArchivedProcessInstancesSearchDescriptor.PROCESS_DEFINITION_ID, processDefinitionId) + .differentFrom(ArchivedProcessInstancesSearchDescriptor.CALLER_ID, "-1") + .done()) + .getResult() + .stream() + .map(ArchivedProcessInstance::getRootProcessInstanceId) + .collect(Collectors.toList()); + if (!rootArchivedProcessInstancesIds.isEmpty()) { + processApi.deleteArchivedProcessInstancesInAllStates(rootArchivedProcessInstancesIds); + } + // Delete all archived process instances long nbDeletedArchivedProcessInstances; do { - nbDeletedArchivedProcessInstances = getProcessAPI().deleteArchivedProcessInstances(processDefinitionId, 0, + nbDeletedArchivedProcessInstances = processApi.deleteArchivedProcessInstances(processDefinitionId, 0, 100); } while (nbDeletedArchivedProcessInstances > 0); diff --git a/bpm/bonita-core/bonita-process-engine/src/main/java/org/bonitasoft/engine/api/impl/ProcessAPIImpl.java b/bpm/bonita-core/bonita-process-engine/src/main/java/org/bonitasoft/engine/api/impl/ProcessAPIImpl.java index fb17876dfaa..3e0dd6c6e71 100644 --- a/bpm/bonita-core/bonita-process-engine/src/main/java/org/bonitasoft/engine/api/impl/ProcessAPIImpl.java +++ b/bpm/bonita-core/bonita-process-engine/src/main/java/org/bonitasoft/engine/api/impl/ProcessAPIImpl.java @@ -497,7 +497,8 @@ public void deleteProcessDefinition(final long processDefinitionId) throws Delet final boolean hasOpenProcessInstances = searchProcessInstances(getTenantAccessor(), searchOptions) .getCount() > 0; checkIfItIsPossibleToDeleteProcessInstance(processDefinitionId, hasOpenProcessInstances); - final boolean hasArchivedProcessInstances = searchArchivedProcessInstances(searchOptions).getCount() > 0; + final boolean hasArchivedProcessInstances = searchArchivedProcessInstancesInAllStates(searchOptions) + .getCount() > 0; checkIfItIsPossibleToDeleteProcessInstance(processDefinitionId, hasArchivedProcessInstances); removeAllCategoriesFromProcessDefinition(processDefinitionId);