Search in sources :

Example 1 with WorkflowRequestAssociationDAO

use of org.wso2.carbon.identity.workflow.mgt.dao.WorkflowRequestAssociationDAO in project carbon-identity-framework by wso2.

the class WorkFlowExecutorManager method handleCallback.

/**
 * Called when callback received for a pending operation.
 *
 * @param uuid             Unique ID of request
 * @param status           Status of approval/disapproval
 * @param additionalParams Additional parameters required to execute operation.
 * @throws WorkflowException
 */
public void handleCallback(String uuid, String status, Map<String, Object> additionalParams) throws WorkflowException {
    List<WorkflowExecutorManagerListener> workflowListenerList = WorkflowServiceDataHolder.getInstance().getExecutorListenerList();
    for (WorkflowExecutorManagerListener workflowListener : workflowListenerList) {
        if (workflowListener.isEnable()) {
            workflowListener.doPreHandleCallback(uuid, status, additionalParams);
        }
    }
    WorkflowRequestAssociationDAO workflowRequestAssociationDAO = new WorkflowRequestAssociationDAO();
    String requestId = workflowRequestAssociationDAO.getRequestIdOfRelationship(uuid);
    WorkflowRequestDAO requestDAO = new WorkflowRequestDAO();
    WorkflowRequest request = requestDAO.retrieveWorkflow(requestId);
    handleCallback(request, status, additionalParams, uuid);
    for (WorkflowExecutorManagerListener workflowListener : workflowListenerList) {
        if (workflowListener.isEnable()) {
            workflowListener.doPostHandleCallback(uuid, status, additionalParams);
        }
    }
}
Also used : WorkflowRequestDAO(org.wso2.carbon.identity.workflow.mgt.dao.WorkflowRequestDAO) WorkflowRequestAssociationDAO(org.wso2.carbon.identity.workflow.mgt.dao.WorkflowRequestAssociationDAO) WorkflowExecutorManagerListener(org.wso2.carbon.identity.workflow.mgt.listener.WorkflowExecutorManagerListener) WorkflowRequest(org.wso2.carbon.identity.workflow.mgt.dto.WorkflowRequest)

Example 2 with WorkflowRequestAssociationDAO

use of org.wso2.carbon.identity.workflow.mgt.dao.WorkflowRequestAssociationDAO in project carbon-identity-framework by wso2.

the class WorkFlowExecutorManager method executeWorkflow.

/**
 * Called when initiate a request that can be engaged with a workflow. Here it determine if operation has engaged
 * with a workflow or not. If workflows engaged this will deploy communicate with relevant workflow engine and
 * return false which will stop continuation of operation. Otherwise this will return true.
 *
 * @param workFlowRequest Workflow request object with request attributes.
 * @return
 * @throws WorkflowException
 */
public WorkflowExecutorResult executeWorkflow(WorkflowRequest workFlowRequest) throws WorkflowException {
    WorkflowRequestAssociationDAO workflowRequestAssociationDAO = new WorkflowRequestAssociationDAO();
    List<WorkflowExecutorManagerListener> workflowListenerList = WorkflowServiceDataHolder.getInstance().getExecutorListenerList();
    for (WorkflowExecutorManagerListener workflowListener : workflowListenerList) {
        if (workflowListener.isEnable()) {
            workflowListener.doPreExecuteWorkflow(workFlowRequest);
        }
    }
    if (StringUtils.isBlank(workFlowRequest.getUuid())) {
        workFlowRequest.setUuid(UUID.randomUUID().toString());
    }
    OMElement xmlRequest = WorkflowRequestBuilder.buildXMLRequest(workFlowRequest);
    WorkflowRequestAssociationDAO requestAssociationDAO = new WorkflowRequestAssociationDAO();
    WorkflowDAO workflowDAO = new WorkflowDAO();
    List<WorkflowAssociation> associations = requestAssociationDAO.getWorkflowAssociationsForRequest(workFlowRequest.getEventType(), workFlowRequest.getTenantId());
    if (CollectionUtils.isEmpty(associations)) {
        return new WorkflowExecutorResult(ExecutorResultState.NO_ASSOCIATION);
    }
    boolean workflowEngaged = false;
    boolean requestSaved = false;
    for (WorkflowAssociation association : associations) {
        try {
            AXIOMXPath axiomxPath = new AXIOMXPath(association.getAssociationCondition());
            if (axiomxPath.booleanValueOf(xmlRequest)) {
                workflowEngaged = true;
                if (!requestSaved) {
                    WorkflowRequestDAO requestDAO = new WorkflowRequestDAO();
                    int tenant = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
                    String currentUser = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
                    requestDAO.addWorkflowEntry(workFlowRequest, currentUser, tenant);
                    requestSaved = true;
                }
                String relationshipId = UUID.randomUUID().toString();
                WorkflowRequest requestToSend = workFlowRequest.clone();
                requestToSend.setUuid(relationshipId);
                Workflow workflow = workflowDAO.getWorkflow(association.getWorkflowId());
                AbstractWorkflow templateImplementation = WorkflowServiceDataHolder.getInstance().getWorkflowImpls().get(workflow.getTemplateId()).get(workflow.getWorkflowImplId());
                List<Parameter> parameterList = workflowDAO.getWorkflowParams(association.getWorkflowId());
                templateImplementation.execute(requestToSend, parameterList);
                workflowRequestAssociationDAO.addNewRelationship(relationshipId, association.getWorkflowId(), workFlowRequest.getUuid(), WorkflowRequestStatus.PENDING.toString(), workFlowRequest.getTenantId());
            }
        } catch (JaxenException e) {
            String errorMsg = "Error when executing the xpath expression:" + association.getAssociationCondition() + " , on " + xmlRequest;
            log.error(errorMsg, e);
            return new WorkflowExecutorResult(ExecutorResultState.FAILED, errorMsg);
        } catch (CloneNotSupportedException e) {
            String errorMsg = "Error while cloning workflowRequest object at executor manager.";
            log.error(errorMsg, e);
            return new WorkflowExecutorResult(ExecutorResultState.FAILED, errorMsg);
        }
    }
    if (!workflowEngaged) {
        // handleCallback(workFlowRequest, WorkflowRequestStatus.SKIPPED.toString(), null, "");
        return new WorkflowExecutorResult(ExecutorResultState.CONDITION_FAILED);
    }
    WorkflowExecutorResult finalResult = new WorkflowExecutorResult(ExecutorResultState.STARTED_ASSOCIATION);
    for (WorkflowExecutorManagerListener workflowListener : workflowListenerList) {
        if (workflowListener.isEnable()) {
            workflowListener.doPostExecuteWorkflow(workFlowRequest, finalResult);
        }
    }
    return finalResult;
}
Also used : WorkflowRequestDAO(org.wso2.carbon.identity.workflow.mgt.dao.WorkflowRequestDAO) AbstractWorkflow(org.wso2.carbon.identity.workflow.mgt.workflow.AbstractWorkflow) Workflow(org.wso2.carbon.identity.workflow.mgt.bean.Workflow) OMElement(org.apache.axiom.om.OMElement) AbstractWorkflow(org.wso2.carbon.identity.workflow.mgt.workflow.AbstractWorkflow) WorkflowDAO(org.wso2.carbon.identity.workflow.mgt.dao.WorkflowDAO) WorkflowRequestAssociationDAO(org.wso2.carbon.identity.workflow.mgt.dao.WorkflowRequestAssociationDAO) JaxenException(org.jaxen.JaxenException) WorkflowExecutorManagerListener(org.wso2.carbon.identity.workflow.mgt.listener.WorkflowExecutorManagerListener) Parameter(org.wso2.carbon.identity.workflow.mgt.bean.Parameter) WorkflowAssociation(org.wso2.carbon.identity.workflow.mgt.bean.WorkflowAssociation) AXIOMXPath(org.apache.axiom.om.xpath.AXIOMXPath) WorkflowRequest(org.wso2.carbon.identity.workflow.mgt.dto.WorkflowRequest)

Example 3 with WorkflowRequestAssociationDAO

use of org.wso2.carbon.identity.workflow.mgt.dao.WorkflowRequestAssociationDAO in project carbon-identity-framework by wso2.

the class WorkFlowExecutorManager method handleCallback.

private void handleCallback(WorkflowRequest request, String status, Map<String, Object> additionalParams, String requestWorkflowId) throws WorkflowException {
    WorkflowRequestAssociationDAO workflowRequestAssociationDAO = new WorkflowRequestAssociationDAO();
    if (request != null) {
        WorkflowRequestDAO workflowRequestDAO = new WorkflowRequestDAO();
        String requestId = request.getUuid();
        workflowRequestAssociationDAO.updateStatusOfRelationship(requestWorkflowId, status);
        workflowRequestDAO.updateLastUpdatedTimeOfRequest(requestId);
        if (StringUtils.isNotBlank(requestWorkflowId) && WorkflowRequestStatus.DELETED.toString().equals(workflowRequestDAO.retrieveStatusOfWorkflow(request.getUuid()))) {
            log.info("Callback received for request " + requestId + " which is already deleted by user. ");
            return;
        }
        if (status.equals(WorkflowRequestStatus.APPROVED.toString()) && !isAllWorkflowsCompleted(workflowRequestAssociationDAO, requestId)) {
            return;
        }
        String eventId = request.getEventType();
        WorkflowRequestHandler requestHandler = WorkflowServiceDataHolder.getInstance().getRequestHandler(eventId);
        if (requestHandler == null) {
            throw new InternalWorkflowException("No request handlers registered for the id: " + eventId);
        }
        if (request.getTenantId() == MultitenantConstants.INVALID_TENANT_ID) {
            throw new InternalWorkflowException("Invalid tenant id for request " + eventId + " with id" + requestId);
        }
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
        try {
            String tenantDomain = WorkflowServiceDataHolder.getInstance().getRealmService().getTenantManager().getDomain(request.getTenantId());
            carbonContext.setTenantId(request.getTenantId());
            carbonContext.setTenantDomain(tenantDomain);
            requestHandler.onWorkflowCompletion(status, request, additionalParams);
            updateDBAtWorkflowCompletion(requestId, status);
        } catch (WorkflowException e) {
            updateDBAtWorkflowCompletion(requestId, WorkflowRequestStatus.FAILED.toString());
            throw e;
        } catch (UserStoreException e) {
            updateDBAtWorkflowCompletion(requestId, WorkflowRequestStatus.FAILED.toString());
            throw new InternalWorkflowException("Error when getting tenant domain for tenant id " + request.getTenantId());
        } finally {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
}
Also used : WorkflowRequestDAO(org.wso2.carbon.identity.workflow.mgt.dao.WorkflowRequestDAO) InternalWorkflowException(org.wso2.carbon.identity.workflow.mgt.exception.InternalWorkflowException) WorkflowRequestAssociationDAO(org.wso2.carbon.identity.workflow.mgt.dao.WorkflowRequestAssociationDAO) InternalWorkflowException(org.wso2.carbon.identity.workflow.mgt.exception.InternalWorkflowException) WorkflowException(org.wso2.carbon.identity.workflow.mgt.exception.WorkflowException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) WorkflowRequestHandler(org.wso2.carbon.identity.workflow.mgt.extension.WorkflowRequestHandler)

Aggregations

WorkflowRequestAssociationDAO (org.wso2.carbon.identity.workflow.mgt.dao.WorkflowRequestAssociationDAO)3 WorkflowRequestDAO (org.wso2.carbon.identity.workflow.mgt.dao.WorkflowRequestDAO)3 WorkflowRequest (org.wso2.carbon.identity.workflow.mgt.dto.WorkflowRequest)2 WorkflowExecutorManagerListener (org.wso2.carbon.identity.workflow.mgt.listener.WorkflowExecutorManagerListener)2 OMElement (org.apache.axiom.om.OMElement)1 AXIOMXPath (org.apache.axiom.om.xpath.AXIOMXPath)1 JaxenException (org.jaxen.JaxenException)1 PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)1 Parameter (org.wso2.carbon.identity.workflow.mgt.bean.Parameter)1 Workflow (org.wso2.carbon.identity.workflow.mgt.bean.Workflow)1 WorkflowAssociation (org.wso2.carbon.identity.workflow.mgt.bean.WorkflowAssociation)1 WorkflowDAO (org.wso2.carbon.identity.workflow.mgt.dao.WorkflowDAO)1 InternalWorkflowException (org.wso2.carbon.identity.workflow.mgt.exception.InternalWorkflowException)1 WorkflowException (org.wso2.carbon.identity.workflow.mgt.exception.WorkflowException)1 WorkflowRequestHandler (org.wso2.carbon.identity.workflow.mgt.extension.WorkflowRequestHandler)1 AbstractWorkflow (org.wso2.carbon.identity.workflow.mgt.workflow.AbstractWorkflow)1 UserStoreException (org.wso2.carbon.user.api.UserStoreException)1