Search in sources :

Example 6 with WorkflowRequest

use of org.wso2.carbon.identity.workflow.mgt.dto.WorkflowRequest 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 7 with WorkflowRequest

use of org.wso2.carbon.identity.workflow.mgt.dto.WorkflowRequest in project carbon-identity-framework by wso2.

the class AbstractWorkflowRequestHandler method onWorkflowCompletion.

@Override
public void onWorkflowCompletion(String status, WorkflowRequest originalRequest, Map<String, Object> responseParams) throws WorkflowException {
    try {
        Map<String, Object> requestParams = new HashMap<String, Object>();
        for (RequestParameter parameter : originalRequest.getRequestParameters()) {
            requestParams.put(parameter.getName(), parameter.getValue());
        }
        if (retryNeedAtCallback()) {
            setWorkFlowCompleted(true);
        }
        onWorkflowCompletion(status, requestParams, responseParams, originalRequest.getTenantId());
    } finally {
        unsetWorkFlowCompleted();
    }
}
Also used : HashMap(java.util.HashMap) RequestParameter(org.wso2.carbon.identity.workflow.mgt.bean.RequestParameter)

Example 8 with WorkflowRequest

use of org.wso2.carbon.identity.workflow.mgt.dto.WorkflowRequest in project carbon-identity-framework by wso2.

the class WorkflowManagementServiceImpl method getRequestsCreatedByUser.

/**
 * Returns array of requests initiated by a user.
 *
 * @param user     User to get requests of, empty String to retrieve requests of all users
 * @param tenantId tenant id of currently logged in user
 * @return
 * @throws WorkflowException
 */
@Override
public WorkflowRequest[] getRequestsCreatedByUser(String user, int tenantId) throws WorkflowException {
    List<WorkflowListener> workflowListenerList = WorkflowServiceDataHolder.getInstance().getWorkflowListenerList();
    for (WorkflowListener workflowListener : workflowListenerList) {
        if (workflowListener.isEnable()) {
            workflowListener.doPreGetRequestsCreatedByUser(user, tenantId);
        }
    }
    WorkflowRequest[] requests = workflowRequestDAO.getRequestsOfUser(user, tenantId);
    for (WorkflowListener workflowListener : workflowListenerList) {
        if (workflowListener.isEnable()) {
            workflowListener.doPostGetRequestsCreatedByUser(user, tenantId, requests);
        }
    }
    return requests;
}
Also used : WorkflowListener(org.wso2.carbon.identity.workflow.mgt.listener.WorkflowListener) WorkflowRequest(org.wso2.carbon.identity.workflow.mgt.bean.WorkflowRequest)

Example 9 with WorkflowRequest

use of org.wso2.carbon.identity.workflow.mgt.dto.WorkflowRequest in project carbon-identity-framework by wso2.

the class WorkflowManagementServiceImpl method deleteWorkflowRequest.

@Override
public void deleteWorkflowRequest(String requestId) throws WorkflowException {
    List<WorkflowListener> workflowListenerList = WorkflowServiceDataHolder.getInstance().getWorkflowListenerList();
    String loggedUser = CarbonContext.getThreadLocalCarbonContext().getUsername();
    String createdUser = workflowRequestDAO.retrieveCreatedUserOfRequest(requestId);
    if (!loggedUser.equals(createdUser)) {
        throw new WorkflowException("User not authorized to delete this request");
    }
    WorkflowRequest workflowRequest = new WorkflowRequest();
    workflowRequest.setRequestId(requestId);
    workflowRequest.setCreatedBy(createdUser);
    for (WorkflowListener workflowListener : workflowListenerList) {
        if (workflowListener.isEnable()) {
            workflowListener.doPreDeleteWorkflowRequest(workflowRequest);
        }
    }
    workflowRequestDAO.updateStatusOfRequest(requestId, WorkflowRequestStatus.DELETED.toString());
    workflowRequestAssociationDAO.updateStatusOfRelationshipsOfPendingRequest(requestId, WFConstant.HT_STATE_SKIPPED);
    requestEntityRelationshipDAO.deleteRelationshipsOfRequest(requestId);
    for (WorkflowListener workflowListener : workflowListenerList) {
        if (workflowListener.isEnable()) {
            workflowListener.doPostDeleteWorkflowRequest(workflowRequest);
        }
    }
}
Also used : InternalWorkflowException(org.wso2.carbon.identity.workflow.mgt.exception.InternalWorkflowException) WorkflowException(org.wso2.carbon.identity.workflow.mgt.exception.WorkflowException) WorkflowListener(org.wso2.carbon.identity.workflow.mgt.listener.WorkflowListener) WorkflowRequest(org.wso2.carbon.identity.workflow.mgt.bean.WorkflowRequest)

Example 10 with WorkflowRequest

use of org.wso2.carbon.identity.workflow.mgt.dto.WorkflowRequest in project carbon-identity-framework by wso2.

the class WorkflowRequestDAO method addWorkflowEntry.

/**
 * Persists WorkflowRequest to be used when workflow is completed
 *
 * @param workflow    The workflow object to be persisted
 * @param currentUser Currently logged in user
 * @param tenantId    Tenant ID of the currently Logged user.
 * @throws WorkflowException
 */
public void addWorkflowEntry(WorkflowRequest workflow, String currentUser, int tenantId) throws WorkflowException {
    Connection connection = IdentityDatabaseUtil.getDBConnection();
    PreparedStatement prepStmt = null;
    String query = SQLConstants.ADD_WORKFLOW_REQUEST_QUERY;
    try {
        Timestamp createdDateStamp = new Timestamp(System.currentTimeMillis());
        prepStmt = connection.prepareStatement(query);
        prepStmt.setString(1, workflow.getUuid());
        prepStmt.setString(2, currentUser);
        prepStmt.setString(3, workflow.getEventType());
        prepStmt.setTimestamp(4, createdDateStamp);
        prepStmt.setTimestamp(5, createdDateStamp);
        prepStmt.setBytes(6, serializeWorkflowRequest(workflow));
        prepStmt.setString(7, WorkflowRequestStatus.PENDING.toString());
        prepStmt.setInt(8, tenantId);
        prepStmt.executeUpdate();
        IdentityDatabaseUtil.commitTransaction(connection);
    } catch (SQLException e) {
        IdentityDatabaseUtil.rollbackTransaction(connection);
        throw new InternalWorkflowException("Error when executing the sql query:" + query, e);
    } catch (IOException e) {
        throw new InternalWorkflowException("Error when serializing the workflow request: " + workflow, e);
    } finally {
        IdentityDatabaseUtil.closeAllConnections(connection, null, prepStmt);
    }
}
Also used : SQLException(java.sql.SQLException) InternalWorkflowException(org.wso2.carbon.identity.workflow.mgt.exception.InternalWorkflowException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) Timestamp(java.sql.Timestamp)

Aggregations

InternalWorkflowException (org.wso2.carbon.identity.workflow.mgt.exception.InternalWorkflowException)5 WorkflowRequest (org.wso2.carbon.identity.workflow.mgt.bean.WorkflowRequest)4 WorkflowRequest (org.wso2.carbon.identity.workflow.mgt.dto.WorkflowRequest)4 WorkflowListener (org.wso2.carbon.identity.workflow.mgt.listener.WorkflowListener)4 RequestParameter (org.wso2.carbon.identity.workflow.mgt.bean.RequestParameter)3 WorkflowRequestAssociationDAO (org.wso2.carbon.identity.workflow.mgt.dao.WorkflowRequestAssociationDAO)3 WorkflowRequestDAO (org.wso2.carbon.identity.workflow.mgt.dao.WorkflowRequestDAO)3 WorkflowException (org.wso2.carbon.identity.workflow.mgt.exception.WorkflowException)3 IOException (java.io.IOException)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 Timestamp (java.sql.Timestamp)2 HashMap (java.util.HashMap)2 WorkflowExecutorManagerListener (org.wso2.carbon.identity.workflow.mgt.listener.WorkflowExecutorManagerListener)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ResultSet (java.sql.ResultSet)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1