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;
}
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();
}
}
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;
}
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);
}
}
}
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);
}
}
Aggregations