Search in sources :

Example 1 with WorkflowAssociation

use of org.wso2.carbon.identity.workflow.mgt.bean.WorkflowAssociation in project carbon-identity-framework by wso2.

the class WorkflowRequestAssociationDAO method getWorkflowAssociationsForRequest.

/**
 * @param eventId
 * @param tenantId
 * @return
 * @throws InternalWorkflowException
 */
public List<WorkflowAssociation> getWorkflowAssociationsForRequest(String eventId, int tenantId) throws InternalWorkflowException {
    Connection connection = IdentityDatabaseUtil.getDBConnection(false);
    PreparedStatement prepStmt = null;
    ResultSet rs;
    List<WorkflowAssociation> associations = new ArrayList<>();
    String query = SQLConstants.GET_ASSOCIATIONS_FOR_EVENT_QUERY;
    try {
        prepStmt = connection.prepareStatement(query);
        prepStmt.setString(1, eventId);
        prepStmt.setInt(2, tenantId);
        rs = prepStmt.executeQuery();
        while (rs.next()) {
            int id = rs.getInt(SQLConstants.ID_COLUMN);
            String condition = rs.getString(SQLConstants.CONDITION_COLUMN);
            String workflowId = rs.getString(SQLConstants.WORKFLOW_ID_COLUMN);
            String associationName = rs.getString(SQLConstants.ASSOCIATION_NAME_COLUMN);
            WorkflowAssociation association = new WorkflowAssociation();
            association.setWorkflowId(workflowId);
            association.setAssociationCondition(condition);
            association.setEventId(eventId);
            association.setAssociationId(id);
            association.setAssociationName(associationName);
            associations.add(association);
        }
    } catch (SQLException e) {
        throw new InternalWorkflowException("Error when executing the sql query:" + query, e);
    } finally {
        IdentityDatabaseUtil.closeAllConnections(connection, null, prepStmt);
    }
    return associations;
}
Also used : SQLException(java.sql.SQLException) InternalWorkflowException(org.wso2.carbon.identity.workflow.mgt.exception.InternalWorkflowException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) WorkflowAssociation(org.wso2.carbon.identity.workflow.mgt.bean.WorkflowAssociation)

Example 2 with WorkflowAssociation

use of org.wso2.carbon.identity.workflow.mgt.bean.WorkflowAssociation in project carbon-identity-framework by wso2.

the class WorkflowManagementServiceImpl method isEventAssociated.

/**
 * Check if an operation is engaged with a workflow or not.
 *
 * @param eventType
 * @return
 * @throws InternalWorkflowException
 */
@Override
public boolean isEventAssociated(String eventType) throws WorkflowException {
    List<WorkflowListener> workflowListenerList = WorkflowServiceDataHolder.getInstance().getWorkflowListenerList();
    for (WorkflowListener workflowListener : workflowListenerList) {
        if (workflowListener.isEnable()) {
            workflowListener.doPreIsEventAssociated(eventType);
        }
    }
    List<WorkflowAssociation> associations = workflowRequestAssociationDAO.getWorkflowAssociationsForRequest(eventType, CarbonContext.getThreadLocalCarbonContext().getTenantId());
    for (WorkflowListener workflowListener : workflowListenerList) {
        if (workflowListener.isEnable()) {
            workflowListener.doPreIsEventAssociated(eventType);
        }
    }
    if (associations.size() > 0) {
        return true;
    } else {
        return false;
    }
}
Also used : WorkflowAssociation(org.wso2.carbon.identity.workflow.mgt.bean.WorkflowAssociation) WorkflowListener(org.wso2.carbon.identity.workflow.mgt.listener.WorkflowListener)

Example 3 with WorkflowAssociation

use of org.wso2.carbon.identity.workflow.mgt.bean.WorkflowAssociation 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)

Aggregations

WorkflowAssociation (org.wso2.carbon.identity.workflow.mgt.bean.WorkflowAssociation)3 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 OMElement (org.apache.axiom.om.OMElement)1 AXIOMXPath (org.apache.axiom.om.xpath.AXIOMXPath)1 JaxenException (org.jaxen.JaxenException)1 Parameter (org.wso2.carbon.identity.workflow.mgt.bean.Parameter)1 Workflow (org.wso2.carbon.identity.workflow.mgt.bean.Workflow)1 WorkflowDAO (org.wso2.carbon.identity.workflow.mgt.dao.WorkflowDAO)1 WorkflowRequestAssociationDAO (org.wso2.carbon.identity.workflow.mgt.dao.WorkflowRequestAssociationDAO)1 WorkflowRequestDAO (org.wso2.carbon.identity.workflow.mgt.dao.WorkflowRequestDAO)1 WorkflowRequest (org.wso2.carbon.identity.workflow.mgt.dto.WorkflowRequest)1 InternalWorkflowException (org.wso2.carbon.identity.workflow.mgt.exception.InternalWorkflowException)1 WorkflowExecutorManagerListener (org.wso2.carbon.identity.workflow.mgt.listener.WorkflowExecutorManagerListener)1 WorkflowListener (org.wso2.carbon.identity.workflow.mgt.listener.WorkflowListener)1 AbstractWorkflow (org.wso2.carbon.identity.workflow.mgt.workflow.AbstractWorkflow)1