Search in sources :

Example 16 with Complete

use of org.wso2.carbon.humantask.core.engine.commands.Complete in project carbon-apimgt by wso2.

the class APIStateChangeWorkflow method completeWorkflow.

@Override
public WorkflowResponse completeWorkflow(WorkflowExecutor workflowExecutor) throws APIManagementException {
    WorkflowResponse response = workflowExecutor.complete(this);
    setStatus(response.getWorkflowStatus());
    if (WorkflowStatus.APPROVED == response.getWorkflowStatus()) {
        if (log.isDebugEnabled()) {
            log.debug("API state change workflow complete: Approved");
        }
        String invoker = getAttribute(APIMgtConstants.WorkflowConstants.ATTRIBUTE_API_LC_INVOKER);
        String currentState = getAttribute(APIMgtConstants.WorkflowConstants.ATTRIBUTE_API_CUR_STATE);
        String targetState = getAttribute(APIMgtConstants.WorkflowConstants.ATTRIBUTE_API_TARGET_STATE);
        boolean hasOwnGateway = Boolean.valueOf(getAttribute(APIMgtConstants.WorkflowConstants.ATTRIBUTE_HAS_OWN_GATEWAY));
        String label = getAttribute(APIMgtConstants.WorkflowConstants.ATTRIBUTE_API_AUTOGEN_LABEL);
        if (hasOwnGateway) {
            // (CREATED to DEPRECATED)
            if ((currentState.equalsIgnoreCase(APIStatus.CREATED.getStatus()) || currentState.equalsIgnoreCase(APIStatus.MAINTENANCE.getStatus()) || currentState.equalsIgnoreCase(APIStatus.PROTOTYPED.getStatus())) && (targetState.equalsIgnoreCase(APIStatus.PUBLISHED.getStatus()) || targetState.equalsIgnoreCase(APIStatus.PROTOTYPED.getStatus()) || targetState.equalsIgnoreCase(APIStatus.DEPRECATED.getStatus()))) {
                try {
                    // No need to auto-generate the label again As hasOwnGateway is true.
                    // create the gateway
                    API api = apiDAO.getAPI(getWorkflowReference());
                    apiGateway.createContainerBasedGateway(label, api);
                } catch (ContainerBasedGatewayException e) {
                    // Revert already added labels
                    DedicatedGateway dedicatedGateway = new DedicatedGateway();
                    dedicatedGateway.setEnabled(false);
                    dedicatedGateway.setApiId(getWorkflowReference());
                    dedicatedGateway.setUpdatedBy(invoker);
                    List<String> labels = new ArrayList<>();
                    labels.add(labelDAO.getLabelIdByNameAndType(APIMgtConstants.DEFAULT_LABEL_NAME, APIMgtConstants.LABEL_TYPE_GATEWAY));
                    labels.add(labelDAO.getLabelIdByNameAndType(APIMgtConstants.DEFAULT_LABEL_NAME, APIMgtConstants.LABEL_TYPE_STORE));
                    apiDAO.updateDedicatedGateway(dedicatedGateway, labels);
                    throw new APIManagementException("Error while updating lifecycle state in Private Jet Mode", e, ExceptionCodes.DEDICATED_CONTAINER_GATEWAY_CREATION_FAILED);
                }
            }
        }
        String localTime = getAttribute(APIMgtConstants.WorkflowConstants.ATTRIBUTE_API_LAST_UPTIME);
        LocalDateTime time = LocalDateTime.parse(localTime);
        updateAPIStatusForWorkflowComplete(getWorkflowReference(), targetState, invoker, time);
        // After publishing the state change to the Gateway, remove the gateway for following occasions.
        if (hasOwnGateway) {
            if ((currentState.equalsIgnoreCase(APIStatus.PUBLISHED.getStatus()) || currentState.equalsIgnoreCase(APIStatus.PROTOTYPED.getStatus()) || currentState.equalsIgnoreCase(APIStatus.DEPRECATED.getStatus())) && (targetState.equalsIgnoreCase(APIStatus.CREATED.getStatus()) || targetState.equalsIgnoreCase(APIStatus.MAINTENANCE.getStatus()) || targetState.equalsIgnoreCase(APIStatus.PROTOTYPED.getStatus())) || targetState.equalsIgnoreCase(APIStatus.RETIRED.getStatus())) {
                // remove gateway
                API api = apiDAO.getAPI(getWorkflowReference());
                apiGateway.removeContainerBasedGateway(label, api);
            }
        }
    } else if (WorkflowStatus.REJECTED == response.getWorkflowStatus()) {
        if (log.isDebugEnabled()) {
            log.debug("API state change workflow complete: Rejected");
        }
        apiDAO.updateAPIWorkflowStatus(getWorkflowReference(), APIMgtConstants.APILCWorkflowStatus.REJECTED);
    }
    updateWorkflowEntries(this);
    return response;
}
Also used : LocalDateTime(java.time.LocalDateTime) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) ContainerBasedGatewayException(org.wso2.carbon.apimgt.core.exception.ContainerBasedGatewayException) API(org.wso2.carbon.apimgt.core.models.API) ArrayList(java.util.ArrayList) List(java.util.List) DedicatedGateway(org.wso2.carbon.apimgt.core.models.DedicatedGateway)

Example 17 with Complete

use of org.wso2.carbon.humantask.core.engine.commands.Complete 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 18 with Complete

use of org.wso2.carbon.humantask.core.engine.commands.Complete in project carbon-apimgt by wso2.

the class DefaultWorkflowExecutor method complete.

/**
 * Complete the external process status
 * Based on the workflow status we will update the status column of the
 * Application table
 *
 * @param workFlow - Workflow
 */
public WorkflowResponse complete(Workflow workFlow) throws WorkflowException {
    if (log.isDebugEnabled()) {
        log.debug("Executing complete() in Workflow for " + workFlow.getWorkflowType());
    }
    WorkflowResponse workflowResponse = new GeneralWorkflowResponse();
    workflowResponse.setWorkflowStatus(workFlow.getStatus());
    return workflowResponse;
}
Also used : WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse)

Example 19 with Complete

use of org.wso2.carbon.humantask.core.engine.commands.Complete 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 20 with Complete

use of org.wso2.carbon.humantask.core.engine.commands.Complete 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)

Aggregations

WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)9 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)3 List (java.util.List)3 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)3 GatewayException (org.wso2.carbon.apimgt.core.exception.GatewayException)3 RepositoryService (org.activiti.engine.RepositoryService)2 Element (org.w3c.dom.Element)2 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)2 ContainerBasedGatewayException (org.wso2.carbon.apimgt.core.exception.ContainerBasedGatewayException)2 Application (org.wso2.carbon.apimgt.core.models.Application)2 HumanTaskException (org.wso2.carbon.humantask.core.engine.HumanTaskException)2 Complete (org.wso2.carbon.humantask.core.engine.commands.Complete)2 HazelcastInstance (com.hazelcast.core.HazelcastInstance)1 File (java.io.File)1 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 Paths (java.nio.file.Paths)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1