added svcapi ui and camunda code
[it/otf.git] / otf-camunda / src / main / java / org / oran / otf / camunda / listener / TaskEndEventListener.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.camunda.listener;\r
18 \r
19 import org.oran.otf.camunda.exception.TestExecutionException;\r
20 import org.oran.otf.camunda.workflow.utility.WorkflowUtility;\r
21 import org.oran.otf.common.utility.Utility;\r
22 import org.camunda.bpm.engine.RuntimeService;\r
23 import org.camunda.bpm.engine.delegate.DelegateExecution;\r
24 import org.camunda.bpm.engine.delegate.ExecutionListener;\r
25 import org.camunda.bpm.engine.impl.context.Context;\r
26 import org.camunda.bpm.engine.impl.interceptor.Command;\r
27 import org.camunda.bpm.engine.impl.interceptor.CommandContext;\r
28 import org.camunda.bpm.engine.runtime.ProcessInstance;\r
29 import org.camunda.bpm.extension.reactor.bus.CamundaSelector;\r
30 import org.camunda.bpm.extension.reactor.spring.listener.ReactorExecutionListener;\r
31 import org.camunda.bpm.model.bpmn.instance.Task;\r
32 import org.slf4j.Logger;\r
33 import org.slf4j.LoggerFactory;\r
34 import org.springframework.beans.factory.annotation.Autowired;\r
35 import org.springframework.data.mongodb.core.MongoTemplate;\r
36 import org.springframework.stereotype.Component;\r
37 \r
38 @Component\r
39 @CamundaSelector(event = ExecutionListener.EVENTNAME_END)\r
40 public class TaskEndEventListener extends ReactorExecutionListener {\r
41 \r
42     @Autowired\r
43     WorkflowUtility utility;\r
44 \r
45     @Autowired\r
46     MongoTemplate mongoOperation;\r
47 \r
48     @Autowired\r
49     RuntimeService runtimeService;\r
50 \r
51     private static Logger LOGGER = LoggerFactory.getLogger(TaskEndEventListener.class);\r
52 \r
53     @Override\r
54     public void notify(DelegateExecution execution) {\r
55         if(execution.getBpmnModelElementInstance() instanceof Task){\r
56             String processInstanceId = execution.getProcessInstanceId();\r
57             ProcessInstance processInstance;\r
58             try {\r
59                 processInstance = checkProcessInstanceStatus(processInstanceId);\r
60             }catch(Exception e){\r
61                throw new TestExecutionException("Error trying to obtain process instance status, error: " + e) ;\r
62             }\r
63             // if process instance not found then terminate the current process\r
64             if(processInstance == null || processInstance.isEnded() || processInstance.isSuspended()){\r
65                 String logPrefix = Utility.getLoggerPrefix();\r
66 \r
67                 LOGGER.info(logPrefix + "Process Instance not found. Terminating current job (thread).");\r
68                 Thread.currentThread().interrupt();\r
69                 throw new TestExecutionException("Terminated Process Instance: " + processInstanceId + ". Process Instance no longer exists, thread has been forcefully interrupted");\r
70             }\r
71         }\r
72     }\r
73 \r
74     private ProcessInstance checkProcessInstanceStatus(String processInstanceId){\r
75         return Context.getProcessEngineConfiguration().getCommandExecutorTxRequiresNew().execute(new Command<ProcessInstance>() {\r
76             @Override\r
77             public ProcessInstance execute(CommandContext commandContext){\r
78                 return runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();\r
79             }\r
80         });\r
81     }\r
82 \r
83 }\r