added svcapi ui and camunda code
[it/otf.git] / otf-camunda / src / main / java / org / oran / otf / camunda / listener / StartEventListener.java
diff --git a/otf-camunda/src/main/java/org/oran/otf/camunda/listener/StartEventListener.java b/otf-camunda/src/main/java/org/oran/otf/camunda/listener/StartEventListener.java
new file mode 100644 (file)
index 0000000..9fa6d14
--- /dev/null
@@ -0,0 +1,97 @@
+/*  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.camunda.listener;\r
+\r
+import org.oran.otf.camunda.exception.TestExecutionException;\r
+import org.oran.otf.camunda.model.ExecutionConstants;\r
+import org.oran.otf.camunda.workflow.utility.WorkflowUtility;\r
+import org.oran.otf.common.model.TestExecution;\r
+import org.oran.otf.common.utility.Utility;\r
+import com.google.gson.JsonObject;\r
+import com.mongodb.client.result.UpdateResult;\r
+import org.camunda.bpm.engine.delegate.DelegateExecution;\r
+import org.camunda.bpm.engine.delegate.ExecutionListener;\r
+import org.camunda.bpm.extension.reactor.bus.CamundaSelector;\r
+import org.camunda.bpm.extension.reactor.spring.listener.ReactorExecutionListener;\r
+import org.camunda.bpm.model.bpmn.instance.StartEvent;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+import org.springframework.beans.factory.annotation.Autowired;\r
+import org.springframework.data.mongodb.core.MongoTemplate;\r
+import org.springframework.data.mongodb.core.query.Criteria;\r
+import org.springframework.data.mongodb.core.query.Query;\r
+import org.springframework.data.mongodb.core.query.Update;\r
+import org.springframework.stereotype.Component;\r
+\r
+@Component\r
+@CamundaSelector(event = ExecutionListener.EVENTNAME_END)\r
+public class StartEventListener extends ReactorExecutionListener {\r
+\r
+  @Autowired\r
+  WorkflowUtility utility;\r
+\r
+  @Autowired\r
+  MongoTemplate mongoOperation;\r
+\r
+  private static Logger LOGGER = LoggerFactory.getLogger(StartEventListener.class);\r
+\r
+  @Override\r
+  public void notify(DelegateExecution execution) {\r
+    if (execution.getBpmnModelElementInstance() instanceof StartEvent) {\r
+      LOGGER.info(execution.getProcessInstanceId() + " has started.");\r
+      //setTestResult(execution, ExecutionConstants.TestResult.STARTED);\r
+    }\r
+  }\r
+\r
+  private void onStartEvent(DelegateExecution execution) {\r
+  }\r
+\r
+  private void onVthStart(DelegateExecution execution) {\r
+    // Useful for reporting back the exact parameters being sent to the VTH as they can be modified\r
+    // in the workflow\r
+  }\r
+\r
+  private void setTestResult(DelegateExecution execution, String result){\r
+    // Get the current test execution object.\r
+    final String logPrefix = Utility.getLoggerPrefix();\r
+\r
+    TestExecution testExecution =\r
+            utility.getExecutionVariable(\r
+                    execution.getVariables(), ExecutionConstants.ExecutionVariable.TEST_EXECUTION, TestExecution.class);\r
+    // Perform a null-check to ensure it is available. It's critical to throw an exception if it\r
+    // is not available since the object is essential for results.\r
+    if (testExecution == null) {\r
+      LOGGER.error(logPrefix + "Test execution is null.");\r
+      throw new TestExecutionException("The test execution was not found.");\r
+    }\r
+    execution.setVariable(ExecutionConstants.ExecutionVariable.TEST_RESULT, result);\r
+\r
+    testExecution.setTestResult(result);\r
+    testExecution.setProcessInstanceId(execution.getProcessInstanceId());\r
+\r
+\r
+    Query query = new Query();\r
+    query.addCriteria(Criteria.where("businessKey").is(execution.getProcessBusinessKey()));\r
+    Update update = new Update();\r
+    update.set("testResult", testExecution.getTestResult());\r
+    update.set("processInstanceId", execution.getProcessInstanceId());\r
+    UpdateResult updateResult = mongoOperation.updateFirst(query, update, TestExecution.class);\r
+\r
+  }\r
+\r
+\r
+}\r