added svcapi ui and camunda code
[it/otf.git] / otf-camunda / src / main / java / org / oran / otf / camunda / listener / StartEventListener.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.model.ExecutionConstants;\r
21 import org.oran.otf.camunda.workflow.utility.WorkflowUtility;\r
22 import org.oran.otf.common.model.TestExecution;\r
23 import org.oran.otf.common.utility.Utility;\r
24 import com.google.gson.JsonObject;\r
25 import com.mongodb.client.result.UpdateResult;\r
26 import org.camunda.bpm.engine.delegate.DelegateExecution;\r
27 import org.camunda.bpm.engine.delegate.ExecutionListener;\r
28 import org.camunda.bpm.extension.reactor.bus.CamundaSelector;\r
29 import org.camunda.bpm.extension.reactor.spring.listener.ReactorExecutionListener;\r
30 import org.camunda.bpm.model.bpmn.instance.StartEvent;\r
31 import org.slf4j.Logger;\r
32 import org.slf4j.LoggerFactory;\r
33 import org.springframework.beans.factory.annotation.Autowired;\r
34 import org.springframework.data.mongodb.core.MongoTemplate;\r
35 import org.springframework.data.mongodb.core.query.Criteria;\r
36 import org.springframework.data.mongodb.core.query.Query;\r
37 import org.springframework.data.mongodb.core.query.Update;\r
38 import org.springframework.stereotype.Component;\r
39 \r
40 @Component\r
41 @CamundaSelector(event = ExecutionListener.EVENTNAME_END)\r
42 public class StartEventListener extends ReactorExecutionListener {\r
43 \r
44   @Autowired\r
45   WorkflowUtility utility;\r
46 \r
47   @Autowired\r
48   MongoTemplate mongoOperation;\r
49 \r
50   private static Logger LOGGER = LoggerFactory.getLogger(StartEventListener.class);\r
51 \r
52   @Override\r
53   public void notify(DelegateExecution execution) {\r
54     if (execution.getBpmnModelElementInstance() instanceof StartEvent) {\r
55       LOGGER.info(execution.getProcessInstanceId() + " has started.");\r
56       //setTestResult(execution, ExecutionConstants.TestResult.STARTED);\r
57     }\r
58   }\r
59 \r
60   private void onStartEvent(DelegateExecution execution) {\r
61   }\r
62 \r
63   private void onVthStart(DelegateExecution execution) {\r
64     // Useful for reporting back the exact parameters being sent to the VTH as they can be modified\r
65     // in the workflow\r
66   }\r
67 \r
68   private void setTestResult(DelegateExecution execution, String result){\r
69     // Get the current test execution object.\r
70     final String logPrefix = Utility.getLoggerPrefix();\r
71 \r
72     TestExecution testExecution =\r
73             utility.getExecutionVariable(\r
74                     execution.getVariables(), ExecutionConstants.ExecutionVariable.TEST_EXECUTION, TestExecution.class);\r
75     // Perform a null-check to ensure it is available. It's critical to throw an exception if it\r
76     // is not available since the object is essential for results.\r
77     if (testExecution == null) {\r
78       LOGGER.error(logPrefix + "Test execution is null.");\r
79       throw new TestExecutionException("The test execution was not found.");\r
80     }\r
81     execution.setVariable(ExecutionConstants.ExecutionVariable.TEST_RESULT, result);\r
82 \r
83     testExecution.setTestResult(result);\r
84     testExecution.setProcessInstanceId(execution.getProcessInstanceId());\r
85 \r
86 \r
87     Query query = new Query();\r
88     query.addCriteria(Criteria.where("businessKey").is(execution.getProcessBusinessKey()));\r
89     Update update = new Update();\r
90     update.set("testResult", testExecution.getTestResult());\r
91     update.set("processInstanceId", execution.getProcessInstanceId());\r
92     UpdateResult updateResult = mongoOperation.updateFirst(query, update, TestExecution.class);\r
93 \r
94   }\r
95 \r
96 \r
97 }\r