added svcapi ui and camunda code
[it/otf.git] / otf-camunda / src / main / java / org / oran / otf / service / impl / DeleteProcessInstanceServiceImpl.java
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\r
3 #   Licensed under the Apache License, Version 2.0 (the "License");            #\r
4 #   you may not use this file except in compliance with the License.           #\r
5 #   You may obtain a copy of the License at                                    #\r
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\r
9 #   Unless required by applicable law or agreed to in writing, software        #\r
10 #   distributed under the License is distributed on an "AS IS" BASIS,          #\r
11 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
12 #   See the License for the specific language governing permissions and        #\r
13 #   limitations under the License.                                             #\r
14 ##############################################################################*/\r
15 \r
16 \r
17 package org.oran.otf.service.impl;\r
18 \r
19 import org.oran.otf.camunda.configuration.OtfCamundaConfiguration;\r
20 import org.oran.otf.camunda.service.ProcessEngineAwareService;\r
21 import org.oran.otf.camunda.workflow.utility.WorkflowTask;\r
22 import org.oran.otf.common.utility.gson.Convert;\r
23 import org.oran.otf.common.utility.http.ResponseUtility;\r
24 import org.oran.otf.service.DeleteProcessInstanceService;\r
25 import java.util.Date;\r
26 import java.util.HashMap;\r
27 import java.util.List;\r
28 import java.util.Map;\r
29 import javax.ws.rs.core.Response;\r
30 \r
31 import org.camunda.bpm.BpmPlatform;\r
32 import org.camunda.bpm.engine.RuntimeService;\r
33 import org.camunda.bpm.engine.runtime.ProcessInstance;\r
34 import org.slf4j.Logger;\r
35 import org.slf4j.LoggerFactory;\r
36 import org.springframework.stereotype.Service;\r
37 \r
38 @Service\r
39 public class DeleteProcessInstanceServiceImpl extends ProcessEngineAwareService\r
40     implements DeleteProcessInstanceService {\r
41 \r
42   private static Logger logger = LoggerFactory.getLogger(DeleteProcessInstanceServiceImpl.class);\r
43 \r
44   @Override\r
45   public Response deleteProcessInstance(String executionId) {\r
46     RuntimeService runtimeService = BpmPlatform.getProcessEngineService().getProcessEngine(OtfCamundaConfiguration.processEngineName).getRuntimeService();\r
47 \r
48     Map<String, Object> response =\r
49         deleteProcessInstanceInternal(\r
50             executionId, "Deleted via TCU endpoint at " + new Date(System.currentTimeMillis()));\r
51 \r
52     try {\r
53       int code = (int) response.get("code");\r
54       String sRes = Convert.mapToJson(response);\r
55       if (code == 404) {\r
56         return ResponseUtility.Build.notFoundWithMessage(sRes);\r
57       } else if (code == 200) {\r
58         return ResponseUtility.Build.okRequestWithMessage(sRes);\r
59       }\r
60     } catch (ClassCastException cce) {\r
61       logger.error(cce.getMessage());\r
62     }\r
63     // Unhandled response\r
64     return ResponseUtility.Build.internalServerError();\r
65   }\r
66 \r
67   public Map<String, Object> deleteProcessInstanceInternal(\r
68       String executionId, String deleteReason) {\r
69     RuntimeService runtimeService = BpmPlatform.getProcessEngineService().getProcessEngine(OtfCamundaConfiguration.processEngineName).getRuntimeService();\r
70 \r
71     ProcessInstance pi =\r
72         runtimeService.createProcessInstanceQuery().processInstanceId(executionId).singleResult();\r
73 \r
74     Map<String, Object> response = new HashMap<>();\r
75 \r
76     if (pi == null) {\r
77       response.put(\r
78           "result",\r
79           String.format("A process instance with the executionId %s was not found.", executionId));\r
80       response.put("code", 404);\r
81     } else {\r
82       List<WorkflowTask> workflowTasks = WorkflowTask.workflowTasksByExecutionId.get(executionId);\r
83       if (workflowTasks != null) {\r
84         for (WorkflowTask workflowTask : workflowTasks) {\r
85           workflowTask.shutdown();\r
86         }\r
87       }\r
88 \r
89       runtimeService.deleteProcessInstance(executionId, deleteReason);\r
90       response.put("result", "Successfully deleted.");\r
91       response.put("code", 200);\r
92     }\r
93 \r
94     return response;\r
95   }\r
96 }\r