use of org.wso2.carbon.apimgt.core.api.WorkflowExecutor in project carbon-apimgt by wso2.
the class WorkflowsApiServiceImpl method workflowsUpdateWorkflowStatusPost.
/**
* This is used to update the workflow status
*
* @param workflowReferenceId workflow reference id that is unique to each workflow
* @param body body should contain the status, optionally can contain a
* description and an attributes object
* @return
*/
@Override
public Response workflowsUpdateWorkflowStatusPost(String workflowReferenceId, WorkflowDTO body, MessageContext messageContext) {
ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
boolean isTenantFlowStarted = false;
String username = RestApiCommonUtil.getLoggedInUsername();
String tenantDomainOfUser = MultitenantUtils.getTenantDomain(username);
try {
if (workflowReferenceId == null) {
RestApiUtil.handleBadRequest("workflowReferenceId is empty", log);
}
org.wso2.carbon.apimgt.impl.dto.WorkflowDTO workflowDTO = apiMgtDAO.retrieveWorkflow(workflowReferenceId);
if (workflowDTO == null) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_WORKFLOW, workflowReferenceId, log);
}
String tenantDomain = workflowDTO.getTenantDomain();
if (tenantDomain != null && !tenantDomain.equals(tenantDomainOfUser)) {
return Response.status(Response.Status.UNAUTHORIZED).build();
}
if (tenantDomain != null && !SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
isTenantFlowStarted = true;
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
}
if (body == null) {
RestApiUtil.handleBadRequest("Request payload is missing", log);
}
if (body.getDescription() != null) {
workflowDTO.setWorkflowDescription(body.getDescription());
}
if (body.getStatus() == null) {
RestApiUtil.handleBadRequest("Workflow status is not defined", log);
} else {
workflowDTO.setStatus(WorkflowStatus.valueOf(body.getStatus().toString()));
}
if (body.getAttributes() != null) {
workflowDTO.setAttributes(body.getAttributes());
}
String workflowType = workflowDTO.getWorkflowType();
WorkflowExecutor workflowExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(workflowType);
workflowExecutor.complete(workflowDTO);
if (WorkflowStatus.APPROVED.equals(workflowDTO.getStatus())) {
WorkflowUtils.sendNotificationAfterWFComplete(workflowDTO, workflowType);
}
return Response.ok().entity(body).build();
} catch (APIManagementException e) {
String msg = "Error while resuming workflow " + workflowReferenceId;
RestApiUtil.handleInternalServerError(msg, e, log);
} catch (WorkflowException e) {
String msg = "Error while resuming workflow " + workflowReferenceId;
RestApiUtil.handleInternalServerError(msg, e, log);
} finally {
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
return null;
}
Aggregations