Search in sources :

Example 26 with WorkflowExecutor

use of org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor in project carbon-apimgt by wso2.

the class ApplicationCreationWorkflow method completeWorkflow.

public WorkflowResponse completeWorkflow(WorkflowExecutor workflowExecutor) throws APIManagementException {
    if (application == null) {
        // this is when complete method is executed through workflow rest api
        application = applicationDAO.getApplication(getWorkflowReference());
    }
    WorkflowResponse response = workflowExecutor.complete(this);
    // setting the workflow status from the one getting from the executor. this gives the executor developer
    // to change the state as well.
    setStatus(response.getWorkflowStatus());
    String applicationState = "";
    if (WorkflowStatus.APPROVED == response.getWorkflowStatus()) {
        if (log.isDebugEnabled()) {
            log.debug("Application Creation workflow complete: Approved");
        }
        getApiGateway().addApplication(application);
        applicationState = APIMgtConstants.ApplicationStatus.APPLICATION_APPROVED;
    } else if (WorkflowStatus.REJECTED == response.getWorkflowStatus()) {
        if (log.isDebugEnabled()) {
            log.debug("Application Creation workflow complete: Rejected");
        }
        applicationState = APIMgtConstants.ApplicationStatus.APPLICATION_REJECTED;
    }
    applicationDAO.updateApplicationState(getWorkflowReference(), applicationState);
    updateWorkflowEntries(this);
    return response;
}
Also used : WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse)

Example 27 with WorkflowExecutor

use of org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor in project carbon-apimgt by wso2.

the class SubscriptionDeletionWorkflow method completeWorkflow.

public WorkflowResponse completeWorkflow(WorkflowExecutor workflowExecutor) throws APIManagementException {
    if (subscription == null) {
        // this is when complete method is executed through workflow rest api
        this.subscription = apiSubscriptionDAO.getAPISubscription(getWorkflowReference());
    }
    WorkflowResponse response = workflowExecutor.complete(this);
    setStatus(response.getWorkflowStatus());
    List<SubscriptionValidationData> subscriptionValidationDataList = null;
    if (WorkflowStatus.APPROVED == response.getWorkflowStatus()) {
        if (log.isDebugEnabled()) {
            log.debug("Subscription deletion workflow complete: Approved");
        }
        if (subscription.getApi() != null && subscription.getApplication() != null) {
            subscriptionValidationDataList = apiSubscriptionDAO.getAPISubscriptionsOfAPIForValidation(subscription.getApi().getContext(), subscription.getApi().getVersion(), subscription.getApplication().getId());
        }
        apiSubscriptionDAO.deleteAPISubscription(getWorkflowReference());
    } else if (WorkflowStatus.REJECTED == response.getWorkflowStatus()) {
        if (log.isDebugEnabled()) {
            log.debug("Subscription deletion workflow complete: Rejected");
        }
    }
    updateWorkflowEntries(this);
    if (WorkflowStatus.APPROVED == response.getWorkflowStatus()) {
        if (subscriptionValidationDataList != null && !subscriptionValidationDataList.isEmpty()) {
            apiGateway.deleteAPISubscription(subscriptionValidationDataList);
            if (log.isDebugEnabled()) {
                log.debug("Subscription for API : " + subscription.getApi().getName() + " with " + "application : " + subscription.getApplication().getName() + " has been successfully " + "removed from gateway");
            }
        }
    }
    return response;
}
Also used : WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) SubscriptionValidationData(org.wso2.carbon.apimgt.core.models.SubscriptionValidationData)

Example 28 with WorkflowExecutor

use of org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor in project carbon-apimgt by wso2.

the class WorkflowConfigHolder method loadWorkflowConfigurations.

private void loadWorkflowConfigurations(WorkflowExecutorInfo workflowConfig, String workflowExecutorType) throws ClassNotFoundException, IllegalAccessException, InstantiationException, WorkflowException {
    String executorClass = workflowConfig.getExecutor();
    Class clazz = WorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
    WorkflowExecutor workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
    List<WorkflowConfigProperties> properties = workflowConfig.getProperty();
    if (properties != null) {
        loadProperties(properties, workFlowExecutor);
    }
    workflowExecutorMap.put(workflowExecutorType, workFlowExecutor);
}
Also used : WorkflowExecutor(org.wso2.carbon.apimgt.core.api.WorkflowExecutor) WorkflowConfigProperties(org.wso2.carbon.apimgt.core.models.WorkflowConfigProperties)

Example 29 with WorkflowExecutor

use of org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor in project carbon-apimgt by wso2.

the class WorkflowsApiServiceImpl method workflowsWorkflowReferenceIdPut.

@Override
public Response workflowsWorkflowReferenceIdPut(String workflowReferenceId, WorkflowRequestDTO body, Request request) throws NotFoundException {
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        Workflow workflow = apiMgtAdminService.retrieveWorkflow(workflowReferenceId);
        if (workflow == null) {
            String errorMessage = "Workflow entry not found for: " + workflowReferenceId;
            APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.WORKFLOW_NOT_FOUND);
            Map<String, String> paramList = new HashMap<>();
            paramList.put(APIMgtConstants.ExceptionsConstants.WORKFLOW_REF_ID, workflowReferenceId);
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
            log.error(errorMessage, e);
            return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
        } else {
            if (WorkflowStatus.APPROVED == workflow.getStatus()) {
                String errorMessage = "Workflow is already in complete state";
                APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.WORKFLOW_ALREADY_COMPLETED);
                Map<String, String> paramList = new HashMap<>();
                paramList.put(APIMgtConstants.ExceptionsConstants.WORKFLOW_REF_ID, workflowReferenceId);
                ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
                log.error(errorMessage, e);
                return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
            } else {
                WorkflowExecutor workflowExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(workflow.getWorkflowType());
                if (body == null) {
                    RestApiUtil.handleBadRequest("Request payload is missing", log);
                }
                if (body.getDescription() != null) {
                    workflow.setWorkflowDescription(body.getDescription());
                }
                if (body.getStatus() == null) {
                    String errorMessage = "Workflow status is not defined";
                    APIManagementException e = new APIManagementException(errorMessage, ExceptionCodes.WORKFLOW_STATE_MISSING);
                    Map<String, String> paramList = new HashMap<>();
                    paramList.put(APIMgtConstants.ExceptionsConstants.WORKFLOW_REF_ID, workflowReferenceId);
                    ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
                    log.error(errorMessage, e);
                    return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
                } else {
                    workflow.setStatus(WorkflowStatus.valueOf(body.getStatus().toString()));
                }
                if (body.getAttributes() != null) {
                    Map<String, String> existingAttributs = workflow.getAttributes();
                    Map<String, String> newAttributes = body.getAttributes();
                    if (existingAttributs == null) {
                        workflow.setAttributes(newAttributes);
                    } else {
                        newAttributes.forEach(existingAttributs::putIfAbsent);
                        workflow.setAttributes(existingAttributs);
                    }
                }
                WorkflowResponse response = apiMgtAdminService.completeWorkflow(workflowExecutor, workflow);
                WorkflowResponseDTO workflowResponseDTO = WorkflowMappingUtil.toWorkflowResponseDTO(response);
                return Response.ok().entity(workflowResponseDTO).build();
            }
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while completing workflow for reference : " + workflowReferenceId + ". " + e.getMessage();
        Map<String, String> paramList = new HashMap<>();
        paramList.put(APIMgtConstants.ExceptionsConstants.WORKFLOW_REF_ID, workflowReferenceId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) Workflow(org.wso2.carbon.apimgt.core.workflow.Workflow) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException) APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) WorkflowExecutor(org.wso2.carbon.apimgt.core.api.WorkflowExecutor) HashMap(java.util.HashMap) Map(java.util.Map)

Example 30 with WorkflowExecutor

use of org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor in project carbon-apimgt by wso2.

the class APIPublisherImpl method cleanupPendingTaskForAPIStateChange.

private void cleanupPendingTaskForAPIStateChange(String apiId) throws APIManagementException {
    Optional<String> workflowExtRef = getWorkflowDAO().getExternalWorkflowReferenceForPendingTask(apiId, WorkflowConstants.WF_TYPE_AM_API_STATE);
    if (workflowExtRef.isPresent()) {
        WorkflowExecutor executor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_API_STATE);
        try {
            executor.cleanUpPendingTask(workflowExtRef.get());
        } catch (WorkflowException e) {
            String warn = "Failed to clean pending api state change task for " + apiId;
            // failed cleanup processes are ignored to prevent failing the deletion process
            log.warn(warn, e.getLocalizedMessage());
        }
        getWorkflowDAO().deleteWorkflowEntryforExternalReference(workflowExtRef.get());
    }
}
Also used : WorkflowException(org.wso2.carbon.apimgt.core.exception.WorkflowException) WorkflowExecutor(org.wso2.carbon.apimgt.core.api.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