Search in sources :

Example 41 with WorkflowExecutor

use of org.wso2.carbon.apimgt.impl.workflow.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;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) WorkflowException(org.wso2.carbon.apimgt.impl.workflow.WorkflowException) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) WorkflowExecutor(org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor)

Aggregations

WorkflowExecutor (org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor)17 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)15 WorkflowExecutor (org.wso2.carbon.apimgt.core.api.WorkflowExecutor)13 WorkflowException (org.wso2.carbon.apimgt.impl.workflow.WorkflowException)13 WorkflowDTO (org.wso2.carbon.apimgt.impl.dto.WorkflowDTO)12 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)10 JSONObject (org.json.simple.JSONObject)9 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)8 SubscriptionWorkflowDTO (org.wso2.carbon.apimgt.impl.dto.SubscriptionWorkflowDTO)7 HashMap (java.util.HashMap)6 ApplicationRegistrationWorkflowDTO (org.wso2.carbon.apimgt.impl.dto.ApplicationRegistrationWorkflowDTO)6 ApplicationWorkflowDTO (org.wso2.carbon.apimgt.impl.dto.ApplicationWorkflowDTO)6 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)5 WorkflowProperties (org.wso2.carbon.apimgt.impl.dto.WorkflowProperties)5 ParseException (org.json.simple.parser.ParseException)4 WorkflowResponse (org.wso2.carbon.apimgt.api.WorkflowResponse)4 Application (org.wso2.carbon.apimgt.core.models.Application)4 Iterator (java.util.Iterator)3 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3