+/* Copyright (c) 2019 AT&T Intellectual Property. #\r
+# #\r
+# Licensed under the Apache License, Version 2.0 (the "License"); #\r
+# you may not use this file except in compliance with the License. #\r
+# You may obtain a copy of the License at #\r
+# #\r
+# http://www.apache.org/licenses/LICENSE-2.0 #\r
+# #\r
+# Unless required by applicable law or agreed to in writing, software #\r
+# distributed under the License is distributed on an "AS IS" BASIS, #\r
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #\r
+# See the License for the specific language governing permissions and #\r
+# limitations under the License. #\r
+##############################################################################*/\r
+\r
+\r
+package org.oran.otf.service.impl;\r
+\r
+import org.oran.otf.camunda.configuration.OtfCamundaConfiguration;\r
+import org.oran.otf.camunda.service.ProcessEngineAwareService;\r
+import org.oran.otf.camunda.workflow.utility.WorkflowTask;\r
+import org.oran.otf.common.utility.gson.Convert;\r
+import org.oran.otf.common.utility.http.ResponseUtility;\r
+import org.oran.otf.service.DeleteProcessInstanceService;\r
+import java.util.Date;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
+import javax.ws.rs.core.Response;\r
+\r
+import org.camunda.bpm.BpmPlatform;\r
+import org.camunda.bpm.engine.RuntimeService;\r
+import org.camunda.bpm.engine.runtime.ProcessInstance;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+import org.springframework.stereotype.Service;\r
+\r
+@Service\r
+public class DeleteProcessInstanceServiceImpl extends ProcessEngineAwareService\r
+ implements DeleteProcessInstanceService {\r
+\r
+ private static Logger logger = LoggerFactory.getLogger(DeleteProcessInstanceServiceImpl.class);\r
+\r
+ @Override\r
+ public Response deleteProcessInstance(String executionId) {\r
+ RuntimeService runtimeService = BpmPlatform.getProcessEngineService().getProcessEngine(OtfCamundaConfiguration.processEngineName).getRuntimeService();\r
+\r
+ Map<String, Object> response =\r
+ deleteProcessInstanceInternal(\r
+ executionId, "Deleted via TCU endpoint at " + new Date(System.currentTimeMillis()));\r
+\r
+ try {\r
+ int code = (int) response.get("code");\r
+ String sRes = Convert.mapToJson(response);\r
+ if (code == 404) {\r
+ return ResponseUtility.Build.notFoundWithMessage(sRes);\r
+ } else if (code == 200) {\r
+ return ResponseUtility.Build.okRequestWithMessage(sRes);\r
+ }\r
+ } catch (ClassCastException cce) {\r
+ logger.error(cce.getMessage());\r
+ }\r
+ // Unhandled response\r
+ return ResponseUtility.Build.internalServerError();\r
+ }\r
+\r
+ public Map<String, Object> deleteProcessInstanceInternal(\r
+ String executionId, String deleteReason) {\r
+ RuntimeService runtimeService = BpmPlatform.getProcessEngineService().getProcessEngine(OtfCamundaConfiguration.processEngineName).getRuntimeService();\r
+\r
+ ProcessInstance pi =\r
+ runtimeService.createProcessInstanceQuery().processInstanceId(executionId).singleResult();\r
+\r
+ Map<String, Object> response = new HashMap<>();\r
+\r
+ if (pi == null) {\r
+ response.put(\r
+ "result",\r
+ String.format("A process instance with the executionId %s was not found.", executionId));\r
+ response.put("code", 404);\r
+ } else {\r
+ List<WorkflowTask> workflowTasks = WorkflowTask.workflowTasksByExecutionId.get(executionId);\r
+ if (workflowTasks != null) {\r
+ for (WorkflowTask workflowTask : workflowTasks) {\r
+ workflowTask.shutdown();\r
+ }\r
+ }\r
+\r
+ runtimeService.deleteProcessInstance(executionId, deleteReason);\r
+ response.put("result", "Successfully deleted.");\r
+ response.put("code", 200);\r
+ }\r
+\r
+ return response;\r
+ }\r
+}\r