--- /dev/null
+/* 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.common.model.local.OTFProcessInstanceCompletionResponse;\r
+import org.oran.otf.common.utility.http.ResponseUtility;\r
+import org.oran.otf.service.ProcessInstanceCompletionService;\r
+import java.util.List;\r
+import javax.ws.rs.core.Response;\r
+\r
+import org.camunda.bpm.BpmPlatform;\r
+import org.camunda.bpm.engine.HistoryService;\r
+import org.camunda.bpm.engine.ManagementService;\r
+import org.camunda.bpm.engine.RuntimeService;\r
+import org.camunda.bpm.engine.history.*;\r
+import org.springframework.boot.context.event.ApplicationReadyEvent;\r
+import org.springframework.context.event.EventListener;\r
+import org.springframework.stereotype.Service;\r
+\r
+@Service\r
+public class ProcessInstanceCompletionServiceImpl implements ProcessInstanceCompletionService {\r
+\r
+ RuntimeService runtimeService;\r
+\r
+ ManagementService managementService;\r
+\r
+ HistoryService historyService;\r
+\r
+\r
+\r
+ private ProcessInstanceCompletionServiceImpl() {\r
+ // prohibit instantiation\r
+ }\r
+\r
+ @EventListener(ApplicationReadyEvent.class)\r
+ private void initialize(){\r
+ if(this.runtimeService == null){\r
+ this.runtimeService = BpmPlatform.getProcessEngineService().getProcessEngine(OtfCamundaConfiguration.processEngineName).getRuntimeService();\r
+ }\r
+ if(this.managementService == null){\r
+ this.managementService = BpmPlatform.getProcessEngineService().getProcessEngine(OtfCamundaConfiguration.processEngineName).getManagementService();\r
+ }\r
+ if(this.historyService == null){\r
+ this.historyService = BpmPlatform.getProcessEngineService().getProcessEngine(OtfCamundaConfiguration.processEngineName).getHistoryService();\r
+ }\r
+\r
+ }\r
+\r
+ @Override\r
+ public Response isProcessInstanceComplete(String processInstanceId) {\r
+\r
+ HistoricProcessInstance historicProcessInstance = historyService\r
+ .createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();\r
+\r
+ List<HistoricActivityInstance> historicActivityInstance = historyService\r
+ .createHistoricActivityInstanceQuery().processInstanceId(processInstanceId).list();\r
+\r
+ List<HistoricIncident> historicIncident =\r
+ historyService.createHistoricIncidentQuery().processInstanceId(processInstanceId).list();\r
+\r
+ List<HistoricJobLog> historicJobLog =\r
+ historyService.createHistoricJobLogQuery().processInstanceId(processInstanceId).list();\r
+\r
+ List<HistoricExternalTaskLog> historicExternalTaskLog =\r
+ historyService.createHistoricExternalTaskLogQuery().processInstanceId(processInstanceId).list();\r
+\r
+ List<HistoricVariableInstance> historicVariableInstance =\r
+ historyService.createHistoricVariableInstanceQuery().processInstanceId(processInstanceId).list();\r
+\r
+\r
+\r
+ OTFProcessInstanceCompletionResponse response = new OTFProcessInstanceCompletionResponse();\r
+ response.setHistoricProcessInstance(historicProcessInstance);\r
+ response.setHistoricActivityInstance(historicActivityInstance);\r
+ response.setHistoricIncident(historicIncident);\r
+ response.setHistoricJobLog(historicJobLog);\r
+ response.setHistoricExternalTaskLog(historicExternalTaskLog);\r
+ response.setHistoricVariableInstance(historicVariableInstance);\r
+\r
+\r
+ return ResponseUtility.Build.okRequestWithObject(response);\r
+\r
+ // Boolean done = runtimeService\r
+// .createProcessInstanceQuery()\r
+// .processInstanceId(processInstanceId)\r
+// .singleResult() == null;\r
+//\r
+// if(done) {\r
+// return Response.ok(new ProcessInstanceCompletionResponse("Completed", "Process Instance Completed Execution")).build();\r
+// }\r
+//\r
+//\r
+// Incident incident = runtimeService.createIncidentQuery().processInstanceId(processInstanceId).singleResult();\r
+// if(incident != null && incident.getIncidentType().equals("failedJob")) {\r
+// String errorMessage = incident.getIncidentMessage();\r
+// return Response.ok(new ProcessInstanceCompletionResponse("Failed", errorMessage)).build();\r
+// }\r
+//\r
+//\r
+// else {\r
+// return Response.ok(new ProcessInstanceCompletionResponse("In Progress", "Process Instance is active")).build();\r
+// }\r
+ }\r
+}\r