use of org.wso2.carbon.identity.workflow.mgt.WorkflowExecutorResult in project carbon-identity-framework by wso2.
the class AbstractWorkflowRequestHandler method startWorkFlow.
/**
* Start a new workflow.
*
* @param wfParams Parameters related to workflow
* @param nonWfParams Other parameters
* @param uuid Unique ID of request
* @return
* @throws WorkflowException
*/
public WorkflowExecutorResult startWorkFlow(Map<String, Object> wfParams, Map<String, Object> nonWfParams, String uuid) throws WorkflowException {
if (isWorkflowCompleted()) {
return new WorkflowExecutorResult(ExecutorResultState.COMPLETED);
}
if (!isAssociated()) {
return new WorkflowExecutorResult(ExecutorResultState.NO_ASSOCIATION);
}
WorkflowRequest workFlowRequest = new WorkflowRequest();
List<RequestParameter> parameters = new ArrayList<RequestParameter>(wfParams.size() + nonWfParams.size() + 1);
for (Map.Entry<String, Object> paramEntry : wfParams.entrySet()) {
parameters.add(getParameter(paramEntry.getKey(), paramEntry.getValue(), true));
}
for (Map.Entry<String, Object> paramEntry : nonWfParams.entrySet()) {
parameters.add(getParameter(paramEntry.getKey(), paramEntry.getValue(), false));
}
RequestParameter uuidParameter = new RequestParameter();
uuidParameter.setName(WFConstant.REQUEST_ID);
uuidParameter.setValue(uuid);
uuidParameter.setRequiredInWorkflow(true);
uuidParameter.setValueType(WorkflowDataType.STRING_TYPE);
parameters.add(uuidParameter);
workFlowRequest.setRequestParameters(parameters);
workFlowRequest.setTenantId(CarbonContext.getThreadLocalCarbonContext().getTenantId());
workFlowRequest.setUuid(uuid);
engageWorkflow(workFlowRequest);
WorkflowExecutorResult workflowExecutorResult = WorkFlowExecutorManager.getInstance().executeWorkflow(workFlowRequest);
if (workflowExecutorResult.getExecutorResultState() == ExecutorResultState.FAILED) {
throw new WorkflowException(workflowExecutorResult.getMessage());
}
return workflowExecutorResult;
}
use of org.wso2.carbon.identity.workflow.mgt.WorkflowExecutorResult 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;
}
Aggregations