added svcapi ui and camunda code
[it/otf.git] / otf-camunda / src / main / java / org / oran / otf / service / impl / ProcessInstanceCompletionServiceImpl.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.common.model.local.OTFProcessInstanceCompletionResponse;\r
21 import org.oran.otf.common.utility.http.ResponseUtility;\r
22 import org.oran.otf.service.ProcessInstanceCompletionService;\r
23 import java.util.List;\r
24 import javax.ws.rs.core.Response;\r
25 \r
26 import org.camunda.bpm.BpmPlatform;\r
27 import org.camunda.bpm.engine.HistoryService;\r
28 import org.camunda.bpm.engine.ManagementService;\r
29 import org.camunda.bpm.engine.RuntimeService;\r
30 import org.camunda.bpm.engine.history.*;\r
31 import org.springframework.boot.context.event.ApplicationReadyEvent;\r
32 import org.springframework.context.event.EventListener;\r
33 import org.springframework.stereotype.Service;\r
34 \r
35 @Service\r
36 public class ProcessInstanceCompletionServiceImpl implements ProcessInstanceCompletionService {\r
37 \r
38     RuntimeService runtimeService;\r
39 \r
40     ManagementService managementService;\r
41 \r
42     HistoryService historyService;\r
43 \r
44 \r
45 \r
46     private ProcessInstanceCompletionServiceImpl() {\r
47         // prohibit instantiation\r
48     }\r
49 \r
50     @EventListener(ApplicationReadyEvent.class)\r
51     private void initialize(){\r
52         if(this.runtimeService == null){\r
53             this.runtimeService = BpmPlatform.getProcessEngineService().getProcessEngine(OtfCamundaConfiguration.processEngineName).getRuntimeService();\r
54         }\r
55         if(this.managementService == null){\r
56             this.managementService = BpmPlatform.getProcessEngineService().getProcessEngine(OtfCamundaConfiguration.processEngineName).getManagementService();\r
57         }\r
58         if(this.historyService == null){\r
59             this.historyService = BpmPlatform.getProcessEngineService().getProcessEngine(OtfCamundaConfiguration.processEngineName).getHistoryService();\r
60         }\r
61 \r
62     }\r
63 \r
64     @Override\r
65     public Response isProcessInstanceComplete(String processInstanceId) {\r
66 \r
67         HistoricProcessInstance historicProcessInstance = historyService\r
68                 .createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();\r
69 \r
70         List<HistoricActivityInstance> historicActivityInstance = historyService\r
71                 .createHistoricActivityInstanceQuery().processInstanceId(processInstanceId).list();\r
72 \r
73         List<HistoricIncident> historicIncident =\r
74                 historyService.createHistoricIncidentQuery().processInstanceId(processInstanceId).list();\r
75 \r
76         List<HistoricJobLog> historicJobLog =\r
77                 historyService.createHistoricJobLogQuery().processInstanceId(processInstanceId).list();\r
78 \r
79         List<HistoricExternalTaskLog> historicExternalTaskLog =\r
80                 historyService.createHistoricExternalTaskLogQuery().processInstanceId(processInstanceId).list();\r
81 \r
82         List<HistoricVariableInstance> historicVariableInstance =\r
83                 historyService.createHistoricVariableInstanceQuery().processInstanceId(processInstanceId).list();\r
84 \r
85 \r
86 \r
87         OTFProcessInstanceCompletionResponse response = new OTFProcessInstanceCompletionResponse();\r
88         response.setHistoricProcessInstance(historicProcessInstance);\r
89         response.setHistoricActivityInstance(historicActivityInstance);\r
90         response.setHistoricIncident(historicIncident);\r
91         response.setHistoricJobLog(historicJobLog);\r
92         response.setHistoricExternalTaskLog(historicExternalTaskLog);\r
93         response.setHistoricVariableInstance(historicVariableInstance);\r
94 \r
95 \r
96         return ResponseUtility.Build.okRequestWithObject(response);\r
97 \r
98         //              Boolean done = runtimeService\r
99 //                      .createProcessInstanceQuery()\r
100 //                      .processInstanceId(processInstanceId)\r
101 //                      .singleResult() == null;\r
102 //\r
103 //              if(done) {\r
104 //                      return Response.ok(new ProcessInstanceCompletionResponse("Completed", "Process Instance Completed Execution")).build();\r
105 //              }\r
106 //\r
107 //\r
108 //              Incident incident = runtimeService.createIncidentQuery().processInstanceId(processInstanceId).singleResult();\r
109 //              if(incident != null && incident.getIncidentType().equals("failedJob")) {\r
110 //                      String errorMessage = incident.getIncidentMessage();\r
111 //                      return Response.ok(new ProcessInstanceCompletionResponse("Failed", errorMessage)).build();\r
112 //              }\r
113 //\r
114 //\r
115 //              else {\r
116 //                      return Response.ok(new ProcessInstanceCompletionResponse("In Progress", "Process Instance is active")).build();\r
117 //              }\r
118     }\r
119 }\r