diff --git a/source/java/org/alfresco/repo/workflow/activiti/ActivitiWorkflowComponentTest.java b/source/java/org/alfresco/repo/workflow/activiti/ActivitiWorkflowComponentTest.java index 0ffddb8a9b..0e927f0e66 100644 --- a/source/java/org/alfresco/repo/workflow/activiti/ActivitiWorkflowComponentTest.java +++ b/source/java/org/alfresco/repo/workflow/activiti/ActivitiWorkflowComponentTest.java @@ -53,6 +53,7 @@ import org.alfresco.service.cmr.workflow.WorkflowException; import org.alfresco.service.cmr.workflow.WorkflowInstance; import org.alfresco.service.cmr.workflow.WorkflowNode; import org.alfresco.service.cmr.workflow.WorkflowPath; +import org.alfresco.service.cmr.workflow.WorkflowTask; import org.alfresco.service.cmr.workflow.WorkflowTaskDefinition; import org.alfresco.service.cmr.workflow.WorkflowTimer; import org.alfresco.service.namespace.QName; @@ -403,6 +404,50 @@ public class ActivitiWorkflowComponentTest extends AbstractActivitiComponentTest assertNull(historicProcessInstance); } + @Test + public void testDeleteFinishedWorkflow() throws Exception + { + WorkflowDefinition def = deployTestAdhocDefinition(); + + ProcessInstance processInstance = runtime.startProcessInstanceById(BPMEngineRegistry.getLocalId(def.getId())); + + // Validate if a workflow exists + List instances = workflowEngine.getActiveWorkflows(def.getId()); + assertNotNull(instances); + assertEquals(1, instances.size()); + assertEquals(processInstance.getId(), BPMEngineRegistry.getLocalId(instances.get(0).getId())); + + WorkflowInstance instance = instances.get(0); + + WorkflowTask startTask = workflowEngine.getStartTask(instance.getId()); + workflowEngine.endTask(startTask.getId(), null); + + WorkflowTask adhocTask = workflowEngine.getTasksForWorkflowPath(instance.getId()).get(0); + workflowEngine.endTask(adhocTask.getId(), null); + + // Validate if workflow is ended and has history + instances = workflowEngine.getActiveWorkflows(def.getId()); + assertNotNull(instances); + assertEquals(0, instances.size()); + + HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery() + .processInstanceId(processInstance.getProcessInstanceId()) + .singleResult(); + + assertNotNull(historicProcessInstance); + + + // Call delete method on component + workflowEngine.deleteWorkflow(instance.getId()); + + // Historic process instance shouldn't be present anymore + historicProcessInstance = historyService.createHistoricProcessInstanceQuery() + .processInstanceId(processInstance.getProcessInstanceId()) + .singleResult(); + + assertNull(historicProcessInstance); + } + @Test public void testDeleteUnexistingWorkflow() throws Exception { diff --git a/source/java/org/alfresco/repo/workflow/activiti/ActivitiWorkflowEngine.java b/source/java/org/alfresco/repo/workflow/activiti/ActivitiWorkflowEngine.java index 7952f485ef..78d428642c 100644 --- a/source/java/org/alfresco/repo/workflow/activiti/ActivitiWorkflowEngine.java +++ b/source/java/org/alfresco/repo/workflow/activiti/ActivitiWorkflowEngine.java @@ -280,19 +280,22 @@ public class ActivitiWorkflowEngine extends BPMEngine implements WorkflowEngine String localId = createLocalId(workflowId); try { + // Delete the runtime process instance if still running, this calls the end-listeners if any ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(localId).singleResult(); - if(processInstance == null) + if(processInstance != null) { - throw new WorkflowException(messageService.getMessage(ERR_DELETE_UNEXISTING_WORKFLOW)); + runtimeService.deleteProcessInstance(processInstance.getId(), ActivitiConstants.DELETE_REASON_DELETED); } - // Delete the process instance - runtimeService.deleteProcessInstance(processInstance.getId(), ActivitiConstants.DELETE_REASON_DELETED); - // Convert historic process instance HistoricProcessInstance deletedInstance = historyService.createHistoricProcessInstanceQuery() - .processInstanceId(processInstance.getId()) + .processInstanceId(localId) .singleResult(); + + if(deletedInstance == null) { + throw new WorkflowException(messageService.getMessage(ERR_DELETE_UNEXISTING_WORKFLOW, localId)); + } + WorkflowInstance result = typeConverter.convert(deletedInstance); // Delete the historic process instance diff --git a/source/java/org/alfresco/service/cmr/workflow/WorkflowService.java b/source/java/org/alfresco/service/cmr/workflow/WorkflowService.java index d04d03999c..e30e01d814 100644 --- a/source/java/org/alfresco/service/cmr/workflow/WorkflowService.java +++ b/source/java/org/alfresco/service/cmr/workflow/WorkflowService.java @@ -307,7 +307,7 @@ public interface WorkflowService public List cancelWorkflows(List workflowIds); /** - * Delete an "in-flight" Workflow instance + * Delete a Workflow instance. * * NOTE: This will force a delete, meaning that the workflow instance may not * go through all the appropriate cancel events.