X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=otf-camunda%2Fsrc%2Fmain%2Fjava%2Forg%2Foran%2Fotf%2Fservice%2Fimpl%2FDeleteProcessInstanceServiceImpl.java;fp=otf-camunda%2Fsrc%2Fmain%2Fjava%2Forg%2Foran%2Fotf%2Fservice%2Fimpl%2FDeleteProcessInstanceServiceImpl.java;h=8b51c9094fa70f0009c02a305649517c36d2c63d;hb=14f6f95c84a4a1fa8774190db4a03fd0214ec55f;hp=0000000000000000000000000000000000000000;hpb=f49bd1efeaaddd4891c1f329b18d8cfb28b3e75b;p=it%2Fotf.git diff --git a/otf-camunda/src/main/java/org/oran/otf/service/impl/DeleteProcessInstanceServiceImpl.java b/otf-camunda/src/main/java/org/oran/otf/service/impl/DeleteProcessInstanceServiceImpl.java new file mode 100644 index 0000000..8b51c90 --- /dev/null +++ b/otf-camunda/src/main/java/org/oran/otf/service/impl/DeleteProcessInstanceServiceImpl.java @@ -0,0 +1,96 @@ +/* Copyright (c) 2019 AT&T Intellectual Property. # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); # +# you may not use this file except in compliance with the License. # +# You may obtain a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +##############################################################################*/ + + +package org.oran.otf.service.impl; + +import org.oran.otf.camunda.configuration.OtfCamundaConfiguration; +import org.oran.otf.camunda.service.ProcessEngineAwareService; +import org.oran.otf.camunda.workflow.utility.WorkflowTask; +import org.oran.otf.common.utility.gson.Convert; +import org.oran.otf.common.utility.http.ResponseUtility; +import org.oran.otf.service.DeleteProcessInstanceService; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import javax.ws.rs.core.Response; + +import org.camunda.bpm.BpmPlatform; +import org.camunda.bpm.engine.RuntimeService; +import org.camunda.bpm.engine.runtime.ProcessInstance; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + +@Service +public class DeleteProcessInstanceServiceImpl extends ProcessEngineAwareService + implements DeleteProcessInstanceService { + + private static Logger logger = LoggerFactory.getLogger(DeleteProcessInstanceServiceImpl.class); + + @Override + public Response deleteProcessInstance(String executionId) { + RuntimeService runtimeService = BpmPlatform.getProcessEngineService().getProcessEngine(OtfCamundaConfiguration.processEngineName).getRuntimeService(); + + Map response = + deleteProcessInstanceInternal( + executionId, "Deleted via TCU endpoint at " + new Date(System.currentTimeMillis())); + + try { + int code = (int) response.get("code"); + String sRes = Convert.mapToJson(response); + if (code == 404) { + return ResponseUtility.Build.notFoundWithMessage(sRes); + } else if (code == 200) { + return ResponseUtility.Build.okRequestWithMessage(sRes); + } + } catch (ClassCastException cce) { + logger.error(cce.getMessage()); + } + // Unhandled response + return ResponseUtility.Build.internalServerError(); + } + + public Map deleteProcessInstanceInternal( + String executionId, String deleteReason) { + RuntimeService runtimeService = BpmPlatform.getProcessEngineService().getProcessEngine(OtfCamundaConfiguration.processEngineName).getRuntimeService(); + + ProcessInstance pi = + runtimeService.createProcessInstanceQuery().processInstanceId(executionId).singleResult(); + + Map response = new HashMap<>(); + + if (pi == null) { + response.put( + "result", + String.format("A process instance with the executionId %s was not found.", executionId)); + response.put("code", 404); + } else { + List workflowTasks = WorkflowTask.workflowTasksByExecutionId.get(executionId); + if (workflowTasks != null) { + for (WorkflowTask workflowTask : workflowTasks) { + workflowTask.shutdown(); + } + } + + runtimeService.deleteProcessInstance(executionId, deleteReason); + response.put("result", "Successfully deleted."); + response.put("code", 200); + } + + return response; + } +}