From 643a396565399d9be9b6fe02d99eb4686c7ee226 Mon Sep 17 00:00:00 2001 From: IsharaSilva Date: Wed, 8 Jun 2022 11:10:41 +0530 Subject: [PATCH 01/16] Implement workflow approvalService --- .gitignore | 4 + .../pom.xml | 228 ++++++++++++++++++ .../DefaultApprovalWorkflow.java | 33 +++ .../DefaultWorkflowEventRequest.java | 75 ++++++ .../DefaultWorkflowEventRequestService.java | 151 ++++++++++++ .../DefaultWorkflowExecutor.java | 38 +++ .../dao/WorkflowEventRequestDAO.java | 66 +++++ .../dao/impl/WorkflowEventRequestDAOImpl.java | 160 ++++++++++++ .../exception/WorkflowEngineException.java | 7 + .../WorkflowEngineRuntimeException.java | 16 ++ .../WorkflowEngineServiceComponent.java | 79 ++++++ .../WorkflowEngineServiceDataHolder.java | 49 ++++ .../util/WorkflowEngineConstants.java | 29 +++ dbfiles/Defaultworkflowengine.sql | 26 ++ pom.xml | 147 +++++++++++ 15 files changed, 1108 insertions(+) create mode 100644 components/org.wso2.carbon.identity.workflow.engine/pom.xml create mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultApprovalWorkflow.java create mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowEventRequest.java create mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowEventRequestService.java create mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowExecutor.java create mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/dao/WorkflowEventRequestDAO.java create mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/dao/impl/WorkflowEventRequestDAOImpl.java create mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/exception/WorkflowEngineException.java create mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/exception/WorkflowEngineRuntimeException.java create mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/internal/WorkflowEngineServiceComponent.java create mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/internal/WorkflowEngineServiceDataHolder.java create mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/util/WorkflowEngineConstants.java create mode 100644 dbfiles/Defaultworkflowengine.sql create mode 100644 pom.xml diff --git a/.gitignore b/.gitignore index a1c2a23..a69f5c4 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,7 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* + +.idea/ +*.iml +target diff --git a/components/org.wso2.carbon.identity.workflow.engine/pom.xml b/components/org.wso2.carbon.identity.workflow.engine/pom.xml new file mode 100644 index 0000000..dad90e4 --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/pom.xml @@ -0,0 +1,228 @@ + + + 4.0.0 + + default.workflow.engine + org.wso2.carbon.identity.workflow.engine + 1.0-SNAPSHOT + ../../pom.xml + + + + workflow.engine + bundle + WSO2 Carbon - Workflow Engine Simple + + + org.testng + testng + 6.9.10 + test + + + org.wso2.carbon.identity.framework + org.wso2.carbon.identity.configuration.mgt.core + 5.18.187 + + + org.wso2.carbon.identity.framework + org.wso2.carbon.user.mgt.common + 5.18.187 + + + + org.wso2.carbon.identity.framework + org.wso2.carbon.user.mgt + 5.18.187 + + + org.wso2.carbon.identity.framework + org.wso2.carbon.identity.workflow.mgt + 5.18.187 + + + org.apache.ws.commons.axiom.wso2 + axiom + 1.2.11-wso2v16 + + + org.apache.felix + org.apache.felix.scr.ds-annotations + ${apache.felix.scr.ds.annotations.version} + + + + + + + + + wso2-nexus + WSO2 internal Repository + https://maven.wso2.org/nexus/content/groups/wso2-public/ + + true + daily + ignore + + + + + wso2.releases + WSO2 internal Repository + https://maven.wso2.org/nexus/content/repositories/releases/ + + true + daily + ignore + + + + + wso2.snapshots + Apache Snapshot Repository + https://maven.wso2.org/nexus/content/repositories/snapshots/ + + true + daily + + + false + + + + + + + + wso2.releases + WSO2 internal Repository + https://maven.wso2.org/nexus/content/repositories/releases/ + + true + daily + ignore + + + + + wso2.snapshots + WSO2 Snapshot Repository + https://maven.wso2.org/nexus/content/repositories/snapshots/ + + true + daily + + + false + + + + wso2-nexus + WSO2 internal Repository + https://maven.wso2.org/nexus/content/groups/wso2-public/ + + true + daily + ignore + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven.compiler.plugin.version} + true + + UTF-8 + 1.8 + 1.8 + + + + org.apache.felix + maven-bundle-plugin + 3.2.0 + true + + NONE + + ${buildNumber} + + org.wso2.carbon.identity.workflow.engine.internal, + + + !org.wso2.carbon.identity.workflow.engine.internal, + org.wso2.carbon.identity.workflow.engine.*; + + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + ${maven.checkstyleplugin.version} + + + validate + validate + + + https://raw.githubusercontent.com/wso2/code-quality-tools/v1.3/checkstyle/checkstyle.xml + + + https://raw.githubusercontent.com/wso2/code-quality-tools/v1.3/checkstyle/suppressions.xml + + UTF-8 + true + true + true + + + check + + + + + + org.codehaus.mojo + findbugs-maven-plugin + ${maven.findbugsplugin.version} + + findbugs-exclude.xml + Max + Low + true + ${project.build.directory}/findbugs + + + + analyze-compile + compile + + check + + + + + + + + + + 8 + 8 + 1.4 + 3.1.0 + 3.0.5 + 3.8.0 + 1.2.8 + + + + \ No newline at end of file diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultApprovalWorkflow.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultApprovalWorkflow.java new file mode 100644 index 0000000..6f72ceb --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultApprovalWorkflow.java @@ -0,0 +1,33 @@ +package org.wso2.carbon.identity.workflow.engine; + +import org.wso2.carbon.identity.workflow.mgt.bean.Parameter; +import org.wso2.carbon.identity.workflow.mgt.bean.metadata.InputData; +import org.wso2.carbon.identity.workflow.mgt.bean.metadata.ParameterMetaData; +import org.wso2.carbon.identity.workflow.mgt.exception.WorkflowException; +import org.wso2.carbon.identity.workflow.mgt.exception.WorkflowRuntimeException; +import org.wso2.carbon.identity.workflow.mgt.workflow.AbstractWorkflow; +import org.wso2.carbon.identity.workflow.mgt.workflow.WorkFlowExecutor; + +import java.util.List; + +public class DefaultApprovalWorkflow extends AbstractWorkflow { + + public DefaultApprovalWorkflow(Class workFlowExecutorClass, String metaDataXML) + throws WorkflowRuntimeException { + + super(null, workFlowExecutorClass, metaDataXML); + } + + @Override + protected InputData getInputData(ParameterMetaData parameterMetaData) { + + return null; + } + + @Override + public void deploy(List parameterList) throws WorkflowException { + + super.deploy(parameterList); + } + +} diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowEventRequest.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowEventRequest.java new file mode 100644 index 0000000..5a6caac --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowEventRequest.java @@ -0,0 +1,75 @@ +package org.wso2.carbon.identity.workflow.engine; + +import org.wso2.carbon.identity.workflow.mgt.bean.Parameter; +import org.wso2.carbon.identity.workflow.mgt.bean.WorkflowAssociation; +import org.wso2.carbon.identity.workflow.mgt.dto.WorkflowRequest; + +import java.util.List; + +public interface DefaultWorkflowEventRequest { + + /** + * Add who approves the relevant request. + * + * @param request workflow request object. + * @param parameterList parameterList. + */ + void addApproversOfRequests(WorkflowRequest request, List parameterList); + + /** + * Get taskId from WF_REQUEST_APPROVAL_RELATION table. + * + * @param eventId the request ID that need to be checked. + * @return task Id. + */ + String getApprovalOfRequest(String eventId); + + /** + * Delete approver details using task Id. + * + * @param taskId random generated unique Id. + */ + void deleteApprovalOfRequest(String taskId); + + /** + * Add current step. + * + * @param eventId the request ID that need to be checked. + * @param workflowId workflow id. + * @param currentStep current step of the flow. + */ + void createStatesOfRequest(String eventId, String workflowId, int currentStep); + + /** + * Get current step from the table. + * + * @param eventId the request ID that need to be checked. + * @param workflowId workflow Id. + * @return currentStep. + */ + int getStateOfRequest(String eventId, String workflowId); + + /** + *Update current step according to the eventId and workflowId. + * + * @param eventId the request ID that need to be checked. + * @param workflowId workflow Id. + */ + void updateStateOfRequest(String eventId, String workflowId); + + /** + * Get related associations. + * + * @param workflowRequest request object. + * @return association list. + */ + List getAssociations(WorkflowRequest workflowRequest); + + /** + * Get relevant workflow id to request. + * + * @param request request object. + * @return workflow Id. + */ + String getWorkflowId(WorkflowRequest request); +} diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowEventRequestService.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowEventRequestService.java new file mode 100644 index 0000000..e17f233 --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowEventRequestService.java @@ -0,0 +1,151 @@ +package org.wso2.carbon.identity.workflow.engine; + +import org.wso2.carbon.identity.workflow.engine.dao.WorkflowEventRequestDAO; +import org.wso2.carbon.identity.workflow.engine.dao.impl.WorkflowEventRequestDAOImpl; +import org.wso2.carbon.identity.workflow.engine.exception.WorkflowEngineException; +import org.wso2.carbon.identity.workflow.engine.exception.WorkflowEngineRuntimeException; +import org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants; +import org.wso2.carbon.identity.workflow.mgt.WorkflowExecutorManagerService; +import org.wso2.carbon.identity.workflow.mgt.WorkflowExecutorManagerServiceImpl; +import org.wso2.carbon.identity.workflow.mgt.WorkflowManagementService; +import org.wso2.carbon.identity.workflow.mgt.WorkflowManagementServiceImpl; +import org.wso2.carbon.identity.workflow.mgt.bean.Parameter; +import org.wso2.carbon.identity.workflow.mgt.bean.RequestParameter; +import org.wso2.carbon.identity.workflow.mgt.bean.Workflow; +import org.wso2.carbon.identity.workflow.mgt.bean.WorkflowAssociation; +import org.wso2.carbon.identity.workflow.mgt.dto.WorkflowRequest; +import org.wso2.carbon.identity.workflow.mgt.exception.InternalWorkflowException; +import org.wso2.carbon.identity.workflow.mgt.exception.WorkflowException; + +import java.util.Collections; +import java.util.List; +import java.util.UUID; + +public class DefaultWorkflowEventRequestService implements DefaultWorkflowEventRequest { + + public void addApproversOfRequests(WorkflowRequest request, List parameterList) { + + WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); + String taskId = UUID.randomUUID().toString(); + String eventId = getRequestId(request); + String workflowId = getWorkflowId(request); + String approverType = null; + String approverName = null; + int currentStepValue = getStateOfRequest(eventId, workflowId); + if (currentStepValue == 0) { + createStatesOfRequest(eventId, workflowId, currentStepValue); + } + currentStepValue += 1; + updateStateOfRequest(eventId, workflowId); + List associations = getAssociations(request); + for (WorkflowAssociation association : associations) { + for (Parameter parameter : parameterList) { + if (parameter.getParamName().equals(WorkflowEngineConstants.ParameterName.USER_AND_ROLE_STEP)) { + String[] stepName = parameter.getqName().split("-"); + int step = Integer.parseInt(stepName[2]); + if (currentStepValue == step) { + approverType = stepName[stepName.length - 1]; + + String approver = parameter.getParamValue(); + String[] approvers = approver.split(","); + if (approvers != null) { + List approverList = Collections.singletonList(approver); + String stepValue = WorkflowEngineConstants.ParameterName.USER_AND_ROLE_STEP + "-step-" + + currentStepValue + "-users"; + if (stepValue.equals(parameter.getqName())) { + for (String name : approverList) { + approverName = name; + } + } + stepValue = WorkflowEngineConstants.ParameterName.USER_AND_ROLE_STEP + "-step-" + + currentStepValue + "-roles"; + if (stepValue.equals(parameter.getqName())) { + for (String name : approverList) { + approverName = name; + } + } + } + } + } + } + } + workflowEventRequestDAO.addApproversOfRequest(taskId, eventId, workflowId, approverType, approverName); + } + + private String getRequestId(WorkflowRequest request) { + + List requestParameter; + Object event = null; + for (int i = 0; i < request.getRequestParameters().size(); i++) { + requestParameter = request.getRequestParameters(); + if (requestParameter.get(i).getName().equals(WorkflowEngineConstants.ParameterName.REQUEST_ID)) { + event = requestParameter.get(i).getValue(); + } + } + return (String) event; + } + + public String getWorkflowId(WorkflowRequest request) { + + WorkflowManagementService workflowManagementService = new WorkflowManagementServiceImpl(); + List associations = getAssociations(request); + Workflow workflow = null; + String workflowId; + for (WorkflowAssociation association : associations) { + try { + workflow = workflowManagementService.getWorkflow(association.getWorkflowId()); + } catch (WorkflowException e) { + throw new WorkflowEngineException("The workflow Id is not valid"); + } + } + workflowId = workflow.getWorkflowId(); + return workflowId; + } + + public List getAssociations(WorkflowRequest workflowRequest) { + + List associations; + WorkflowExecutorManagerService workFlowExecutorManagerService = new WorkflowExecutorManagerServiceImpl(); + try { + associations = workFlowExecutorManagerService.getWorkflowAssociationsForRequest( + workflowRequest.getEventType(), workflowRequest.getTenantId()); + } catch (InternalWorkflowException e) { + throw new WorkflowEngineRuntimeException("The associations are not connecting with any request"); + } + return associations; + } + + public String getApprovalOfRequest(String eventId) { + + WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); + return workflowEventRequestDAO.getApproversOfRequest(eventId); + } + + public void deleteApprovalOfRequest(String taskId) { + + WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); + workflowEventRequestDAO.deleteApproversOfRequest(taskId); + } + + public void createStatesOfRequest(String eventId, String workflowId, int currentStep) { + + WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); + workflowEventRequestDAO.createStatesOfRequest(eventId, workflowId, currentStep); + } + + public int getStateOfRequest(String eventId, String workflowId) { + + WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); + return workflowEventRequestDAO.getStateOfRequest(eventId, workflowId); + } + + public void updateStateOfRequest(String eventId, String workflowId) { + + WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); + + int currentStep = getStateOfRequest(eventId, workflowId); + currentStep += 1; + workflowEventRequestDAO.updateStateOfRequest(eventId, workflowId, currentStep); + } +} + diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowExecutor.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowExecutor.java new file mode 100644 index 0000000..6d09967 --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowExecutor.java @@ -0,0 +1,38 @@ +package org.wso2.carbon.identity.workflow.engine; + +import org.wso2.carbon.identity.workflow.mgt.bean.Parameter; +import org.wso2.carbon.identity.workflow.mgt.dto.WorkflowRequest; +import org.wso2.carbon.identity.workflow.mgt.workflow.WorkFlowExecutor; + +import java.util.List; + +public class DefaultWorkflowExecutor implements WorkFlowExecutor { + + List parameterList; + private static final String EXECUTOR_NAME = "WorkflowEngineSimple"; + + @Override + public boolean canHandle(WorkflowRequest workflowRequest) { + + return true; + } + + @Override + public void initialize(List parameterList) { + + this.parameterList = parameterList; + } + + @Override + public void execute(WorkflowRequest request) { + + DefaultWorkflowEventRequest defaultWorkflowEventRequest = new DefaultWorkflowEventRequestService(); + defaultWorkflowEventRequest.addApproversOfRequests(request, parameterList); + } + + @Override + public String getName() { + + return EXECUTOR_NAME; + } +} diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/dao/WorkflowEventRequestDAO.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/dao/WorkflowEventRequestDAO.java new file mode 100644 index 0000000..d047da7 --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/dao/WorkflowEventRequestDAO.java @@ -0,0 +1,66 @@ +package org.wso2.carbon.identity.workflow.engine.dao; + +public interface WorkflowEventRequestDAO { + + /** + * Add who approves the relevant request. + * + * @param taskId random generated unique Id. + * @param eventId the request ID that need to be checked. + * @param workflowId workflow ID. + * @param approverType the type of the approved user EX: user or Role. + * @param approverName the value of the approver type. + * @return event ID. + */ + String addApproversOfRequest(String taskId, String eventId, String workflowId, String approverType, String approverName); + + /** + * Get taskId from table. + * + * @param eventId the request ID that need to be checked. + * @return task Id. + */ + String getApproversOfRequest(String eventId); + + /** + * Get approverName related to currentStep. + * + * @param eventId the request ID that need to be checked. + * @return the value of the approver type. + */ + String getApproversOfCurrentStep(String eventId); + + /** + * Delete approver details using task Id. + * + * @param taskId random generated unique Id. + */ + void deleteApproversOfRequest(String taskId); + + /** + * Add what step to approve. + * + * @param eventId the request ID that need to be checked. + * @param workflowId workflow ID. + * @param currentStep the current step. + */ + void createStatesOfRequest(String eventId, String workflowId, int currentStep); + + /** + * Returns the current step given the event ID and workflow ID. + * + * @param eventId the request ID that need to be checked. + * @param workflowId workflow ID. + * @return current step value. + */ + int getStateOfRequest(String eventId, String workflowId); + + /** + * Updates a state of request given the event ID, workflow ID and current step. + * + * @param eventId the request ID that need to be checked. + * @param workflowId workflow ID. + * @param currentStep the current step. + */ + void updateStateOfRequest(String eventId, String workflowId, int currentStep); +} diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/dao/impl/WorkflowEventRequestDAOImpl.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/dao/impl/WorkflowEventRequestDAOImpl.java new file mode 100644 index 0000000..e41c60e --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/dao/impl/WorkflowEventRequestDAOImpl.java @@ -0,0 +1,160 @@ +package org.wso2.carbon.identity.workflow.engine.dao.impl; + +import org.wso2.carbon.database.utils.jdbc.JdbcTemplate; +import org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException; +import org.wso2.carbon.identity.configuration.mgt.core.util.JdbcUtils; +import org.wso2.carbon.identity.workflow.engine.dao.WorkflowEventRequestDAO; +import org.wso2.carbon.identity.workflow.engine.exception.WorkflowEngineRuntimeException; +import org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants; + +import java.sql.PreparedStatement; +import java.sql.SQLException; + +import static org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants.APPROVER_NAME_COLUMN; +import static org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants.CURRENT_STEP_COLUMN; +import static org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants.SqlQueries.ADD_CURRENT_STEP_FOR_EVENT; +import static org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants.TASK_ID_COLUMN; + +public class WorkflowEventRequestDAOImpl implements WorkflowEventRequestDAO { + + @Override + public String addApproversOfRequest(String taskId, String eventId, String workflowId, String approverType, + String approverName) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + try { + jdbcTemplate.executeUpdate(WorkflowEngineConstants.SqlQueries.ADD_APPROVAL_LIST_RELATED_TO_USER, + preparedStatement -> { + preparedStatement.setString(1, taskId); + preparedStatement.setString(2, eventId); + preparedStatement.setString(3, workflowId); + preparedStatement.setString(4, approverType); + preparedStatement.setString(5, approverName); + }); + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while adding request details" + + "in eventId: %s & workflowId: %s", eventId, workflowId); + throw new WorkflowEngineRuntimeException(errorMessage); + } + return taskId; + } + + public String getApproversOfRequest(String eventId) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + String taskIdExists; + try { + taskIdExists = jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries. + GET_TASK_ID_RELATED_TO_USER, + ((resultSet, i) -> ( + resultSet.getString(TASK_ID_COLUMN))), + preparedStatement -> preparedStatement.setString(1, eventId)); + if (taskIdExists == null) { + return null; + } + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while retrieving taskId from" + + "task Id: %s", eventId); + throw new WorkflowEngineRuntimeException(errorMessage); + } + return taskIdExists; + } + + public void deleteApproversOfRequest(String taskId) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + try { + jdbcTemplate.executeUpdate(WorkflowEngineConstants.SqlQueries.DELETE_APPROVAL_LIST_RELATED_TO_USER, + preparedStatement -> preparedStatement.setString(1, taskId)); + } catch (DataAccessException e) { + String errorMessage = String.format("Error while deleting the approver details from taskId:%s", taskId); + throw new WorkflowEngineRuntimeException(errorMessage); + } + } + + @Override + public void createStatesOfRequest(String eventId, String workflowId, int currentStep) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + try { + jdbcTemplate.executeUpdate(ADD_CURRENT_STEP_FOR_EVENT, + preparedStatement -> { + preparedStatement.setString(1, eventId); + preparedStatement.setString(2, workflowId); + preparedStatement.setInt(3, currentStep); + }); + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while adding request approval steps" + + "in event Id: %s & workflowId: %s", eventId, workflowId); + throw new WorkflowEngineRuntimeException(errorMessage); + } + } + + @Override + public int getStateOfRequest(String eventId, String workflowId) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + String stepExists; + try { + stepExists = jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries.GET_CURRENT_STEP, + ((resultSet, i) -> ( + Integer.toString(resultSet.getInt(CURRENT_STEP_COLUMN)))), + preparedStatement -> { + preparedStatement.setString(1, eventId); + preparedStatement.setString(2, workflowId); + }); + if (stepExists == null) { + return 0; + } + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while retrieving currentStep from" + + "event Id: %s", eventId); + throw new WorkflowEngineRuntimeException(errorMessage); + } + return Integer.parseInt(stepExists); + } + + public void updateStateOfRequest(String eventId, String workflowId, int currentStep) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + try { + jdbcTemplate.executeUpdate(WorkflowEngineConstants.SqlQueries.UPDATE_STATE_OF_REQUEST, + (preparedStatement -> { + setPreparedStatementForStateOfRequest(currentStep, eventId, workflowId, preparedStatement); + preparedStatement.setInt(1, currentStep); + preparedStatement.setString(2, eventId); + preparedStatement.setString(3, workflowId); + })); + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while updating state from" + + "eventIs:%s", eventId); + throw new WorkflowEngineRuntimeException(errorMessage); + } + } + + public String getApproversOfCurrentStep(String eventId) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + String approverName; + try { + approverName = jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries. + GET_APPROVER_NAME_RELATED_TO_CURRENT_STEP, + ((resultSet, i) -> ( + resultSet.getString(APPROVER_NAME_COLUMN))), + preparedStatement -> preparedStatement.setString(1, eventId)); + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while retrieving currentStep from" + + "event Id: %s", eventId); + throw new WorkflowEngineRuntimeException(errorMessage); + } + return approverName; + } + + private void setPreparedStatementForStateOfRequest(int currentStep, String eventId, String workflowId, + PreparedStatement preparedStatement) throws SQLException { + + preparedStatement.setInt(1, currentStep); + preparedStatement.setString(2, eventId); + preparedStatement.setString(3, workflowId); + } +} \ No newline at end of file diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/exception/WorkflowEngineException.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/exception/WorkflowEngineException.java new file mode 100644 index 0000000..d1c348b --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/exception/WorkflowEngineException.java @@ -0,0 +1,7 @@ +package org.wso2.carbon.identity.workflow.engine.exception; + +public class WorkflowEngineException extends java.lang.RuntimeException { + public WorkflowEngineException(String message) { + super(message); + } +} diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/exception/WorkflowEngineRuntimeException.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/exception/WorkflowEngineRuntimeException.java new file mode 100644 index 0000000..aa89779 --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/exception/WorkflowEngineRuntimeException.java @@ -0,0 +1,16 @@ +package org.wso2.carbon.identity.workflow.engine.exception; + +import org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException; + +public class WorkflowEngineRuntimeException extends RuntimeException { + + public WorkflowEngineRuntimeException(String message) { + + super(message); + } + public WorkflowEngineRuntimeException(String message, DataAccessException e) { + + super(message); + } + +} diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/internal/WorkflowEngineServiceComponent.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/internal/WorkflowEngineServiceComponent.java new file mode 100644 index 0000000..ecab051 --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/internal/WorkflowEngineServiceComponent.java @@ -0,0 +1,79 @@ +package org.wso2.carbon.identity.workflow.engine.internal; + +import org.osgi.framework.BundleContext; +import org.osgi.service.component.ComponentContext; +import org.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; +import org.osgi.service.component.annotations.ReferenceCardinality; +import org.osgi.service.component.annotations.ReferencePolicy; +import org.wso2.carbon.identity.workflow.engine.DefaultApprovalWorkflow; +import org.wso2.carbon.identity.workflow.engine.DefaultWorkflowExecutor; +import org.wso2.carbon.identity.workflow.mgt.WorkflowExecutorManagerService; +import org.wso2.carbon.identity.workflow.mgt.WorkflowManagementService; +import org.wso2.carbon.identity.workflow.mgt.workflow.AbstractWorkflow; + +@Component( + name = "org.wso2.carbon.identity.workflow.engine", + immediate = true) +public class WorkflowEngineServiceComponent { + @Activate + protected void activate(ComponentContext context) { + + BundleContext bundleContext = context.getBundleContext(); + bundleContext.registerService(AbstractWorkflow.class, new DefaultApprovalWorkflow(DefaultWorkflowExecutor.class, + getMetaDataXML()), null); + } + + private String getMetaDataXML() { + + return "\n" + + "\n" + + " newWorkflowImpl\n" + + " ApprovalWorkflow\n" + + " Approval Workflow\n" + + " MultiStepApprovalTemplate\n" + + " \n" + + " \n" + + " BPS Profile(Server Profile Name)\n" + + " \n" + + " \n" + + " Task Subject(Approval task subject to display)\n" + + " \n" + + " \n" + + " Task Detail(Approval task description)\n" + + " \n" + + " \n" + + "\n" + + ""; + } + + @Reference( + name = "org.wso2.carbon.identity.workflow.mgt", + service = org.wso2.carbon.identity.workflow.mgt.WorkflowManagementService.class, + cardinality = ReferenceCardinality.MANDATORY, + policy = ReferencePolicy.DYNAMIC, + unbind = "unsetWorkflowManagementService") + protected void setWorkflowManagementService(WorkflowManagementService workflowManagementService) { + + WorkflowEngineServiceDataHolder.getInstance().setWorkflowManagementService(workflowManagementService); + } + + protected void unsetWorkflowManagementService(WorkflowManagementService workflowManagementService) { + WorkflowEngineServiceDataHolder.getInstance().setWorkflowManagementService(null); + } + + @Reference( + name = "org.wso2.carbon.identity.workflow.mgt.executor", + service = org.wso2.carbon.identity.workflow.mgt.WorkflowExecutorManagerService.class, + cardinality = ReferenceCardinality.MANDATORY, + policy = ReferencePolicy.DYNAMIC, + unbind = "unsetWorkflowExecutorManagerService") + protected void setWorkflowExecutorManagerService(WorkflowExecutorManagerService workflowExecutorManagerService) { + WorkflowEngineServiceDataHolder.getInstance().setWorkflowExecutorManagerService(workflowExecutorManagerService); + } + + protected void unsetWorkflowExecutorManagerService(WorkflowExecutorManagerService workflowExecutorManagerService) { + WorkflowEngineServiceDataHolder.getInstance().setWorkflowExecutorManagerService(workflowExecutorManagerService); + } +} diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/internal/WorkflowEngineServiceDataHolder.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/internal/WorkflowEngineServiceDataHolder.java new file mode 100644 index 0000000..6dc492e --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/internal/WorkflowEngineServiceDataHolder.java @@ -0,0 +1,49 @@ +package org.wso2.carbon.identity.workflow.engine.internal; + +import org.wso2.carbon.identity.workflow.mgt.WorkflowExecutorManagerService; +import org.wso2.carbon.identity.workflow.mgt.WorkflowManagementService; +import org.wso2.carbon.identity.workflow.mgt.workflow.AbstractWorkflow; + +import java.util.HashMap; +import java.util.Map; + +public class WorkflowEngineServiceDataHolder { + + private static WorkflowEngineServiceDataHolder instance = new WorkflowEngineServiceDataHolder(); + + private WorkflowManagementService workflowManagementService; + + private WorkflowExecutorManagerService workflowExecutorManagerService; + + private WorkflowEngineServiceDataHolder() { + + } + + public static WorkflowEngineServiceDataHolder getInstance() { + + return instance; + } + + public WorkflowManagementService getWorkflowManagementService() { + return workflowManagementService; + } + + public void setWorkflowManagementService( + WorkflowManagementService workflowManagementService) { + this.workflowManagementService = workflowManagementService; + } + + public WorkflowExecutorManagerService getWorkflowExecutorManagerService() { + + return workflowExecutorManagerService; + } + + public void setWorkflowExecutorManagerService( + WorkflowExecutorManagerService workflowExecutorManagerService) { + this.workflowExecutorManagerService = workflowExecutorManagerService; + } + private Map> workflowImpls = new HashMap>(); + public Map> getWorkflowImpls() { + return workflowImpls; + } +} diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/util/WorkflowEngineConstants.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/util/WorkflowEngineConstants.java new file mode 100644 index 0000000..675f46c --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/util/WorkflowEngineConstants.java @@ -0,0 +1,29 @@ +package org.wso2.carbon.identity.workflow.engine.util; + +public class WorkflowEngineConstants { + + public static final String CURRENT_STEP_COLUMN = "CURRENT_STEP"; + public static final String TASK_ID_COLUMN = "TASK_ID"; + public static final String APPROVER_NAME_COLUMN = "APPROVER_NAME"; + + public static class SqlQueries { + + public static final String ADD_APPROVAL_LIST_RELATED_TO_USER = "INSERT INTO WF_WORKFLOW_APPROVAL_RELATION (TASK_ID,EVENT_ID,WORKFLOW_ID,APPROVER_TYPE,APPROVER_NAME) VALUES (?,?,?,?,?)"; + public static final String GET_TASK_ID_RELATED_TO_USER = "SELECT TASK_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE EVENT_ID = ?"; + public static final String GET_APPROVER_NAME_RELATED_TO_CURRENT_STEP = "SELECT APPROVER_NAME APPROVER_NAME_COLUMN FROM WF_WORKFLOW_APPROVAL_RELATION WHERE EVENT_ID = ?"; + public static final String DELETE_APPROVAL_LIST_RELATED_TO_USER = "DELETE FROM WF_WORKFLOW_APPROVAL_RELATION WHERE TASK_ID=?"; + public static final String ADD_CURRENT_STEP_FOR_EVENT = "INSERT INTO WF_WORKFLOW_APPROVAL_STATE (EVENT_ID,WORKFLOW_ID, CURRENT_STEP) VALUES (?,?,?)"; + public static final String GET_CURRENT_STEP = "SELECT CURRENT_STEP FROM WF_WORKFLOW_APPROVAL_STATE WHERE EVENT_ID = ? AND WORKFLOW_ID = ?"; + public static final String UPDATE_STATE_OF_REQUEST = "UPDATE WF_WORKFLOW_APPROVAL_STATE SET CURRENT_STEP=? WHERE EVENT_ID = ? AND WORKFLOW_ID = ?"; + } + + public enum EventState{ + PENDING, + APPROVED + } + + public static class ParameterName { + public static final String USER_AND_ROLE_STEP = "UserAndRole" ; + public static final String REQUEST_ID = "REQUEST ID" ; + } +} diff --git a/dbfiles/Defaultworkflowengine.sql b/dbfiles/Defaultworkflowengine.sql new file mode 100644 index 0000000..73dff38 --- /dev/null +++ b/dbfiles/Defaultworkflowengine.sql @@ -0,0 +1,26 @@ +CREATE TABLE WF_WORKFLOW_APPROVAL_RELATION ( +TASK_ID VARCHAR(45) NOT NULL, +EVENT_ID VARCHAR(45) NOT NULL, +WORKFLOW_ID VARCHAR(45) NOT NULL, +APPROVER_TYPE VARCHAR(45) NOT NULL, +APPROVER_NAME VARCHAR(255) NOT NULL, +PRIMARY KEY(TASK_ID), +FOREIGN KEY (EVENT_ID) REFERENCES WF_REQUEST(UUID) ON DELETE CASCADE, +FOREIGN KEY (WORKFLOW_ID) REFERENCES WF_WORKFLOW(ID) ON DELETE CASCADE +); + +CREATE TABLE WF_WORKFLOW_APPROVAL_STATE ( +EVENT_ID VARCHAR(45) NOT NULL, +WORKFLOW_ID VARCHAR(45) NOT NULL, +CURRENT_STEP INTEGER, +CONSTRAINT PK_WF_APPROVAL_STATE_CONSTRAINT PRIMARY KEY (EVENT_ID, WORKFLOW_ID), +FOREIGN KEY (EVENT_ID) REFERENCES WF_REQUEST(UUID) ON DELETE CASCADE, +FOREIGN KEY (WORKFLOW_ID) REFERENCES WF_WORKFLOW(ID) ON DELETE CASCADE +); + + +CREATE INDEX IDX_WF_APPROVAL_STATE ON WF_WORKFLOW_APPROVAL_STATE(EVENT_ID,WORKFLOW_ID); + +CREATE INDEX IDX_WF_APPROVAL_RELATION ON WF_WORKFLOW_APPROVAL_RELATION(TASK_ID); + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..1ce6fa4 --- /dev/null +++ b/pom.xml @@ -0,0 +1,147 @@ + + + + org.wso2 + wso2 + 1.4 + + + org.wso2.carbon.identity.workflow.engine + 4.0.0 + default.workflow.engine + 1.0-SNAPSHOT + pom + WSO2 Carbon - Identity Workflow Simple Module + + + + http://wso2.org + + + https://github.com/wso2-incubator/identity-workflow-simple.git + scm:git:https://github.com/wso2-incubator/identity-workflow-simple.git + + scm:git:https://github.com/wso2-incubator/identity-workflow-simple.git + HEAD + + + + components/org.wso2.carbon.identity.workflow.engine + + + + + + + org.apache.felix + maven-bundle-plugin + 3.2.0 + true + + NONE + + ${buildNumber} + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + ${maven.checkstyleplugin.version} + + + validate + validate + + + https://raw.githubusercontent.com/wso2/code-quality-tools/v1.3/checkstyle/checkstyle.xml + + + https://raw.githubusercontent.com/wso2/code-quality-tools/v1.3/checkstyle/suppressions.xml + + UTF-8 + true + true + true + + + check + + + + + + org.codehaus.mojo + buildnumber-maven-plugin + ${maven.buildnumber.plugin.version} + + + validate + + create + + + + + false + false + + + + + + + + org.testng + testng + ${testng.version} + test + + + org.wso2.carbon.identity.framework + org.wso2.carbon.identity.configuration.mgt.core + ${carbon.identity.framework.version} + + + org.wso2.carbon.identity.framework + org.wso2.carbon.user.mgt.common + ${carbon.identity.framework.version} + + + + org.wso2.carbon.identity.framework + org.wso2.carbon.user.mgt + ${carbon.identity.framework.version} + + + org.wso2.carbon.identity.framework + org.wso2.carbon.identity.workflow.mgt + ${carbon.identity.framework.version} + + + org.apache.ws.commons.axiom.wso2 + axiom + ${axiom.version} + + + org.apache.felix + org.apache.felix.scr.ds-annotations + ${apache.felix.scr.ds.annotations.version} + + + + + + + 8 + 8 + 1.4 + 3.1.0 + 3.0.5 + 3.8.0 + 1.2.8 + + + \ No newline at end of file From 0f8bc6df9d488542344585725d992c952c1b640b Mon Sep 17 00:00:00 2001 From: IsharaSilva Date: Thu, 9 Jun 2022 22:27:08 +0530 Subject: [PATCH 02/16] Implement workflow approvalService --- .../pom.xml | 12 -- .../DefaultApprovalWorkflow.java | 16 +- .../DefaultWorkflowEventRequest.java | 3 + .../DefaultWorkflowEventRequestService.java | 57 +++++-- .../DefaultWorkflowExecutor.java | 15 ++ .../dao/WorkflowEventRequestDAO.java | 43 ++++++ .../dao/impl/WorkflowEventRequestDAOImpl.java | 144 +++++++++++++++++- .../exception/WorkflowEngineException.java | 3 + .../WorkflowEngineRuntimeException.java | 3 + .../WorkflowEngineServiceComponent.java | 16 +- .../WorkflowEngineServiceDataHolder.java | 11 +- .../util/WorkflowEngineConstants.java | 23 ++- pom.xml | 5 - 13 files changed, 303 insertions(+), 48 deletions(-) diff --git a/components/org.wso2.carbon.identity.workflow.engine/pom.xml b/components/org.wso2.carbon.identity.workflow.engine/pom.xml index dad90e4..6fd476a 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/pom.xml +++ b/components/org.wso2.carbon.identity.workflow.engine/pom.xml @@ -10,7 +10,6 @@ ../../pom.xml - workflow.engine bundle WSO2 Carbon - Workflow Engine Simple @@ -31,7 +30,6 @@ org.wso2.carbon.user.mgt.common 5.18.187 - org.wso2.carbon.identity.framework org.wso2.carbon.user.mgt @@ -52,12 +50,9 @@ org.apache.felix.scr.ds-annotations ${apache.felix.scr.ds.annotations.version} - - - wso2-nexus WSO2 internal Repository @@ -68,7 +63,6 @@ ignore - wso2.releases WSO2 internal Repository @@ -79,7 +73,6 @@ ignore - wso2.snapshots Apache Snapshot Repository @@ -92,7 +85,6 @@ false - @@ -106,7 +98,6 @@ ignore - wso2.snapshots WSO2 Snapshot Repository @@ -213,7 +204,6 @@ - 8 8 @@ -223,6 +213,4 @@ 3.8.0 1.2.8 - - \ No newline at end of file diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultApprovalWorkflow.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultApprovalWorkflow.java index 6f72ceb..48de9a6 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultApprovalWorkflow.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultApprovalWorkflow.java @@ -6,24 +6,34 @@ import org.wso2.carbon.identity.workflow.mgt.exception.WorkflowException; import org.wso2.carbon.identity.workflow.mgt.exception.WorkflowRuntimeException; import org.wso2.carbon.identity.workflow.mgt.workflow.AbstractWorkflow; +import org.wso2.carbon.identity.workflow.mgt.workflow.TemplateInitializer; import org.wso2.carbon.identity.workflow.mgt.workflow.WorkFlowExecutor; import java.util.List; +/** + * The class that extends the AbstractWorkflow class. + */ public class DefaultApprovalWorkflow extends AbstractWorkflow { - public DefaultApprovalWorkflow(Class workFlowExecutorClass, String metaDataXML) + public DefaultApprovalWorkflow(Class templateInitializerClass, Class workFlowExecutorClass, String metaDataXML) throws WorkflowRuntimeException { - super(null, workFlowExecutorClass, metaDataXML); + super(templateInitializerClass, workFlowExecutorClass, metaDataXML); } + /** + *{@inheritDoc} + */ @Override protected InputData getInputData(ParameterMetaData parameterMetaData) { - return null; + return new InputData(); } + /** + *{@inheritDoc} + */ @Override public void deploy(List parameterList) throws WorkflowException { diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowEventRequest.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowEventRequest.java index 5a6caac..5444431 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowEventRequest.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowEventRequest.java @@ -6,6 +6,9 @@ import java.util.List; +/** + * Default Workflow Event Request service interface. + */ public interface DefaultWorkflowEventRequest { /** diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowEventRequestService.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowEventRequestService.java index e17f233..4395682 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowEventRequestService.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowEventRequestService.java @@ -17,12 +17,18 @@ import org.wso2.carbon.identity.workflow.mgt.exception.InternalWorkflowException; import org.wso2.carbon.identity.workflow.mgt.exception.WorkflowException; -import java.util.Collections; import java.util.List; import java.util.UUID; +/** + * Default Workflow Event Request service implementation. + */ public class DefaultWorkflowEventRequestService implements DefaultWorkflowEventRequest { + /** + * {@inheritDoc} + */ + @Override public void addApproversOfRequests(WorkflowRequest request, List parameterList) { WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); @@ -47,21 +53,13 @@ public void addApproversOfRequests(WorkflowRequest request, List para approverType = stepName[stepName.length - 1]; String approver = parameter.getParamValue(); - String[] approvers = approver.split(","); - if (approvers != null) { - List approverList = Collections.singletonList(approver); - String stepValue = WorkflowEngineConstants.ParameterName.USER_AND_ROLE_STEP + "-step-" + - currentStepValue + "-users"; - if (stepValue.equals(parameter.getqName())) { - for (String name : approverList) { - approverName = name; - } - } - stepValue = WorkflowEngineConstants.ParameterName.USER_AND_ROLE_STEP + "-step-" + - currentStepValue + "-roles"; - if (stepValue.equals(parameter.getqName())) { - for (String name : approverList) { + if (approver != null && !approver.isEmpty()) { + String[] approvers = approver.split("[,]", 0); + if (approvers != null) { + for (String name : approvers) { approverName = name; + workflowEventRequestDAO.addApproversOfRequest(taskId, eventId, workflowId, + approverType, approverName); } } } @@ -69,7 +67,6 @@ public void addApproversOfRequests(WorkflowRequest request, List para } } } - workflowEventRequestDAO.addApproversOfRequest(taskId, eventId, workflowId, approverType, approverName); } private String getRequestId(WorkflowRequest request) { @@ -85,6 +82,10 @@ private String getRequestId(WorkflowRequest request) { return (String) event; } + /** + * {@inheritDoc} + */ + @Override public String getWorkflowId(WorkflowRequest request) { WorkflowManagementService workflowManagementService = new WorkflowManagementServiceImpl(); @@ -102,6 +103,10 @@ public String getWorkflowId(WorkflowRequest request) { return workflowId; } + /** + * {@inheritDoc} + */ + @Override public List getAssociations(WorkflowRequest workflowRequest) { List associations; @@ -115,30 +120,50 @@ public List getAssociations(WorkflowRequest workflowRequest return associations; } + /** + * {@inheritDoc} + */ + @Override public String getApprovalOfRequest(String eventId) { WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); return workflowEventRequestDAO.getApproversOfRequest(eventId); } + /** + * {@inheritDoc} + */ + @Override public void deleteApprovalOfRequest(String taskId) { WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); workflowEventRequestDAO.deleteApproversOfRequest(taskId); } + /** + * {@inheritDoc} + */ + @Override public void createStatesOfRequest(String eventId, String workflowId, int currentStep) { WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); workflowEventRequestDAO.createStatesOfRequest(eventId, workflowId, currentStep); } + /** + * {@inheritDoc} + */ + @Override public int getStateOfRequest(String eventId, String workflowId) { WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); return workflowEventRequestDAO.getStateOfRequest(eventId, workflowId); } + /** + * {@inheritDoc} + */ + @Override public void updateStateOfRequest(String eventId, String workflowId) { WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowExecutor.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowExecutor.java index 6d09967..f5fc18c 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowExecutor.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowExecutor.java @@ -6,23 +6,35 @@ import java.util.List; +/** + * Implementation of Workflow Executor Interface. + */ public class DefaultWorkflowExecutor implements WorkFlowExecutor { List parameterList; private static final String EXECUTOR_NAME = "WorkflowEngineSimple"; + /** + *{@inheritDoc} + */ @Override public boolean canHandle(WorkflowRequest workflowRequest) { return true; } + /** + *{@inheritDoc} + */ @Override public void initialize(List parameterList) { this.parameterList = parameterList; } + /** + *{@inheritDoc} + */ @Override public void execute(WorkflowRequest request) { @@ -30,6 +42,9 @@ public void execute(WorkflowRequest request) { defaultWorkflowEventRequest.addApproversOfRequests(request, parameterList); } + /** + *{@inheritDoc} + */ @Override public String getName() { diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/dao/WorkflowEventRequestDAO.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/dao/WorkflowEventRequestDAO.java index d047da7..3a33e5c 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/dao/WorkflowEventRequestDAO.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/dao/WorkflowEventRequestDAO.java @@ -1,5 +1,8 @@ package org.wso2.carbon.identity.workflow.engine.dao; +/** + * Perform CRUD operations for workflow Event Request properties. + */ public interface WorkflowEventRequestDAO { /** @@ -63,4 +66,44 @@ public interface WorkflowEventRequestDAO { * @param currentStep the current step. */ void updateStateOfRequest(String eventId, String workflowId, int currentStep); + + /** + * Returns the workflow ID given the task ID. + * + * @param taskId random generated unique Id. + * @return workflow ID. + */ + String getWorkflowID(String taskId); + + /** + * Returns the request ID given the task ID. + * + * @param taskId random generated unique Id. + * @return request Id. + */ + String getRequestID(String taskId); + + /** + * Returns the initiator given the request ID. + * + * @param requestId the request ID that need to be checked. + * @return string initiator. + */ + String getInitiatedUser(String requestId); + + /** + * Returns the status of request given the request ID. + * + * @param requestId the request ID that need to be checked. + * @return string status of request. + */ + String getStatusOfRequest(String requestId); + + /** + * Returns the entity name given the request ID. + * + * @param requestId the request ID that need to be checked. + * @return string entity name. + */ + String getEntityName(String requestId); } diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/dao/impl/WorkflowEventRequestDAOImpl.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/dao/impl/WorkflowEventRequestDAOImpl.java index e41c60e..e000da5 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/dao/impl/WorkflowEventRequestDAOImpl.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/dao/impl/WorkflowEventRequestDAOImpl.java @@ -9,14 +9,27 @@ import java.sql.PreparedStatement; import java.sql.SQLException; +import java.util.Collections; +import java.util.List; import static org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants.APPROVER_NAME_COLUMN; +import static org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants.CREATED_BY_COLUMN; import static org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants.CURRENT_STEP_COLUMN; +import static org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants.ENTITY_NAME_COLUMN; +import static org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants.REQUEST_ID_COLUMN; +import static org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants.STATUS_COLUMN; import static org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants.SqlQueries.ADD_CURRENT_STEP_FOR_EVENT; import static org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants.TASK_ID_COLUMN; +import static org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants.WORKFLOW_ID_COLUMN; +/** + * Workflow Event Request DAO implementation. + */ public class WorkflowEventRequestDAOImpl implements WorkflowEventRequestDAO { + /** + *{@inheritDoc} + */ @Override public String addApproversOfRequest(String taskId, String eventId, String workflowId, String approverType, String approverName) { @@ -39,6 +52,9 @@ public String addApproversOfRequest(String taskId, String eventId, String workfl return taskId; } + /** + *{@inheritDoc} + */ public String getApproversOfRequest(String eventId) { JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); @@ -60,6 +76,9 @@ public String getApproversOfRequest(String eventId) { return taskIdExists; } + /** + *{@inheritDoc} + */ public void deleteApproversOfRequest(String taskId) { JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); @@ -72,6 +91,9 @@ public void deleteApproversOfRequest(String taskId) { } } + /** + *{@inheritDoc} + */ @Override public void createStatesOfRequest(String eventId, String workflowId, int currentStep) { @@ -90,6 +112,9 @@ public void createStatesOfRequest(String eventId, String workflowId, int current } } + /** + *{@inheritDoc} + */ @Override public int getStateOfRequest(String eventId, String workflowId) { @@ -114,6 +139,9 @@ public int getStateOfRequest(String eventId, String workflowId) { return Integer.parseInt(stepExists); } + /** + *{@inheritDoc} + */ public void updateStateOfRequest(String eventId, String workflowId, int currentStep) { JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); @@ -132,15 +160,18 @@ public void updateStateOfRequest(String eventId, String workflowId, int currentS } } + /** + *{@inheritDoc} + */ public String getApproversOfCurrentStep(String eventId) { JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); String approverName; try { - approverName = jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries. - GET_APPROVER_NAME_RELATED_TO_CURRENT_STEP, - ((resultSet, i) -> ( - resultSet.getString(APPROVER_NAME_COLUMN))), + approverName= jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries. + GET_APPROVER_NAME_RELATED_TO_CURRENT_STEP, + ((resultSet, i) -> ( + resultSet.getString(APPROVER_NAME_COLUMN))), preparedStatement -> preparedStatement.setString(1, eventId)); } catch (DataAccessException e) { String errorMessage = String.format("Error occurred while retrieving currentStep from" + @@ -150,6 +181,111 @@ public String getApproversOfCurrentStep(String eventId) { return approverName; } + /** + *{@inheritDoc} + */ + public String getWorkflowID(String taskId) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + String workflowId; + try { + workflowId = jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries. + GET_WORKFLOW_ID, + ((resultSet, i) -> ( + resultSet.getString(WORKFLOW_ID_COLUMN))), + preparedStatement -> preparedStatement.setString(1, taskId)); + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while retrieving currentStep from" + + "task Id: %s", taskId); + throw new WorkflowEngineRuntimeException(errorMessage); + } + return workflowId; + } + + /** + *{@inheritDoc} + */ + public String getRequestID(String taskId) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + String requestId; + try { + requestId = jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries. + GET_REQUEST_ID, + ((resultSet, i) -> ( + resultSet.getString(REQUEST_ID_COLUMN))), + preparedStatement -> preparedStatement.setString(1, taskId)); + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while retrieving currentStep from" + + "task Id: %s", taskId); + throw new WorkflowEngineRuntimeException(errorMessage); + } + return requestId; + } + + /** + *{@inheritDoc} + */ + public String getInitiatedUser(String requestId) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + String created_By; + try { + created_By = jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries. + GET_CREATED_USER, + ((resultSet, i) -> ( + resultSet.getString(CREATED_BY_COLUMN))), + preparedStatement -> preparedStatement.setString(1, requestId)); + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while retrieving currentStep from" + + "request Id: %s", requestId); + throw new WorkflowEngineRuntimeException(errorMessage); + } + return created_By; + } + + /** + *{@inheritDoc} + */ + public String getStatusOfRequest(String requestId) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + String status; + try { + status = jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries. + GET_STATES_OF_REQUEST, + ((resultSet, i) -> ( + resultSet.getString(STATUS_COLUMN))), + preparedStatement -> preparedStatement.setString(1, requestId)); + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while retrieving currentStep from" + + "request Id: %s", requestId); + throw new WorkflowEngineRuntimeException(errorMessage); + } + return status; + } + + /** + *{@inheritDoc} + */ + public String getEntityName(String requestId) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + String workflowId; + try { + workflowId = jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries. + GET_USER_DETAILS, + ((resultSet, i) -> ( + resultSet.getString(ENTITY_NAME_COLUMN))), + preparedStatement -> preparedStatement.setString(1, requestId)); + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while retrieving currentStep from" + + "request Id: %s", requestId); + throw new WorkflowEngineRuntimeException(errorMessage); + } + return workflowId; + } + private void setPreparedStatementForStateOfRequest(int currentStep, String eventId, String workflowId, PreparedStatement preparedStatement) throws SQLException { diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/exception/WorkflowEngineException.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/exception/WorkflowEngineException.java index d1c348b..3b000db 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/exception/WorkflowEngineException.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/exception/WorkflowEngineException.java @@ -1,5 +1,8 @@ package org.wso2.carbon.identity.workflow.engine.exception; +/** + * Exception class to handle workflow engine exceptions. + */ public class WorkflowEngineException extends java.lang.RuntimeException { public WorkflowEngineException(String message) { super(message); diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/exception/WorkflowEngineRuntimeException.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/exception/WorkflowEngineRuntimeException.java index aa89779..f2e59de 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/exception/WorkflowEngineRuntimeException.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/exception/WorkflowEngineRuntimeException.java @@ -2,6 +2,9 @@ import org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException; +/** + * Exception class to handle runtime exceptions. + */ public class WorkflowEngineRuntimeException extends RuntimeException { public WorkflowEngineRuntimeException(String message) { diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/internal/WorkflowEngineServiceComponent.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/internal/WorkflowEngineServiceComponent.java index ecab051..1c6ad1f 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/internal/WorkflowEngineServiceComponent.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/internal/WorkflowEngineServiceComponent.java @@ -12,16 +12,27 @@ import org.wso2.carbon.identity.workflow.mgt.WorkflowExecutorManagerService; import org.wso2.carbon.identity.workflow.mgt.WorkflowManagementService; import org.wso2.carbon.identity.workflow.mgt.workflow.AbstractWorkflow; +import org.wso2.carbon.identity.workflow.mgt.workflow.TemplateInitializer; +/** + * OSGi declarative services component which handles registration and un-registration of workflow engine management + * service. + */ @Component( name = "org.wso2.carbon.identity.workflow.engine", immediate = true) public class WorkflowEngineServiceComponent { + + /** + * Register Default Approval Workflow as an OSGi service. + * + * @param context OSGi service component context. + */ @Activate protected void activate(ComponentContext context) { BundleContext bundleContext = context.getBundleContext(); - bundleContext.registerService(AbstractWorkflow.class, new DefaultApprovalWorkflow(DefaultWorkflowExecutor.class, + bundleContext.registerService(AbstractWorkflow.class, new DefaultApprovalWorkflow(null, DefaultWorkflowExecutor.class, getMetaDataXML()), null); } @@ -60,6 +71,7 @@ protected void setWorkflowManagementService(WorkflowManagementService workflowMa } protected void unsetWorkflowManagementService(WorkflowManagementService workflowManagementService) { + WorkflowEngineServiceDataHolder.getInstance().setWorkflowManagementService(null); } @@ -70,10 +82,12 @@ protected void unsetWorkflowManagementService(WorkflowManagementService workflow policy = ReferencePolicy.DYNAMIC, unbind = "unsetWorkflowExecutorManagerService") protected void setWorkflowExecutorManagerService(WorkflowExecutorManagerService workflowExecutorManagerService) { + WorkflowEngineServiceDataHolder.getInstance().setWorkflowExecutorManagerService(workflowExecutorManagerService); } protected void unsetWorkflowExecutorManagerService(WorkflowExecutorManagerService workflowExecutorManagerService) { + WorkflowEngineServiceDataHolder.getInstance().setWorkflowExecutorManagerService(workflowExecutorManagerService); } } diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/internal/WorkflowEngineServiceDataHolder.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/internal/WorkflowEngineServiceDataHolder.java index 6dc492e..e71142e 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/internal/WorkflowEngineServiceDataHolder.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/internal/WorkflowEngineServiceDataHolder.java @@ -7,6 +7,9 @@ import java.util.HashMap; import java.util.Map; +/** + * A class to keep the data of the workflow engine component. + */ public class WorkflowEngineServiceDataHolder { private static WorkflowEngineServiceDataHolder instance = new WorkflowEngineServiceDataHolder(); @@ -25,11 +28,13 @@ public static WorkflowEngineServiceDataHolder getInstance() { } public WorkflowManagementService getWorkflowManagementService() { + return workflowManagementService; } public void setWorkflowManagementService( WorkflowManagementService workflowManagementService) { + this.workflowManagementService = workflowManagementService; } @@ -40,10 +45,14 @@ public WorkflowExecutorManagerService getWorkflowExecutorManagerService() { public void setWorkflowExecutorManagerService( WorkflowExecutorManagerService workflowExecutorManagerService) { + this.workflowExecutorManagerService = workflowExecutorManagerService; } - private Map> workflowImpls = new HashMap>(); + + private Map> workflowImpls = new HashMap>(); + public Map> getWorkflowImpls() { + return workflowImpls; } } diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/util/WorkflowEngineConstants.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/util/WorkflowEngineConstants.java index 675f46c..0614f87 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/util/WorkflowEngineConstants.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/util/WorkflowEngineConstants.java @@ -1,29 +1,40 @@ package org.wso2.carbon.identity.workflow.engine.util; +/** + * This class holds the constants used in the module, identity-workflow-simple. + */ public class WorkflowEngineConstants { public static final String CURRENT_STEP_COLUMN = "CURRENT_STEP"; public static final String TASK_ID_COLUMN = "TASK_ID"; public static final String APPROVER_NAME_COLUMN = "APPROVER_NAME"; + public static final String WORKFLOW_ID_COLUMN="WORKFLOW_ID"; + public static final String REQUEST_ID_COLUMN="REQUEST_ID"; + public static final String CREATED_BY_COLUMN="CREATED_BY"; + public static final String STATUS_COLUMN="STATUS"; + public static final String ENTITY_NAME_COLUMN="ENTITY_NAME"; public static class SqlQueries { public static final String ADD_APPROVAL_LIST_RELATED_TO_USER = "INSERT INTO WF_WORKFLOW_APPROVAL_RELATION (TASK_ID,EVENT_ID,WORKFLOW_ID,APPROVER_TYPE,APPROVER_NAME) VALUES (?,?,?,?,?)"; public static final String GET_TASK_ID_RELATED_TO_USER = "SELECT TASK_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE EVENT_ID = ?"; - public static final String GET_APPROVER_NAME_RELATED_TO_CURRENT_STEP = "SELECT APPROVER_NAME APPROVER_NAME_COLUMN FROM WF_WORKFLOW_APPROVAL_RELATION WHERE EVENT_ID = ?"; + public static final String GET_APPROVER_NAME_RELATED_TO_CURRENT_STEP = "SELECT APPROVER_TYPE, APPROVER_NAME FROM WF_WORKFLOW_APPROVAL_RELATION WHERE EVENT_ID = ?"; public static final String DELETE_APPROVAL_LIST_RELATED_TO_USER = "DELETE FROM WF_WORKFLOW_APPROVAL_RELATION WHERE TASK_ID=?"; public static final String ADD_CURRENT_STEP_FOR_EVENT = "INSERT INTO WF_WORKFLOW_APPROVAL_STATE (EVENT_ID,WORKFLOW_ID, CURRENT_STEP) VALUES (?,?,?)"; public static final String GET_CURRENT_STEP = "SELECT CURRENT_STEP FROM WF_WORKFLOW_APPROVAL_STATE WHERE EVENT_ID = ? AND WORKFLOW_ID = ?"; public static final String UPDATE_STATE_OF_REQUEST = "UPDATE WF_WORKFLOW_APPROVAL_STATE SET CURRENT_STEP=? WHERE EVENT_ID = ? AND WORKFLOW_ID = ?"; - } - - public enum EventState{ - PENDING, - APPROVED + public static final String GET_WORKFLOW_ID = "SELECT WORKFLOW_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE TASK_ID = ?"; + public static final String GET_REQUEST_ID = "SELECT EVENT_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE TASK_ID = ?"; + public static final String GET_CREATED_USER = "SELECT CREATED_BY FROM WF_REQUEST WHERE UUID= ?"; + public static final String GET_STATES_OF_REQUEST= "SELECT STATUS FROM WF_WORKFLOW_REQUEST_RELATION WHERE UUID= ?"; + public static final String GET_USER_DETAILS="SELECT ENTITY_NAME FROM WF_REQUEST_ENTITY_RELATIONSHIP WHERE REQUEST_ID"; } public static class ParameterName { public static final String USER_AND_ROLE_STEP = "UserAndRole" ; public static final String REQUEST_ID = "REQUEST ID" ; + public static final String TASK_SUBJECT = "HTSubject"; + public static final String TASK_DESCRIPTION ="HTDescription"; + public static final String PRIORITY= "0"; } } diff --git a/pom.xml b/pom.xml index 1ce6fa4..ffdfc44 100644 --- a/pom.xml +++ b/pom.xml @@ -15,7 +15,6 @@ pom WSO2 Carbon - Identity Workflow Simple Module - http://wso2.org @@ -31,7 +30,6 @@ components/org.wso2.carbon.identity.workflow.engine - @@ -109,7 +107,6 @@ org.wso2.carbon.user.mgt.common ${carbon.identity.framework.version} - org.wso2.carbon.identity.framework org.wso2.carbon.user.mgt @@ -130,7 +127,6 @@ org.apache.felix.scr.ds-annotations ${apache.felix.scr.ds.annotations.version} - @@ -143,5 +139,4 @@ 3.8.0 1.2.8 - \ No newline at end of file From 1efe7290e33c8d81b98ab903efb3afbb746943e8 Mon Sep 17 00:00:00 2001 From: IsharaSilva Date: Fri, 10 Jun 2022 13:45:19 +0530 Subject: [PATCH 03/16] Implement workflow approvalService --- components/org.wso2.carbon.identity.workflow.engine/pom.xml | 3 ++- .../DefaultWorkflowEventRequestService.java | 1 - .../dao/impl/WorkflowEventRequestDAOImpl.java | 2 +- .../exception/WorkflowEngineRuntimeException.java | 2 +- pom.xml | 6 ++++-- 5 files changed, 8 insertions(+), 6 deletions(-) diff --git a/components/org.wso2.carbon.identity.workflow.engine/pom.xml b/components/org.wso2.carbon.identity.workflow.engine/pom.xml index 6fd476a..1336f83 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/pom.xml +++ b/components/org.wso2.carbon.identity.workflow.engine/pom.xml @@ -213,4 +213,5 @@ 3.8.0 1.2.8 - \ No newline at end of file + + diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowEventRequestService.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowEventRequestService.java index 4395682..4b4f5a6 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowEventRequestService.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowEventRequestService.java @@ -173,4 +173,3 @@ public void updateStateOfRequest(String eventId, String workflowId) { workflowEventRequestDAO.updateStateOfRequest(eventId, workflowId, currentStep); } } - diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/dao/impl/WorkflowEventRequestDAOImpl.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/dao/impl/WorkflowEventRequestDAOImpl.java index e000da5..d723061 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/dao/impl/WorkflowEventRequestDAOImpl.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/dao/impl/WorkflowEventRequestDAOImpl.java @@ -293,4 +293,4 @@ private void setPreparedStatementForStateOfRequest(int currentStep, String event preparedStatement.setString(2, eventId); preparedStatement.setString(3, workflowId); } -} \ No newline at end of file +} diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/exception/WorkflowEngineRuntimeException.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/exception/WorkflowEngineRuntimeException.java index f2e59de..1d5be10 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/exception/WorkflowEngineRuntimeException.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/exception/WorkflowEngineRuntimeException.java @@ -1,6 +1,7 @@ package org.wso2.carbon.identity.workflow.engine.exception; import org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException; +import org.wso2.carbon.identity.workflow.mgt.exception.InternalWorkflowException; /** * Exception class to handle runtime exceptions. @@ -15,5 +16,4 @@ public WorkflowEngineRuntimeException(String message, DataAccessException e) { super(message); } - } diff --git a/pom.xml b/pom.xml index ffdfc44..c515015 100644 --- a/pom.xml +++ b/pom.xml @@ -89,6 +89,7 @@ + @@ -128,8 +129,8 @@ ${apache.felix.scr.ds.annotations.version} - + 8 8 @@ -139,4 +140,5 @@ 3.8.0 1.2.8 - \ No newline at end of file + + From 42e8139761d8a8b489f0edc139a9b522fb5f011a Mon Sep 17 00:00:00 2001 From: IsharaSilva Date: Tue, 21 Jun 2022 19:38:23 +0530 Subject: [PATCH 04/16] Implement workflow approvalService --- .../pom.xml | 283 +++++------------ .../dao/impl/WorkflowEventRequestDAOImpl.java | 296 ------------------ .../util/WorkflowEngineConstants.java | 40 --- .../engine}/DefaultApprovalWorkflow.java | 3 +- .../engine}/DefaultWorkflowEventRequest.java | 0 .../DefaultWorkflowEventRequestService.java | 13 +- .../engine}/DefaultWorkflowExecutor.java | 0 .../exception/WorkflowEngineException.java | 0 .../WorkflowEngineRuntimeException.java | 1 - .../WorkflowEngineServiceComponent.java | 14 +- .../WorkflowEngineServiceDataHolder.java | 13 + .../dao/WorkflowEventRequestDAO.java | 53 +--- .../dao/impl/WorkflowEventRequestDAOImpl.java | 162 ++++++++++ .../engine/util/WorkflowEngineConstants.java | 49 +++ 14 files changed, 321 insertions(+), 606 deletions(-) delete mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/dao/impl/WorkflowEventRequestDAOImpl.java delete mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/util/WorkflowEngineConstants.java rename components/org.wso2.carbon.identity.workflow.engine/src/main/java/{org.wso2.carbon.identity.workflow.engine => org/wso2/carbon/identity/workflow/engine}/DefaultApprovalWorkflow.java (90%) rename components/org.wso2.carbon.identity.workflow.engine/src/main/java/{org.wso2.carbon.identity.workflow.engine => org/wso2/carbon/identity/workflow/engine}/DefaultWorkflowEventRequest.java (100%) rename components/org.wso2.carbon.identity.workflow.engine/src/main/java/{org.wso2.carbon.identity.workflow.engine => org/wso2/carbon/identity/workflow/engine}/DefaultWorkflowEventRequestService.java (93%) rename components/org.wso2.carbon.identity.workflow.engine/src/main/java/{org.wso2.carbon.identity.workflow.engine => org/wso2/carbon/identity/workflow/engine}/DefaultWorkflowExecutor.java (100%) rename components/org.wso2.carbon.identity.workflow.engine/src/main/java/{org.wso2.carbon.identity.workflow.engine => org/wso2/carbon/identity/workflow/engine}/exception/WorkflowEngineException.java (100%) rename components/org.wso2.carbon.identity.workflow.engine/src/main/java/{org.wso2.carbon.identity.workflow.engine => org/wso2/carbon/identity/workflow/engine}/exception/WorkflowEngineRuntimeException.java (85%) rename components/org.wso2.carbon.identity.workflow.engine/src/main/java/{org.wso2.carbon.identity.workflow.engine => org/wso2/carbon/identity/workflow/engine}/internal/WorkflowEngineServiceComponent.java (90%) rename components/org.wso2.carbon.identity.workflow.engine/src/main/java/{org.wso2.carbon.identity.workflow.engine => org/wso2/carbon/identity/workflow/engine}/internal/WorkflowEngineServiceDataHolder.java (86%) rename components/org.wso2.carbon.identity.workflow.engine/src/main/java/{org.wso2.carbon.identity.workflow.engine => org/wso2/carbon/identity/workflow/engine/internal}/dao/WorkflowEventRequestDAO.java (56%) create mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImpl.java create mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java diff --git a/components/org.wso2.carbon.identity.workflow.engine/pom.xml b/components/org.wso2.carbon.identity.workflow.engine/pom.xml index 1336f83..7d9da20 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/pom.xml +++ b/components/org.wso2.carbon.identity.workflow.engine/pom.xml @@ -6,212 +6,83 @@ default.workflow.engine org.wso2.carbon.identity.workflow.engine - 1.0-SNAPSHOT - ../../pom.xml - + 1.0-SNAPSHOT + ../../pom.xml + + workflow.engine + bundle + WSO2 Carbon - Workflow Engine Simple - workflow.engine - bundle - WSO2 Carbon - Workflow Engine Simple - - - org.testng - testng - 6.9.10 - test - - - org.wso2.carbon.identity.framework - org.wso2.carbon.identity.configuration.mgt.core - 5.18.187 - - - org.wso2.carbon.identity.framework - org.wso2.carbon.user.mgt.common - 5.18.187 - - - org.wso2.carbon.identity.framework - org.wso2.carbon.user.mgt - 5.18.187 - - - org.wso2.carbon.identity.framework - org.wso2.carbon.identity.workflow.mgt - 5.18.187 - - - org.apache.ws.commons.axiom.wso2 - axiom - 1.2.11-wso2v16 - - - org.apache.felix - org.apache.felix.scr.ds-annotations - ${apache.felix.scr.ds.annotations.version} - - + + + org.wso2.carbon.identity.framework + org.wso2.carbon.identity.configuration.mgt.core + + + org.wso2.carbon.identity.framework + org.wso2.carbon.user.mgt.common + + + org.wso2.carbon.identity.framework + org.wso2.carbon.user.mgt + + + org.wso2.carbon.identity.framework + org.wso2.carbon.identity.workflow.mgt + + + org.apache.felix + org.apache.felix.scr.ds-annotations + + + org.wso2.carbon.identity.framework + org.wso2.carbon.user.mgt + + - - - wso2-nexus - WSO2 internal Repository - https://maven.wso2.org/nexus/content/groups/wso2-public/ - - true - daily - ignore - - - - wso2.releases - WSO2 internal Repository - https://maven.wso2.org/nexus/content/repositories/releases/ - - true - daily - ignore - - - - wso2.snapshots - Apache Snapshot Repository - https://maven.wso2.org/nexus/content/repositories/snapshots/ - - true - daily - - - false - - - + + + + org.apache.felix + maven-bundle-plugin + 3.2.0 + true + + NONE + + ${buildNumber} + + org.wso2.carbon.identity.workflow.engine.internal, + + + !org.wso2.carbon.identity.workflow.engine.internal, + org.wso2.carbon.identity.workflow.engine.*; + + + + + + org.codehaus.mojo + findbugs-maven-plugin + ${maven.findbugsplugin.version} + + findbugs-exclude.xml + Max + Low + true + ${project.build.directory}/findbugs + + + + analyze-compile + compile + + check + + + + + + - - - wso2.releases - WSO2 internal Repository - https://maven.wso2.org/nexus/content/repositories/releases/ - - true - daily - ignore - - - - wso2.snapshots - WSO2 Snapshot Repository - https://maven.wso2.org/nexus/content/repositories/snapshots/ - - true - daily - - - false - - - - wso2-nexus - WSO2 internal Repository - https://maven.wso2.org/nexus/content/groups/wso2-public/ - - true - daily - ignore - - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - ${maven.compiler.plugin.version} - true - - UTF-8 - 1.8 - 1.8 - - - - org.apache.felix - maven-bundle-plugin - 3.2.0 - true - - NONE - - ${buildNumber} - - org.wso2.carbon.identity.workflow.engine.internal, - - - !org.wso2.carbon.identity.workflow.engine.internal, - org.wso2.carbon.identity.workflow.engine.*; - - - - - - org.apache.maven.plugins - maven-checkstyle-plugin - ${maven.checkstyleplugin.version} - - - validate - validate - - - https://raw.githubusercontent.com/wso2/code-quality-tools/v1.3/checkstyle/checkstyle.xml - - - https://raw.githubusercontent.com/wso2/code-quality-tools/v1.3/checkstyle/suppressions.xml - - UTF-8 - true - true - true - - - check - - - - - - org.codehaus.mojo - findbugs-maven-plugin - ${maven.findbugsplugin.version} - - findbugs-exclude.xml - Max - Low - true - ${project.build.directory}/findbugs - - - - analyze-compile - compile - - check - - - - - - - - - 8 - 8 - 1.4 - 3.1.0 - 3.0.5 - 3.8.0 - 1.2.8 - - - + diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/dao/impl/WorkflowEventRequestDAOImpl.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/dao/impl/WorkflowEventRequestDAOImpl.java deleted file mode 100644 index d723061..0000000 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/dao/impl/WorkflowEventRequestDAOImpl.java +++ /dev/null @@ -1,296 +0,0 @@ -package org.wso2.carbon.identity.workflow.engine.dao.impl; - -import org.wso2.carbon.database.utils.jdbc.JdbcTemplate; -import org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException; -import org.wso2.carbon.identity.configuration.mgt.core.util.JdbcUtils; -import org.wso2.carbon.identity.workflow.engine.dao.WorkflowEventRequestDAO; -import org.wso2.carbon.identity.workflow.engine.exception.WorkflowEngineRuntimeException; -import org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants; - -import java.sql.PreparedStatement; -import java.sql.SQLException; -import java.util.Collections; -import java.util.List; - -import static org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants.APPROVER_NAME_COLUMN; -import static org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants.CREATED_BY_COLUMN; -import static org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants.CURRENT_STEP_COLUMN; -import static org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants.ENTITY_NAME_COLUMN; -import static org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants.REQUEST_ID_COLUMN; -import static org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants.STATUS_COLUMN; -import static org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants.SqlQueries.ADD_CURRENT_STEP_FOR_EVENT; -import static org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants.TASK_ID_COLUMN; -import static org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants.WORKFLOW_ID_COLUMN; - -/** - * Workflow Event Request DAO implementation. - */ -public class WorkflowEventRequestDAOImpl implements WorkflowEventRequestDAO { - - /** - *{@inheritDoc} - */ - @Override - public String addApproversOfRequest(String taskId, String eventId, String workflowId, String approverType, - String approverName) { - - JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); - try { - jdbcTemplate.executeUpdate(WorkflowEngineConstants.SqlQueries.ADD_APPROVAL_LIST_RELATED_TO_USER, - preparedStatement -> { - preparedStatement.setString(1, taskId); - preparedStatement.setString(2, eventId); - preparedStatement.setString(3, workflowId); - preparedStatement.setString(4, approverType); - preparedStatement.setString(5, approverName); - }); - } catch (DataAccessException e) { - String errorMessage = String.format("Error occurred while adding request details" + - "in eventId: %s & workflowId: %s", eventId, workflowId); - throw new WorkflowEngineRuntimeException(errorMessage); - } - return taskId; - } - - /** - *{@inheritDoc} - */ - public String getApproversOfRequest(String eventId) { - - JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); - String taskIdExists; - try { - taskIdExists = jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries. - GET_TASK_ID_RELATED_TO_USER, - ((resultSet, i) -> ( - resultSet.getString(TASK_ID_COLUMN))), - preparedStatement -> preparedStatement.setString(1, eventId)); - if (taskIdExists == null) { - return null; - } - } catch (DataAccessException e) { - String errorMessage = String.format("Error occurred while retrieving taskId from" + - "task Id: %s", eventId); - throw new WorkflowEngineRuntimeException(errorMessage); - } - return taskIdExists; - } - - /** - *{@inheritDoc} - */ - public void deleteApproversOfRequest(String taskId) { - - JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); - try { - jdbcTemplate.executeUpdate(WorkflowEngineConstants.SqlQueries.DELETE_APPROVAL_LIST_RELATED_TO_USER, - preparedStatement -> preparedStatement.setString(1, taskId)); - } catch (DataAccessException e) { - String errorMessage = String.format("Error while deleting the approver details from taskId:%s", taskId); - throw new WorkflowEngineRuntimeException(errorMessage); - } - } - - /** - *{@inheritDoc} - */ - @Override - public void createStatesOfRequest(String eventId, String workflowId, int currentStep) { - - JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); - try { - jdbcTemplate.executeUpdate(ADD_CURRENT_STEP_FOR_EVENT, - preparedStatement -> { - preparedStatement.setString(1, eventId); - preparedStatement.setString(2, workflowId); - preparedStatement.setInt(3, currentStep); - }); - } catch (DataAccessException e) { - String errorMessage = String.format("Error occurred while adding request approval steps" + - "in event Id: %s & workflowId: %s", eventId, workflowId); - throw new WorkflowEngineRuntimeException(errorMessage); - } - } - - /** - *{@inheritDoc} - */ - @Override - public int getStateOfRequest(String eventId, String workflowId) { - - JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); - String stepExists; - try { - stepExists = jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries.GET_CURRENT_STEP, - ((resultSet, i) -> ( - Integer.toString(resultSet.getInt(CURRENT_STEP_COLUMN)))), - preparedStatement -> { - preparedStatement.setString(1, eventId); - preparedStatement.setString(2, workflowId); - }); - if (stepExists == null) { - return 0; - } - } catch (DataAccessException e) { - String errorMessage = String.format("Error occurred while retrieving currentStep from" + - "event Id: %s", eventId); - throw new WorkflowEngineRuntimeException(errorMessage); - } - return Integer.parseInt(stepExists); - } - - /** - *{@inheritDoc} - */ - public void updateStateOfRequest(String eventId, String workflowId, int currentStep) { - - JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); - try { - jdbcTemplate.executeUpdate(WorkflowEngineConstants.SqlQueries.UPDATE_STATE_OF_REQUEST, - (preparedStatement -> { - setPreparedStatementForStateOfRequest(currentStep, eventId, workflowId, preparedStatement); - preparedStatement.setInt(1, currentStep); - preparedStatement.setString(2, eventId); - preparedStatement.setString(3, workflowId); - })); - } catch (DataAccessException e) { - String errorMessage = String.format("Error occurred while updating state from" + - "eventIs:%s", eventId); - throw new WorkflowEngineRuntimeException(errorMessage); - } - } - - /** - *{@inheritDoc} - */ - public String getApproversOfCurrentStep(String eventId) { - - JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); - String approverName; - try { - approverName= jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries. - GET_APPROVER_NAME_RELATED_TO_CURRENT_STEP, - ((resultSet, i) -> ( - resultSet.getString(APPROVER_NAME_COLUMN))), - preparedStatement -> preparedStatement.setString(1, eventId)); - } catch (DataAccessException e) { - String errorMessage = String.format("Error occurred while retrieving currentStep from" + - "event Id: %s", eventId); - throw new WorkflowEngineRuntimeException(errorMessage); - } - return approverName; - } - - /** - *{@inheritDoc} - */ - public String getWorkflowID(String taskId) { - - JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); - String workflowId; - try { - workflowId = jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries. - GET_WORKFLOW_ID, - ((resultSet, i) -> ( - resultSet.getString(WORKFLOW_ID_COLUMN))), - preparedStatement -> preparedStatement.setString(1, taskId)); - } catch (DataAccessException e) { - String errorMessage = String.format("Error occurred while retrieving currentStep from" + - "task Id: %s", taskId); - throw new WorkflowEngineRuntimeException(errorMessage); - } - return workflowId; - } - - /** - *{@inheritDoc} - */ - public String getRequestID(String taskId) { - - JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); - String requestId; - try { - requestId = jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries. - GET_REQUEST_ID, - ((resultSet, i) -> ( - resultSet.getString(REQUEST_ID_COLUMN))), - preparedStatement -> preparedStatement.setString(1, taskId)); - } catch (DataAccessException e) { - String errorMessage = String.format("Error occurred while retrieving currentStep from" + - "task Id: %s", taskId); - throw new WorkflowEngineRuntimeException(errorMessage); - } - return requestId; - } - - /** - *{@inheritDoc} - */ - public String getInitiatedUser(String requestId) { - - JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); - String created_By; - try { - created_By = jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries. - GET_CREATED_USER, - ((resultSet, i) -> ( - resultSet.getString(CREATED_BY_COLUMN))), - preparedStatement -> preparedStatement.setString(1, requestId)); - } catch (DataAccessException e) { - String errorMessage = String.format("Error occurred while retrieving currentStep from" + - "request Id: %s", requestId); - throw new WorkflowEngineRuntimeException(errorMessage); - } - return created_By; - } - - /** - *{@inheritDoc} - */ - public String getStatusOfRequest(String requestId) { - - JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); - String status; - try { - status = jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries. - GET_STATES_OF_REQUEST, - ((resultSet, i) -> ( - resultSet.getString(STATUS_COLUMN))), - preparedStatement -> preparedStatement.setString(1, requestId)); - } catch (DataAccessException e) { - String errorMessage = String.format("Error occurred while retrieving currentStep from" + - "request Id: %s", requestId); - throw new WorkflowEngineRuntimeException(errorMessage); - } - return status; - } - - /** - *{@inheritDoc} - */ - public String getEntityName(String requestId) { - - JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); - String workflowId; - try { - workflowId = jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries. - GET_USER_DETAILS, - ((resultSet, i) -> ( - resultSet.getString(ENTITY_NAME_COLUMN))), - preparedStatement -> preparedStatement.setString(1, requestId)); - } catch (DataAccessException e) { - String errorMessage = String.format("Error occurred while retrieving currentStep from" + - "request Id: %s", requestId); - throw new WorkflowEngineRuntimeException(errorMessage); - } - return workflowId; - } - - private void setPreparedStatementForStateOfRequest(int currentStep, String eventId, String workflowId, - PreparedStatement preparedStatement) throws SQLException { - - preparedStatement.setInt(1, currentStep); - preparedStatement.setString(2, eventId); - preparedStatement.setString(3, workflowId); - } -} diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/util/WorkflowEngineConstants.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/util/WorkflowEngineConstants.java deleted file mode 100644 index 0614f87..0000000 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/util/WorkflowEngineConstants.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.wso2.carbon.identity.workflow.engine.util; - -/** - * This class holds the constants used in the module, identity-workflow-simple. - */ -public class WorkflowEngineConstants { - - public static final String CURRENT_STEP_COLUMN = "CURRENT_STEP"; - public static final String TASK_ID_COLUMN = "TASK_ID"; - public static final String APPROVER_NAME_COLUMN = "APPROVER_NAME"; - public static final String WORKFLOW_ID_COLUMN="WORKFLOW_ID"; - public static final String REQUEST_ID_COLUMN="REQUEST_ID"; - public static final String CREATED_BY_COLUMN="CREATED_BY"; - public static final String STATUS_COLUMN="STATUS"; - public static final String ENTITY_NAME_COLUMN="ENTITY_NAME"; - - public static class SqlQueries { - - public static final String ADD_APPROVAL_LIST_RELATED_TO_USER = "INSERT INTO WF_WORKFLOW_APPROVAL_RELATION (TASK_ID,EVENT_ID,WORKFLOW_ID,APPROVER_TYPE,APPROVER_NAME) VALUES (?,?,?,?,?)"; - public static final String GET_TASK_ID_RELATED_TO_USER = "SELECT TASK_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE EVENT_ID = ?"; - public static final String GET_APPROVER_NAME_RELATED_TO_CURRENT_STEP = "SELECT APPROVER_TYPE, APPROVER_NAME FROM WF_WORKFLOW_APPROVAL_RELATION WHERE EVENT_ID = ?"; - public static final String DELETE_APPROVAL_LIST_RELATED_TO_USER = "DELETE FROM WF_WORKFLOW_APPROVAL_RELATION WHERE TASK_ID=?"; - public static final String ADD_CURRENT_STEP_FOR_EVENT = "INSERT INTO WF_WORKFLOW_APPROVAL_STATE (EVENT_ID,WORKFLOW_ID, CURRENT_STEP) VALUES (?,?,?)"; - public static final String GET_CURRENT_STEP = "SELECT CURRENT_STEP FROM WF_WORKFLOW_APPROVAL_STATE WHERE EVENT_ID = ? AND WORKFLOW_ID = ?"; - public static final String UPDATE_STATE_OF_REQUEST = "UPDATE WF_WORKFLOW_APPROVAL_STATE SET CURRENT_STEP=? WHERE EVENT_ID = ? AND WORKFLOW_ID = ?"; - public static final String GET_WORKFLOW_ID = "SELECT WORKFLOW_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE TASK_ID = ?"; - public static final String GET_REQUEST_ID = "SELECT EVENT_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE TASK_ID = ?"; - public static final String GET_CREATED_USER = "SELECT CREATED_BY FROM WF_REQUEST WHERE UUID= ?"; - public static final String GET_STATES_OF_REQUEST= "SELECT STATUS FROM WF_WORKFLOW_REQUEST_RELATION WHERE UUID= ?"; - public static final String GET_USER_DETAILS="SELECT ENTITY_NAME FROM WF_REQUEST_ENTITY_RELATIONSHIP WHERE REQUEST_ID"; - } - - public static class ParameterName { - public static final String USER_AND_ROLE_STEP = "UserAndRole" ; - public static final String REQUEST_ID = "REQUEST ID" ; - public static final String TASK_SUBJECT = "HTSubject"; - public static final String TASK_DESCRIPTION ="HTDescription"; - public static final String PRIORITY= "0"; - } -} diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultApprovalWorkflow.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultApprovalWorkflow.java similarity index 90% rename from components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultApprovalWorkflow.java rename to components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultApprovalWorkflow.java index 48de9a6..2bc5418 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultApprovalWorkflow.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultApprovalWorkflow.java @@ -16,7 +16,8 @@ */ public class DefaultApprovalWorkflow extends AbstractWorkflow { - public DefaultApprovalWorkflow(Class templateInitializerClass, Class workFlowExecutorClass, String metaDataXML) + public DefaultApprovalWorkflow(Class templateInitializerClass, + Class workFlowExecutorClass, String metaDataXML) throws WorkflowRuntimeException { super(templateInitializerClass, workFlowExecutorClass, metaDataXML); diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowEventRequest.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultWorkflowEventRequest.java similarity index 100% rename from components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowEventRequest.java rename to components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultWorkflowEventRequest.java diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowEventRequestService.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultWorkflowEventRequestService.java similarity index 93% rename from components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowEventRequestService.java rename to components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultWorkflowEventRequestService.java index 4b4f5a6..45d6c69 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowEventRequestService.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultWorkflowEventRequestService.java @@ -1,7 +1,5 @@ package org.wso2.carbon.identity.workflow.engine; -import org.wso2.carbon.identity.workflow.engine.dao.WorkflowEventRequestDAO; -import org.wso2.carbon.identity.workflow.engine.dao.impl.WorkflowEventRequestDAOImpl; import org.wso2.carbon.identity.workflow.engine.exception.WorkflowEngineException; import org.wso2.carbon.identity.workflow.engine.exception.WorkflowEngineRuntimeException; import org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants; @@ -16,6 +14,8 @@ import org.wso2.carbon.identity.workflow.mgt.dto.WorkflowRequest; import org.wso2.carbon.identity.workflow.mgt.exception.InternalWorkflowException; import org.wso2.carbon.identity.workflow.mgt.exception.WorkflowException; +import org.wso2.carbon.identity.workflow.engine.internal.dao.WorkflowEventRequestDAO; +import org.wso2.carbon.identity.workflow.engine.internal.dao.impl.WorkflowEventRequestDAOImpl; import java.util.List; import java.util.UUID; @@ -35,8 +35,8 @@ public void addApproversOfRequests(WorkflowRequest request, List para String taskId = UUID.randomUUID().toString(); String eventId = getRequestId(request); String workflowId = getWorkflowId(request); - String approverType = null; - String approverName = null; + String approverType; + String approverName; int currentStepValue = getStateOfRequest(eventId, workflowId); if (currentStepValue == 0) { createStatesOfRequest(eventId, workflowId, currentStepValue); @@ -58,8 +58,9 @@ public void addApproversOfRequests(WorkflowRequest request, List para if (approvers != null) { for (String name : approvers) { approverName = name; + String taskStatus= WorkflowEngineConstants.ParameterName.TASK_STATUS_DEFAULT; workflowEventRequestDAO.addApproversOfRequest(taskId, eventId, workflowId, - approverType, approverName); + approverType, approverName,taskStatus); } } } @@ -126,7 +127,7 @@ public List getAssociations(WorkflowRequest workflowRequest @Override public String getApprovalOfRequest(String eventId) { - WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); + WorkflowEventRequestDAO workflowEventRequestDAO=new WorkflowEventRequestDAOImpl(); return workflowEventRequestDAO.getApproversOfRequest(eventId); } diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowExecutor.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultWorkflowExecutor.java similarity index 100% rename from components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/DefaultWorkflowExecutor.java rename to components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultWorkflowExecutor.java diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/exception/WorkflowEngineException.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/exception/WorkflowEngineException.java similarity index 100% rename from components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/exception/WorkflowEngineException.java rename to components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/exception/WorkflowEngineException.java diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/exception/WorkflowEngineRuntimeException.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/exception/WorkflowEngineRuntimeException.java similarity index 85% rename from components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/exception/WorkflowEngineRuntimeException.java rename to components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/exception/WorkflowEngineRuntimeException.java index 1d5be10..ffb7764 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/exception/WorkflowEngineRuntimeException.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/exception/WorkflowEngineRuntimeException.java @@ -1,7 +1,6 @@ package org.wso2.carbon.identity.workflow.engine.exception; import org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException; -import org.wso2.carbon.identity.workflow.mgt.exception.InternalWorkflowException; /** * Exception class to handle runtime exceptions. diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/internal/WorkflowEngineServiceComponent.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/WorkflowEngineServiceComponent.java similarity index 90% rename from components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/internal/WorkflowEngineServiceComponent.java rename to components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/WorkflowEngineServiceComponent.java index 1c6ad1f..7230a7b 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/internal/WorkflowEngineServiceComponent.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/WorkflowEngineServiceComponent.java @@ -7,19 +7,19 @@ import org.osgi.service.component.annotations.Reference; import org.osgi.service.component.annotations.ReferenceCardinality; import org.osgi.service.component.annotations.ReferencePolicy; +import org.wso2.carbon.identity.workflow.engine.ApprovalEventService; import org.wso2.carbon.identity.workflow.engine.DefaultApprovalWorkflow; -import org.wso2.carbon.identity.workflow.engine.DefaultWorkflowExecutor; import org.wso2.carbon.identity.workflow.mgt.WorkflowExecutorManagerService; import org.wso2.carbon.identity.workflow.mgt.WorkflowManagementService; import org.wso2.carbon.identity.workflow.mgt.workflow.AbstractWorkflow; -import org.wso2.carbon.identity.workflow.mgt.workflow.TemplateInitializer; +import org.wso2.carbon.identity.workflow.engine.DefaultWorkflowExecutor; /** * OSGi declarative services component which handles registration and un-registration of workflow engine management * service. */ @Component( - name = "org.wso2.carbon.identity.workflow.engine", + name = "simple.workflow.engine", immediate = true) public class WorkflowEngineServiceComponent { @@ -32,15 +32,17 @@ public class WorkflowEngineServiceComponent { protected void activate(ComponentContext context) { BundleContext bundleContext = context.getBundleContext(); - bundleContext.registerService(AbstractWorkflow.class, new DefaultApprovalWorkflow(null, DefaultWorkflowExecutor.class, - getMetaDataXML()), null); + bundleContext.registerService(AbstractWorkflow.class, new DefaultApprovalWorkflow(null, + DefaultWorkflowExecutor.class, getMetaDataXML()), null); + ApprovalEventService approvalEventService=new ApprovalEventService(); + bundleContext.registerService(ApprovalEventService.class, approvalEventService, null); } private String getMetaDataXML() { return "\n" + "\n" + - " newWorkflowImpl\n" + + " workflowImplSimple\n" + " ApprovalWorkflow\n" + " Approval Workflow\n" + " MultiStepApprovalTemplate\n" + diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/internal/WorkflowEngineServiceDataHolder.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/WorkflowEngineServiceDataHolder.java similarity index 86% rename from components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/internal/WorkflowEngineServiceDataHolder.java rename to components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/WorkflowEngineServiceDataHolder.java index e71142e..188de1e 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/internal/WorkflowEngineServiceDataHolder.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/WorkflowEngineServiceDataHolder.java @@ -1,5 +1,6 @@ package org.wso2.carbon.identity.workflow.engine.internal; +import org.osgi.framework.BundleContext; import org.wso2.carbon.identity.workflow.mgt.WorkflowExecutorManagerService; import org.wso2.carbon.identity.workflow.mgt.WorkflowManagementService; import org.wso2.carbon.identity.workflow.mgt.workflow.AbstractWorkflow; @@ -18,6 +19,8 @@ public class WorkflowEngineServiceDataHolder { private WorkflowExecutorManagerService workflowExecutorManagerService; + private BundleContext bundleContext; + private WorkflowEngineServiceDataHolder() { } @@ -32,6 +35,16 @@ public WorkflowManagementService getWorkflowManagementService() { return workflowManagementService; } + public BundleContext getBundleContext() { + + return bundleContext; + } + + public void setBundleContext(BundleContext bundleContext) { + + this.bundleContext = bundleContext; + } + public void setWorkflowManagementService( WorkflowManagementService workflowManagementService) { diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/dao/WorkflowEventRequestDAO.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/WorkflowEventRequestDAO.java similarity index 56% rename from components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/dao/WorkflowEventRequestDAO.java rename to components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/WorkflowEventRequestDAO.java index 3a33e5c..325bb2b 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org.wso2.carbon.identity.workflow.engine/dao/WorkflowEventRequestDAO.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/WorkflowEventRequestDAO.java @@ -1,4 +1,4 @@ -package org.wso2.carbon.identity.workflow.engine.dao; +package org.wso2.carbon.identity.workflow.engine.internal.dao; /** * Perform CRUD operations for workflow Event Request properties. @@ -13,9 +13,10 @@ public interface WorkflowEventRequestDAO { * @param workflowId workflow ID. * @param approverType the type of the approved user EX: user or Role. * @param approverName the value of the approver type. + * @param taskStatus state of the tasks [RESERVED, READY or COMPLETED]. * @return event ID. */ - String addApproversOfRequest(String taskId, String eventId, String workflowId, String approverType, String approverName); + String addApproversOfRequest(String taskId, String eventId, String workflowId, String approverType, String approverName, String taskStatus); /** * Get taskId from table. @@ -25,14 +26,6 @@ public interface WorkflowEventRequestDAO { */ String getApproversOfRequest(String eventId); - /** - * Get approverName related to currentStep. - * - * @param eventId the request ID that need to be checked. - * @return the value of the approver type. - */ - String getApproversOfCurrentStep(String eventId); - /** * Delete approver details using task Id. * @@ -66,44 +59,4 @@ public interface WorkflowEventRequestDAO { * @param currentStep the current step. */ void updateStateOfRequest(String eventId, String workflowId, int currentStep); - - /** - * Returns the workflow ID given the task ID. - * - * @param taskId random generated unique Id. - * @return workflow ID. - */ - String getWorkflowID(String taskId); - - /** - * Returns the request ID given the task ID. - * - * @param taskId random generated unique Id. - * @return request Id. - */ - String getRequestID(String taskId); - - /** - * Returns the initiator given the request ID. - * - * @param requestId the request ID that need to be checked. - * @return string initiator. - */ - String getInitiatedUser(String requestId); - - /** - * Returns the status of request given the request ID. - * - * @param requestId the request ID that need to be checked. - * @return string status of request. - */ - String getStatusOfRequest(String requestId); - - /** - * Returns the entity name given the request ID. - * - * @param requestId the request ID that need to be checked. - * @return string entity name. - */ - String getEntityName(String requestId); } diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImpl.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImpl.java new file mode 100644 index 0000000..7f1c8f2 --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImpl.java @@ -0,0 +1,162 @@ +package org.wso2.carbon.identity.workflow.engine.internal.dao.impl; + +import org.wso2.carbon.database.utils.jdbc.JdbcTemplate; +import org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException; +import org.wso2.carbon.identity.configuration.mgt.core.util.JdbcUtils; +import org.wso2.carbon.identity.workflow.engine.exception.WorkflowEngineRuntimeException; +import org.wso2.carbon.identity.workflow.engine.internal.dao.WorkflowEventRequestDAO; +import org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants; + +import java.sql.PreparedStatement; +import java.sql.SQLException; + +/** + * Workflow Event Request DAO implementation. + */ +public class WorkflowEventRequestDAOImpl implements WorkflowEventRequestDAO { + + /** + * {@inheritDoc} + */ + @Override + public String addApproversOfRequest(String taskId, String eventId, String workflowId, String approverType, + String approverName, String taskStatus) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + try { + jdbcTemplate.executeUpdate(WorkflowEngineConstants.SqlQueries.ADD_APPROVAL_LIST_RELATED_TO_USER, + preparedStatement -> { + preparedStatement.setString(1, taskId); + preparedStatement.setString(2, eventId); + preparedStatement.setString(3, workflowId); + preparedStatement.setString(4, approverType); + preparedStatement.setString(5, approverName); + preparedStatement.setString(6, taskStatus); + }); + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while adding request details" + + "in eventId: %s & workflowId: %s", eventId, workflowId); + throw new WorkflowEngineRuntimeException(errorMessage); + } + return taskId; + } + + /** + *{@inheritDoc} + */ + @Override + public String getApproversOfRequest(String eventId) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + String taskIdExists; + try { + taskIdExists = jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries. + GET_TASK_ID_RELATED_TO_USER, + ((resultSet, i) -> ( + resultSet.getString(WorkflowEngineConstants.TASK_ID_COLUMN))), + preparedStatement -> preparedStatement.setString(1, eventId)); + if (taskIdExists == null) { + return null; + } + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while retrieving taskId from" + + "requestId: %s", eventId); + throw new WorkflowEngineRuntimeException(errorMessage); + } + return taskIdExists; + } + + /** + *{@inheritDoc} + */ + @Override + public void deleteApproversOfRequest(String taskId) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + try { + jdbcTemplate.executeUpdate(WorkflowEngineConstants.SqlQueries.DELETE_APPROVAL_LIST_RELATED_TO_USER, + preparedStatement -> preparedStatement.setString(1, taskId)); + } catch (DataAccessException e) { + String errorMessage = String.format("Error while deleting the approver details from taskId:%s", taskId); + throw new WorkflowEngineRuntimeException(errorMessage); + } + } + + /** + *{@inheritDoc} + */ + @Override + public void createStatesOfRequest(String eventId, String workflowId, int currentStep) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + try { + jdbcTemplate.executeUpdate(WorkflowEngineConstants.SqlQueries.ADD_CURRENT_STEP_FOR_EVENT, + preparedStatement -> { + preparedStatement.setString(1, eventId); + preparedStatement.setString(2, workflowId); + preparedStatement.setInt(3, currentStep); + }); + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while adding request approval steps" + + "in event Id: %s & workflowId: %s", eventId, workflowId); + throw new WorkflowEngineRuntimeException(errorMessage); + } + } + + /** + * {@inheritDoc} + */ + @Override + public int getStateOfRequest(String eventId, String workflowId) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + String stepExists; + try { + stepExists = jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries.GET_CURRENT_STEP, + ((resultSet, i) -> ( + Integer.toString(resultSet.getInt(WorkflowEngineConstants.CURRENT_STEP_COLUMN)))), + preparedStatement -> { + preparedStatement.setString(1, eventId); + preparedStatement.setString(2, workflowId); + }); + if (stepExists == null) { + return 0; + } + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while retrieving currentStep from" + + "event Id: %s & workflowId: %s", eventId); + throw new WorkflowEngineRuntimeException(errorMessage); + } + return Integer.parseInt(stepExists); + } + + /** + *{@inheritDoc} + */ + @Override + public void updateStateOfRequest(String eventId, String workflowId, int currentStep) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + try { + jdbcTemplate.executeUpdate(WorkflowEngineConstants.SqlQueries.UPDATE_STATE_OF_REQUEST, + (preparedStatement -> { + setPreparedStatementForStateOfRequest(currentStep, eventId, workflowId, preparedStatement); + preparedStatement.setInt(1, currentStep); + preparedStatement.setString(2, eventId); + preparedStatement.setString(3, workflowId); + })); + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while updating state from" + + "eventId: %s", eventId); + throw new WorkflowEngineRuntimeException(errorMessage); + } + } + + private void setPreparedStatementForStateOfRequest(int currentStep, String eventId, String workflowId, + PreparedStatement preparedStatement) throws SQLException { + + preparedStatement.setInt(1, currentStep); + preparedStatement.setString(2, eventId); + preparedStatement.setString(3, workflowId); + } +} diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java new file mode 100644 index 0000000..272d868 --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java @@ -0,0 +1,49 @@ +package org.wso2.carbon.identity.workflow.engine.util; + +/** + * This class holds the constants used in the module, identity-workflow-simple. + */ +public class WorkflowEngineConstants { + + public static final String CURRENT_STEP_COLUMN = "CURRENT_STEP"; + public static final String TASK_ID_COLUMN = "TASK_ID"; + + public static class SqlQueries { + + public static final String ADD_APPROVAL_LIST_RELATED_TO_USER = "INSERT INTO WF_WORKFLOW_APPROVAL_RELATION (TASK_ID,EVENT_ID,WORKFLOW_ID,APPROVER_TYPE,APPROVER_NAME, TASK_STATUS) VALUES (?,?,?,?,?,?)"; + public static final String GET_TASK_ID_RELATED_TO_USER = "SELECT DISTINCT TASK_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE EVENT_ID = ?"; + public static final String DELETE_APPROVAL_LIST_RELATED_TO_USER = "DELETE FROM WF_WORKFLOW_APPROVAL_RELATION WHERE TASK_ID=?"; + public static final String ADD_CURRENT_STEP_FOR_EVENT = "INSERT INTO WF_WORKFLOW_APPROVAL_STATE (EVENT_ID,WORKFLOW_ID, CURRENT_STEP) VALUES (?,?,?)"; + public static final String GET_CURRENT_STEP = "SELECT CURRENT_STEP FROM WF_WORKFLOW_APPROVAL_STATE WHERE EVENT_ID = ? AND WORKFLOW_ID = ?"; + public static final String UPDATE_STATE_OF_REQUEST = "UPDATE WF_WORKFLOW_APPROVAL_STATE SET CURRENT_STEP=? WHERE EVENT_ID = ? AND WORKFLOW_ID = ?"; + + } + + public static class ParameterName { + + public static final String USER_AND_ROLE_STEP = "UserAndRole"; + public static final String TASK_STATUS_DEFAULT = "RESERVED"; + public static final String REQUEST_ID = "REQUEST ID"; + public static final String TASK_SUBJECT = "HTSubject"; + public static final String TASK_DESCRIPTION = "HTDescription"; + public static final String PRIORITY = "0"; + public static final String TENANT_NAME = "TenantNameFromContext"; + public static final String APPROVAL_TASK = "Approve task"; + public static final String ADMIN = "admin"; + public static final String INTERNAL_ADMIN = "Internal/"; + public static final String ASSIGNEE_TYPE = "Type"; + public static final String STATUS_ERROR = "Status is notAcceptable"; + public static final String NOT_ACCEPTABLE_INPUT_FOR_NEXT_STATE = "Unacceptable input provided\",\n" + + " \"Only [CLAIM, RELEASE, APPROVED, REJECTED] are acceptable"; + } + + public enum TaskStatus { + READY, + RESERVED, + COMPLETED; + + private TaskStatus() { + + } + } +} From 74fb8517f595064eb302f895d0bfc76b901cbeb6 Mon Sep 17 00:00:00 2001 From: IsharaSilva Date: Tue, 21 Jun 2022 20:16:09 +0530 Subject: [PATCH 05/16] Implement workflow event approval action --- .../workflow/engine/ApprovalEventService.java | 432 ++++++++++++++++++ .../workflow/engine/dto/PropertyDTO.java | 48 ++ .../workflow/engine/dto/StateDTO.java | 36 ++ .../workflow/engine/dto/TaskDataDTO.java | 148 ++++++ .../workflow/engine/dto/TaskSummaryDTO.java | 124 +++++ .../internal/dao/WorkflowEventRequestDAO.java | 99 ++++ .../dao/impl/WorkflowEventRequestDAOImpl.java | 273 +++++++++++ .../workflow/engine/model/TStatus.java | 27 ++ .../workflow/engine/model/TaskDetails.java | 35 ++ .../workflow/engine/model/TaskModel.java | 94 ++++ .../workflow/engine/model/TaskParam.java | 46 ++ .../engine/util/WorkflowEngineConstants.java | 23 +- dbfiles/Defaultworkflowengine.sql | 6 +- pom.xml | 89 +++- 14 files changed, 1468 insertions(+), 12 deletions(-) create mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/ApprovalEventService.java create mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/PropertyDTO.java create mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/StateDTO.java create mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/TaskDataDTO.java create mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/TaskSummaryDTO.java create mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TStatus.java create mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TaskDetails.java create mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TaskModel.java create mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TaskParam.java diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/ApprovalEventService.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/ApprovalEventService.java new file mode 100644 index 0000000..5730c78 --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/ApprovalEventService.java @@ -0,0 +1,432 @@ +package org.wso2.carbon.identity.workflow.engine; + +import org.apache.commons.collections.CollectionUtils; +import org.wso2.carbon.base.MultitenantConstants; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.identity.application.common.model.User; +import org.wso2.carbon.identity.core.util.IdentityUtil; +import org.wso2.carbon.identity.workflow.engine.dto.PropertyDTO; +import org.wso2.carbon.identity.workflow.engine.dto.StateDTO; +import org.wso2.carbon.identity.workflow.engine.dto.TaskDataDTO; +import org.wso2.carbon.identity.workflow.engine.dto.TaskSummaryDTO; +import org.wso2.carbon.identity.workflow.engine.exception.WorkflowEngineException; +import org.wso2.carbon.identity.workflow.engine.exception.WorkflowEngineRuntimeException; +import org.wso2.carbon.identity.workflow.engine.internal.dao.WorkflowEventRequestDAO; +import org.wso2.carbon.identity.workflow.engine.internal.dao.impl.WorkflowEventRequestDAOImpl; +import org.wso2.carbon.identity.workflow.engine.model.TStatus; +import org.wso2.carbon.identity.workflow.engine.model.TaskDetails; +import org.wso2.carbon.identity.workflow.engine.model.TaskModel; +import org.wso2.carbon.identity.workflow.engine.model.TaskParam; +import org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants; +import org.wso2.carbon.identity.workflow.mgt.WorkflowExecutorManagerService; +import org.wso2.carbon.identity.workflow.mgt.WorkflowExecutorManagerServiceImpl; +import org.wso2.carbon.identity.workflow.mgt.WorkflowManagementService; +import org.wso2.carbon.identity.workflow.mgt.WorkflowManagementServiceImpl; +import org.wso2.carbon.identity.workflow.mgt.bean.Parameter; +import org.wso2.carbon.identity.workflow.mgt.bean.RequestParameter; +import org.wso2.carbon.identity.workflow.mgt.bean.WorkflowAssociation; +import org.wso2.carbon.identity.workflow.mgt.callback.WSWorkflowCallBackService; +import org.wso2.carbon.identity.workflow.mgt.callback.WSWorkflowResponse; +import org.wso2.carbon.identity.workflow.mgt.dto.WorkflowRequest; +import org.wso2.carbon.identity.workflow.mgt.exception.InternalWorkflowException; +import org.wso2.carbon.identity.workflow.mgt.exception.WorkflowException; +import org.wso2.carbon.user.core.UserStoreConfigConstants; + +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * Call internal osgi services to perform user's approval task related operations + */ +public class ApprovalEventService { + + protected TStatus[] localStatus; + protected boolean localStatusTracker = false; + private static final String PENDING = "PENDING"; + private static final String APPROVED = "APPROVED"; + private static final String REJECTED = "REJECTED"; + private static final String RELEASED = "RELEASED"; + private static final String CLAIMED = "CLAIMED"; + + /** + * Search available approval tasks for the current authenticated user. + * + * @param limit number of records to be returned. + * @param offset start page. + * @param status state of the tasks [RESERVED, READY or COMPLETED]. + * @return taskSummaryDTO list. + */ + public List listTasks(Integer limit, Integer offset, List status) { + + int initialOffset = offset; + offset += limit; + limit = initialOffset + 1; + + TStatus[] tStatuses = getRequiredTStatuses(status); + setStatus(tStatuses); + + User user = getUserFromContext(); + String userName = user.getUserName(); + WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); + WorkflowExecutorManagerService workFlowExecutorManagerService = new WorkflowExecutorManagerServiceImpl(); + DefaultWorkflowEventRequest defaultWorkflowEventRequest = new DefaultWorkflowEventRequestService(); + List requestsList; + if (userName.equals(WorkflowEngineConstants.ParameterName.ADMIN)) { + String name = WorkflowEngineConstants.ParameterName.INTERNAL_ADMIN.concat(userName); + requestsList = workflowEventRequestDAO.getRequestsFromAdmin(userName, name); + } else { + requestsList = workflowEventRequestDAO.getRequestIdFromApprover(userName); + } + List taskSummaryDTOList = new ArrayList<>(); + for (int i = 0; i < requestsList.size(); i++) { + TaskSummaryDTO summeryDTO = new TaskSummaryDTO(); + WorkflowRequest request; + try { + request = workFlowExecutorManagerService.retrieveWorkflow(requestsList.get(i)); + } catch (InternalWorkflowException e) { + throw new WorkflowEngineRuntimeException("Cannot get workflow request given the request Id"); + } + TaskDetails details = getTaskDetails(request); + String eventType = workflowEventRequestDAO.getEventType(request.getUuid()); + String taskId = defaultWorkflowEventRequest.getApprovalOfRequest(request.getUuid()); + String taskStatus = workflowEventRequestDAO.getTaskStatusOfRequest(taskId); + String[] taskStatusValue = taskStatus.split("[,]", 0); + Timestamp createdTime = workflowEventRequestDAO.getCreatedAtTimeInMill(request.getUuid()); + summeryDTO.setId(taskId); + summeryDTO.setName(taskId + WorkflowEngineConstants.ParameterName.APPROVAL_TASK); + summeryDTO.setTaskType(eventType); + summeryDTO.setPresentationName(details.getTaskSubject()); + summeryDTO.setPresentationSubject(details.getTaskDescription()); + summeryDTO.setCreatedTimeInMillis(String.valueOf(createdTime)); + summeryDTO.setPriority(Integer.valueOf(WorkflowEngineConstants.ParameterName.PRIORITY)); + summeryDTO.setStatus(TaskSummaryDTO.StatusEnum.valueOf(taskStatusValue[0])); + taskSummaryDTOList.add(summeryDTO); + } + return taskSummaryDTOList; + } + + /** + * Get details of a task identified by the taskId. + * + * @param taskId the unique ID. + * @return TaskDataDto object. + */ + public TaskDataDTO getTaskData(String taskId) { + + WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); + WorkflowExecutorManagerService workFlowExecutorManagerService = new WorkflowExecutorManagerServiceImpl(); + String requestId = workflowEventRequestDAO.getRequestID(taskId); + WorkflowRequest request; + try { + request = workFlowExecutorManagerService.retrieveWorkflow(requestId); + } catch (InternalWorkflowException e) { + throw new WorkflowEngineRuntimeException("Cannot get workflow request given the request Id"); + } + TaskDetails details = getTaskDetails(request); + String initiator = workflowEventRequestDAO.getInitiatedUser(requestId); + List approvers = workflowEventRequestDAO.listApprovers(taskId); + Map assigneeMap = null; + for (String assignee : approvers) { + assigneeMap = new HashMap<>(); + assigneeMap.put(WorkflowEngineConstants.ParameterName.ASSIGNEE_TYPE, assignee); + } + String status = workflowEventRequestDAO.getTaskStatusOfRequest(taskId); + String statusValue; + if (status.equals(String.valueOf(WorkflowEngineConstants.TaskStatus.RESERVED))) { + statusValue = PENDING; + } else if (status.equals(String.valueOf(WorkflowEngineConstants.TaskStatus.READY))) { + statusValue = PENDING; + } else if (status.equals(String.valueOf(WorkflowEngineConstants.TaskStatus.COMPLETED))) { + statusValue = APPROVED; + } else { + statusValue = REJECTED; + } + List params = getRequestParameters(request); + List properties = getPropertyDTOs(params); + TaskDataDTO taskDataDTO = new TaskDataDTO(); + taskDataDTO.setId(taskId); + taskDataDTO.setSubject(details.getTaskSubject()); + taskDataDTO.setDescription(details.getTaskDescription()); + taskDataDTO.setApprovalStatus(TaskDataDTO.ApprovalStatusEnum.valueOf(statusValue)); + taskDataDTO.setInitiator(initiator); + taskDataDTO.setPriority(Integer.valueOf(WorkflowEngineConstants.ParameterName.PRIORITY)); + TaskModel taskModel = new TaskModel(); + taskModel.setAssignees(assigneeMap); + taskDataDTO.setAssignees(getPropertyDTOs(taskModel.getAssignees())); + taskDataDTO.setProperties(properties); + return taskDataDTO; + } + + /** + * Update the state of a task identified by the task id. + * User can reserve the task by claiming, or release a reserved task to himself. + * Or user can approve or reject a task. + * + * @param taskId the unique ID to update the state. + * @param nextState event status. + */ + public void updateStatus(String taskId, StateDTO nextState) { + + WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); + switch (nextState.getAction()) { + case APPROVE: + User user = getUserFromContext(); + String userName = user.getUserName(); + updateTaskStatusOfRequest(taskId, APPROVED); + updateStepDetailsOfRequest(taskId, APPROVED, userName); + break; + case REJECT: + String eventId = workflowEventRequestDAO.getRequestID(taskId); + updateTaskStatusOfRequest(taskId, REJECTED); + completeRequest(eventId, REJECTED); + break; + case RELEASE: + updateTaskStatusOfRequest(taskId, RELEASED); + break; + case CLAIM: + updateTaskStatusOfRequest(taskId, CLAIMED); + break; + default: + break; + } + } + + private void updateStepDetailsOfRequest(String taskId, String status, String approverName) { + + validateApprovers(taskId, approverName); + WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); + WorkflowExecutorManagerService workFlowExecutorManagerService = new WorkflowExecutorManagerServiceImpl(); + String eventId = workflowEventRequestDAO.getRequestID(taskId); + WorkflowRequest request; + try { + request = workFlowExecutorManagerService.retrieveWorkflow(eventId); + } catch (InternalWorkflowException e) { + throw new WorkflowEngineRuntimeException("Cannot get workflow request given the request Id"); + } + DefaultWorkflowEventRequest defaultWorkflowEventRequest = new DefaultWorkflowEventRequestService(); + List parameterList = getParameterList(request); + String workflowId = defaultWorkflowEventRequest.getWorkflowId(request); + int stepValue = defaultWorkflowEventRequest.getStateOfRequest(eventId, workflowId); + if (stepValue < numOfStates(request)) { + defaultWorkflowEventRequest.deleteApprovalOfRequest(taskId); + defaultWorkflowEventRequest.addApproversOfRequests(request, parameterList); + } else { + completeRequest(eventId, status); + } + } + + private boolean validateApprovers(String taskId, String approverName) { + + WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); + List tasks = workflowEventRequestDAO.getTaskIdList(approverName); + for (String task : tasks) { + if (taskId.equals(task)) { + return true; + } + } + return false; + } + + private int numOfStates(WorkflowRequest request) { + + List parameterList = getParameterList(request); + int count = 0; + for (Parameter parameter : parameterList) { + if (parameter.getParamName().equals(WorkflowEngineConstants.ParameterName.USER_AND_ROLE_STEP) + && !parameter.getParamValue().isEmpty()) { + count++; + } + } + return count; + } + + private void updateTaskStatusOfRequest(String taskId, String status) { + + WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); + switch (status) { + case APPROVED: + status = WorkflowEngineConstants.TaskStatus.COMPLETED.toString(); + break; + case REJECTED: + status = WorkflowEngineConstants.TaskStatus.COMPLETED.toString().concat(",").concat(REJECTED); + break; + case RELEASED: + status = WorkflowEngineConstants.TaskStatus.READY.toString(); + break; + case CLAIMED: + status = WorkflowEngineConstants.TaskStatus.RESERVED.toString(); + break; + default: + status = WorkflowEngineConstants.ParameterName.STATUS_ERROR; + break; + } + workflowEventRequestDAO.updateStatusOfRequest(taskId, status); + } + + private void completeRequest(String eventId, String status) { + + WSWorkflowResponse wsWorkflowResponse = new WSWorkflowResponse(); + WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); + String relationshipId = workflowEventRequestDAO.getRelationshipId(eventId); + wsWorkflowResponse.setUuid(relationshipId); + wsWorkflowResponse.setStatus(status); + WSWorkflowCallBackService wsWorkflowCallBackService = new WSWorkflowCallBackService(); + wsWorkflowCallBackService.onCallback(wsWorkflowResponse); + } + + private TaskDetails getTaskDetails(WorkflowRequest workflowRequest) { + + List parameterList = getParameterList(workflowRequest); + TaskDetails taskDetails = new TaskDetails(); + for (Parameter parameter : parameterList) { + if (parameter.getParamName().equals(WorkflowEngineConstants.ParameterName.TASK_SUBJECT)) { + taskDetails.setTaskSubject(parameter.getParamValue()); + } + if (parameter.getParamName().equals(WorkflowEngineConstants.ParameterName.TASK_DESCRIPTION)) { + taskDetails.setTaskDescription(parameter.getParamValue()); + } + } + return taskDetails; + } + + private List getParameterList(WorkflowRequest request) { + + WorkflowManagementService workflowManagementService = new WorkflowManagementServiceImpl(); + DefaultWorkflowEventRequest defaultWorkflowEventRequest = new DefaultWorkflowEventRequestService(); + List associations = defaultWorkflowEventRequest.getAssociations(request); + List parameterList = null; + for (WorkflowAssociation association : associations) { + try { + parameterList = workflowManagementService.getWorkflowParameters(association.getWorkflowId()); + } catch (WorkflowException e) { + throw new WorkflowEngineException("The parameterList can't get given the associated workflowId"); + } + } + return parameterList; + } + + private List getRequestParameters(WorkflowRequest request) { + + List requestParameter; + List taskParamsList = new ArrayList<>(); + for (int i = 0; i < request.getRequestParameters().size(); i++) { + requestParameter = request.getRequestParameters(); + TaskParam taskParam = new TaskParam(); + Object value = requestParameter.get(i).getValue(); + if (!(value == null)) { + taskParam.setItemValue(requestParameter.get(i).getValue().toString()); + taskParam.setItemName(requestParameter.get(i).getName()); + taskParamsList.add(taskParam); + } + } + return taskParamsList; + } + + private List getPropertyDTOs(Map props) { + + return props.entrySet().stream().map(p -> getPropertyDTO(p.getKey(), p.getValue())) + .collect(Collectors.toList()); + } + + private List getPropertyDTOs(List props) { + + return props.stream().map(p -> getPropertyDTO(p.getItemName(), p.getItemValue())) + .collect(Collectors.toList()); + } + + private PropertyDTO getPropertyDTO(String key, String value) { + + PropertyDTO prop = new PropertyDTO(); + prop.setKey(key); + prop.setValue(value); + return prop; + } + + private static String getUsernameFromContext() { + + return PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + } + + private static String getTenantDomainFromContext() { + + String tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME; + if (IdentityUtil.threadLocalProperties.get().get(WorkflowEngineConstants.ParameterName.TENANT_NAME) + != null) { + tenantDomain = (String) IdentityUtil.threadLocalProperties.get().get( + WorkflowEngineConstants.ParameterName.TENANT_NAME); + } + return tenantDomain; + } + + private static User getUserFromContext() { + + return getUser(getTenantDomainFromContext(), getUsernameFromContext()); + } + + private static User getUser(String tenantDomain, String decodedUsername) { + + String realm = UserStoreConfigConstants.PRIMARY; + String username; + String[] strComponent = decodedUsername.split("/"); + + if (strComponent.length == 1) { + username = strComponent[0]; + } else if (strComponent.length == 2) { + realm = strComponent[0]; + username = strComponent[1]; + } else { + throw new WorkflowEngineRuntimeException("Provided Username is not in the correct format."); + } + + User user = new User(); + user.setUserName(username); + user.setUserStoreDomain(realm); + user.setTenantDomain(tenantDomain); + return user; + } + + private TStatus[] getRequiredTStatuses(List status) { + + List allStatuses = Arrays.asList(WorkflowEngineConstants.TaskStatus.RESERVED.toString(), + WorkflowEngineConstants.TaskStatus.READY.toString(), + WorkflowEngineConstants.TaskStatus.COMPLETED.toString()); + TStatus[] tStatuses = getTStatus(allStatuses); + + if (CollectionUtils.isNotEmpty(status)) { + List requestedStatus = status.stream().filter(allStatuses::contains).collect + (Collectors.toList()); + if (CollectionUtils.isNotEmpty(requestedStatus)) { + tStatuses = getTStatus(requestedStatus); + } + } + return tStatuses; + } + + private TStatus[] getTStatus(List statuses) { + + return statuses.stream().map(s -> getTStatus(s)).toArray(TStatus[]::new); + } + + private TStatus getTStatus(String status) { + + TStatus tStatus = new TStatus(); + tStatus.setTStatus(status); + return tStatus; + } + + private TStatus[] getStatus() { + + return this.localStatus; + } + + private void setStatus(TStatus[] param) { + + this.localStatusTracker = param != null; + this.localStatus = param; + } +} diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/PropertyDTO.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/PropertyDTO.java new file mode 100644 index 0000000..1da10b5 --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/PropertyDTO.java @@ -0,0 +1,48 @@ +package org.wso2.carbon.identity.workflow.engine.dto; + +/** + * DTO class for task properties, map the task properties as key & value. + */ +public class PropertyDTO { + + private String key = null; + + private String value = null; + + /** + * The key of the task property. + **/ + public String getKey() { + + return key; + } + + public void setKey(String key) { + + this.key = key; + } + + /** + * The value of the key of the task property. + **/ + public String getValue() { + + return value; + } + + public void setValue(String value) { + + this.value = value; + } + + @Override + public String toString() { + + StringBuilder sb = new StringBuilder(); + sb.append("class PropertyDTO {\n"); + sb.append(" key: ").append(key).append("\n"); + sb.append(" value: ").append(value).append("\n"); + sb.append("}\n"); + return sb.toString(); + } +} \ No newline at end of file diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/StateDTO.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/StateDTO.java new file mode 100644 index 0000000..0e85d41 --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/StateDTO.java @@ -0,0 +1,36 @@ +package org.wso2.carbon.identity.workflow.engine.dto; + +/** + * Action to perform on the task dto. + */ +public class StateDTO { + + public enum ActionEnum { + CLAIM, RELEASE, APPROVE, REJECT, + }; + + private ActionEnum action = null; + + /** + * Action to perform on the task. + **/ + public ActionEnum getAction() { + + return action; + } + + public void setAction(ActionEnum action) { + + this.action = action; + } + + @Override + public String toString() { + + StringBuilder sb = new StringBuilder(); + sb.append("class StateDTO {\n"); + sb.append(" action: ").append(action).append("\n"); + sb.append("}\n"); + return sb.toString(); + } +} diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/TaskDataDTO.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/TaskDataDTO.java new file mode 100644 index 0000000..5000f79 --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/TaskDataDTO.java @@ -0,0 +1,148 @@ +package org.wso2.carbon.identity.workflow.engine.dto; + +import java.util.ArrayList; +import java.util.List; + +/** + * DTO class for task details. + */ +public class TaskDataDTO { + + private String id = null; + + private String subject = null; + + private String description = null; + + private Integer priority = null; + + private String initiator = null; + + public enum ApprovalStatusEnum { + PENDING, APPROVED, REJECTED, + } + + private ApprovalStatusEnum approvalStatus = null; + + private List assignees = new ArrayList(); + + private List properties = new ArrayList(); + + /** + * Unique ID to represent a approval task. + **/ + public String getId() { + + return id; + } + + public void setId(String id) { + + this.id = id; + } + + /** + * Subject of the Approval. + **/ + public String getSubject() { + + return subject; + } + + public void setSubject(String subject) { + + this.subject = subject; + } + + /** + * Description on the Approval task. + **/ + + public String getDescription() { + + return description; + } + + public void setDescription(String description) { + + this.description = description; + } + + /** + * Priority of the Approval task. + **/ + public Integer getPriority() { + + return priority; + } + + public void setPriority(Integer priority) { + + this.priority = priority; + } + + /** + * The user who initiated the task. + **/ + + public String getInitiator() { + + return initiator; + } + + public void setInitiator(String initiator) { + + this.initiator = initiator; + } + + /** + * Available only for the completed Tasks, APPROVED or REJECTED if the task has been completed, PENDING otherwise\n + **/ + public ApprovalStatusEnum getApprovalStatus() { + + return approvalStatus; + } + + public void setApprovalStatus(ApprovalStatusEnum approvalStatus) { + + this.approvalStatus = approvalStatus; + } + + /** + * To whom the task is assigned:\n * user - username(s) if the task is reserved for specific user(s).\n * group - role name(s) if the task is assignable for group(s).\n + **/ + public List getAssignees() { + return assignees; + } + public void setAssignees(List assignees) { + this.assignees = assignees; + } + + + /** + * Task parameters: username, role, claims, requestId. + **/ + public List getProperties() { + return properties; + } + public void setProperties(List properties) { + this.properties = properties; + } + + @Override + public String toString() { + + StringBuilder sb = new StringBuilder(); + sb.append("class TaskDataDTO {\n"); + sb.append(" id: ").append(id).append("\n"); + sb.append(" subject: ").append(subject).append("\n"); + sb.append(" description: ").append(description).append("\n"); + sb.append(" priority: ").append(priority).append("\n"); + sb.append(" initiator: ").append(initiator).append("\n"); + sb.append(" approvalStatus: ").append(approvalStatus).append("\n"); + sb.append(" assignees: ").append(assignees).append("\n"); + sb.append(" properties: ").append(properties).append("\n"); + sb.append("}\n"); + return sb.toString(); + } +} diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/TaskSummaryDTO.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/TaskSummaryDTO.java new file mode 100644 index 0000000..91e82d3 --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/TaskSummaryDTO.java @@ -0,0 +1,124 @@ +package org.wso2.carbon.identity.workflow.engine.dto; + +/** + * DTO class for tasks list. + */ +public class TaskSummaryDTO { + + private String id = null; + + private String name = null; + + private String presentationSubject = null; + + private String presentationName = null; + + private String taskType = null; + + public enum StatusEnum { + READY, RESERVED, COMPLETED, + } + + private StatusEnum status = null; + + private Integer priority = null; + + private String createdTimeInMillis = null; + + /** + * Unique ID to represent an Approval Task + **/ + public String getId() { + return id; + } + public void setId(String id) { + this.id = id; + } + + /** + * Unique name for the Approval Task + **/ + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + + /** + * Display value for Approval Operation + **/ + public String getPresentationSubject() { + return presentationSubject; + } + public void setPresentationSubject(String presentationSubject) { + this.presentationSubject = presentationSubject; + } + + /** + * Display value for Approval Task + **/ + public String getPresentationName() { + return presentationName; + } + public void setPresentationName(String presentationName) { + this.presentationName = presentationName; + } + + /** + * Type of the Approval + **/ + public String getTaskType() { + return taskType; + } + public void setTaskType(String taskType) { + this.taskType = taskType; + } + + /** + * State of the Approval task + **/ + public StatusEnum getStatus() { + return status; + } + public void setStatus(StatusEnum status) { + this.status = status; + } + + /** + * Priority of the Approval task + **/ + public Integer getPriority() { + return priority; + } + public void setPriority(Integer priority) { + this.priority = priority; + } + + /** + * The time that the operation for approval initiated + **/ + public String getCreatedTimeInMillis() { + return createdTimeInMillis; + } + public void setCreatedTimeInMillis(String createdTimeInMillis) { + this.createdTimeInMillis = createdTimeInMillis; + } + + @Override + public String toString() { + + StringBuilder sb = new StringBuilder(); + sb.append("class TaskSummaryDTO {\n"); + sb.append(" id: ").append(id).append("\n"); + sb.append(" name: ").append(name).append("\n"); + sb.append(" presentationSubject: ").append(presentationSubject).append("\n"); + sb.append(" presentationName: ").append(presentationName).append("\n"); + sb.append(" taskType: ").append(taskType).append("\n"); + sb.append(" status: ").append(status).append("\n"); + sb.append(" priority: ").append(priority).append("\n"); + sb.append(" createdTimeInMillis: ").append(createdTimeInMillis).append("\n"); + sb.append("}\n"); + return sb.toString(); + } +} diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/WorkflowEventRequestDAO.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/WorkflowEventRequestDAO.java index 325bb2b..af40af7 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/WorkflowEventRequestDAO.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/WorkflowEventRequestDAO.java @@ -1,5 +1,8 @@ package org.wso2.carbon.identity.workflow.engine.internal.dao; +import java.sql.Timestamp; +import java.util.List; + /** * Perform CRUD operations for workflow Event Request properties. */ @@ -59,4 +62,100 @@ public interface WorkflowEventRequestDAO { * @param currentStep the current step. */ void updateStateOfRequest(String eventId, String workflowId, int currentStep); + + /** + * Returns the request ID given the task ID. + * + * @param taskId random generated unique Id. + * @return request Id. + */ + String getRequestID(String taskId); + + /** + * Returns the initiator given the request ID. + * + * @param requestId the request ID that need to be checked. + * @return string initiator. + */ + String getInitiatedUser(String requestId); + + /** + * Returns the events list given the authenticated approver name. + * + * @param approverName the approver name that need to be checked. + * @return requests list. + */ + List getRequestIdFromApprover(String approverName); + + /** + * Returns the tasks list given the authenticated approver name. + * + * @param approverName the approver name that need to be checked. + * @return tasks list. + */ + List getTaskIdList(String approverName); + + /** + * Returns the events list according to the admin. + * + * @param approverName admin user. + * @param name admin role. + * @return events list. + */ + List getRequestsFromAdmin(String approverName, String name); + + /** + * Returns the event type given the request ID. + * + * @param requestId the request ID that need to be checked. + * @return event type of the request. + */ + String getEventType(String requestId); + + /** + * Returns the task status given the task ID [RESERVED, READY or COMPLETED]. + * + * @param taskId the task ID that need to be checked. + * @return task Status. + */ + String getTaskStatusOfRequest(String taskId); + + /** + * Update the task status given the task ID. + * + * @param taskId the task ID that need to be checked. + * @param taskStatus state of the tasks [RESERVED, READY or COMPLETED]. + */ + void updateStatusOfRequest(String taskId, String taskStatus); + + /** + * Returns the created time of the request. + * + * @param requestId the request ID that need to be checked. + * @return the created time. + */ + Timestamp getCreatedAtTimeInMill(String requestId); + + /** + * Returns the relationship ID given the request ID. + * + * @param eventId the event ID that need to be checked. + * @return the relationship ID. + */ + String getRelationshipId(String eventId); + + /** + * Returns the approvers list given the authenticated approver name. + * + * @param taskId the task ID that need to be checked. + * @return approvers list. + */ + List listApprovers(String taskId); + /** + * Get approverName related to currentStep. + * + * @param taskId the unique ID that need to be checked. + * @return the value of the approver type. + */ + String getApproversOfCurrentStep(String taskId); } diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImpl.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImpl.java index 7f1c8f2..ff98d26 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImpl.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImpl.java @@ -9,6 +9,8 @@ import java.sql.PreparedStatement; import java.sql.SQLException; +import java.sql.Timestamp; +import java.util.List; /** * Workflow Event Request DAO implementation. @@ -159,4 +161,275 @@ private void setPreparedStatementForStateOfRequest(int currentStep, String event preparedStatement.setString(2, eventId); preparedStatement.setString(3, workflowId); } + + /** + * {@inheritDoc} + */ + @Override + public String getApproversOfCurrentStep(String taskId) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + String approverName; + try { + approverName = jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries. + GET_APPROVER_NAME_RELATED_TO_CURRENT_TASK_ID, + ((resultSet, i) -> ( + resultSet.getString(WorkflowEngineConstants.APPROVER_NAME_COLUMN))), + preparedStatement -> preparedStatement.setString(1, taskId)); + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while retrieving approverName from" + + "task Id: %s", taskId); + throw new WorkflowEngineRuntimeException(errorMessage); + } + return approverName; + } + + /** + * {@inheritDoc} + */ + @Override + public List listApprovers(String taskId) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + List requestsList; + try { + requestsList = jdbcTemplate.executeQuery(WorkflowEngineConstants.SqlQueries. + GET_APPROVER_NAME_RELATED_TO_CURRENT_TASK_ID, (resultSet, rowNumber) -> + new String(resultSet.getString(WorkflowEngineConstants.APPROVER_NAME_COLUMN)), + preparedStatement -> { + preparedStatement.setString(1, taskId); + }); + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while retrieving approvers from" + + "task Id: %s", taskId); + throw new WorkflowEngineRuntimeException(errorMessage); + } + return requestsList; + } + + /** + * {@inheritDoc} + */ + @Override + public String getRequestID(String taskId) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + String requestId; + try { + requestId = jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries. + GET_REQUEST_ID, + ((resultSet, i) -> ( + resultSet.getString(WorkflowEngineConstants.Event_ID_COLUMN))), + preparedStatement -> preparedStatement.setString(1, taskId)); + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while retrieving request ID from" + + "task Id: %s", taskId); + throw new WorkflowEngineRuntimeException(errorMessage); + } + return requestId; + } + + /** + *{@inheritDoc} + */ + @Override + public String getInitiatedUser(String requestId) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + String createdBy; + try { + createdBy = jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries. + GET_CREATED_USER, + ((resultSet, i) -> ( + resultSet.getString(WorkflowEngineConstants.CREATED_USER_COLUMN))), + preparedStatement -> preparedStatement.setString(1, requestId)); + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while retrieving initiator from" + + "requestId: %s", requestId); + throw new WorkflowEngineRuntimeException(errorMessage); + } + return createdBy; + } + + /** + * {@inheritDoc} + */ + @Override + public List getRequestIdFromApprover(String approverName) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + List requestIdList; + try { + requestIdList = jdbcTemplate.executeQuery(WorkflowEngineConstants.SqlQueries. + GET_EVENT_ID_FROM_APPROVER, (resultSet, rowNumber) -> + new String(resultSet.getString(WorkflowEngineConstants.EVENT_ID_FROM_APPROVER_COLUMN)), + preparedStatement -> { + preparedStatement.setString(1, approverName); + }); + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while retrieving requestID from" + + "approver name: %s", approverName); + throw new WorkflowEngineRuntimeException(errorMessage); + } + return requestIdList; + } + + /** + * {@inheritDoc} + */ + public List getTaskIdList(String approverName) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + List requestIdList; + try { + requestIdList = jdbcTemplate.executeQuery(WorkflowEngineConstants.SqlQueries. + GET_TASK_ID_FROM_APPROVER, (resultSet, rowNumber) -> + new String(resultSet.getString(WorkflowEngineConstants.TASK_ID_COLUMN)), + preparedStatement -> { + preparedStatement.setString(1, approverName); + }); + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while retrieving task id from" + + "approver name: %s", approverName); + throw new WorkflowEngineRuntimeException(errorMessage); + } + return requestIdList; + } + + /** + * {@inheritDoc} + */ + @Override + public List getRequestsFromAdmin(String approverName, String name) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + List requestsList; + try { + requestsList = jdbcTemplate.executeQuery(WorkflowEngineConstants.SqlQueries. + GET_EVENT_ID_FROM_ADMIN, (resultSet, rowNumber) -> + new String(resultSet.getString(WorkflowEngineConstants.EVENT_ID_FROM_APPROVER_COLUMN)), + preparedStatement -> { + preparedStatement.setString(1, approverName); + preparedStatement.setString(2, name); + }); + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while retrieving requestID from" + + "admin: %s", approverName); + throw new WorkflowEngineRuntimeException(errorMessage); + } + return requestsList; + } + + /** + * {@inheritDoc} + */ + @Override + public String getEventType(String requestId) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + String eventType; + try { + eventType = jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries. + GET_EVENT_TYPE, + ((resultSet, i) -> ( + resultSet.getString(WorkflowEngineConstants.EVENT_TYPE_COLUMN))), + preparedStatement -> preparedStatement.setString(1, requestId)); + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while retrieving event type from" + + "request Id: %s", requestId); + throw new WorkflowEngineRuntimeException(errorMessage); + } + return eventType; + } + + /** + * {@inheritDoc} + */ + @Override + public String getTaskStatusOfRequest(String taskId) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + String taskStatus; + try { + taskStatus = jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries. + GET_TASK_STATUS, + ((resultSet, i) -> ( + resultSet.getString(WorkflowEngineConstants.TASK_STATUS_COLUMN))), + preparedStatement -> preparedStatement.setString(1, taskId)); + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while retrieving task status from" + + "task Id: %s", taskId); + throw new WorkflowEngineRuntimeException(errorMessage); + } + return taskStatus; + } + + @Override + public void updateStatusOfRequest(String taskId, String taskStatus) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + try { + jdbcTemplate.executeUpdate(WorkflowEngineConstants.SqlQueries.UPDATE_TASK_STATUS, + (preparedStatement -> { + setPreparedStatementForStatusOfRequest(taskStatus, taskId, preparedStatement); + preparedStatement.setString(1, taskStatus); + preparedStatement.setString(2, taskId); + })); + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while updating status from" + + "taskID: %s", taskId); + throw new WorkflowEngineRuntimeException(errorMessage); + } + } + + /** + * {@inheritDoc} + */ + @Override + public String getRelationshipId(String eventId) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + String taskStatus; + try { + taskStatus = jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries. + GET_REQUEST_ID_OF_RELATIONSHIP, + ((resultSet, i) -> ( + resultSet.getString(WorkflowEngineConstants.RELATIONSHIP_ID_IN_REQUEST_COLUMN))), + preparedStatement -> preparedStatement.setString(1, eventId)); + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while retrieving relationship ID from" + + "event Id: %s", eventId); + throw new WorkflowEngineRuntimeException(errorMessage); + } + return taskStatus; + } + + /** + * {@inheritDoc} + */ + @Override + public Timestamp getCreatedAtTimeInMill(String requestId) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + java.sql.Timestamp createdTime; + try { + createdTime = jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries. + GET_CREATED_TIME_IN_MILL, + ((resultSet, i) -> ( + resultSet.getTimestamp(WorkflowEngineConstants.CREATED_AT_IN_MILL_COLUMN))), + preparedStatement -> preparedStatement.setString(1, requestId)); + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while retrieving createdAt time from" + + "request Id: %s", requestId); + throw new WorkflowEngineRuntimeException(errorMessage); + } + return createdTime; + } + + private void setPreparedStatementForStatusOfRequest(String taskStatus, String taskId, + PreparedStatement preparedStatement) throws SQLException { + + preparedStatement.setString(1, taskStatus); + preparedStatement.setString(2, taskId); + } } diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TStatus.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TStatus.java new file mode 100644 index 0000000..63a99f4 --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TStatus.java @@ -0,0 +1,27 @@ +package org.wso2.carbon.identity.workflow.engine.model; + +/** + * Model class for task status [RESERVED, READY or COMPLETED]. + */ +public class TStatus { + + protected String localTStatus; + + public TStatus() { + + } + + /** + * The status of the task [RESERVED, READY or COMPLETED]. + */ + public String getTStatus() { + + return this.localTStatus; + } + + public void setTStatus(String param) { + + this.localTStatus = param; + } + +} diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TaskDetails.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TaskDetails.java new file mode 100644 index 0000000..6a6fbf3 --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TaskDetails.java @@ -0,0 +1,35 @@ +package org.wso2.carbon.identity.workflow.engine.model; + +/** + * model class for task subject & task description. + */ +public class TaskDetails { + String taskDescription; + String taskSubject; + + /** + * The subject of the task. + */ + public String getTaskDescription() { + + return taskDescription; + } + + public void setTaskDescription(String taskDescription) { + + this.taskDescription = taskDescription; + } + + /** + * The description of the task. + */ + public String getTaskSubject() { + + return taskSubject; + } + + public void setTaskSubject(String taskSubject) { + + this.taskSubject = taskSubject; + } +} diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TaskModel.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TaskModel.java new file mode 100644 index 0000000..8d8a54f --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TaskModel.java @@ -0,0 +1,94 @@ +package org.wso2.carbon.identity.workflow.engine.model; + +import java.util.Map; + +/** + * Model Class for task. + */ +public class TaskModel { + + private String id; + private String taskSubject; + private String taskDescription; + private String priority; + private String htInitiator; + private String approvalStatus; + private Map assignees; + + /** + * Unique ID to represent a approval task + **/ + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + /** + * Subject of the Approval + **/ + public String getTaskSubject() { + return taskSubject; + } + + public void setTaskSubject(String taskSubject) { + this.taskSubject = taskSubject; + } + + /** + * Description on the Approval task + **/ + public String getTaskDescription() { + return taskDescription; + } + + public void setTaskDescription(String taskDescription) { + this.taskDescription = taskDescription; + } + + /** + * Priority of the Approval task + **/ + public String getPriority() { + return priority; + } + + public void setPriority(String priority) { + this.priority = priority; + } + + /** + * The user who initiated the task + **/ + public String getHtInitiator() { + return htInitiator; + } + + public void setHtInitiator(String htInitiator) { + this.htInitiator = htInitiator; + } + + /** + * Available only for the completed Tasks, APPROVED or REJECTED if the task has been completed, PENDING otherwise\n + **/ + public String getApprovalStatus() { + return approvalStatus; + } + + public void setApprovalStatus(String approvalStatus) { + this.approvalStatus = approvalStatus; + } + + /** + * To whom the task is assigned:\n * user - username(s) if the task is reserved for specific user(s).\n * group - role name(s) if the task is assignable for group(s).\n + **/ + public Map getAssignees() { + return assignees; + } + + public void setAssignees(Map assignees) { + this.assignees = assignees; + } +} diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TaskParam.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TaskParam.java new file mode 100644 index 0000000..6924c71 --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TaskParam.java @@ -0,0 +1,46 @@ +package org.wso2.carbon.identity.workflow.engine.model; + +/** + * Task Parameter model, Get task parameters as list. + */ +public class TaskParam { + + private String itemName; + private String itemValue; + + /** + * Get the Item name of the task parameters. + * + * @return The Item name of the task parameters. + */ + public String getItemName() { + return itemName; + } + + /** + * Set the Item name for the task parameters. + * + * @param itemName The Item name for the task parameters. + */ + public void setItemName(String itemName) { + this.itemName = itemName; + } + + /** + * Get the Item value of the task parameters. + * + * @return The Item value of the task parameters. + */ + public String getItemValue() { + return itemValue; + } + + /** + * Set the Item value for the task parameters. + * + * @param itemValue The Item value for the task parameters. + */ + public void setItemValue(String itemValue) { + this.itemValue = itemValue; + } +} diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java index 272d868..e1cb062 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java @@ -7,6 +7,14 @@ public class WorkflowEngineConstants { public static final String CURRENT_STEP_COLUMN = "CURRENT_STEP"; public static final String TASK_ID_COLUMN = "TASK_ID"; + public static final String APPROVER_NAME_COLUMN = "APPROVER_NAME"; + public static final String Event_ID_COLUMN = "EVENT_ID"; + public static final String CREATED_USER_COLUMN = "CREATED_BY"; + public static final String EVENT_ID_FROM_APPROVER_COLUMN = "EVENT_ID"; + public static final String EVENT_TYPE_COLUMN = "OPERATION_TYPE"; + public static final String TASK_STATUS_COLUMN = "TASK_STATUS"; + public static final String CREATED_AT_IN_MILL_COLUMN = "CREATED_AT"; + public static final String RELATIONSHIP_ID_IN_REQUEST_COLUMN = "RELATIONSHIP_ID"; public static class SqlQueries { @@ -16,7 +24,18 @@ public static class SqlQueries { public static final String ADD_CURRENT_STEP_FOR_EVENT = "INSERT INTO WF_WORKFLOW_APPROVAL_STATE (EVENT_ID,WORKFLOW_ID, CURRENT_STEP) VALUES (?,?,?)"; public static final String GET_CURRENT_STEP = "SELECT CURRENT_STEP FROM WF_WORKFLOW_APPROVAL_STATE WHERE EVENT_ID = ? AND WORKFLOW_ID = ?"; public static final String UPDATE_STATE_OF_REQUEST = "UPDATE WF_WORKFLOW_APPROVAL_STATE SET CURRENT_STEP=? WHERE EVENT_ID = ? AND WORKFLOW_ID = ?"; - + public static final String GET_APPROVER_NAME_RELATED_TO_CURRENT_TASK_ID = "SELECT DISTINCT APPROVER_NAME FROM WF_WORKFLOW_APPROVAL_RELATION WHERE TASK_ID = ?"; + public static final String UPDATE_TASK_STATUS = "UPDATE WF_WORKFLOW_APPROVAL_RELATION SET TASK_STATUS=? WHERE TASK_ID=?"; + public static final String GET_REQUEST_ID = "SELECT DISTINCT EVENT_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE TASK_ID = ?"; + public static final String GET_EVENT_ID_FROM_APPROVER = "SELECT EVENT_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE APPROVER_NAME= ?"; + public static final String GET_TASK_ID_FROM_APPROVER = "SELECT TASK_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE APPROVER_NAME= ?"; + public static final String GET_EVENT_ID_FROM_ADMIN = "SELECT EVENT_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE APPROVER_NAME= ? OR APPROVER_NAME=?"; + public static final String GET_TASK_STATUS = "SELECT DISTINCT TASK_STATUS FROM WF_WORKFLOW_APPROVAL_RELATION WHERE TASK_ID = ?"; + public static final String GET_CREATED_USER = "SELECT CREATED_BY FROM WF_REQUEST WHERE UUID= ?"; + public static final String GET_EVENT_TYPE = "SELECT OPERATION_TYPE FROM WF_REQUEST WHERE UUID= ?"; + public static final String GET_CREATED_TIME_IN_MILL = "SELECT CREATED_AT FROM WF_REQUEST WHERE UUID= ?"; + public static final String GET_REQUEST_ID_OF_RELATIONSHIP = "SELECT RELATIONSHIP_ID FROM " + + "WF_WORKFLOW_REQUEST_RELATION WHERE REQUEST_ID = ?"; } public static class ParameterName { @@ -33,8 +52,6 @@ public static class ParameterName { public static final String INTERNAL_ADMIN = "Internal/"; public static final String ASSIGNEE_TYPE = "Type"; public static final String STATUS_ERROR = "Status is notAcceptable"; - public static final String NOT_ACCEPTABLE_INPUT_FOR_NEXT_STATE = "Unacceptable input provided\",\n" + - " \"Only [CLAIM, RELEASE, APPROVED, REJECTED] are acceptable"; } public enum TaskStatus { diff --git a/dbfiles/Defaultworkflowengine.sql b/dbfiles/Defaultworkflowengine.sql index 73dff38..d6258ab 100644 --- a/dbfiles/Defaultworkflowengine.sql +++ b/dbfiles/Defaultworkflowengine.sql @@ -4,7 +4,8 @@ EVENT_ID VARCHAR(45) NOT NULL, WORKFLOW_ID VARCHAR(45) NOT NULL, APPROVER_TYPE VARCHAR(45) NOT NULL, APPROVER_NAME VARCHAR(255) NOT NULL, -PRIMARY KEY(TASK_ID), +TASK_STATUS VARCHAR(255), +CONSTRAINT PK_WF_WORKFLOW_RELATION_CONSTRAINT PRIMARY KEY (TASK_ID, APPROVER_TYPE, APPROVER_NAME), FOREIGN KEY (EVENT_ID) REFERENCES WF_REQUEST(UUID) ON DELETE CASCADE, FOREIGN KEY (WORKFLOW_ID) REFERENCES WF_WORKFLOW(ID) ON DELETE CASCADE ); @@ -18,9 +19,8 @@ FOREIGN KEY (EVENT_ID) REFERENCES WF_REQUEST(UUID) ON DELETE CASCADE, FOREIGN KEY (WORKFLOW_ID) REFERENCES WF_WORKFLOW(ID) ON DELETE CASCADE ); - CREATE INDEX IDX_WF_APPROVAL_STATE ON WF_WORKFLOW_APPROVAL_STATE(EVENT_ID,WORKFLOW_ID); -CREATE INDEX IDX_WF_APPROVAL_RELATION ON WF_WORKFLOW_APPROVAL_RELATION(TASK_ID); +CREATE INDEX IDX_WF_APPROVAL_RELATION ON WF_WORKFLOW_APPROVAL_RELATION(TASK_ID, APPROVER_TYPE, APPROVER_NAME); diff --git a/pom.xml b/pom.xml index c515015..dcfdb31 100644 --- a/pom.xml +++ b/pom.xml @@ -90,13 +90,83 @@ - + + + wso2.releases + WSO2 internal Repository + https://maven.wso2.org/nexus/content/repositories/releases/ + + true + daily + ignore + + + + wso2.snapshots + WSO2 Snapshot Repository + https://maven.wso2.org/nexus/content/repositories/snapshots/ + + true + daily + + + false + + + + wso2-nexus + WSO2 internal Repository + https://maven.wso2.org/nexus/content/groups/wso2-public/ + + true + daily + ignore + + + + + + + wso2-nexus + WSO2 internal Repository + https://maven.wso2.org/nexus/content/groups/wso2-public/ + + true + daily + ignore + + + + wso2.releases + WSO2 internal Repository + https://maven.wso2.org/nexus/content/repositories/releases/ + + true + daily + ignore + + + + wso2.snapshots + Apache Snapshot Repository + https://maven.wso2.org/nexus/content/repositories/snapshots/ + + true + daily + + + false + + + + + + - org.testng - testng - ${testng.version} - test + org.apache.maven + maven-core + ${maven.version} org.wso2.carbon.identity.framework @@ -121,13 +191,18 @@ org.apache.ws.commons.axiom.wso2 axiom - ${axiom.version} + ${axiom.wso2.version} org.apache.felix org.apache.felix.scr.ds-annotations ${apache.felix.scr.ds.annotations.version} + + org.wso2.carbon.identity.framework + org.wso2.carbon.user.mgt + ${carbon.identity.framework.version} + @@ -139,6 +214,8 @@ 3.0.5 3.8.0 1.2.8 + 5.18.187 + 1.2.11-wso2v16 From ebb61e96cb92dc836532cb668877fc96b265caf1 Mon Sep 17 00:00:00 2001 From: IsharaSilva Date: Sun, 26 Jun 2022 02:16:18 +0530 Subject: [PATCH 06/16] Implement workflow event approval action --- .../pom.xml | 1 + .../workflow/engine/ApprovalEventService.java | 298 ++++++++++-------- .../engine/DefaultApprovalWorkflow.java | 32 +- .../engine/DefaultTemplateInitializer.java | 61 ++++ .../WorkflowEngineServiceComponent.java | 11 +- .../internal/dao/WorkflowEventRequestDAO.java | 19 +- .../dao/impl/WorkflowEventRequestDAOImpl.java | 126 ++++++-- .../workflow/engine/model/PagePagination.java | 41 +++ .../engine/util/WorkflowEngineConstants.java | 36 ++- 9 files changed, 434 insertions(+), 191 deletions(-) create mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultTemplateInitializer.java create mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/PagePagination.java diff --git a/components/org.wso2.carbon.identity.workflow.engine/pom.xml b/components/org.wso2.carbon.identity.workflow.engine/pom.xml index 7d9da20..9cca4df 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/pom.xml +++ b/components/org.wso2.carbon.identity.workflow.engine/pom.xml @@ -6,6 +6,7 @@ default.workflow.engine org.wso2.carbon.identity.workflow.engine + 1.0-SNAPSHOT ../../pom.xml diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/ApprovalEventService.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/ApprovalEventService.java index 5730c78..536269e 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/ApprovalEventService.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/ApprovalEventService.java @@ -1,10 +1,7 @@ package org.wso2.carbon.identity.workflow.engine; import org.apache.commons.collections.CollectionUtils; -import org.wso2.carbon.base.MultitenantConstants; -import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.identity.application.common.model.User; -import org.wso2.carbon.identity.core.util.IdentityUtil; +import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.identity.workflow.engine.dto.PropertyDTO; import org.wso2.carbon.identity.workflow.engine.dto.StateDTO; import org.wso2.carbon.identity.workflow.engine.dto.TaskDataDTO; @@ -13,6 +10,7 @@ import org.wso2.carbon.identity.workflow.engine.exception.WorkflowEngineRuntimeException; import org.wso2.carbon.identity.workflow.engine.internal.dao.WorkflowEventRequestDAO; import org.wso2.carbon.identity.workflow.engine.internal.dao.impl.WorkflowEventRequestDAOImpl; +import org.wso2.carbon.identity.workflow.engine.model.PagePagination; import org.wso2.carbon.identity.workflow.engine.model.TStatus; import org.wso2.carbon.identity.workflow.engine.model.TaskDetails; import org.wso2.carbon.identity.workflow.engine.model.TaskModel; @@ -30,28 +28,30 @@ import org.wso2.carbon.identity.workflow.mgt.dto.WorkflowRequest; import org.wso2.carbon.identity.workflow.mgt.exception.InternalWorkflowException; import org.wso2.carbon.identity.workflow.mgt.exception.WorkflowException; -import org.wso2.carbon.user.core.UserStoreConfigConstants; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.stream.Collectors; +import java.util.stream.Stream; /** - * Call internal osgi services to perform user's approval task related operations + * Call internal osgi services to perform user's approval task related operations. */ public class ApprovalEventService { - protected TStatus[] localStatus; - protected boolean localStatusTracker = false; private static final String PENDING = "PENDING"; private static final String APPROVED = "APPROVED"; private static final String REJECTED = "REJECTED"; private static final String RELEASED = "RELEASED"; private static final String CLAIMED = "CLAIMED"; + private static final Integer LIMIT = 20; + private static final Integer OFFSET = 0; /** * Search available approval tasks for the current authenticated user. @@ -63,53 +63,126 @@ public class ApprovalEventService { */ public List listTasks(Integer limit, Integer offset, List status) { - int initialOffset = offset; - offset += limit; - limit = initialOffset + 1; + PagePagination pagePagination = new PagePagination(); + if (limit == null || offset == null) { + pagePagination.setPageSize(LIMIT); + pagePagination.setPageNumber(OFFSET); + } - TStatus[] tStatuses = getRequiredTStatuses(status); - setStatus(tStatuses); + if (limit != null && limit > 0) { + pagePagination.setPageSize(limit); + } + if (offset != null && offset > 0) { + pagePagination.setPageNumber(offset); + } + + final int[] taskCount = new int[1]; + int pageSize = pagePagination.getPageSize() > 0 ? pagePagination.getPageSize() : 20; + //Number of pages showing the task list. + int pages = (int) Math.ceil((double) taskCount[0] / (double) pageSize); + Set taskSummaryDTOs = null; + List tasks = listTasksOfApprovers(status); + int taskListSize = tasks.size(); + for (int i = 0; i < taskListSize; ++i) { + taskSummaryDTOs = new HashSet<>(tasks); + } + if (taskSummaryDTOs == null) { + return new ArrayList<>(0); + } + return new ArrayList<>(taskSummaryDTOs); + } + + private List listTasksOfApprovers(List status) { - User user = getUserFromContext(); - String userName = user.getUserName(); WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); - WorkflowExecutorManagerService workFlowExecutorManagerService = new WorkflowExecutorManagerServiceImpl(); DefaultWorkflowEventRequest defaultWorkflowEventRequest = new DefaultWorkflowEventRequestService(); - List requestsList; - if (userName.equals(WorkflowEngineConstants.ParameterName.ADMIN)) { - String name = WorkflowEngineConstants.ParameterName.INTERNAL_ADMIN.concat(userName); - requestsList = workflowEventRequestDAO.getRequestsFromAdmin(userName, name); - } else { - requestsList = workflowEventRequestDAO.getRequestIdFromApprover(userName); - } + List allRequestsList = getAllRequestRelatedUserAndRole(); List taskSummaryDTOList = new ArrayList<>(); - for (int i = 0; i < requestsList.size(); i++) { + for (int i = 0; i < allRequestsList.size(); i++) { TaskSummaryDTO summeryDTO = new TaskSummaryDTO(); - WorkflowRequest request; - try { - request = workFlowExecutorManagerService.retrieveWorkflow(requestsList.get(i)); - } catch (InternalWorkflowException e) { - throw new WorkflowEngineRuntimeException("Cannot get workflow request given the request Id"); + String eventId = getTaskRelatedStatus(allRequestsList.get(i), status); + if (eventId != null) { + WorkflowRequest request = getWorkflowRequest(eventId); + TaskDetails taskDetails = getTaskDetails(request); + String eventType = workflowEventRequestDAO.getEventType(request.getUuid()); + String taskId = defaultWorkflowEventRequest.getApprovalOfRequest(request.getUuid()); + String taskStatus = workflowEventRequestDAO.getTaskStatusOfRequest(taskId); + String[] taskStatusValue = taskStatus.split(",", 0); + Timestamp createdTime = workflowEventRequestDAO.getCreatedAtTimeInMill(request.getUuid()); + summeryDTO.setId(taskId); + summeryDTO.setName(WorkflowEngineConstants.ParameterName.APPROVAL_TASK); + summeryDTO.setTaskType(eventType); + summeryDTO.setPresentationName(taskDetails.getTaskSubject()); + summeryDTO.setPresentationSubject(taskDetails.getTaskDescription()); + summeryDTO.setCreatedTimeInMillis(String.valueOf(createdTime)); + summeryDTO.setPriority(Integer.valueOf(WorkflowEngineConstants.ParameterName.PRIORITY)); + summeryDTO.setStatus(TaskSummaryDTO.StatusEnum.valueOf(taskStatusValue[0])); + taskSummaryDTOList.add(summeryDTO); } - TaskDetails details = getTaskDetails(request); - String eventType = workflowEventRequestDAO.getEventType(request.getUuid()); - String taskId = defaultWorkflowEventRequest.getApprovalOfRequest(request.getUuid()); - String taskStatus = workflowEventRequestDAO.getTaskStatusOfRequest(taskId); - String[] taskStatusValue = taskStatus.split("[,]", 0); - Timestamp createdTime = workflowEventRequestDAO.getCreatedAtTimeInMill(request.getUuid()); - summeryDTO.setId(taskId); - summeryDTO.setName(taskId + WorkflowEngineConstants.ParameterName.APPROVAL_TASK); - summeryDTO.setTaskType(eventType); - summeryDTO.setPresentationName(details.getTaskSubject()); - summeryDTO.setPresentationSubject(details.getTaskDescription()); - summeryDTO.setCreatedTimeInMillis(String.valueOf(createdTime)); - summeryDTO.setPriority(Integer.valueOf(WorkflowEngineConstants.ParameterName.PRIORITY)); - summeryDTO.setStatus(TaskSummaryDTO.StatusEnum.valueOf(taskStatusValue[0])); - taskSummaryDTOList.add(summeryDTO); } return taskSummaryDTOList; } + private List getAllRequestRelatedUserAndRole(){ + + String userName = CarbonContext.getThreadLocalCarbonContext().getUsername(); + WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); + List roleNames = getRoleNamesFromUser(userName); + List roleRequestsList; + List lst = new ArrayList<>(); + for (String roleName : roleNames) { + String[] names = roleName.split("/", 0); + String[] newName = roleName.split("_", 0); + if (names[0].equals(WorkflowEngineConstants.ParameterName.APPLICATION_USER)) { + roleRequestsList = workflowEventRequestDAO.getRequestsList(roleName); + lst.addAll(roleRequestsList); + } else if (newName[0].equals(WorkflowEngineConstants.ParameterName.SYSTEM_USER)) { + String newRoleName = WorkflowEngineConstants.ParameterName.SYSTEM_PRIMARY_USER.concat(names[0]); + roleRequestsList = workflowEventRequestDAO.getRequestsList(newRoleName); + lst.addAll(roleRequestsList); + } else { + String newRoleName = WorkflowEngineConstants.ParameterName.INTERNAL_USER.concat(names[0]); + roleRequestsList = workflowEventRequestDAO.getRequestsList(newRoleName); + lst.addAll(roleRequestsList); + } + } + + List userRequestList = workflowEventRequestDAO.getRequestIdFromApprover(userName); + return Stream.concat(lst.stream(), userRequestList.stream()).collect(Collectors.toList()); + } + + private List getRoleNamesFromUser(String approverName) { + + WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); + List roleIDList = workflowEventRequestDAO.getRolesID(approverName); + List roleNameList; + List lst = new ArrayList<>(); + for (Integer roleId : roleIDList) { + roleNameList = workflowEventRequestDAO.getRoleNames(roleId); + lst.addAll(roleNameList); + } + return new ArrayList<>(lst); + } + + private String getTaskRelatedStatus(String requestId, List status) { + + WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); + + TStatus[] tStatuses = getRequiredTStatuses(status); + String taskStatus = workflowEventRequestDAO.getStatusOfTask(requestId); + String[] taskStatusValue = taskStatus.split(",", 0); + String eventId = null; + String value; + for (TStatus tStatus : tStatuses) { + value = tStatus.getTStatus(); + if (value.equals(taskStatusValue[0])) { + eventId = requestId; + break; + } + } + return eventId; + } + /** * Get details of a task identified by the taskId. * @@ -119,14 +192,8 @@ public List listTasks(Integer limit, Integer offset, List approvers = workflowEventRequestDAO.listApprovers(taskId); @@ -173,47 +240,52 @@ public TaskDataDTO getTaskData(String taskId) { public void updateStatus(String taskId, StateDTO nextState) { WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); - switch (nextState.getAction()) { - case APPROVE: - User user = getUserFromContext(); - String userName = user.getUserName(); - updateTaskStatusOfRequest(taskId, APPROVED); - updateStepDetailsOfRequest(taskId, APPROVED, userName); - break; - case REJECT: - String eventId = workflowEventRequestDAO.getRequestID(taskId); - updateTaskStatusOfRequest(taskId, REJECTED); - completeRequest(eventId, REJECTED); - break; - case RELEASE: - updateTaskStatusOfRequest(taskId, RELEASED); - break; - case CLAIM: - updateTaskStatusOfRequest(taskId, CLAIMED); - break; - default: - break; + String userName = CarbonContext.getThreadLocalCarbonContext().getUsername(); + validateApprovers(taskId, userName); + switch (nextState.getAction()) { + case APPROVE: + updateTaskStatusOfRequest(taskId, APPROVED); + updateStepDetailsOfRequest(taskId, APPROVED); + break; + case REJECT: + String eventId = workflowEventRequestDAO.getRequestID(taskId); + updateTaskStatusOfRequest(taskId, REJECTED); + completeRequest(eventId, REJECTED); + break; + case RELEASE: + updateTaskStatusOfRequest(taskId, RELEASED); + break; + case CLAIM: + updateTaskStatusOfRequest(taskId, CLAIMED); + break; + default: + break; } } - private void updateStepDetailsOfRequest(String taskId, String status, String approverName) { + private WorkflowRequest getWorkflowRequest(String requestId) { - validateApprovers(taskId, approverName); - WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); WorkflowExecutorManagerService workFlowExecutorManagerService = new WorkflowExecutorManagerServiceImpl(); - String eventId = workflowEventRequestDAO.getRequestID(taskId); WorkflowRequest request; try { - request = workFlowExecutorManagerService.retrieveWorkflow(eventId); + request = workFlowExecutorManagerService.retrieveWorkflow(requestId); } catch (InternalWorkflowException e) { throw new WorkflowEngineRuntimeException("Cannot get workflow request given the request Id"); } + return request; + } + + private void updateStepDetailsOfRequest(String taskId, String status) { + + WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); + String eventId = workflowEventRequestDAO.getRequestID(taskId); + WorkflowRequest request = getWorkflowRequest(eventId); DefaultWorkflowEventRequest defaultWorkflowEventRequest = new DefaultWorkflowEventRequestService(); List parameterList = getParameterList(request); String workflowId = defaultWorkflowEventRequest.getWorkflowId(request); + defaultWorkflowEventRequest.deleteApprovalOfRequest(taskId); int stepValue = defaultWorkflowEventRequest.getStateOfRequest(eventId, workflowId); if (stepValue < numOfStates(request)) { - defaultWorkflowEventRequest.deleteApprovalOfRequest(taskId); defaultWorkflowEventRequest.addApproversOfRequests(request, parameterList); } else { completeRequest(eventId, status); @@ -223,8 +295,15 @@ private void updateStepDetailsOfRequest(String taskId, String status, String app private boolean validateApprovers(String taskId, String approverName) { WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); - List tasks = workflowEventRequestDAO.getTaskIdList(approverName); - for (String task : tasks) { + List eventList =getAllRequestRelatedUserAndRole(); + List taskList; + List lst = new ArrayList<>(); + for (String event : eventList) { + taskList = workflowEventRequestDAO.getTaskId(event); + lst.addAll(taskList); + } + + for (String task : lst) { if (taskId.equals(task)) { return true; } @@ -318,7 +397,10 @@ private List getRequestParameters(WorkflowRequest request) { requestParameter = request.getRequestParameters(); TaskParam taskParam = new TaskParam(); Object value = requestParameter.get(i).getValue(); - if (!(value == null)) { + if (requestParameter.get(i).getName().equals(WorkflowEngineConstants.ParameterName.CREDENTIAL)) { + continue; + } + if (value != null) { taskParam.setItemValue(requestParameter.get(i).getValue().toString()); taskParam.setItemName(requestParameter.get(i).getName()); taskParamsList.add(taskParam); @@ -347,49 +429,6 @@ private PropertyDTO getPropertyDTO(String key, String value) { return prop; } - private static String getUsernameFromContext() { - - return PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - } - - private static String getTenantDomainFromContext() { - - String tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME; - if (IdentityUtil.threadLocalProperties.get().get(WorkflowEngineConstants.ParameterName.TENANT_NAME) - != null) { - tenantDomain = (String) IdentityUtil.threadLocalProperties.get().get( - WorkflowEngineConstants.ParameterName.TENANT_NAME); - } - return tenantDomain; - } - - private static User getUserFromContext() { - - return getUser(getTenantDomainFromContext(), getUsernameFromContext()); - } - - private static User getUser(String tenantDomain, String decodedUsername) { - - String realm = UserStoreConfigConstants.PRIMARY; - String username; - String[] strComponent = decodedUsername.split("/"); - - if (strComponent.length == 1) { - username = strComponent[0]; - } else if (strComponent.length == 2) { - realm = strComponent[0]; - username = strComponent[1]; - } else { - throw new WorkflowEngineRuntimeException("Provided Username is not in the correct format."); - } - - User user = new User(); - user.setUserName(username); - user.setUserStoreDomain(realm); - user.setTenantDomain(tenantDomain); - return user; - } - private TStatus[] getRequiredTStatuses(List status) { List allStatuses = Arrays.asList(WorkflowEngineConstants.TaskStatus.RESERVED.toString(), @@ -409,7 +448,7 @@ private TStatus[] getRequiredTStatuses(List status) { private TStatus[] getTStatus(List statuses) { - return statuses.stream().map(s -> getTStatus(s)).toArray(TStatus[]::new); + return statuses.stream().map(this::getTStatus).toArray(TStatus[]::new); } private TStatus getTStatus(String status) { @@ -418,15 +457,4 @@ private TStatus getTStatus(String status) { tStatus.setTStatus(status); return tStatus; } - - private TStatus[] getStatus() { - - return this.localStatus; - } - - private void setStatus(TStatus[] param) { - - this.localStatusTracker = param != null; - this.localStatus = param; - } } diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultApprovalWorkflow.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultApprovalWorkflow.java index 2bc5418..af28d29 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultApprovalWorkflow.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultApprovalWorkflow.java @@ -2,6 +2,8 @@ import org.wso2.carbon.identity.workflow.mgt.bean.Parameter; import org.wso2.carbon.identity.workflow.mgt.bean.metadata.InputData; +import org.wso2.carbon.identity.workflow.mgt.bean.metadata.Item; +import org.wso2.carbon.identity.workflow.mgt.bean.metadata.MapType; import org.wso2.carbon.identity.workflow.mgt.bean.metadata.ParameterMetaData; import org.wso2.carbon.identity.workflow.mgt.exception.WorkflowException; import org.wso2.carbon.identity.workflow.mgt.exception.WorkflowRuntimeException; @@ -11,6 +13,9 @@ import java.util.List; +import static org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants.ParameterName.BPS_PROFILE; +import static org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants.ParameterName.HT_SUBJECT; + /** * The class that extends the AbstractWorkflow class. */ @@ -29,7 +34,31 @@ public DefaultApprovalWorkflow(Class templateInit @Override protected InputData getInputData(ParameterMetaData parameterMetaData) { - return new InputData(); + InputData inputData = null; + if (parameterMetaData != null && parameterMetaData.getName() != null) { + String parameterName = parameterMetaData.getName(); + if (BPS_PROFILE.equals(parameterName)) { + inputData = new InputData(); + MapType mapType = new MapType(); + inputData.setMapType(mapType); + Item item = new Item(); + item.setKey("embeded_bps"); + item.setValue("embeded_bps"); + mapType.setItem(new Item[]{item}); + } else if (HT_SUBJECT.equals(parameterName)) { + inputData = new InputData(); + MapType mapType = new MapType(); + inputData.setMapType(mapType); + Item item1 = new Item(); + item1.setKey("subject1"); + item1.setValue("subject1"); + Item item2 = new Item(); + item2.setKey("subject2"); + item2.setValue("subject2"); + mapType.setItem(new Item[]{item1, item2}); + } + } + return inputData; } /** @@ -40,5 +69,4 @@ public void deploy(List parameterList) throws WorkflowException { super.deploy(parameterList); } - } diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultTemplateInitializer.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultTemplateInitializer.java new file mode 100644 index 0000000..7f3d3c9 --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultTemplateInitializer.java @@ -0,0 +1,61 @@ +package org.wso2.carbon.identity.workflow.engine; + +import org.apache.commons.lang.StringUtils; +import org.wso2.carbon.context.CarbonContext; +import org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants; +import org.wso2.carbon.identity.workflow.mgt.bean.Parameter; +import org.wso2.carbon.identity.workflow.mgt.util.WorkflowManagementUtil; +import org.wso2.carbon.identity.workflow.mgt.workflow.TemplateInitializer; +import org.wso2.carbon.utils.multitenancy.MultitenantConstants; + +import java.util.List; + +/** + * Implementation of TemplateInitializer interface that serves as the extension point for external workflow templates. + */ +public class DefaultTemplateInitializer implements TemplateInitializer { + + private String processName; + private String htName; + + private String role; + private String tenantContext = "" ; + private static final String HT_SUFFIX = "Task"; + + /** + * Initialize template at start up. + * + * @return false. + */ + @Override + public boolean initNeededAtStartUp() { + + return false; + } + + /** + * Initialize template. + * + * @param list parameters of template. + */ + @Override + public void initialize(List list) { + + Parameter wfNameParameter = WorkflowManagementUtil + .getParameter(list, WorkflowEngineConstants.ParameterValue.WORKFLOW_NAME, + WorkflowEngineConstants.ParameterHolder + .WORKFLOW_IMPL); + + if (wfNameParameter != null) { + processName = StringUtils.deleteWhitespace(wfNameParameter.getParamValue()); + role = WorkflowManagementUtil + .createWorkflowRoleName(StringUtils.deleteWhitespace(wfNameParameter.getParamValue())); + } + + String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain(); + if(!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)){ + tenantContext = "t/" + tenantDomain + "/"; + } + htName = processName + DefaultTemplateInitializer.HT_SUFFIX; + } +} diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/WorkflowEngineServiceComponent.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/WorkflowEngineServiceComponent.java index 7230a7b..3d307d1 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/WorkflowEngineServiceComponent.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/WorkflowEngineServiceComponent.java @@ -9,10 +9,11 @@ import org.osgi.service.component.annotations.ReferencePolicy; import org.wso2.carbon.identity.workflow.engine.ApprovalEventService; import org.wso2.carbon.identity.workflow.engine.DefaultApprovalWorkflow; +import org.wso2.carbon.identity.workflow.engine.DefaultTemplateInitializer; +import org.wso2.carbon.identity.workflow.engine.DefaultWorkflowExecutor; import org.wso2.carbon.identity.workflow.mgt.WorkflowExecutorManagerService; import org.wso2.carbon.identity.workflow.mgt.WorkflowManagementService; import org.wso2.carbon.identity.workflow.mgt.workflow.AbstractWorkflow; -import org.wso2.carbon.identity.workflow.engine.DefaultWorkflowExecutor; /** * OSGi declarative services component which handles registration and un-registration of workflow engine management @@ -32,7 +33,7 @@ public class WorkflowEngineServiceComponent { protected void activate(ComponentContext context) { BundleContext bundleContext = context.getBundleContext(); - bundleContext.registerService(AbstractWorkflow.class, new DefaultApprovalWorkflow(null, + bundleContext.registerService(AbstractWorkflow.class, new DefaultApprovalWorkflow(DefaultTemplateInitializer.class, DefaultWorkflowExecutor.class, getMetaDataXML()), null); ApprovalEventService approvalEventService=new ApprovalEventService(); bundleContext.registerService(ApprovalEventService.class, approvalEventService, null); @@ -43,12 +44,12 @@ private String getMetaDataXML() { return "\n" + "\n" + " workflowImplSimple\n" + - " ApprovalWorkflow\n" + - " Approval Workflow\n" + + " SimpleWorkflowEngine\n" + + " Simple WorkflowEngine\n" + " MultiStepApprovalTemplate\n" + " \n" + " \n" + - " BPS Profile(Server Profile Name)\n" + + " Profile(Server Profile Name)\n" + " \n" + " \n" + " Task Subject(Approval task subject to display)\n" + diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/WorkflowEventRequestDAO.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/WorkflowEventRequestDAO.java index af40af7..5cc828f 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/WorkflowEventRequestDAO.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/WorkflowEventRequestDAO.java @@ -87,6 +87,10 @@ public interface WorkflowEventRequestDAO { */ List getRequestIdFromApprover(String approverName); + List getRolesID(String userName); + + List getRoleNames(int roleId); + /** * Returns the tasks list given the authenticated approver name. * @@ -96,13 +100,12 @@ public interface WorkflowEventRequestDAO { List getTaskIdList(String approverName); /** - * Returns the events list according to the admin. + * Returns the events list according to the user. * * @param approverName admin user. - * @param name admin role. * @return events list. */ - List getRequestsFromAdmin(String approverName, String name); + List getRequestsList(String approverName); /** * Returns the event type given the request ID. @@ -151,11 +154,13 @@ public interface WorkflowEventRequestDAO { * @return approvers list. */ List listApprovers(String taskId); + /** - * Get approverName related to currentStep. + * Returns the task status given the request ID [RESERVED, READY or COMPLETED]. * - * @param taskId the unique ID that need to be checked. - * @return the value of the approver type. + * @param requestId the request ID that need to be checked. + * @return task status. */ - String getApproversOfCurrentStep(String taskId); + String getStatusOfTask(String requestId); + List getTaskId(String eventId); } diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImpl.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImpl.java index ff98d26..7b83351 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImpl.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImpl.java @@ -166,45 +166,63 @@ private void setPreparedStatementForStateOfRequest(int currentStep, String event * {@inheritDoc} */ @Override - public String getApproversOfCurrentStep(String taskId) { + public List listApprovers(String taskId) { JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); - String approverName; + List requestsList; try { - approverName = jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries. - GET_APPROVER_NAME_RELATED_TO_CURRENT_TASK_ID, - ((resultSet, i) -> ( - resultSet.getString(WorkflowEngineConstants.APPROVER_NAME_COLUMN))), + requestsList = jdbcTemplate.executeQuery(WorkflowEngineConstants.SqlQueries. + GET_APPROVER_NAME_RELATED_TO_CURRENT_TASK_ID, (resultSet, rowNumber) -> + new String(resultSet.getString(WorkflowEngineConstants.APPROVER_NAME_COLUMN)), preparedStatement -> preparedStatement.setString(1, taskId)); } catch (DataAccessException e) { - String errorMessage = String.format("Error occurred while retrieving approverName from" + + String errorMessage = String.format("Error occurred while retrieving approvers from" + "task Id: %s", taskId); throw new WorkflowEngineRuntimeException(errorMessage); } - return approverName; + return requestsList; } /** * {@inheritDoc} */ @Override - public List listApprovers(String taskId) { + public List getRolesID(String userName) { JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); - List requestsList; + List roleIdList; try { - requestsList = jdbcTemplate.executeQuery(WorkflowEngineConstants.SqlQueries. - GET_APPROVER_NAME_RELATED_TO_CURRENT_TASK_ID, (resultSet, rowNumber) -> - new String(resultSet.getString(WorkflowEngineConstants.APPROVER_NAME_COLUMN)), - preparedStatement -> { - preparedStatement.setString(1, taskId); - }); + roleIdList = jdbcTemplate.executeQuery(WorkflowEngineConstants.SqlQueries. + GET_ROLE_ID, (resultSet, rowNumber) -> + resultSet.getInt(WorkflowEngineConstants.ROLE_ID_COLUMN), + preparedStatement -> preparedStatement.setString(1, userName)); } catch (DataAccessException e) { - String errorMessage = String.format("Error occurred while retrieving approvers from" + - "task Id: %s", taskId); + String errorMessage = String.format("Error occurred while retrieving Role ID from" + + "user name: %s", userName); throw new WorkflowEngineRuntimeException(errorMessage); } - return requestsList; + return roleIdList; + } + + /** + * {@inheritDoc} + */ + @Override + public List getRoleNames(int roleId) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + List roleNameList; + try { + roleNameList = jdbcTemplate.executeQuery(WorkflowEngineConstants.SqlQueries. + GET_ROLE_NAME, (resultSet, rowNumber) -> + new String(resultSet.getString(WorkflowEngineConstants.ROLE_NAME)), + preparedStatement -> preparedStatement.setInt(1, roleId)); + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while retrieving Role name from" + + "role ID: %d", roleId); + throw new WorkflowEngineRuntimeException(errorMessage); + } + return roleNameList; } /** @@ -261,14 +279,12 @@ public List getRequestIdFromApprover(String approverName) { List requestIdList; try { requestIdList = jdbcTemplate.executeQuery(WorkflowEngineConstants.SqlQueries. - GET_EVENT_ID_FROM_APPROVER, (resultSet, rowNumber) -> - new String(resultSet.getString(WorkflowEngineConstants.EVENT_ID_FROM_APPROVER_COLUMN)), - preparedStatement -> { - preparedStatement.setString(1, approverName); - }); + GET_REQUEST_ID_FROM_APPROVER, (resultSet, rowNumber) -> + new String(resultSet.getString(WorkflowEngineConstants.Event_ID_COLUMN)), + preparedStatement -> preparedStatement.setString(1, approverName)); } catch (DataAccessException e) { String errorMessage = String.format("Error occurred while retrieving requestID from" + - "approver name: %s", approverName); + "user: %s", approverName); throw new WorkflowEngineRuntimeException(errorMessage); } return requestIdList; @@ -300,24 +316,23 @@ public List getTaskIdList(String approverName) { * {@inheritDoc} */ @Override - public List getRequestsFromAdmin(String approverName, String name) { + public List getRequestsList(String approverName) { JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); - List requestsList; + List requestIdList; try { - requestsList = jdbcTemplate.executeQuery(WorkflowEngineConstants.SqlQueries. - GET_EVENT_ID_FROM_ADMIN, (resultSet, rowNumber) -> - new String(resultSet.getString(WorkflowEngineConstants.EVENT_ID_FROM_APPROVER_COLUMN)), + requestIdList = jdbcTemplate.executeQuery(WorkflowEngineConstants.SqlQueries. + GET_REQUEST_ID_FROM_APPROVER, (resultSet, rowNumber) -> + new String(resultSet.getString(WorkflowEngineConstants.Event_ID_COLUMN)), preparedStatement -> { preparedStatement.setString(1, approverName); - preparedStatement.setString(2, name); }); } catch (DataAccessException e) { - String errorMessage = String.format("Error occurred while retrieving requestID from" + - "admin: %s", approverName); + String errorMessage = String.format("Error occurred while retrieving request id from" + + "approver name: %s", approverName); throw new WorkflowEngineRuntimeException(errorMessage); } - return requestsList; + return requestIdList; } /** @@ -336,7 +351,7 @@ public String getEventType(String requestId) { preparedStatement -> preparedStatement.setString(1, requestId)); } catch (DataAccessException e) { String errorMessage = String.format("Error occurred while retrieving event type from" + - "request Id: %s", requestId); + "request : %s", requestId); throw new WorkflowEngineRuntimeException(errorMessage); } return eventType; @@ -364,6 +379,28 @@ public String getTaskStatusOfRequest(String taskId) { return taskStatus; } + /** + * {@inheritDoc} + */ + @Override + public String getStatusOfTask(String requestId) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + String taskStatus; + try { + taskStatus = jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries. + GET_STATUS, + ((resultSet, i) -> ( + resultSet.getString(WorkflowEngineConstants.TASK_STATUS_COLUMN))), + preparedStatement -> preparedStatement.setString(1, requestId)); + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while retrieving task status from" + + "request Id: %s", requestId); + throw new WorkflowEngineRuntimeException(errorMessage); + } + return taskStatus; + } + @Override public void updateStatusOfRequest(String taskId, String taskStatus) { @@ -404,6 +441,25 @@ public String getRelationshipId(String eventId) { return taskStatus; } + public List getTaskId(String eventId) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + List taskIdList; + try { + taskIdList = jdbcTemplate.executeQuery(WorkflowEngineConstants.SqlQueries. + GET_TASK_ID_FROM_REQUEST, (resultSet, rowNumber) -> + new String(resultSet.getString(WorkflowEngineConstants.TASK_ID_COLUMN)), + preparedStatement -> { + preparedStatement.setString(1, eventId); + }); + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while retrieving tasks from" + + "request id : %s", eventId); + throw new WorkflowEngineRuntimeException(errorMessage); + } + return taskIdList; + } + /** * {@inheritDoc} */ diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/PagePagination.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/PagePagination.java new file mode 100644 index 0000000..6bc47db --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/PagePagination.java @@ -0,0 +1,41 @@ +package org.wso2.carbon.identity.workflow.engine.model; + +/** + * The model class for set up the page pagination. + */ +public class PagePagination { + + private int localPageSize; + private boolean localPageSizeTracker = false; + private int localPageNumber; + private boolean localPageNumberTracker = false; + + /** + * Maximum number of results expected. + */ + public int getPageSize() { + + return this.localPageSize; + } + + public void setPageSize(int param) { + + this.localPageSizeTracker = param != Integer.MIN_VALUE; + this.localPageSize = param; + } + + /** + * Start index of the search. + */ + public int getPageNumber() { + + return this.localPageNumber; + } + + public void setPageNumber(int param) { + + this.localPageNumberTracker = param != Integer.MIN_VALUE; + this.localPageNumber = param; + } + +} diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java index e1cb062..23e0dca 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java @@ -10,11 +10,12 @@ public class WorkflowEngineConstants { public static final String APPROVER_NAME_COLUMN = "APPROVER_NAME"; public static final String Event_ID_COLUMN = "EVENT_ID"; public static final String CREATED_USER_COLUMN = "CREATED_BY"; - public static final String EVENT_ID_FROM_APPROVER_COLUMN = "EVENT_ID"; public static final String EVENT_TYPE_COLUMN = "OPERATION_TYPE"; public static final String TASK_STATUS_COLUMN = "TASK_STATUS"; public static final String CREATED_AT_IN_MILL_COLUMN = "CREATED_AT"; public static final String RELATIONSHIP_ID_IN_REQUEST_COLUMN = "RELATIONSHIP_ID"; + public static final String ROLE_ID_COLUMN = "UM_ROLE_ID"; + public static final String ROLE_NAME = "UM_ROLE_NAME"; public static class SqlQueries { @@ -27,15 +28,18 @@ public static class SqlQueries { public static final String GET_APPROVER_NAME_RELATED_TO_CURRENT_TASK_ID = "SELECT DISTINCT APPROVER_NAME FROM WF_WORKFLOW_APPROVAL_RELATION WHERE TASK_ID = ?"; public static final String UPDATE_TASK_STATUS = "UPDATE WF_WORKFLOW_APPROVAL_RELATION SET TASK_STATUS=? WHERE TASK_ID=?"; public static final String GET_REQUEST_ID = "SELECT DISTINCT EVENT_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE TASK_ID = ?"; - public static final String GET_EVENT_ID_FROM_APPROVER = "SELECT EVENT_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE APPROVER_NAME= ?"; public static final String GET_TASK_ID_FROM_APPROVER = "SELECT TASK_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE APPROVER_NAME= ?"; - public static final String GET_EVENT_ID_FROM_ADMIN = "SELECT EVENT_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE APPROVER_NAME= ? OR APPROVER_NAME=?"; + public static final String GET_TASK_ID_FROM_REQUEST = "SELECT TASK_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE EVENT_ID= ?"; + public static final String GET_REQUEST_ID_FROM_APPROVER = "SELECT EVENT_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE APPROVER_NAME= ?"; public static final String GET_TASK_STATUS = "SELECT DISTINCT TASK_STATUS FROM WF_WORKFLOW_APPROVAL_RELATION WHERE TASK_ID = ?"; + public static final String GET_STATUS = "SELECT DISTINCT TASK_STATUS FROM WF_WORKFLOW_APPROVAL_RELATION WHERE EVENT_ID = ?"; public static final String GET_CREATED_USER = "SELECT CREATED_BY FROM WF_REQUEST WHERE UUID= ?"; public static final String GET_EVENT_TYPE = "SELECT OPERATION_TYPE FROM WF_REQUEST WHERE UUID= ?"; public static final String GET_CREATED_TIME_IN_MILL = "SELECT CREATED_AT FROM WF_REQUEST WHERE UUID= ?"; public static final String GET_REQUEST_ID_OF_RELATIONSHIP = "SELECT RELATIONSHIP_ID FROM " + "WF_WORKFLOW_REQUEST_RELATION WHERE REQUEST_ID = ?"; + public static final String GET_ROLE_ID = "SELECT UM_ROLE_ID FROM UM_HYBRID_USER_ROLE WHERE UM_USER_NAME=?"; + public static final String GET_ROLE_NAME = "SELECT UM_ROLE_NAME FROM UM_HYBRID_ROLE WHERE UM_ID=?"; } public static class ParameterName { @@ -46,12 +50,16 @@ public static class ParameterName { public static final String TASK_SUBJECT = "HTSubject"; public static final String TASK_DESCRIPTION = "HTDescription"; public static final String PRIORITY = "0"; - public static final String TENANT_NAME = "TenantNameFromContext"; - public static final String APPROVAL_TASK = "Approve task"; - public static final String ADMIN = "admin"; - public static final String INTERNAL_ADMIN = "Internal/"; + public static final String APPROVAL_TASK = "Approval task"; + public static final String INTERNAL_USER = "Internal/"; + public static final String APPLICATION_USER = "Application"; + public static final String SYSTEM_USER = "system"; + public static final String SYSTEM_PRIMARY_USER = "system_primary_"; public static final String ASSIGNEE_TYPE = "Type"; public static final String STATUS_ERROR = "Status is notAcceptable"; + public static final String CREDENTIAL = "Credential"; + public static final String BPS_PROFILE = "BPSProfile"; + public static final String HT_SUBJECT = "HTSubject"; } public enum TaskStatus { @@ -63,4 +71,18 @@ private TaskStatus() { } } + + public static class ParameterHolder { + public static final String WORKFLOW_IMPL = "WorkflowImpl"; + + public ParameterHolder() { + } + } + + public static class ParameterValue { + public static final String WORKFLOW_NAME = "WorkflowName"; + + public ParameterValue() { + } + } } From 0556293adf795bc897d3d251752d90ef5201ecd6 Mon Sep 17 00:00:00 2001 From: IsharaSilva Date: Wed, 29 Jun 2022 19:45:52 +0530 Subject: [PATCH 07/16] Implement workflow event approval action --- .../workflow/engine/ApprovalEventService.java | 22 +++---- .../workflow/engine/dto/PropertyDTO.java | 12 ++-- .../internal/dao/WorkflowEventRequestDAO.java | 3 +- .../dao/impl/WorkflowEventRequestDAOImpl.java | 36 +++++------ .../workflow/engine/model/TaskModel.java | 60 ------------------- .../engine/util/WorkflowEngineConstants.java | 3 +- 6 files changed, 31 insertions(+), 105 deletions(-) diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/ApprovalEventService.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/ApprovalEventService.java index 536269e..7761c36 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/ApprovalEventService.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/ApprovalEventService.java @@ -76,10 +76,6 @@ public List listTasks(Integer limit, Integer offset, List 0 ? pagePagination.getPageSize() : 20; - //Number of pages showing the task list. - int pages = (int) Math.ceil((double) taskCount[0] / (double) pageSize); Set taskSummaryDTOs = null; List tasks = listTasksOfApprovers(status); int taskListSize = tasks.size(); @@ -98,9 +94,9 @@ private List listTasksOfApprovers(List status) { DefaultWorkflowEventRequest defaultWorkflowEventRequest = new DefaultWorkflowEventRequestService(); List allRequestsList = getAllRequestRelatedUserAndRole(); List taskSummaryDTOList = new ArrayList<>(); - for (int i = 0; i < allRequestsList.size(); i++) { + for (String s : allRequestsList) { TaskSummaryDTO summeryDTO = new TaskSummaryDTO(); - String eventId = getTaskRelatedStatus(allRequestsList.get(i), status); + String eventId = getTaskRelatedStatus(s, status); if (eventId != null) { WorkflowRequest request = getWorkflowRequest(eventId); TaskDetails taskDetails = getTaskDetails(request); @@ -240,12 +236,11 @@ public TaskDataDTO getTaskData(String taskId) { public void updateStatus(String taskId, StateDTO nextState) { WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); - String userName = CarbonContext.getThreadLocalCarbonContext().getUsername(); - validateApprovers(taskId, userName); + validateApprovers(taskId); switch (nextState.getAction()) { case APPROVE: updateTaskStatusOfRequest(taskId, APPROVED); - updateStepDetailsOfRequest(taskId, APPROVED); + updateStepDetailsOfRequest(taskId); break; case REJECT: String eventId = workflowEventRequestDAO.getRequestID(taskId); @@ -275,7 +270,7 @@ private WorkflowRequest getWorkflowRequest(String requestId) { return request; } - private void updateStepDetailsOfRequest(String taskId, String status) { + private void updateStepDetailsOfRequest(String taskId) { WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); String eventId = workflowEventRequestDAO.getRequestID(taskId); @@ -288,11 +283,11 @@ private void updateStepDetailsOfRequest(String taskId, String status) { if (stepValue < numOfStates(request)) { defaultWorkflowEventRequest.addApproversOfRequests(request, parameterList); } else { - completeRequest(eventId, status); + completeRequest(eventId, ApprovalEventService.APPROVED); } } - private boolean validateApprovers(String taskId, String approverName) { + private void validateApprovers(String taskId) { WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); List eventList =getAllRequestRelatedUserAndRole(); @@ -305,10 +300,9 @@ private boolean validateApprovers(String taskId, String approverName) { for (String task : lst) { if (taskId.equals(task)) { - return true; + return; } } - return false; } private int numOfStates(WorkflowRequest request) { diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/PropertyDTO.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/PropertyDTO.java index 1da10b5..bd0329d 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/PropertyDTO.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/PropertyDTO.java @@ -38,11 +38,11 @@ public void setValue(String value) { @Override public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("class PropertyDTO {\n"); - sb.append(" key: ").append(key).append("\n"); - sb.append(" value: ").append(value).append("\n"); - sb.append("}\n"); - return sb.toString(); + StringBuilder builder = new StringBuilder(); + builder.append("class PropertyDTO {\n"); + builder.append(" key: ").append(key).append("\n"); + builder.append(" value: ").append(value).append("\n"); + builder.append("}\n"); + return builder.toString(); } } \ No newline at end of file diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/WorkflowEventRequestDAO.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/WorkflowEventRequestDAO.java index 5cc828f..86a860f 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/WorkflowEventRequestDAO.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/WorkflowEventRequestDAO.java @@ -17,9 +17,8 @@ public interface WorkflowEventRequestDAO { * @param approverType the type of the approved user EX: user or Role. * @param approverName the value of the approver type. * @param taskStatus state of the tasks [RESERVED, READY or COMPLETED]. - * @return event ID. */ - String addApproversOfRequest(String taskId, String eventId, String workflowId, String approverType, String approverName, String taskStatus); + void addApproversOfRequest(String taskId, String eventId, String workflowId, String approverType, String approverName, String taskStatus); /** * Get taskId from table. diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImpl.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImpl.java index 7b83351..aaf291a 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImpl.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImpl.java @@ -21,8 +21,8 @@ public class WorkflowEventRequestDAOImpl implements WorkflowEventRequestDAO { * {@inheritDoc} */ @Override - public String addApproversOfRequest(String taskId, String eventId, String workflowId, String approverType, - String approverName, String taskStatus) { + public void addApproversOfRequest(String taskId, String eventId, String workflowId, String approverType, + String approverName, String taskStatus) { JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); try { @@ -40,7 +40,6 @@ public String addApproversOfRequest(String taskId, String eventId, String workfl "in eventId: %s & workflowId: %s", eventId, workflowId); throw new WorkflowEngineRuntimeException(errorMessage); } - return taskId; } /** @@ -126,7 +125,7 @@ public int getStateOfRequest(String eventId, String workflowId) { } } catch (DataAccessException e) { String errorMessage = String.format("Error occurred while retrieving currentStep from" + - "event Id: %s & workflowId: %s", eventId); + "event Id: %s & workflowId: %s", eventId, workflowId); throw new WorkflowEngineRuntimeException(errorMessage); } return Integer.parseInt(stepExists); @@ -173,11 +172,10 @@ public List listApprovers(String taskId) { try { requestsList = jdbcTemplate.executeQuery(WorkflowEngineConstants.SqlQueries. GET_APPROVER_NAME_RELATED_TO_CURRENT_TASK_ID, (resultSet, rowNumber) -> - new String(resultSet.getString(WorkflowEngineConstants.APPROVER_NAME_COLUMN)), + resultSet.getString(WorkflowEngineConstants.APPROVER_NAME_COLUMN), preparedStatement -> preparedStatement.setString(1, taskId)); } catch (DataAccessException e) { - String errorMessage = String.format("Error occurred while retrieving approvers from" + - "task Id: %s", taskId); + String errorMessage = "Error occurred while retrieving approvers"; throw new WorkflowEngineRuntimeException(errorMessage); } return requestsList; @@ -215,7 +213,7 @@ public List getRoleNames(int roleId) { try { roleNameList = jdbcTemplate.executeQuery(WorkflowEngineConstants.SqlQueries. GET_ROLE_NAME, (resultSet, rowNumber) -> - new String(resultSet.getString(WorkflowEngineConstants.ROLE_NAME)), + resultSet.getString(WorkflowEngineConstants.ROLE_NAME), preparedStatement -> preparedStatement.setInt(1, roleId)); } catch (DataAccessException e) { String errorMessage = String.format("Error occurred while retrieving Role name from" + @@ -237,7 +235,7 @@ public String getRequestID(String taskId) { requestId = jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries. GET_REQUEST_ID, ((resultSet, i) -> ( - resultSet.getString(WorkflowEngineConstants.Event_ID_COLUMN))), + resultSet.getString(WorkflowEngineConstants.EVENT_ID))), preparedStatement -> preparedStatement.setString(1, taskId)); } catch (DataAccessException e) { String errorMessage = String.format("Error occurred while retrieving request ID from" + @@ -280,7 +278,7 @@ public List getRequestIdFromApprover(String approverName) { try { requestIdList = jdbcTemplate.executeQuery(WorkflowEngineConstants.SqlQueries. GET_REQUEST_ID_FROM_APPROVER, (resultSet, rowNumber) -> - new String(resultSet.getString(WorkflowEngineConstants.Event_ID_COLUMN)), + resultSet.getString(WorkflowEngineConstants.EVENT_ID), preparedStatement -> preparedStatement.setString(1, approverName)); } catch (DataAccessException e) { String errorMessage = String.format("Error occurred while retrieving requestID from" + @@ -300,10 +298,8 @@ public List getTaskIdList(String approverName) { try { requestIdList = jdbcTemplate.executeQuery(WorkflowEngineConstants.SqlQueries. GET_TASK_ID_FROM_APPROVER, (resultSet, rowNumber) -> - new String(resultSet.getString(WorkflowEngineConstants.TASK_ID_COLUMN)), - preparedStatement -> { - preparedStatement.setString(1, approverName); - }); + resultSet.getString(WorkflowEngineConstants.TASK_ID_COLUMN), + preparedStatement -> preparedStatement.setString(1, approverName)); } catch (DataAccessException e) { String errorMessage = String.format("Error occurred while retrieving task id from" + "approver name: %s", approverName); @@ -323,10 +319,8 @@ public List getRequestsList(String approverName) { try { requestIdList = jdbcTemplate.executeQuery(WorkflowEngineConstants.SqlQueries. GET_REQUEST_ID_FROM_APPROVER, (resultSet, rowNumber) -> - new String(resultSet.getString(WorkflowEngineConstants.Event_ID_COLUMN)), - preparedStatement -> { - preparedStatement.setString(1, approverName); - }); + resultSet.getString(WorkflowEngineConstants.EVENT_ID), + preparedStatement -> preparedStatement.setString(1, approverName)); } catch (DataAccessException e) { String errorMessage = String.format("Error occurred while retrieving request id from" + "approver name: %s", approverName); @@ -448,10 +442,8 @@ public List getTaskId(String eventId) { try { taskIdList = jdbcTemplate.executeQuery(WorkflowEngineConstants.SqlQueries. GET_TASK_ID_FROM_REQUEST, (resultSet, rowNumber) -> - new String(resultSet.getString(WorkflowEngineConstants.TASK_ID_COLUMN)), - preparedStatement -> { - preparedStatement.setString(1, eventId); - }); + resultSet.getString(WorkflowEngineConstants.TASK_ID_COLUMN), + preparedStatement -> preparedStatement.setString(1, eventId)); } catch (DataAccessException e) { String errorMessage = String.format("Error occurred while retrieving tasks from" + "request id : %s", eventId); diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TaskModel.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TaskModel.java index 8d8a54f..a698964 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TaskModel.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TaskModel.java @@ -8,11 +8,6 @@ public class TaskModel { private String id; - private String taskSubject; - private String taskDescription; - private String priority; - private String htInitiator; - private String approvalStatus; private Map assignees; /** @@ -26,61 +21,6 @@ public void setId(String id) { this.id = id; } - /** - * Subject of the Approval - **/ - public String getTaskSubject() { - return taskSubject; - } - - public void setTaskSubject(String taskSubject) { - this.taskSubject = taskSubject; - } - - /** - * Description on the Approval task - **/ - public String getTaskDescription() { - return taskDescription; - } - - public void setTaskDescription(String taskDescription) { - this.taskDescription = taskDescription; - } - - /** - * Priority of the Approval task - **/ - public String getPriority() { - return priority; - } - - public void setPriority(String priority) { - this.priority = priority; - } - - /** - * The user who initiated the task - **/ - public String getHtInitiator() { - return htInitiator; - } - - public void setHtInitiator(String htInitiator) { - this.htInitiator = htInitiator; - } - - /** - * Available only for the completed Tasks, APPROVED or REJECTED if the task has been completed, PENDING otherwise\n - **/ - public String getApprovalStatus() { - return approvalStatus; - } - - public void setApprovalStatus(String approvalStatus) { - this.approvalStatus = approvalStatus; - } - /** * To whom the task is assigned:\n * user - username(s) if the task is reserved for specific user(s).\n * group - role name(s) if the task is assignable for group(s).\n **/ diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java index 23e0dca..c5fd42b 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java @@ -8,7 +8,7 @@ public class WorkflowEngineConstants { public static final String CURRENT_STEP_COLUMN = "CURRENT_STEP"; public static final String TASK_ID_COLUMN = "TASK_ID"; public static final String APPROVER_NAME_COLUMN = "APPROVER_NAME"; - public static final String Event_ID_COLUMN = "EVENT_ID"; + public static final String EVENT_ID = "EVENT_ID"; public static final String CREATED_USER_COLUMN = "CREATED_BY"; public static final String EVENT_TYPE_COLUMN = "OPERATION_TYPE"; public static final String TASK_STATUS_COLUMN = "TASK_STATUS"; @@ -21,6 +21,7 @@ public static class SqlQueries { public static final String ADD_APPROVAL_LIST_RELATED_TO_USER = "INSERT INTO WF_WORKFLOW_APPROVAL_RELATION (TASK_ID,EVENT_ID,WORKFLOW_ID,APPROVER_TYPE,APPROVER_NAME, TASK_STATUS) VALUES (?,?,?,?,?,?)"; public static final String GET_TASK_ID_RELATED_TO_USER = "SELECT DISTINCT TASK_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE EVENT_ID = ?"; + public static final String GET_WORKFLOW_ID_RELATED_TO_TASK = "SELECT DISTINCT WORKFLOW_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE TASK_ID = ?"; public static final String DELETE_APPROVAL_LIST_RELATED_TO_USER = "DELETE FROM WF_WORKFLOW_APPROVAL_RELATION WHERE TASK_ID=?"; public static final String ADD_CURRENT_STEP_FOR_EVENT = "INSERT INTO WF_WORKFLOW_APPROVAL_STATE (EVENT_ID,WORKFLOW_ID, CURRENT_STEP) VALUES (?,?,?)"; public static final String GET_CURRENT_STEP = "SELECT CURRENT_STEP FROM WF_WORKFLOW_APPROVAL_STATE WHERE EVENT_ID = ? AND WORKFLOW_ID = ?"; From 665b26c02069ff9b4b0aa9d30524aede62215454 Mon Sep 17 00:00:00 2001 From: IsharaSilva Date: Wed, 29 Jun 2022 20:25:43 +0530 Subject: [PATCH 08/16] Implement workflow event approval action --- .../org.wso2.carbon.identity.workflow.engine/pom.xml | 3 +-- .../workflow/engine/DefaultApprovalWorkflow.java | 11 +---------- .../workflow/engine/DefaultTemplateInitializer.java | 1 - pom.xml | 2 +- 4 files changed, 3 insertions(+), 14 deletions(-) diff --git a/components/org.wso2.carbon.identity.workflow.engine/pom.xml b/components/org.wso2.carbon.identity.workflow.engine/pom.xml index 9cca4df..386bf1a 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/pom.xml +++ b/components/org.wso2.carbon.identity.workflow.engine/pom.xml @@ -6,8 +6,7 @@ default.workflow.engine org.wso2.carbon.identity.workflow.engine - - 1.0-SNAPSHOT + 1.0.0-SNAPSHOT ../../pom.xml workflow.engine diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultApprovalWorkflow.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultApprovalWorkflow.java index af28d29..c42e25f 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultApprovalWorkflow.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultApprovalWorkflow.java @@ -13,7 +13,6 @@ import java.util.List; -import static org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants.ParameterName.BPS_PROFILE; import static org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants.ParameterName.HT_SUBJECT; /** @@ -37,15 +36,7 @@ protected InputData getInputData(ParameterMetaData parameterMetaData) { InputData inputData = null; if (parameterMetaData != null && parameterMetaData.getName() != null) { String parameterName = parameterMetaData.getName(); - if (BPS_PROFILE.equals(parameterName)) { - inputData = new InputData(); - MapType mapType = new MapType(); - inputData.setMapType(mapType); - Item item = new Item(); - item.setKey("embeded_bps"); - item.setValue("embeded_bps"); - mapType.setItem(new Item[]{item}); - } else if (HT_SUBJECT.equals(parameterName)) { + if (HT_SUBJECT.equals(parameterName)) { inputData = new InputData(); MapType mapType = new MapType(); inputData.setMapType(mapType); diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultTemplateInitializer.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultTemplateInitializer.java index 7f3d3c9..deee0bc 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultTemplateInitializer.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultTemplateInitializer.java @@ -17,7 +17,6 @@ public class DefaultTemplateInitializer implements TemplateInitializer { private String processName; private String htName; - private String role; private String tenantContext = "" ; private static final String HT_SUFFIX = "Task"; diff --git a/pom.xml b/pom.xml index dcfdb31..3273a1c 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ org.wso2.carbon.identity.workflow.engine 4.0.0 default.workflow.engine - 1.0-SNAPSHOT + 1.0.0-SNAPSHOT pom WSO2 Carbon - Identity Workflow Simple Module From f476a48788bc184917fca90eb0979603987f8a48 Mon Sep 17 00:00:00 2001 From: IsharaSilva Date: Wed, 29 Jun 2022 21:51:47 +0530 Subject: [PATCH 09/16] Implement workflow approvalService --- .../pom.xml | 2 +- .../workflow/engine/ApprovalEventService.java | 4 +-- .../DefaultWorkflowEventRequestService.java | 25 ++++++++----------- .../workflow/engine/dto/PropertyDTO.java | 2 +- .../WorkflowEngineRuntimeException.java | 6 ----- .../WorkflowEngineServiceComponent.java | 7 ++---- .../workflow/engine/model/PagePagination.java | 1 - .../workflow/engine/model/TStatus.java | 1 - .../engine/util/WorkflowEngineConstants.java | 1 - pom.xml | 2 +- 10 files changed, 17 insertions(+), 34 deletions(-) diff --git a/components/org.wso2.carbon.identity.workflow.engine/pom.xml b/components/org.wso2.carbon.identity.workflow.engine/pom.xml index 386bf1a..7d9da20 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/pom.xml +++ b/components/org.wso2.carbon.identity.workflow.engine/pom.xml @@ -6,7 +6,7 @@ default.workflow.engine org.wso2.carbon.identity.workflow.engine - 1.0.0-SNAPSHOT + 1.0-SNAPSHOT ../../pom.xml workflow.engine diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/ApprovalEventService.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/ApprovalEventService.java index 7761c36..8fbd69a 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/ApprovalEventService.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/ApprovalEventService.java @@ -94,9 +94,9 @@ private List listTasksOfApprovers(List status) { DefaultWorkflowEventRequest defaultWorkflowEventRequest = new DefaultWorkflowEventRequestService(); List allRequestsList = getAllRequestRelatedUserAndRole(); List taskSummaryDTOList = new ArrayList<>(); - for (String s : allRequestsList) { + for (String task : allRequestsList) { TaskSummaryDTO summeryDTO = new TaskSummaryDTO(); - String eventId = getTaskRelatedStatus(s, status); + String eventId = getTaskRelatedStatus(task, status); if (eventId != null) { WorkflowRequest request = getWorkflowRequest(eventId); TaskDetails taskDetails = getTaskDetails(request); diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultWorkflowEventRequestService.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultWorkflowEventRequestService.java index 45d6c69..aae8b30 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultWorkflowEventRequestService.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultWorkflowEventRequestService.java @@ -43,8 +43,6 @@ public void addApproversOfRequests(WorkflowRequest request, List para } currentStepValue += 1; updateStateOfRequest(eventId, workflowId); - List associations = getAssociations(request); - for (WorkflowAssociation association : associations) { for (Parameter parameter : parameterList) { if (parameter.getParamName().equals(WorkflowEngineConstants.ParameterName.USER_AND_ROLE_STEP)) { String[] stepName = parameter.getqName().split("-"); @@ -54,19 +52,16 @@ public void addApproversOfRequests(WorkflowRequest request, List para String approver = parameter.getParamValue(); if (approver != null && !approver.isEmpty()) { - String[] approvers = approver.split("[,]", 0); - if (approvers != null) { - for (String name : approvers) { - approverName = name; - String taskStatus= WorkflowEngineConstants.ParameterName.TASK_STATUS_DEFAULT; - workflowEventRequestDAO.addApproversOfRequest(taskId, eventId, workflowId, - approverType, approverName,taskStatus); - } + String[] approvers = approver.split(",", 0); + for (String name : approvers) { + approverName = name; + String taskStatus= WorkflowEngineConstants.ParameterName.TASK_STATUS_DEFAULT; + workflowEventRequestDAO.addApproversOfRequest(taskId, eventId, workflowId, + approverType, approverName,taskStatus); } } } } - } } } @@ -91,16 +86,16 @@ public String getWorkflowId(WorkflowRequest request) { WorkflowManagementService workflowManagementService = new WorkflowManagementServiceImpl(); List associations = getAssociations(request); - Workflow workflow = null; - String workflowId; + String workflowId = null; for (WorkflowAssociation association : associations) { try { - workflow = workflowManagementService.getWorkflow(association.getWorkflowId()); + Workflow workflow = workflowManagementService.getWorkflow(association.getWorkflowId()); + workflowId = workflow.getWorkflowId(); } catch (WorkflowException e) { throw new WorkflowEngineException("The workflow Id is not valid"); } } - workflowId = workflow.getWorkflowId(); + return workflowId; } diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/PropertyDTO.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/PropertyDTO.java index bd0329d..71b0785 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/PropertyDTO.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/PropertyDTO.java @@ -45,4 +45,4 @@ public String toString() { builder.append("}\n"); return builder.toString(); } -} \ No newline at end of file +} diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/exception/WorkflowEngineRuntimeException.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/exception/WorkflowEngineRuntimeException.java index ffb7764..c5cdd8e 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/exception/WorkflowEngineRuntimeException.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/exception/WorkflowEngineRuntimeException.java @@ -1,7 +1,5 @@ package org.wso2.carbon.identity.workflow.engine.exception; -import org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException; - /** * Exception class to handle runtime exceptions. */ @@ -9,10 +7,6 @@ public class WorkflowEngineRuntimeException extends RuntimeException { public WorkflowEngineRuntimeException(String message) { - super(message); - } - public WorkflowEngineRuntimeException(String message, DataAccessException e) { - super(message); } } diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/WorkflowEngineServiceComponent.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/WorkflowEngineServiceComponent.java index 3d307d1..47aed3e 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/WorkflowEngineServiceComponent.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/WorkflowEngineServiceComponent.java @@ -48,14 +48,11 @@ private String getMetaDataXML() { " Simple WorkflowEngine\n" + " MultiStepApprovalTemplate\n" + " \n" + - " \n" + - " Profile(Server Profile Name)\n" + - " \n" + " \n" + - " Task Subject(Approval task subject to display)\n" + + " Subject(Approval task subject to display)\n" + " \n" + " \n" + - " Task Detail(Approval task description)\n" + + " Detail(Approval task description)\n" + " \n" + " \n" + "\n" + diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/PagePagination.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/PagePagination.java index 6bc47db..1b24a62 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/PagePagination.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/PagePagination.java @@ -37,5 +37,4 @@ public void setPageNumber(int param) { this.localPageNumberTracker = param != Integer.MIN_VALUE; this.localPageNumber = param; } - } diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TStatus.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TStatus.java index 63a99f4..e949a77 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TStatus.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TStatus.java @@ -23,5 +23,4 @@ public void setTStatus(String param) { this.localTStatus = param; } - } diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java index c5fd42b..93c33dc 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java @@ -21,7 +21,6 @@ public static class SqlQueries { public static final String ADD_APPROVAL_LIST_RELATED_TO_USER = "INSERT INTO WF_WORKFLOW_APPROVAL_RELATION (TASK_ID,EVENT_ID,WORKFLOW_ID,APPROVER_TYPE,APPROVER_NAME, TASK_STATUS) VALUES (?,?,?,?,?,?)"; public static final String GET_TASK_ID_RELATED_TO_USER = "SELECT DISTINCT TASK_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE EVENT_ID = ?"; - public static final String GET_WORKFLOW_ID_RELATED_TO_TASK = "SELECT DISTINCT WORKFLOW_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE TASK_ID = ?"; public static final String DELETE_APPROVAL_LIST_RELATED_TO_USER = "DELETE FROM WF_WORKFLOW_APPROVAL_RELATION WHERE TASK_ID=?"; public static final String ADD_CURRENT_STEP_FOR_EVENT = "INSERT INTO WF_WORKFLOW_APPROVAL_STATE (EVENT_ID,WORKFLOW_ID, CURRENT_STEP) VALUES (?,?,?)"; public static final String GET_CURRENT_STEP = "SELECT CURRENT_STEP FROM WF_WORKFLOW_APPROVAL_STATE WHERE EVENT_ID = ? AND WORKFLOW_ID = ?"; diff --git a/pom.xml b/pom.xml index 3273a1c..dcfdb31 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ org.wso2.carbon.identity.workflow.engine 4.0.0 default.workflow.engine - 1.0.0-SNAPSHOT + 1.0-SNAPSHOT pom WSO2 Carbon - Identity Workflow Simple Module From 48d48bd979b858c676630d93bcdea9f7792eb135 Mon Sep 17 00:00:00 2001 From: IsharaSilva Date: Tue, 5 Jul 2022 13:07:04 +0530 Subject: [PATCH 10/16] Implement workflow approvalService --- .../workflow/engine/ApprovalEventService.java | 4 +++ .../engine/DefaultWorkflowEventRequest.java | 7 +++++ .../DefaultWorkflowEventRequestService.java | 10 +++++++ .../internal/dao/WorkflowEventRequestDAO.java | 26 +++++++++++++++++++ .../dao/impl/WorkflowEventRequestDAOImpl.java | 24 +++++++++++++++++ .../engine/util/WorkflowEngineConstants.java | 10 ++----- 6 files changed, 73 insertions(+), 8 deletions(-) diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/ApprovalEventService.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/ApprovalEventService.java index 8fbd69a..51c87f4 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/ApprovalEventService.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/ApprovalEventService.java @@ -236,6 +236,7 @@ public TaskDataDTO getTaskData(String taskId) { public void updateStatus(String taskId, StateDTO nextState) { WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); + DefaultWorkflowEventRequest defaultWorkflowEventRequest = new DefaultWorkflowEventRequestService(); validateApprovers(taskId); switch (nextState.getAction()) { case APPROVE: @@ -245,6 +246,7 @@ public void updateStatus(String taskId, StateDTO nextState) { case REJECT: String eventId = workflowEventRequestDAO.getRequestID(taskId); updateTaskStatusOfRequest(taskId, REJECTED); + defaultWorkflowEventRequest.deleteApprovalOfRequest(taskId); completeRequest(eventId, REJECTED); break; case RELEASE: @@ -345,11 +347,13 @@ private void completeRequest(String eventId, String status) { WSWorkflowResponse wsWorkflowResponse = new WSWorkflowResponse(); WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); + DefaultWorkflowEventRequest defaultWorkflowEventRequest = new DefaultWorkflowEventRequestService(); String relationshipId = workflowEventRequestDAO.getRelationshipId(eventId); wsWorkflowResponse.setUuid(relationshipId); wsWorkflowResponse.setStatus(status); WSWorkflowCallBackService wsWorkflowCallBackService = new WSWorkflowCallBackService(); wsWorkflowCallBackService.onCallback(wsWorkflowResponse); + defaultWorkflowEventRequest.deleteStateOfRequest(eventId); } private TaskDetails getTaskDetails(WorkflowRequest workflowRequest) { diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultWorkflowEventRequest.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultWorkflowEventRequest.java index 5444431..60d82e4 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultWorkflowEventRequest.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultWorkflowEventRequest.java @@ -75,4 +75,11 @@ public interface DefaultWorkflowEventRequest { * @return workflow Id. */ String getWorkflowId(WorkflowRequest request); + + /** + * Delete the current step using giving eventId + * + * @param eventId the request ID that need to be checked. + */ + void deleteStateOfRequest(String eventId); } diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultWorkflowEventRequestService.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultWorkflowEventRequestService.java index aae8b30..c1b151f 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultWorkflowEventRequestService.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultWorkflowEventRequestService.java @@ -168,4 +168,14 @@ public void updateStateOfRequest(String eventId, String workflowId) { currentStep += 1; workflowEventRequestDAO.updateStateOfRequest(eventId, workflowId, currentStep); } + + /** + * {@inheritDoc} + */ + @Override + public void deleteStateOfRequest(String eventId) { + + WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); + workflowEventRequestDAO.deleteCurrentStepOfRequest(eventId); + } } diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/WorkflowEventRequestDAO.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/WorkflowEventRequestDAO.java index 86a860f..0aa5b78 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/WorkflowEventRequestDAO.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/WorkflowEventRequestDAO.java @@ -86,8 +86,20 @@ public interface WorkflowEventRequestDAO { */ List getRequestIdFromApprover(String approverName); + /** + * Retrieve the role id list giving the username. + * + * @param userName the username that need to be checked. + * @return role ID list. + */ List getRolesID(String userName); + /** + * Get the role name giving the role ID. + * + * @param roleId the roleID that need to be checked. + * @return role name list. + */ List getRoleNames(int roleId); /** @@ -161,5 +173,19 @@ public interface WorkflowEventRequestDAO { * @return task status. */ String getStatusOfTask(String requestId); + + /** + * Retrieve the tasks list giving event ID. + * + * @param eventId the request ID that need to be checked. + * @return tasks list. + */ List getTaskId(String eventId); + + /** + * Delete the current step using giving event ID. + * + * @param eventId the request ID that need to be checked. + */ + void deleteCurrentStepOfRequest(String eventId); } diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImpl.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImpl.java index aaf291a..e785420 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImpl.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImpl.java @@ -291,6 +291,7 @@ public List getRequestIdFromApprover(String approverName) { /** * {@inheritDoc} */ + @Override public List getTaskIdList(String approverName) { JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); @@ -395,6 +396,9 @@ public String getStatusOfTask(String requestId) { return taskStatus; } + /** + * {@inheritDoc} + */ @Override public void updateStatusOfRequest(String taskId, String taskStatus) { @@ -435,6 +439,10 @@ public String getRelationshipId(String eventId) { return taskStatus; } + /** + * {@inheritDoc} + */ + @Override public List getTaskId(String eventId) { JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); @@ -480,4 +488,20 @@ private void setPreparedStatementForStatusOfRequest(String taskStatus, String ta preparedStatement.setString(1, taskStatus); preparedStatement.setString(2, taskId); } + + /** + * {@inheritDoc} + */ + @Override + public void deleteCurrentStepOfRequest(String eventId) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + try { + jdbcTemplate.executeUpdate(WorkflowEngineConstants.SqlQueries.DELETE_CURRENT_STEP_OF_REQUEST, + preparedStatement -> preparedStatement.setString(1, eventId)); + } catch (DataAccessException e) { + String errorMessage = String.format("Error while deleting the current step from eventID:%s", eventId); + throw new WorkflowEngineRuntimeException(errorMessage); + } + } } diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java index 93c33dc..2d996e5 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java @@ -25,6 +25,7 @@ public static class SqlQueries { public static final String ADD_CURRENT_STEP_FOR_EVENT = "INSERT INTO WF_WORKFLOW_APPROVAL_STATE (EVENT_ID,WORKFLOW_ID, CURRENT_STEP) VALUES (?,?,?)"; public static final String GET_CURRENT_STEP = "SELECT CURRENT_STEP FROM WF_WORKFLOW_APPROVAL_STATE WHERE EVENT_ID = ? AND WORKFLOW_ID = ?"; public static final String UPDATE_STATE_OF_REQUEST = "UPDATE WF_WORKFLOW_APPROVAL_STATE SET CURRENT_STEP=? WHERE EVENT_ID = ? AND WORKFLOW_ID = ?"; + public static final String DELETE_CURRENT_STEP_OF_REQUEST = "DELETE FROM WF_WORKFLOW_APPROVAL_STATE WHERE EVENT_ID=?"; public static final String GET_APPROVER_NAME_RELATED_TO_CURRENT_TASK_ID = "SELECT DISTINCT APPROVER_NAME FROM WF_WORKFLOW_APPROVAL_RELATION WHERE TASK_ID = ?"; public static final String UPDATE_TASK_STATUS = "UPDATE WF_WORKFLOW_APPROVAL_RELATION SET TASK_STATUS=? WHERE TASK_ID=?"; public static final String GET_REQUEST_ID = "SELECT DISTINCT EVENT_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE TASK_ID = ?"; @@ -58,7 +59,6 @@ public static class ParameterName { public static final String ASSIGNEE_TYPE = "Type"; public static final String STATUS_ERROR = "Status is notAcceptable"; public static final String CREDENTIAL = "Credential"; - public static final String BPS_PROFILE = "BPSProfile"; public static final String HT_SUBJECT = "HTSubject"; } @@ -67,22 +67,16 @@ public enum TaskStatus { RESERVED, COMPLETED; - private TaskStatus() { + TaskStatus() { } } public static class ParameterHolder { public static final String WORKFLOW_IMPL = "WorkflowImpl"; - - public ParameterHolder() { - } } public static class ParameterValue { public static final String WORKFLOW_NAME = "WorkflowName"; - - public ParameterValue() { - } } } From eba539f9b8c6d8a4e16172840c608c360166adfe Mon Sep 17 00:00:00 2001 From: IsharaSilva Date: Wed, 6 Jul 2022 11:38:18 +0530 Subject: [PATCH 11/16] Implement workflow approvalService --- components/org.wso2.carbon.identity.workflow.engine/pom.xml | 4 ---- pom.xml | 5 ----- 2 files changed, 9 deletions(-) diff --git a/components/org.wso2.carbon.identity.workflow.engine/pom.xml b/components/org.wso2.carbon.identity.workflow.engine/pom.xml index 7d9da20..66d8aaa 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/pom.xml +++ b/components/org.wso2.carbon.identity.workflow.engine/pom.xml @@ -22,10 +22,6 @@ org.wso2.carbon.identity.framework org.wso2.carbon.user.mgt.common - - org.wso2.carbon.identity.framework - org.wso2.carbon.user.mgt - org.wso2.carbon.identity.framework org.wso2.carbon.identity.workflow.mgt diff --git a/pom.xml b/pom.xml index dcfdb31..6bbb9b2 100644 --- a/pom.xml +++ b/pom.xml @@ -178,11 +178,6 @@ org.wso2.carbon.user.mgt.common ${carbon.identity.framework.version} - - org.wso2.carbon.identity.framework - org.wso2.carbon.user.mgt - ${carbon.identity.framework.version} - org.wso2.carbon.identity.framework org.wso2.carbon.identity.workflow.mgt From 4f77e5258e9c35c0bdd4b39d2b196b8c892b449a Mon Sep 17 00:00:00 2001 From: IsharaSilva Date: Tue, 12 Jul 2022 19:19:14 +0530 Subject: [PATCH 12/16] Implement workflow event approval action --- .../workflow/engine/ApprovalEventService.java | 120 ++++++---- .../DefaultWorkflowEventRequestService.java | 73 ++++-- .../workflow/engine/dto/TaskDataDTO.java | 14 +- .../workflow/engine/dto/TaskSummaryDTO.java | 6 +- .../WorkflowEngineClientException.java | 12 + .../exception/WorkflowEngineException.java | 12 +- .../WorkflowEngineRuntimeException.java | 12 - .../WorkflowEngineServerException.java | 17 ++ .../internal/dao/WorkflowEventRequestDAO.java | 32 ++- .../dao/impl/WorkflowEventRequestDAOImpl.java | 209 ++++++++++++++---- .../engine/util/WorkflowEngineConstants.java | 59 ++++- 11 files changed, 436 insertions(+), 130 deletions(-) create mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/exception/WorkflowEngineClientException.java delete mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/exception/WorkflowEngineRuntimeException.java create mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/exception/WorkflowEngineServerException.java diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/ApprovalEventService.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/ApprovalEventService.java index 51c87f4..a232169 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/ApprovalEventService.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/ApprovalEventService.java @@ -7,7 +7,6 @@ import org.wso2.carbon.identity.workflow.engine.dto.TaskDataDTO; import org.wso2.carbon.identity.workflow.engine.dto.TaskSummaryDTO; import org.wso2.carbon.identity.workflow.engine.exception.WorkflowEngineException; -import org.wso2.carbon.identity.workflow.engine.exception.WorkflowEngineRuntimeException; import org.wso2.carbon.identity.workflow.engine.internal.dao.WorkflowEventRequestDAO; import org.wso2.carbon.identity.workflow.engine.internal.dao.impl.WorkflowEventRequestDAOImpl; import org.wso2.carbon.identity.workflow.engine.model.PagePagination; @@ -32,6 +31,7 @@ import java.sql.Timestamp; import java.util.ArrayList; import java.util.Arrays; +import java.util.Calendar; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -52,6 +52,7 @@ public class ApprovalEventService { private static final String CLAIMED = "CLAIMED"; private static final Integer LIMIT = 20; private static final Integer OFFSET = 0; + protected long localCreatedTime; /** * Search available approval tasks for the current authenticated user. @@ -104,14 +105,22 @@ private List listTasksOfApprovers(List status) { String taskId = defaultWorkflowEventRequest.getApprovalOfRequest(request.getUuid()); String taskStatus = workflowEventRequestDAO.getTaskStatusOfRequest(taskId); String[] taskStatusValue = taskStatus.split(",", 0); + String workflowID = workflowEventRequestDAO.getWorkflowID(taskId); + String workflowName = workflowEventRequestDAO.getWorkflowName(workflowID); + String entityNameOfRequest = workflowEventRequestDAO.getEntityNameOfRequest(request.getUuid()); Timestamp createdTime = workflowEventRequestDAO.getCreatedAtTimeInMill(request.getUuid()); - summeryDTO.setId(taskId); + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(createdTime.getTime()); + long cal = calendar.getTimeInMillis(); + setCreatedTime(cal); + summeryDTO.setId(taskId.concat(" " + workflowName).concat(WorkflowEngineConstants.ParameterName. + ENTITY_NAME + entityNameOfRequest)); summeryDTO.setName(WorkflowEngineConstants.ParameterName.APPROVAL_TASK); summeryDTO.setTaskType(eventType); summeryDTO.setPresentationName(taskDetails.getTaskSubject()); summeryDTO.setPresentationSubject(taskDetails.getTaskDescription()); - summeryDTO.setCreatedTimeInMillis(String.valueOf(createdTime)); - summeryDTO.setPriority(Integer.valueOf(WorkflowEngineConstants.ParameterName.PRIORITY)); + summeryDTO.setCreatedTimeInMillis(String.valueOf(getCreatedTime())); + summeryDTO.setPriority(WorkflowEngineConstants.ParameterName.PRIORITY); summeryDTO.setStatus(TaskSummaryDTO.StatusEnum.valueOf(taskStatusValue[0])); taskSummaryDTOList.add(summeryDTO); } @@ -163,7 +172,6 @@ private List getRoleNamesFromUser(String approverName) { private String getTaskRelatedStatus(String requestId, List status) { WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); - TStatus[] tStatuses = getRequiredTStatuses(status); String taskStatus = workflowEventRequestDAO.getStatusOfTask(requestId); String[] taskStatusValue = taskStatus.split(",", 0); @@ -179,18 +187,30 @@ private String getTaskRelatedStatus(String requestId, List status) { return eventId; } + private long getCreatedTime() { + + return this.localCreatedTime; + } + + private void setCreatedTime(long param) { + + this.localCreatedTime = param; + } + /** * Get details of a task identified by the taskId. * - * @param taskId the unique ID. + * @param task the unique ID. * @return TaskDataDto object. */ - public TaskDataDTO getTaskData(String taskId) { + public TaskDataDTO getTaskData(String task) { WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); + String[] taskArray = task.split(" ", 0); + String taskId = taskArray[0]; String requestId = workflowEventRequestDAO.getRequestID(taskId); WorkflowRequest request = getWorkflowRequest(requestId); - TaskDetails details = getTaskDetails(request); + TaskDetails taskDetails = getTaskDetails(request); String initiator = workflowEventRequestDAO.getInitiatedUser(requestId); List approvers = workflowEventRequestDAO.listApprovers(taskId); Map assigneeMap = null; @@ -198,6 +218,26 @@ public TaskDataDTO getTaskData(String taskId) { assigneeMap = new HashMap<>(); assigneeMap.put(WorkflowEngineConstants.ParameterName.ASSIGNEE_TYPE, assignee); } + List params = getRequestParameters(request); + List properties = getPropertyDTOs(params); + TaskDataDTO taskDataDTO = new TaskDataDTO(); + taskDataDTO.setId(taskId); + taskDataDTO.setSubject(taskDetails.getTaskSubject()); + taskDataDTO.setDescription(taskDetails.getTaskDescription()); + String statusValue = setStatusOfTask(taskId); + taskDataDTO.setApprovalStatus(TaskDataDTO.ApprovalStatusEnum.valueOf(statusValue)); + taskDataDTO.setInitiator(WorkflowEngineConstants.ParameterName.INITIATED_BY + initiator); + taskDataDTO.setPriority(WorkflowEngineConstants.ParameterName.PRIORITY); + TaskModel taskModel = new TaskModel(); + taskModel.setAssignees(assigneeMap); + taskDataDTO.setAssignees(getPropertyDTOs(taskModel.getAssignees())); + taskDataDTO.setProperties(properties); + return taskDataDTO; + } + + private String setStatusOfTask(String taskId) { + + WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); String status = workflowEventRequestDAO.getTaskStatusOfRequest(taskId); String statusValue; if (status.equals(String.valueOf(WorkflowEngineConstants.TaskStatus.RESERVED))) { @@ -209,20 +249,7 @@ public TaskDataDTO getTaskData(String taskId) { } else { statusValue = REJECTED; } - List params = getRequestParameters(request); - List properties = getPropertyDTOs(params); - TaskDataDTO taskDataDTO = new TaskDataDTO(); - taskDataDTO.setId(taskId); - taskDataDTO.setSubject(details.getTaskSubject()); - taskDataDTO.setDescription(details.getTaskDescription()); - taskDataDTO.setApprovalStatus(TaskDataDTO.ApprovalStatusEnum.valueOf(statusValue)); - taskDataDTO.setInitiator(initiator); - taskDataDTO.setPriority(Integer.valueOf(WorkflowEngineConstants.ParameterName.PRIORITY)); - TaskModel taskModel = new TaskModel(); - taskModel.setAssignees(assigneeMap); - taskDataDTO.setAssignees(getPropertyDTOs(taskModel.getAssignees())); - taskDataDTO.setProperties(properties); - return taskDataDTO; + return statusValue; } /** @@ -238,25 +265,25 @@ public void updateStatus(String taskId, StateDTO nextState) { WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); DefaultWorkflowEventRequest defaultWorkflowEventRequest = new DefaultWorkflowEventRequestService(); validateApprovers(taskId); - switch (nextState.getAction()) { - case APPROVE: - updateTaskStatusOfRequest(taskId, APPROVED); - updateStepDetailsOfRequest(taskId); - break; - case REJECT: - String eventId = workflowEventRequestDAO.getRequestID(taskId); - updateTaskStatusOfRequest(taskId, REJECTED); - defaultWorkflowEventRequest.deleteApprovalOfRequest(taskId); - completeRequest(eventId, REJECTED); - break; - case RELEASE: - updateTaskStatusOfRequest(taskId, RELEASED); - break; - case CLAIM: - updateTaskStatusOfRequest(taskId, CLAIMED); - break; - default: - break; + switch (nextState.getAction()) { + case APPROVE: + updateTaskStatusOfRequest(taskId, APPROVED); + updateStepDetailsOfRequest(taskId); + break; + case REJECT: + String eventId = workflowEventRequestDAO.getRequestID(taskId); + updateTaskStatusOfRequest(taskId, REJECTED); + defaultWorkflowEventRequest.deleteApprovalOfRequest(taskId); + completeRequest(eventId, REJECTED); + break; + case RELEASE: + updateTaskStatusOfRequest(taskId, RELEASED); + break; + case CLAIM: + updateTaskStatusOfRequest(taskId, CLAIMED); + break; + default: + break; } } @@ -267,7 +294,10 @@ private WorkflowRequest getWorkflowRequest(String requestId) { try { request = workFlowExecutorManagerService.retrieveWorkflow(requestId); } catch (InternalWorkflowException e) { - throw new WorkflowEngineRuntimeException("Cannot get workflow request given the request Id"); + throw new WorkflowEngineException( + WorkflowEngineConstants.ErrorMessages.ERROR_OCCURRED_WHILE_RETRIEVING_WORKFLOW_REQUEST.getCode(), + WorkflowEngineConstants.ErrorMessages.ERROR_OCCURRED_WHILE_RETRIEVING_WORKFLOW_REQUEST. + getDescription()); } return request; } @@ -381,7 +411,11 @@ private List getParameterList(WorkflowRequest request) { try { parameterList = workflowManagementService.getWorkflowParameters(association.getWorkflowId()); } catch (WorkflowException e) { - throw new WorkflowEngineException("The parameterList can't get given the associated workflowId"); + throw new WorkflowEngineException( + WorkflowEngineConstants.ErrorMessages.ERROR_OCCURRED_WHILE_RETRIEVING_PARAMETER_LIST.getCode(), + WorkflowEngineConstants.ErrorMessages.ERROR_OCCURRED_WHILE_RETRIEVING_PARAMETER_LIST. + getDescription() + ); } } return parameterList; diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultWorkflowEventRequestService.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultWorkflowEventRequestService.java index c1b151f..84f0728 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultWorkflowEventRequestService.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/DefaultWorkflowEventRequestService.java @@ -1,7 +1,12 @@ package org.wso2.carbon.identity.workflow.engine; -import org.wso2.carbon.identity.workflow.engine.exception.WorkflowEngineException; -import org.wso2.carbon.identity.workflow.engine.exception.WorkflowEngineRuntimeException; +import org.apache.commons.lang.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.identity.workflow.engine.exception.WorkflowEngineClientException; +import org.wso2.carbon.identity.workflow.engine.exception.WorkflowEngineServerException; +import org.wso2.carbon.identity.workflow.engine.internal.dao.WorkflowEventRequestDAO; +import org.wso2.carbon.identity.workflow.engine.internal.dao.impl.WorkflowEventRequestDAOImpl; import org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants; import org.wso2.carbon.identity.workflow.mgt.WorkflowExecutorManagerService; import org.wso2.carbon.identity.workflow.mgt.WorkflowExecutorManagerServiceImpl; @@ -14,8 +19,6 @@ import org.wso2.carbon.identity.workflow.mgt.dto.WorkflowRequest; import org.wso2.carbon.identity.workflow.mgt.exception.InternalWorkflowException; import org.wso2.carbon.identity.workflow.mgt.exception.WorkflowException; -import org.wso2.carbon.identity.workflow.engine.internal.dao.WorkflowEventRequestDAO; -import org.wso2.carbon.identity.workflow.engine.internal.dao.impl.WorkflowEventRequestDAOImpl; import java.util.List; import java.util.UUID; @@ -25,13 +28,15 @@ */ public class DefaultWorkflowEventRequestService implements DefaultWorkflowEventRequest { + private WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); + private static final Log log = LogFactory.getLog(DefaultWorkflowEventRequestService.class); + /** * {@inheritDoc} */ @Override public void addApproversOfRequests(WorkflowRequest request, List parameterList) { - WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); String taskId = UUID.randomUUID().toString(); String eventId = getRequestId(request); String workflowId = getWorkflowId(request); @@ -92,10 +97,11 @@ public String getWorkflowId(WorkflowRequest request) { Workflow workflow = workflowManagementService.getWorkflow(association.getWorkflowId()); workflowId = workflow.getWorkflowId(); } catch (WorkflowException e) { - throw new WorkflowEngineException("The workflow Id is not valid"); + throw new WorkflowEngineClientException( + WorkflowEngineConstants.ErrorMessages.ASSOCIATION_NOT_FOUND.getCode(), + WorkflowEngineConstants.ErrorMessages.WORKFLOW_ID_NOT_FOUND.getDescription()); } } - return workflowId; } @@ -111,7 +117,9 @@ public List getAssociations(WorkflowRequest workflowRequest associations = workFlowExecutorManagerService.getWorkflowAssociationsForRequest( workflowRequest.getEventType(), workflowRequest.getTenantId()); } catch (InternalWorkflowException e) { - throw new WorkflowEngineRuntimeException("The associations are not connecting with any request"); + throw new WorkflowEngineClientException( + WorkflowEngineConstants.ErrorMessages.ASSOCIATION_NOT_FOUND.getCode(), + WorkflowEngineConstants.ErrorMessages.ASSOCIATION_NOT_FOUND.getDescription()); } return associations; } @@ -122,7 +130,12 @@ public List getAssociations(WorkflowRequest workflowRequest @Override public String getApprovalOfRequest(String eventId) { - WorkflowEventRequestDAO workflowEventRequestDAO=new WorkflowEventRequestDAOImpl(); + if (StringUtils.isEmpty(eventId)) { + if (log.isDebugEnabled()) { + log.debug("Cannot retrieve task from eventID: " + eventId); + } + throw buildTaskNotFoundError(); + } return workflowEventRequestDAO.getApproversOfRequest(eventId); } @@ -132,7 +145,11 @@ public String getApprovalOfRequest(String eventId) { @Override public void deleteApprovalOfRequest(String taskId) { - WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); + if (StringUtils.isEmpty(taskId)) { + if (log.isDebugEnabled()) { + log.debug("Cannot delete task from taskID: " + taskId); + } + } workflowEventRequestDAO.deleteApproversOfRequest(taskId); } @@ -142,7 +159,12 @@ public void deleteApprovalOfRequest(String taskId) { @Override public void createStatesOfRequest(String eventId, String workflowId, int currentStep) { - WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); + if (StringUtils.isEmpty(eventId) || StringUtils.isEmpty(workflowId)) { + if (log.isDebugEnabled()) { + log.debug("Cannot add task from event ID: " + eventId); + } + throw buildTaskNotFoundError(); + } workflowEventRequestDAO.createStatesOfRequest(eventId, workflowId, currentStep); } @@ -152,7 +174,12 @@ public void createStatesOfRequest(String eventId, String workflowId, int current @Override public int getStateOfRequest(String eventId, String workflowId) { - WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); + if (StringUtils.isEmpty(eventId) || StringUtils.isEmpty(workflowId)) { + if (log.isDebugEnabled()) { + log.debug("Cannot retrieve task step from eventID: " + eventId); + } + throw buildTaskNotFoundError(); + } return workflowEventRequestDAO.getStateOfRequest(eventId, workflowId); } @@ -162,8 +189,12 @@ public int getStateOfRequest(String eventId, String workflowId) { @Override public void updateStateOfRequest(String eventId, String workflowId) { - WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); - + if (StringUtils.isEmpty(eventId) || StringUtils.isEmpty(workflowId)) { + if (log.isDebugEnabled()) { + log.debug("Cannot update task from eventID: " + eventId); + } + throw buildTaskNotFoundError(); + } int currentStep = getStateOfRequest(eventId, workflowId); currentStep += 1; workflowEventRequestDAO.updateStateOfRequest(eventId, workflowId, currentStep); @@ -175,7 +206,19 @@ public void updateStateOfRequest(String eventId, String workflowId) { @Override public void deleteStateOfRequest(String eventId) { - WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); + if (StringUtils.isEmpty(eventId)) { + if (log.isDebugEnabled()) { + log.debug("Cannot delete task from eventID: " + eventId); + } + throw buildTaskNotFoundError(); + } workflowEventRequestDAO.deleteCurrentStepOfRequest(eventId); } + + private WorkflowEngineServerException buildTaskNotFoundError() { + + return new WorkflowEngineServerException( + WorkflowEngineConstants.ErrorMessages.TASK_NOT_FOUND.getCode(), + WorkflowEngineConstants.ErrorMessages.TASK_NOT_FOUND.getDescription()); + } } diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/TaskDataDTO.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/TaskDataDTO.java index 5000f79..d0a75cd 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/TaskDataDTO.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/TaskDataDTO.java @@ -14,7 +14,7 @@ public class TaskDataDTO { private String description = null; - private Integer priority = null; + private String priority = null; private String initiator = null; @@ -71,12 +71,12 @@ public void setDescription(String description) { /** * Priority of the Approval task. **/ - public Integer getPriority() { + public String getPriority() { return priority; } - public void setPriority(Integer priority) { + public void setPriority(String priority) { this.priority = priority; } @@ -128,6 +128,13 @@ public List getProperties() { public void setProperties(List properties) { this.properties = properties; } + private String createdTimeInMillis = null; + public String getCreatedTimeInMillis() { + return createdTimeInMillis; + } + public void setCreatedTimeInMillis(String createdTimeInMillis) { + this.createdTimeInMillis = createdTimeInMillis; + } @Override public String toString() { @@ -142,6 +149,7 @@ public String toString() { sb.append(" approvalStatus: ").append(approvalStatus).append("\n"); sb.append(" assignees: ").append(assignees).append("\n"); sb.append(" properties: ").append(properties).append("\n"); + sb.append(" createdTimeInMillis: ").append(createdTimeInMillis).append("\n"); sb.append("}\n"); return sb.toString(); } diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/TaskSummaryDTO.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/TaskSummaryDTO.java index 91e82d3..f52974c 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/TaskSummaryDTO.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/TaskSummaryDTO.java @@ -21,7 +21,7 @@ public enum StatusEnum { private StatusEnum status = null; - private Integer priority = null; + private String priority = null; private String createdTimeInMillis = null; @@ -88,10 +88,10 @@ public void setStatus(StatusEnum status) { /** * Priority of the Approval task **/ - public Integer getPriority() { + public String getPriority() { return priority; } - public void setPriority(Integer priority) { + public void setPriority(String priority) { this.priority = priority; } diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/exception/WorkflowEngineClientException.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/exception/WorkflowEngineClientException.java new file mode 100644 index 0000000..fbbb152 --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/exception/WorkflowEngineClientException.java @@ -0,0 +1,12 @@ +package org.wso2.carbon.identity.workflow.engine.exception; + +/** + * Exception class to handle client exceptions. + */ +public class WorkflowEngineClientException extends WorkflowEngineException { + + public WorkflowEngineClientException(String message, String errorCode) { + + super(message, errorCode); + } +} diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/exception/WorkflowEngineException.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/exception/WorkflowEngineException.java index 3b000db..9248bbd 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/exception/WorkflowEngineException.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/exception/WorkflowEngineException.java @@ -4,7 +4,17 @@ * Exception class to handle workflow engine exceptions. */ public class WorkflowEngineException extends java.lang.RuntimeException { - public WorkflowEngineException(String message) { + + private String errorCode; + + public WorkflowEngineException(String message, String errorCode) { + super(message); + this.errorCode = errorCode; + } + + public WorkflowEngineException(String message, Throwable cause) { + + super(message, cause); } } diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/exception/WorkflowEngineRuntimeException.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/exception/WorkflowEngineRuntimeException.java deleted file mode 100644 index c5cdd8e..0000000 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/exception/WorkflowEngineRuntimeException.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.wso2.carbon.identity.workflow.engine.exception; - -/** - * Exception class to handle runtime exceptions. - */ -public class WorkflowEngineRuntimeException extends RuntimeException { - - public WorkflowEngineRuntimeException(String message) { - - super(message); - } -} diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/exception/WorkflowEngineServerException.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/exception/WorkflowEngineServerException.java new file mode 100644 index 0000000..7ea5985 --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/exception/WorkflowEngineServerException.java @@ -0,0 +1,17 @@ +package org.wso2.carbon.identity.workflow.engine.exception; + +/** + * This class is used to define the server side errors which need to be handled. + */ +public class WorkflowEngineServerException extends WorkflowEngineException { + + public WorkflowEngineServerException(String message, String errorCode) { + + super(message, errorCode); + } + + public WorkflowEngineServerException(String message, Throwable cause) { + + super(message, cause); + } +} diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/WorkflowEventRequestDAO.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/WorkflowEventRequestDAO.java index 0aa5b78..8506eb8 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/WorkflowEventRequestDAO.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/WorkflowEventRequestDAO.java @@ -102,14 +102,6 @@ public interface WorkflowEventRequestDAO { */ List getRoleNames(int roleId); - /** - * Returns the tasks list given the authenticated approver name. - * - * @param approverName the approver name that need to be checked. - * @return tasks list. - */ - List getTaskIdList(String approverName); - /** * Returns the events list according to the user. * @@ -188,4 +180,28 @@ public interface WorkflowEventRequestDAO { * @param eventId the request ID that need to be checked. */ void deleteCurrentStepOfRequest(String eventId); + + /** + * Retrieve the workflow ID giving task ID. + * + * @param taskId the task ID that need to be checked. + * @return workflow ID. + */ + String getWorkflowID(String taskId); + + /** + * Retrieve the workflow name giving workflow ID. + * + * @param workflowID workflow ID. + * @return workflow definition name. + */ + String getWorkflowName(String workflowID); + + /** + * Retrieve the entity name giving request ID. + * + * @param requestID the request ID that need to be checked. + * @return entity name of the request + */ + String getEntityNameOfRequest(String requestID); } diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImpl.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImpl.java index e785420..6e38f00 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImpl.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImpl.java @@ -1,9 +1,11 @@ package org.wso2.carbon.identity.workflow.engine.internal.dao.impl; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.wso2.carbon.database.utils.jdbc.JdbcTemplate; import org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException; import org.wso2.carbon.identity.configuration.mgt.core.util.JdbcUtils; -import org.wso2.carbon.identity.workflow.engine.exception.WorkflowEngineRuntimeException; +import org.wso2.carbon.identity.workflow.engine.exception.WorkflowEngineServerException; import org.wso2.carbon.identity.workflow.engine.internal.dao.WorkflowEventRequestDAO; import org.wso2.carbon.identity.workflow.engine.util.WorkflowEngineConstants; @@ -17,6 +19,8 @@ */ public class WorkflowEventRequestDAOImpl implements WorkflowEventRequestDAO { + private static final Log log = LogFactory.getLog(WorkflowEventRequestDAOImpl.class.getName()); + /** * {@inheritDoc} */ @@ -38,7 +42,10 @@ public void addApproversOfRequest(String taskId, String eventId, String workflow } catch (DataAccessException e) { String errorMessage = String.format("Error occurred while adding request details" + "in eventId: %s & workflowId: %s", eventId, workflowId); - throw new WorkflowEngineRuntimeException(errorMessage); + if (log.isDebugEnabled()) { + log.debug(errorMessage, e); + } + throw new WorkflowEngineServerException(errorMessage,e); } } @@ -62,7 +69,10 @@ public String getApproversOfRequest(String eventId) { } catch (DataAccessException e) { String errorMessage = String.format("Error occurred while retrieving taskId from" + "requestId: %s", eventId); - throw new WorkflowEngineRuntimeException(errorMessage); + if (log.isDebugEnabled()) { + log.debug(errorMessage, e); + } + throw new WorkflowEngineServerException(errorMessage, e); } return taskIdExists; } @@ -79,7 +89,10 @@ public void deleteApproversOfRequest(String taskId) { preparedStatement -> preparedStatement.setString(1, taskId)); } catch (DataAccessException e) { String errorMessage = String.format("Error while deleting the approver details from taskId:%s", taskId); - throw new WorkflowEngineRuntimeException(errorMessage); + if (log.isDebugEnabled()) { + log.debug(errorMessage, e); + } + throw new WorkflowEngineServerException(errorMessage, e); } } @@ -100,7 +113,10 @@ public void createStatesOfRequest(String eventId, String workflowId, int current } catch (DataAccessException e) { String errorMessage = String.format("Error occurred while adding request approval steps" + "in event Id: %s & workflowId: %s", eventId, workflowId); - throw new WorkflowEngineRuntimeException(errorMessage); + if (log.isDebugEnabled()) { + log.debug(errorMessage, e); + } + throw new WorkflowEngineServerException(errorMessage, e); } } @@ -126,7 +142,10 @@ public int getStateOfRequest(String eventId, String workflowId) { } catch (DataAccessException e) { String errorMessage = String.format("Error occurred while retrieving currentStep from" + "event Id: %s & workflowId: %s", eventId, workflowId); - throw new WorkflowEngineRuntimeException(errorMessage); + if (log.isDebugEnabled()) { + log.debug(errorMessage, e); + } + throw new WorkflowEngineServerException(errorMessage, e); } return Integer.parseInt(stepExists); } @@ -149,7 +168,10 @@ public void updateStateOfRequest(String eventId, String workflowId, int currentS } catch (DataAccessException e) { String errorMessage = String.format("Error occurred while updating state from" + "eventId: %s", eventId); - throw new WorkflowEngineRuntimeException(errorMessage); + if (log.isDebugEnabled()) { + log.debug(errorMessage, e); + } + throw new WorkflowEngineServerException(errorMessage, e); } } @@ -176,7 +198,10 @@ public List listApprovers(String taskId) { preparedStatement -> preparedStatement.setString(1, taskId)); } catch (DataAccessException e) { String errorMessage = "Error occurred while retrieving approvers"; - throw new WorkflowEngineRuntimeException(errorMessage); + if (log.isDebugEnabled()) { + log.debug(errorMessage, e); + } + throw new WorkflowEngineServerException(errorMessage, e); } return requestsList; } @@ -197,7 +222,10 @@ public List getRolesID(String userName) { } catch (DataAccessException e) { String errorMessage = String.format("Error occurred while retrieving Role ID from" + "user name: %s", userName); - throw new WorkflowEngineRuntimeException(errorMessage); + if (log.isDebugEnabled()) { + log.debug(errorMessage, e); + } + throw new WorkflowEngineServerException(errorMessage, e); } return roleIdList; } @@ -218,7 +246,10 @@ public List getRoleNames(int roleId) { } catch (DataAccessException e) { String errorMessage = String.format("Error occurred while retrieving Role name from" + "role ID: %d", roleId); - throw new WorkflowEngineRuntimeException(errorMessage); + if (log.isDebugEnabled()) { + log.debug(errorMessage, e); + } + throw new WorkflowEngineServerException(errorMessage, e); } return roleNameList; } @@ -239,8 +270,11 @@ public String getRequestID(String taskId) { preparedStatement -> preparedStatement.setString(1, taskId)); } catch (DataAccessException e) { String errorMessage = String.format("Error occurred while retrieving request ID from" + - "task Id: %s", taskId); - throw new WorkflowEngineRuntimeException(errorMessage); + "taskID: %s", taskId); + if (log.isDebugEnabled()) { + log.debug(errorMessage, e); + } + throw new WorkflowEngineServerException(errorMessage, e); } return requestId; } @@ -262,7 +296,10 @@ public String getInitiatedUser(String requestId) { } catch (DataAccessException e) { String errorMessage = String.format("Error occurred while retrieving initiator from" + "requestId: %s", requestId); - throw new WorkflowEngineRuntimeException(errorMessage); + if (log.isDebugEnabled()) { + log.debug(errorMessage, e); + } + throw new WorkflowEngineServerException(errorMessage, e); } return createdBy; } @@ -283,28 +320,10 @@ public List getRequestIdFromApprover(String approverName) { } catch (DataAccessException e) { String errorMessage = String.format("Error occurred while retrieving requestID from" + "user: %s", approverName); - throw new WorkflowEngineRuntimeException(errorMessage); - } - return requestIdList; - } - - /** - * {@inheritDoc} - */ - @Override - public List getTaskIdList(String approverName) { - - JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); - List requestIdList; - try { - requestIdList = jdbcTemplate.executeQuery(WorkflowEngineConstants.SqlQueries. - GET_TASK_ID_FROM_APPROVER, (resultSet, rowNumber) -> - resultSet.getString(WorkflowEngineConstants.TASK_ID_COLUMN), - preparedStatement -> preparedStatement.setString(1, approverName)); - } catch (DataAccessException e) { - String errorMessage = String.format("Error occurred while retrieving task id from" + - "approver name: %s", approverName); - throw new WorkflowEngineRuntimeException(errorMessage); + if (log.isDebugEnabled()) { + log.debug(errorMessage, e); + } + throw new WorkflowEngineServerException(errorMessage, e); } return requestIdList; } @@ -325,7 +344,10 @@ public List getRequestsList(String approverName) { } catch (DataAccessException e) { String errorMessage = String.format("Error occurred while retrieving request id from" + "approver name: %s", approverName); - throw new WorkflowEngineRuntimeException(errorMessage); + if (log.isDebugEnabled()) { + log.debug(errorMessage, e); + } + throw new WorkflowEngineServerException(errorMessage, e); } return requestIdList; } @@ -347,7 +369,10 @@ public String getEventType(String requestId) { } catch (DataAccessException e) { String errorMessage = String.format("Error occurred while retrieving event type from" + "request : %s", requestId); - throw new WorkflowEngineRuntimeException(errorMessage); + if (log.isDebugEnabled()) { + log.debug(errorMessage, e); + } + throw new WorkflowEngineServerException(errorMessage, e); } return eventType; } @@ -369,7 +394,10 @@ public String getTaskStatusOfRequest(String taskId) { } catch (DataAccessException e) { String errorMessage = String.format("Error occurred while retrieving task status from" + "task Id: %s", taskId); - throw new WorkflowEngineRuntimeException(errorMessage); + if (log.isDebugEnabled()) { + log.debug(errorMessage, e); + } + throw new WorkflowEngineServerException(errorMessage, e); } return taskStatus; } @@ -391,7 +419,10 @@ public String getStatusOfTask(String requestId) { } catch (DataAccessException e) { String errorMessage = String.format("Error occurred while retrieving task status from" + "request Id: %s", requestId); - throw new WorkflowEngineRuntimeException(errorMessage); + if (log.isDebugEnabled()) { + log.debug(errorMessage, e); + } + throw new WorkflowEngineServerException(errorMessage, e); } return taskStatus; } @@ -413,7 +444,10 @@ public void updateStatusOfRequest(String taskId, String taskStatus) { } catch (DataAccessException e) { String errorMessage = String.format("Error occurred while updating status from" + "taskID: %s", taskId); - throw new WorkflowEngineRuntimeException(errorMessage); + if (log.isDebugEnabled()) { + log.debug(errorMessage, e); + } + throw new WorkflowEngineServerException(errorMessage, e); } } @@ -434,7 +468,10 @@ public String getRelationshipId(String eventId) { } catch (DataAccessException e) { String errorMessage = String.format("Error occurred while retrieving relationship ID from" + "event Id: %s", eventId); - throw new WorkflowEngineRuntimeException(errorMessage); + if (log.isDebugEnabled()) { + log.debug(errorMessage, e); + } + throw new WorkflowEngineServerException(errorMessage, e); } return taskStatus; } @@ -455,7 +492,10 @@ public List getTaskId(String eventId) { } catch (DataAccessException e) { String errorMessage = String.format("Error occurred while retrieving tasks from" + "request id : %s", eventId); - throw new WorkflowEngineRuntimeException(errorMessage); + if (log.isDebugEnabled()) { + log.debug(errorMessage, e); + } + throw new WorkflowEngineServerException(errorMessage, e); } return taskIdList; } @@ -477,7 +517,10 @@ public Timestamp getCreatedAtTimeInMill(String requestId) { } catch (DataAccessException e) { String errorMessage = String.format("Error occurred while retrieving createdAt time from" + "request Id: %s", requestId); - throw new WorkflowEngineRuntimeException(errorMessage); + if (log.isDebugEnabled()) { + log.debug(errorMessage, e); + } + throw new WorkflowEngineServerException(errorMessage, e); } return createdTime; } @@ -501,7 +544,85 @@ public void deleteCurrentStepOfRequest(String eventId) { preparedStatement -> preparedStatement.setString(1, eventId)); } catch (DataAccessException e) { String errorMessage = String.format("Error while deleting the current step from eventID:%s", eventId); - throw new WorkflowEngineRuntimeException(errorMessage); + if (log.isDebugEnabled()) { + log.debug(errorMessage, e); + } + throw new WorkflowEngineServerException(errorMessage, e); + } + } + + /** + *{@inheritDoc} + */ + @Override + public String getWorkflowID(String taskId) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + String taskStatus; + try { + taskStatus = jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries. + GET_WORKFLOW_ID, + ((resultSet, i) -> ( + resultSet.getString(WorkflowEngineConstants.WORKFLOW_ID))), + preparedStatement -> preparedStatement.setString(1, taskId)); + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while retrieving workflow from" + + "task Id: %s", taskId); + if (log.isDebugEnabled()) { + log.debug(errorMessage, e); + } + throw new WorkflowEngineServerException(errorMessage, e); } + return taskStatus; + } + + /** + *{@inheritDoc} + */ + @Override + public String getWorkflowName(String workflowID) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + String taskStatus; + try { + taskStatus = jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries. + GET_WORKFLOW_NAME, + ((resultSet, i) -> ( + resultSet.getString(WorkflowEngineConstants.WORKFLOW_NAME))), + preparedStatement -> preparedStatement.setString(1, workflowID)); + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while retrieving workflow name from" + + "workflow Id: %s", workflowID); + if (log.isDebugEnabled()) { + log.debug(errorMessage, e); + } + throw new WorkflowEngineServerException(errorMessage, e); + } + return taskStatus; + } + + /** + *{@inheritDoc} + */ + @Override + public String getEntityNameOfRequest(String requestID) { + + JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); + String taskStatus; + try { + taskStatus = jdbcTemplate.fetchSingleRecord(WorkflowEngineConstants.SqlQueries. + GET_ENTITY_NAME, + ((resultSet, i) -> ( + resultSet.getString(WorkflowEngineConstants.ENTITY_NAME))), + preparedStatement -> preparedStatement.setString(1, requestID)); + } catch (DataAccessException e) { + String errorMessage = String.format("Error occurred while retrieving workflow name from" + + "workflow Id: %s", requestID); + if (log.isDebugEnabled()) { + log.debug(errorMessage, e); + } + throw new WorkflowEngineServerException(errorMessage, e); + } + return taskStatus; } } diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java index 2d996e5..321750a 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java @@ -9,14 +9,20 @@ public class WorkflowEngineConstants { public static final String TASK_ID_COLUMN = "TASK_ID"; public static final String APPROVER_NAME_COLUMN = "APPROVER_NAME"; public static final String EVENT_ID = "EVENT_ID"; + public static final String WORKFLOW_ID = "WORKFLOW_ID"; + public static final String WORKFLOW_NAME = "WF_NAME"; public static final String CREATED_USER_COLUMN = "CREATED_BY"; public static final String EVENT_TYPE_COLUMN = "OPERATION_TYPE"; + public static final String ENTITY_NAME = "ENTITY_NAME"; public static final String TASK_STATUS_COLUMN = "TASK_STATUS"; public static final String CREATED_AT_IN_MILL_COLUMN = "CREATED_AT"; public static final String RELATIONSHIP_ID_IN_REQUEST_COLUMN = "RELATIONSHIP_ID"; public static final String ROLE_ID_COLUMN = "UM_ROLE_ID"; public static final String ROLE_NAME = "UM_ROLE_NAME"; + /** + * SQL Query definitions. + */ public static class SqlQueries { public static final String ADD_APPROVAL_LIST_RELATED_TO_USER = "INSERT INTO WF_WORKFLOW_APPROVAL_RELATION (TASK_ID,EVENT_ID,WORKFLOW_ID,APPROVER_TYPE,APPROVER_NAME, TASK_STATUS) VALUES (?,?,?,?,?,?)"; @@ -27,9 +33,11 @@ public static class SqlQueries { public static final String UPDATE_STATE_OF_REQUEST = "UPDATE WF_WORKFLOW_APPROVAL_STATE SET CURRENT_STEP=? WHERE EVENT_ID = ? AND WORKFLOW_ID = ?"; public static final String DELETE_CURRENT_STEP_OF_REQUEST = "DELETE FROM WF_WORKFLOW_APPROVAL_STATE WHERE EVENT_ID=?"; public static final String GET_APPROVER_NAME_RELATED_TO_CURRENT_TASK_ID = "SELECT DISTINCT APPROVER_NAME FROM WF_WORKFLOW_APPROVAL_RELATION WHERE TASK_ID = ?"; + public static final String GET_WORKFLOW_ID = "SELECT DISTINCT WORKFLOW_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE TASK_ID = ?"; public static final String UPDATE_TASK_STATUS = "UPDATE WF_WORKFLOW_APPROVAL_RELATION SET TASK_STATUS=? WHERE TASK_ID=?"; public static final String GET_REQUEST_ID = "SELECT DISTINCT EVENT_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE TASK_ID = ?"; public static final String GET_TASK_ID_FROM_APPROVER = "SELECT TASK_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE APPROVER_NAME= ?"; + public static final String GET_ENTITY_NAME = "SELECT ENTITY_NAME FROM WF_REQUEST_ENTITY_RELATIONSHIP WHERE REQUEST_ID = ?"; public static final String GET_TASK_ID_FROM_REQUEST = "SELECT TASK_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE EVENT_ID= ?"; public static final String GET_REQUEST_ID_FROM_APPROVER = "SELECT EVENT_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE APPROVER_NAME= ?"; public static final String GET_TASK_STATUS = "SELECT DISTINCT TASK_STATUS FROM WF_WORKFLOW_APPROVAL_RELATION WHERE TASK_ID = ?"; @@ -39,6 +47,7 @@ public static class SqlQueries { public static final String GET_CREATED_TIME_IN_MILL = "SELECT CREATED_AT FROM WF_REQUEST WHERE UUID= ?"; public static final String GET_REQUEST_ID_OF_RELATIONSHIP = "SELECT RELATIONSHIP_ID FROM " + "WF_WORKFLOW_REQUEST_RELATION WHERE REQUEST_ID = ?"; + public static final String GET_WORKFLOW_NAME = "SELECT WF_NAME FROM WF_WORKFLOW WHERE ID =? "; public static final String GET_ROLE_ID = "SELECT UM_ROLE_ID FROM UM_HYBRID_USER_ROLE WHERE UM_USER_NAME=?"; public static final String GET_ROLE_NAME = "SELECT UM_ROLE_NAME FROM UM_HYBRID_ROLE WHERE UM_ID=?"; } @@ -50,7 +59,7 @@ public static class ParameterName { public static final String REQUEST_ID = "REQUEST ID"; public static final String TASK_SUBJECT = "HTSubject"; public static final String TASK_DESCRIPTION = "HTDescription"; - public static final String PRIORITY = "0"; + public static final String PRIORITY = "High"; public static final String APPROVAL_TASK = "Approval task"; public static final String INTERNAL_USER = "Internal/"; public static final String APPLICATION_USER = "Application"; @@ -60,6 +69,8 @@ public static class ParameterName { public static final String STATUS_ERROR = "Status is notAcceptable"; public static final String CREDENTIAL = "Credential"; public static final String HT_SUBJECT = "HTSubject"; + public static final String INITIATED_BY = "Initiated_by "; + public static final String ENTITY_NAME = " User_Name:"; } public enum TaskStatus { @@ -73,10 +84,56 @@ public enum TaskStatus { } public static class ParameterHolder { + public static final String WORKFLOW_IMPL = "WorkflowImpl"; } public static class ParameterValue { + public static final String WORKFLOW_NAME = "WorkflowName"; } + + /** + * Enum contains Error Codes and Error Messages. + */ + public enum ErrorMessages { + + TASK_NOT_FOUND("SWE_00001", "Invalid event ID"), + ASSOCIATION_NOT_FOUND("SWE_00002", "The associations are not connecting with any request"), + WORKFLOW_ID_NOT_FOUND("SWE_00003", "The workflow Id is not valid"), + ERROR_OCCURRED_WHILE_RETRIEVING_WORKFLOW_REQUEST("SWE_00004", + "Cannot get the workflow request given the request Id"), + ERROR_OCCURRED_WHILE_RETRIEVING_PARAMETER_LIST("SWE_00005", + "The parameterList can't get given the associated workflowId"); + private final String code; + private final String description; + + /** + * Error Messages + * + * @param code Code of the error message. + * @param description Error message string. + */ + ErrorMessages(String code, String description) { + + this.code = code; + this.description = description; + } + + public String getCode() { + + return code; + } + + public String getDescription() { + + return this.description; + } + + public String toString() { + + return this.code + " - " + this.description; + } + } + } From c0f5a19fd160f3d09d10aa638f56b42e44e2c403 Mon Sep 17 00:00:00 2001 From: IsharaSilva Date: Tue, 26 Jul 2022 16:38:26 +0530 Subject: [PATCH 13/16] Implement workflow event approval action --- .../pom.xml | 22 ++++++ pom.xml | 66 ++++++++++++++++- ..._wso2_carbon_identity_workflow_engine.html | 71 +++++++++++++++++++ ...g_wso2_carbon_identity_workflow_engine.xml | 4 ++ 4 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 test-output/All Test Suite/_home_ishara_identity-workflow-simple_components_org.wso2.carbon.identity.workflow.engine_src_test_java_org_wso2_carbon_identity_workflow_engine.html create mode 100644 test-output/All Test Suite/_home_ishara_identity-workflow-simple_components_org.wso2.carbon.identity.workflow.engine_src_test_java_org_wso2_carbon_identity_workflow_engine.xml diff --git a/components/org.wso2.carbon.identity.workflow.engine/pom.xml b/components/org.wso2.carbon.identity.workflow.engine/pom.xml index 66d8aaa..4ded88a 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/pom.xml +++ b/components/org.wso2.carbon.identity.workflow.engine/pom.xml @@ -34,6 +34,28 @@ org.wso2.carbon.identity.framework org.wso2.carbon.user.mgt + + org.testng + testng + + + org.powermock + powermock-api-mockito + + + org.powermock + powermock-module-testng + test + + + com.h2database + h2 + test + + + org.ops4j.pax.logging + pax-logging-api + diff --git a/pom.xml b/pom.xml index 6bbb9b2..6d6277f 100644 --- a/pom.xml +++ b/pom.xml @@ -160,7 +160,6 @@ - @@ -198,6 +197,66 @@ org.wso2.carbon.user.mgt ${carbon.identity.framework.version} + + org.wso2.carbon.identity.framework + identity-framework + ${carbon.identity.framework.version} + + + org.testng + testng + ${testng.version} + test + + + org.powermock + powermock-api-mockito + ${powermock.version} + + + org.powermock + powermock-module-testng-common + ${powermock.version} + + + org.powermock + powermock-module-testng + ${powermock.version} + + + com.h2database + h2 + test + ${h2database.version} + + + org.ops4j.pax.logging + pax-logging-api + ${pax.logging.api.version} + + + org.someexternallib + someexternallibartifact + ... + + + org.slf4j + slf4j-log4j12 + + + org.slf4j + slf4j-simple + + + org.slf4j + slf4j-api + + + log4j + log4j + + + @@ -211,6 +270,11 @@ 1.2.8 5.18.187 1.2.11-wso2v16 + 7.4.0 + 3.2.4 + 1.7.4 + 2.1.210 + 1.10.1 diff --git a/test-output/All Test Suite/_home_ishara_identity-workflow-simple_components_org.wso2.carbon.identity.workflow.engine_src_test_java_org_wso2_carbon_identity_workflow_engine.html b/test-output/All Test Suite/_home_ishara_identity-workflow-simple_components_org.wso2.carbon.identity.workflow.engine_src_test_java_org_wso2_carbon_identity_workflow_engine.html new file mode 100644 index 0000000..40b7379 --- /dev/null +++ b/test-output/All Test Suite/_home_ishara_identity-workflow-simple_components_org.wso2.carbon.identity.workflow.engine_src_test_java_org_wso2_carbon_identity_workflow_engine.html @@ -0,0 +1,71 @@ + + +TestNG: /home/ishara/identity-workflow-simple/components/org.wso2.carbon.identity.workflow.engine/src/test/java/org/wso2/carbon/identity/workflow/engine + + + + + + + + +

/home/ishara/identity-workflow-simple/components/org.wso2.carbon.identity.workflow.engine/src/test/java/org/wso2/carbon/identity/workflow/engine

+ + + + + + + + + + + +
Tests passed/Failed/Skipped:0/0/0
Started on:Fri Jul 15 11:20:20 IST 2022
Total time:0 seconds (9 ms)
Included groups:
Excluded groups:

+(Hover the method name to see the test class name)

+ + \ No newline at end of file diff --git a/test-output/All Test Suite/_home_ishara_identity-workflow-simple_components_org.wso2.carbon.identity.workflow.engine_src_test_java_org_wso2_carbon_identity_workflow_engine.xml b/test-output/All Test Suite/_home_ishara_identity-workflow-simple_components_org.wso2.carbon.identity.workflow.engine_src_test_java_org_wso2_carbon_identity_workflow_engine.xml new file mode 100644 index 0000000..22559cf --- /dev/null +++ b/test-output/All Test Suite/_home_ishara_identity-workflow-simple_components_org.wso2.carbon.identity.workflow.engine_src_test_java_org_wso2_carbon_identity_workflow_engine.xml @@ -0,0 +1,4 @@ + + + + From 751f56827eafe51addebf64f366c1802bcaf4b8d Mon Sep 17 00:00:00 2001 From: IsharaSilva Date: Tue, 26 Jul 2022 18:08:39 +0530 Subject: [PATCH 14/16] Implement workflow event approval action --- .../workflow/engine/ApprovalEventService.java | 221 +++++++++++------- .../workflow/engine/dto/PropertyDTO.java | 6 + .../workflow/engine/dto/StateDTO.java | 5 +- .../workflow/engine/dto/TaskDataDTO.java | 41 +++- .../workflow/engine/dto/TaskSummaryDTO.java | 54 ++++- .../exception/WorkflowEngineException.java | 2 +- .../internal/dao/WorkflowEventRequestDAO.java | 8 - .../dao/impl/WorkflowEventRequestDAOImpl.java | 24 -- .../workflow/engine/model/PagePagination.java | 8 +- .../workflow/engine/model/TStatus.java | 5 +- .../workflow/engine/model/TaskDetails.java | 7 + .../workflow/engine/model/TaskModel.java | 10 + .../engine/util/WorkflowEngineConstants.java | 14 +- 13 files changed, 275 insertions(+), 130 deletions(-) diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/ApprovalEventService.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/ApprovalEventService.java index a232169..a399687 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/ApprovalEventService.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/ApprovalEventService.java @@ -6,7 +6,9 @@ import org.wso2.carbon.identity.workflow.engine.dto.StateDTO; import org.wso2.carbon.identity.workflow.engine.dto.TaskDataDTO; import org.wso2.carbon.identity.workflow.engine.dto.TaskSummaryDTO; +import org.wso2.carbon.identity.workflow.engine.exception.WorkflowEngineClientException; import org.wso2.carbon.identity.workflow.engine.exception.WorkflowEngineException; +import org.wso2.carbon.identity.workflow.engine.exception.WorkflowEngineServerException; import org.wso2.carbon.identity.workflow.engine.internal.dao.WorkflowEventRequestDAO; import org.wso2.carbon.identity.workflow.engine.internal.dao.impl.WorkflowEventRequestDAOImpl; import org.wso2.carbon.identity.workflow.engine.model.PagePagination; @@ -64,29 +66,36 @@ public class ApprovalEventService { */ public List listTasks(Integer limit, Integer offset, List status) { - PagePagination pagePagination = new PagePagination(); - if (limit == null || offset == null) { - pagePagination.setPageSize(LIMIT); - pagePagination.setPageNumber(OFFSET); - } + try { + PagePagination pagePagination = new PagePagination(); + if (limit == null || offset == null) { + pagePagination.setPageSize(LIMIT); + pagePagination.setPageNumber(OFFSET); + } - if (limit != null && limit > 0) { - pagePagination.setPageSize(limit); - } - if (offset != null && offset > 0) { - pagePagination.setPageNumber(offset); - } + if (limit != null && limit > 0) { + pagePagination.setPageSize(limit); + } + if (offset != null && offset > 0) { + pagePagination.setPageNumber(offset); + } - Set taskSummaryDTOs = null; - List tasks = listTasksOfApprovers(status); - int taskListSize = tasks.size(); - for (int i = 0; i < taskListSize; ++i) { - taskSummaryDTOs = new HashSet<>(tasks); - } - if (taskSummaryDTOs == null) { - return new ArrayList<>(0); + Set taskSummaryDTOs = null; + List tasks = listTasksOfApprovers(status); + int taskListSize = tasks.size(); + for (int i = 0; i < taskListSize; ++i) { + taskSummaryDTOs = new HashSet<>(tasks); + } + if (taskSummaryDTOs == null) { + return new ArrayList<>(0); + } + return new ArrayList<>(taskSummaryDTOs); + } catch (WorkflowEngineServerException e) { + throw new WorkflowEngineClientException( + WorkflowEngineConstants.ErrorMessages.ERROR_OCCURRED_WHILE_RETRIEVING_APPROVALS_FOR_USER.getCode(), + WorkflowEngineConstants.ErrorMessages.ERROR_OCCURRED_WHILE_RETRIEVING_APPROVALS_FOR_USER. + getDescription()); } - return new ArrayList<>(taskSummaryDTOs); } private List listTasksOfApprovers(List status) { @@ -128,7 +137,7 @@ private List listTasksOfApprovers(List status) { return taskSummaryDTOList; } - private List getAllRequestRelatedUserAndRole(){ + private List getAllRequestRelatedUserAndRole() { String userName = CarbonContext.getThreadLocalCarbonContext().getUsername(); WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); @@ -152,7 +161,7 @@ private List getAllRequestRelatedUserAndRole(){ } } - List userRequestList = workflowEventRequestDAO.getRequestIdFromApprover(userName); + List userRequestList = workflowEventRequestDAO.getRequestsList(userName); return Stream.concat(lst.stream(), userRequestList.stream()).collect(Collectors.toList()); } @@ -205,34 +214,45 @@ private void setCreatedTime(long param) { */ public TaskDataDTO getTaskData(String task) { - WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); - String[] taskArray = task.split(" ", 0); - String taskId = taskArray[0]; - String requestId = workflowEventRequestDAO.getRequestID(taskId); - WorkflowRequest request = getWorkflowRequest(requestId); - TaskDetails taskDetails = getTaskDetails(request); - String initiator = workflowEventRequestDAO.getInitiatedUser(requestId); - List approvers = workflowEventRequestDAO.listApprovers(taskId); - Map assigneeMap = null; - for (String assignee : approvers) { - assigneeMap = new HashMap<>(); - assigneeMap.put(WorkflowEngineConstants.ParameterName.ASSIGNEE_TYPE, assignee); + try { + WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); + String[] taskArray = task.split(" ", 0); + String taskId = taskArray[0]; + String requestId = workflowEventRequestDAO.getRequestID(taskId); + WorkflowRequest request = getWorkflowRequest(requestId); + TaskDetails taskDetails = getTaskDetails(request); + String initiator = workflowEventRequestDAO.getInitiatedUser(requestId); + List approvers = workflowEventRequestDAO.listApprovers(taskId); + Map assigneeMap = null; + for (String assignee : approvers) { + assigneeMap = new HashMap<>(); + assigneeMap.put(WorkflowEngineConstants.ParameterName.ASSIGNEE_TYPE, assignee); + } + List params = getRequestParameters(request); + List properties = getPropertyDTOs(params); + TaskDataDTO taskDataDTO = new TaskDataDTO(); + taskDataDTO.setId(taskId); + taskDataDTO.setSubject(taskDetails.getTaskSubject()); + taskDataDTO.setDescription(taskDetails.getTaskDescription()); + String statusValue = setStatusOfTask(taskId); + taskDataDTO.setApprovalStatus(TaskDataDTO.ApprovalStatusEnum.valueOf(statusValue)); + taskDataDTO.setInitiator(WorkflowEngineConstants.ParameterName.INITIATED_BY + initiator); + taskDataDTO.setPriority(WorkflowEngineConstants.ParameterName.PRIORITY); + TaskModel taskModel = new TaskModel(); + taskModel.setAssignees(assigneeMap); + taskDataDTO.setAssignees(getPropertyDTOs(taskModel.getAssignees())); + taskDataDTO.setProperties(properties); + return taskDataDTO; + } catch (WorkflowEngineClientException e) { + throw new WorkflowEngineClientException( + WorkflowEngineConstants.ErrorMessages.USER_ERROR_NON_EXISTING_TASK_ID.getCode(), + WorkflowEngineConstants.ErrorMessages.USER_ERROR_NON_EXISTING_TASK_ID.getDescription()); + } catch (WorkflowEngineServerException e) { + throw new WorkflowEngineClientException( + WorkflowEngineConstants.ErrorMessages.ERROR_OCCURRED_WHILE_RETRIEVING_APPROVAL_OF_USER.getCode(), + WorkflowEngineConstants.ErrorMessages.ERROR_OCCURRED_WHILE_RETRIEVING_APPROVAL_OF_USER. + getDescription()); } - List params = getRequestParameters(request); - List properties = getPropertyDTOs(params); - TaskDataDTO taskDataDTO = new TaskDataDTO(); - taskDataDTO.setId(taskId); - taskDataDTO.setSubject(taskDetails.getTaskSubject()); - taskDataDTO.setDescription(taskDetails.getTaskDescription()); - String statusValue = setStatusOfTask(taskId); - taskDataDTO.setApprovalStatus(TaskDataDTO.ApprovalStatusEnum.valueOf(statusValue)); - taskDataDTO.setInitiator(WorkflowEngineConstants.ParameterName.INITIATED_BY + initiator); - taskDataDTO.setPriority(WorkflowEngineConstants.ParameterName.PRIORITY); - TaskModel taskModel = new TaskModel(); - taskModel.setAssignees(assigneeMap); - taskDataDTO.setAssignees(getPropertyDTOs(taskModel.getAssignees())); - taskDataDTO.setProperties(properties); - return taskDataDTO; } private String setStatusOfTask(String taskId) { @@ -265,25 +285,40 @@ public void updateStatus(String taskId, StateDTO nextState) { WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); DefaultWorkflowEventRequest defaultWorkflowEventRequest = new DefaultWorkflowEventRequestService(); validateApprovers(taskId); - switch (nextState.getAction()) { - case APPROVE: - updateTaskStatusOfRequest(taskId, APPROVED); - updateStepDetailsOfRequest(taskId); - break; - case REJECT: - String eventId = workflowEventRequestDAO.getRequestID(taskId); - updateTaskStatusOfRequest(taskId, REJECTED); - defaultWorkflowEventRequest.deleteApprovalOfRequest(taskId); - completeRequest(eventId, REJECTED); - break; - case RELEASE: - updateTaskStatusOfRequest(taskId, RELEASED); - break; - case CLAIM: - updateTaskStatusOfRequest(taskId, CLAIMED); - break; - default: - break; + try { + switch (nextState.getAction()) { + case APPROVE: + updateTaskStatusOfRequest(taskId, APPROVED); + updateStepDetailsOfRequest(taskId); + break; + case REJECT: + String eventId = workflowEventRequestDAO.getRequestID(taskId); + updateTaskStatusOfRequest(taskId, REJECTED); + defaultWorkflowEventRequest.deleteApprovalOfRequest(taskId); + completeRequest(eventId, REJECTED); + break; + case RELEASE: + updateTaskStatusOfRequest(taskId, RELEASED); + break; + case CLAIM: + updateTaskStatusOfRequest(taskId, CLAIMED); + break; + default: + throw new WorkflowEngineClientException( + WorkflowEngineConstants.ErrorMessages.USER_ERROR_NOT_ACCEPTABLE_INPUT_FOR_NEXT_STATE. + getCode(), + WorkflowEngineConstants.ErrorMessages.USER_ERROR_NOT_ACCEPTABLE_INPUT_FOR_NEXT_STATE. + getDescription()); + } + } catch (WorkflowEngineClientException e) { + throw new WorkflowEngineClientException( + WorkflowEngineConstants.ErrorMessages.USER_ERROR_NON_EXISTING_TASK_ID.getCode(), + WorkflowEngineConstants.ErrorMessages.USER_ERROR_NON_EXISTING_TASK_ID.getDescription()); + } catch (WorkflowEngineServerException e) { + throw new WorkflowEngineClientException( + WorkflowEngineConstants.ErrorMessages.ERROR_OCCURRED_WHILE_CHANGING_APPROVALS_STATE.getCode(), + WorkflowEngineConstants.ErrorMessages.ERROR_OCCURRED_WHILE_CHANGING_APPROVALS_STATE. + getDescription()); } } @@ -322,7 +357,7 @@ private void updateStepDetailsOfRequest(String taskId) { private void validateApprovers(String taskId) { WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); - List eventList =getAllRequestRelatedUserAndRole(); + List eventList = getAllRequestRelatedUserAndRole(); List taskList; List lst = new ArrayList<>(); for (String event : eventList) { @@ -353,24 +388,38 @@ private int numOfStates(WorkflowRequest request) { private void updateTaskStatusOfRequest(String taskId, String status) { WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); - switch (status) { - case APPROVED: - status = WorkflowEngineConstants.TaskStatus.COMPLETED.toString(); - break; - case REJECTED: - status = WorkflowEngineConstants.TaskStatus.COMPLETED.toString().concat(",").concat(REJECTED); - break; - case RELEASED: - status = WorkflowEngineConstants.TaskStatus.READY.toString(); - break; - case CLAIMED: - status = WorkflowEngineConstants.TaskStatus.RESERVED.toString(); - break; - default: - status = WorkflowEngineConstants.ParameterName.STATUS_ERROR; - break; + try { + switch (status) { + case APPROVED: + status = WorkflowEngineConstants.TaskStatus.COMPLETED.toString(); + break; + case REJECTED: + status = WorkflowEngineConstants.TaskStatus.COMPLETED.toString().concat(",").concat(REJECTED); + break; + case RELEASED: + status = WorkflowEngineConstants.TaskStatus.READY.toString(); + break; + case CLAIMED: + status = WorkflowEngineConstants.TaskStatus.RESERVED.toString(); + break; + default: + throw new WorkflowEngineClientException( + WorkflowEngineConstants.ErrorMessages.USER_ERROR_NOT_ACCEPTABLE_INPUT_FOR_NEXT_STATE. + getCode(), + WorkflowEngineConstants.ErrorMessages.USER_ERROR_NOT_ACCEPTABLE_INPUT_FOR_NEXT_STATE. + getDescription()); + } + workflowEventRequestDAO.updateStatusOfRequest(taskId, status); + } catch (WorkflowEngineClientException e) { + throw new WorkflowEngineClientException( + WorkflowEngineConstants.ErrorMessages.USER_ERROR_NON_EXISTING_TASK_ID.getCode(), + WorkflowEngineConstants.ErrorMessages.USER_ERROR_NON_EXISTING_TASK_ID.getDescription()); + } catch (WorkflowEngineServerException e) { + throw new WorkflowEngineClientException( + WorkflowEngineConstants.ErrorMessages.ERROR_OCCURRED_WHILE_CHANGING_APPROVALS_STATE.getCode(), + WorkflowEngineConstants.ErrorMessages.ERROR_OCCURRED_WHILE_CHANGING_APPROVALS_STATE. + getDescription()); } - workflowEventRequestDAO.updateStatusOfRequest(taskId, status); } private void completeRequest(String eventId, String status) { diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/PropertyDTO.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/PropertyDTO.java index 71b0785..385fccf 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/PropertyDTO.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/PropertyDTO.java @@ -17,6 +17,9 @@ public String getKey() { return key; } + /** + * Set Key of the task property. + **/ public void setKey(String key) { this.key = key; @@ -30,6 +33,9 @@ public String getValue() { return value; } + /** + * Set Value of the key of the task property. + **/ public void setValue(String value) { this.value = value; diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/StateDTO.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/StateDTO.java index 0e85d41..38866e3 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/StateDTO.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/StateDTO.java @@ -12,13 +12,16 @@ public enum ActionEnum { private ActionEnum action = null; /** - * Action to perform on the task. + * getAction to perform on the task. **/ public ActionEnum getAction() { return action; } + /** + * Set Action to perform on the task. + **/ public void setAction(ActionEnum action) { this.action = action; diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/TaskDataDTO.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/TaskDataDTO.java index d0a75cd..1ca5ebe 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/TaskDataDTO.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/TaskDataDTO.java @@ -18,6 +18,8 @@ public class TaskDataDTO { private String initiator = null; + private String createdTimeInMillis = null; + public enum ApprovalStatusEnum { PENDING, APPROVED, REJECTED, } @@ -36,6 +38,9 @@ public String getId() { return id; } + /** + * Set Unique ID to represent a approval task. + **/ public void setId(String id) { this.id = id; @@ -49,6 +54,9 @@ public String getSubject() { return subject; } + /** + * Set Subject of the Approval. + **/ public void setSubject(String subject) { this.subject = subject; @@ -63,6 +71,9 @@ public String getDescription() { return description; } + /** + * Set Description on the Approval task. + **/ public void setDescription(String description) { this.description = description; @@ -76,6 +87,9 @@ public String getPriority() { return priority; } + /** + * Set Priority of the Approval task. + **/ public void setPriority(String priority) { this.priority = priority; @@ -90,6 +104,9 @@ public String getInitiator() { return initiator; } + /** + * Set the user who initiated the task. + **/ public void setInitiator(String initiator) { this.initiator = initiator; @@ -103,6 +120,9 @@ public ApprovalStatusEnum getApprovalStatus() { return approvalStatus; } + /** + * Set available only for the completed Tasks, APPROVED or REJECTED if the task has been completed, PENDING otherwise\n + **/ public void setApprovalStatus(ApprovalStatusEnum approvalStatus) { this.approvalStatus = approvalStatus; @@ -112,27 +132,44 @@ public void setApprovalStatus(ApprovalStatusEnum approvalStatus) { * To whom the task is assigned:\n * user - username(s) if the task is reserved for specific user(s).\n * group - role name(s) if the task is assignable for group(s).\n **/ public List getAssignees() { + return assignees; } + public void setAssignees(List assignees) { + this.assignees = assignees; } - /** * Task parameters: username, role, claims, requestId. **/ public List getProperties() { + return properties; } + + /** + * Set Task parameters: username, role, claims, requestId. + **/ public void setProperties(List properties) { + this.properties = properties; } - private String createdTimeInMillis = null; + + /** + * The created time of the request. + **/ public String getCreatedTimeInMillis() { + return createdTimeInMillis; } + + /** + * Set the created time of the request. + **/ public void setCreatedTimeInMillis(String createdTimeInMillis) { + this.createdTimeInMillis = createdTimeInMillis; } diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/TaskSummaryDTO.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/TaskSummaryDTO.java index f52974c..a4ca744 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/TaskSummaryDTO.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/dto/TaskSummaryDTO.java @@ -1,7 +1,7 @@ package org.wso2.carbon.identity.workflow.engine.dto; /** - * DTO class for tasks list. + * DTO class for tasks list. */ public class TaskSummaryDTO { @@ -16,7 +16,7 @@ public class TaskSummaryDTO { private String taskType = null; public enum StatusEnum { - READY, RESERVED, COMPLETED, + READY, RESERVED, COMPLETED, } private StatusEnum status = null; @@ -29,9 +29,15 @@ public enum StatusEnum { * Unique ID to represent an Approval Task **/ public String getId() { + return id; } + + /** + * Set Unique ID to represent an Approval Task + **/ public void setId(String id) { + this.id = id; } @@ -39,9 +45,15 @@ public void setId(String id) { * Unique name for the Approval Task **/ public String getName() { + return name; } + + /** + * Set Unique name for the Approval Task + **/ public void setName(String name) { + this.name = name; } @@ -49,9 +61,15 @@ public void setName(String name) { * Display value for Approval Operation **/ public String getPresentationSubject() { + return presentationSubject; } + + /** + * Set display value for Approval Operation + **/ public void setPresentationSubject(String presentationSubject) { + this.presentationSubject = presentationSubject; } @@ -59,9 +77,15 @@ public void setPresentationSubject(String presentationSubject) { * Display value for Approval Task **/ public String getPresentationName() { + return presentationName; } + + /** + * Set display value for Approval Task + **/ public void setPresentationName(String presentationName) { + this.presentationName = presentationName; } @@ -69,9 +93,15 @@ public void setPresentationName(String presentationName) { * Type of the Approval **/ public String getTaskType() { + return taskType; } + + /** + * Set type of the Approval + **/ public void setTaskType(String taskType) { + this.taskType = taskType; } @@ -79,9 +109,15 @@ public void setTaskType(String taskType) { * State of the Approval task **/ public StatusEnum getStatus() { + return status; } + + /** + * Set state of the Approval task + **/ public void setStatus(StatusEnum status) { + this.status = status; } @@ -89,9 +125,15 @@ public void setStatus(StatusEnum status) { * Priority of the Approval task **/ public String getPriority() { + return priority; } + + /** + * Set priority of the Approval task + **/ public void setPriority(String priority) { + this.priority = priority; } @@ -99,14 +141,20 @@ public void setPriority(String priority) { * The time that the operation for approval initiated **/ public String getCreatedTimeInMillis() { + return createdTimeInMillis; } + + /** + * Set the time that the operation for approval initiated + **/ public void setCreatedTimeInMillis(String createdTimeInMillis) { + this.createdTimeInMillis = createdTimeInMillis; } @Override - public String toString() { + public String toString() { StringBuilder sb = new StringBuilder(); sb.append("class TaskSummaryDTO {\n"); diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/exception/WorkflowEngineException.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/exception/WorkflowEngineException.java index 9248bbd..c446961 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/exception/WorkflowEngineException.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/exception/WorkflowEngineException.java @@ -1,7 +1,7 @@ package org.wso2.carbon.identity.workflow.engine.exception; /** - * Exception class to handle workflow engine exceptions. + * Base exception for handling workflow engine exceptions. */ public class WorkflowEngineException extends java.lang.RuntimeException { diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/WorkflowEventRequestDAO.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/WorkflowEventRequestDAO.java index 8506eb8..5f91725 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/WorkflowEventRequestDAO.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/WorkflowEventRequestDAO.java @@ -78,14 +78,6 @@ public interface WorkflowEventRequestDAO { */ String getInitiatedUser(String requestId); - /** - * Returns the events list given the authenticated approver name. - * - * @param approverName the approver name that need to be checked. - * @return requests list. - */ - List getRequestIdFromApprover(String approverName); - /** * Retrieve the role id list giving the username. * diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImpl.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImpl.java index 6e38f00..6d199c5 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImpl.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImpl.java @@ -304,30 +304,6 @@ public String getInitiatedUser(String requestId) { return createdBy; } - /** - * {@inheritDoc} - */ - @Override - public List getRequestIdFromApprover(String approverName) { - - JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); - List requestIdList; - try { - requestIdList = jdbcTemplate.executeQuery(WorkflowEngineConstants.SqlQueries. - GET_REQUEST_ID_FROM_APPROVER, (resultSet, rowNumber) -> - resultSet.getString(WorkflowEngineConstants.EVENT_ID), - preparedStatement -> preparedStatement.setString(1, approverName)); - } catch (DataAccessException e) { - String errorMessage = String.format("Error occurred while retrieving requestID from" + - "user: %s", approverName); - if (log.isDebugEnabled()) { - log.debug(errorMessage, e); - } - throw new WorkflowEngineServerException(errorMessage, e); - } - return requestIdList; - } - /** * {@inheritDoc} */ diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/PagePagination.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/PagePagination.java index 1b24a62..2c746ce 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/PagePagination.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/PagePagination.java @@ -18,6 +18,9 @@ public int getPageSize() { return this.localPageSize; } + /** + * Set maximum number of results expected. + */ public void setPageSize(int param) { this.localPageSizeTracker = param != Integer.MIN_VALUE; @@ -25,13 +28,16 @@ public void setPageSize(int param) { } /** - * Start index of the search. + * Start index of the search. */ public int getPageNumber() { return this.localPageNumber; } + /** + * Set start index of the search. + */ public void setPageNumber(int param) { this.localPageNumberTracker = param != Integer.MIN_VALUE; diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TStatus.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TStatus.java index e949a77..a5cfad9 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TStatus.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TStatus.java @@ -12,13 +12,16 @@ public TStatus() { } /** - * The status of the task [RESERVED, READY or COMPLETED]. + * Get the status of the task [RESERVED, READY or COMPLETED]. */ public String getTStatus() { return this.localTStatus; } + /** + * Set the status of the task [RESERVED, READY or COMPLETED]. + */ public void setTStatus(String param) { this.localTStatus = param; diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TaskDetails.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TaskDetails.java index 6a6fbf3..3a7adc9 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TaskDetails.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TaskDetails.java @@ -4,6 +4,7 @@ * model class for task subject & task description. */ public class TaskDetails { + String taskDescription; String taskSubject; @@ -15,6 +16,9 @@ public String getTaskDescription() { return taskDescription; } + /** + * Set the subject of the task. + */ public void setTaskDescription(String taskDescription) { this.taskDescription = taskDescription; @@ -28,6 +32,9 @@ public String getTaskSubject() { return taskSubject; } + /** + * Set the description of the task. + */ public void setTaskSubject(String taskSubject) { this.taskSubject = taskSubject; diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TaskModel.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TaskModel.java index a698964..ab72009 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TaskModel.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/model/TaskModel.java @@ -14,10 +14,15 @@ public class TaskModel { * Unique ID to represent a approval task **/ public String getId() { + return id; } + /** + * Set unique ID to represent a approval task + **/ public void setId(String id) { + this.id = id; } @@ -25,10 +30,15 @@ public void setId(String id) { * To whom the task is assigned:\n * user - username(s) if the task is reserved for specific user(s).\n * group - role name(s) if the task is assignable for group(s).\n **/ public Map getAssignees() { + return assignees; } + /** + * Set to whom the task is assigned:\n * user - username(s) if the task is reserved for specific user(s).\n * group - role name(s) if the task is assignable for group(s).\n + **/ public void setAssignees(Map assignees) { + this.assignees = assignees; } } diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java index 321750a..b27dc06 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java +++ b/components/org.wso2.carbon.identity.workflow.engine/src/main/java/org/wso2/carbon/identity/workflow/engine/util/WorkflowEngineConstants.java @@ -36,7 +36,6 @@ public static class SqlQueries { public static final String GET_WORKFLOW_ID = "SELECT DISTINCT WORKFLOW_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE TASK_ID = ?"; public static final String UPDATE_TASK_STATUS = "UPDATE WF_WORKFLOW_APPROVAL_RELATION SET TASK_STATUS=? WHERE TASK_ID=?"; public static final String GET_REQUEST_ID = "SELECT DISTINCT EVENT_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE TASK_ID = ?"; - public static final String GET_TASK_ID_FROM_APPROVER = "SELECT TASK_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE APPROVER_NAME= ?"; public static final String GET_ENTITY_NAME = "SELECT ENTITY_NAME FROM WF_REQUEST_ENTITY_RELATIONSHIP WHERE REQUEST_ID = ?"; public static final String GET_TASK_ID_FROM_REQUEST = "SELECT TASK_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE EVENT_ID= ?"; public static final String GET_REQUEST_ID_FROM_APPROVER = "SELECT EVENT_ID FROM WF_WORKFLOW_APPROVAL_RELATION WHERE APPROVER_NAME= ?"; @@ -66,7 +65,6 @@ public static class ParameterName { public static final String SYSTEM_USER = "system"; public static final String SYSTEM_PRIMARY_USER = "system_primary_"; public static final String ASSIGNEE_TYPE = "Type"; - public static final String STATUS_ERROR = "Status is notAcceptable"; public static final String CREDENTIAL = "Credential"; public static final String HT_SUBJECT = "HTSubject"; public static final String INITIATED_BY = "Initiated_by "; @@ -104,7 +102,16 @@ public enum ErrorMessages { ERROR_OCCURRED_WHILE_RETRIEVING_WORKFLOW_REQUEST("SWE_00004", "Cannot get the workflow request given the request Id"), ERROR_OCCURRED_WHILE_RETRIEVING_PARAMETER_LIST("SWE_00005", - "The parameterList can't get given the associated workflowId"); + "The parameterList can't get given the associated workflowId"), + ERROR_OCCURRED_WHILE_CHANGING_APPROVALS_STATE("SWE_00006", "Unable to update the approval status, " + + "Server encountered an error while updating the approval task status."), + ERROR_OCCURRED_WHILE_RETRIEVING_APPROVAL_OF_USER("SWE_00007", "Unable to retrieve the user approval, " + + "Server encountered an error while retrieving information on the approval task."), + ERROR_OCCURRED_WHILE_RETRIEVING_APPROVALS_FOR_USER("SWE_00008", "Unable to retrieve approvals for the user, " + + "Server encountered an error while retrieving approvals for user."), + USER_ERROR_NON_EXISTING_TASK_ID("SWE_10001", "Task does not exist."), + USER_ERROR_NOT_ACCEPTABLE_INPUT_FOR_NEXT_STATE("10005", "Unacceptable input provided, " + + "Only [CLAIM, RELEASE, APPROVED, REJECTED] are acceptable."); private final String code; private final String description; @@ -130,6 +137,7 @@ public String getDescription() { return this.description; } + @Override public String toString() { return this.code + " - " + this.description; From bfb8fe351edfdad5f1f2bfe041db145acc4eca0b Mon Sep 17 00:00:00 2001 From: IsharaSilva Date: Tue, 26 Jul 2022 20:19:21 +0530 Subject: [PATCH 15/16] Implement tase cases for workflows --- ...efaultWorkflowEventRequestServiceTest.java | 237 +++++++++++ .../impl/WorkflowEventRequestDAOImplTest.java | 368 ++++++++++++++++++ .../workflow/engine/util/TestUtils.java | 82 ++++ .../src/test/resources/dbscripts/h2.sql | 39 ++ .../src/test/resources/testng.xml | 27 ++ 5 files changed, 753 insertions(+) create mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/test/java/org/wso2/carbon/identity/workflow/engine/DefaultWorkflowEventRequestServiceTest.java create mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/test/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImplTest.java create mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/test/java/org/wso2/carbon/identity/workflow/engine/util/TestUtils.java create mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/test/resources/dbscripts/h2.sql create mode 100644 components/org.wso2.carbon.identity.workflow.engine/src/test/resources/testng.xml diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/test/java/org/wso2/carbon/identity/workflow/engine/DefaultWorkflowEventRequestServiceTest.java b/components/org.wso2.carbon.identity.workflow.engine/src/test/java/org/wso2/carbon/identity/workflow/engine/DefaultWorkflowEventRequestServiceTest.java new file mode 100644 index 0000000..9495c01 --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/src/test/java/org/wso2/carbon/identity/workflow/engine/DefaultWorkflowEventRequestServiceTest.java @@ -0,0 +1,237 @@ +package org.wso2.carbon.identity.workflow.engine; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.testng.PowerMockTestCase; +import org.testng.Assert; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; +import org.wso2.carbon.identity.core.util.IdentityDatabaseUtil; +import org.wso2.carbon.identity.core.util.IdentityUtil; +import org.wso2.carbon.identity.workflow.engine.exception.WorkflowEngineServerException; +import org.wso2.carbon.identity.workflow.engine.internal.dao.WorkflowEventRequestDAO; +import org.wso2.carbon.identity.workflow.engine.internal.dao.impl.WorkflowEventRequestDAOImpl; +import org.wso2.carbon.identity.workflow.engine.util.TestUtils; + +import java.sql.Connection; +import java.sql.SQLException; +import javax.sql.DataSource; + +import static org.powermock.api.mockito.PowerMockito.mock; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.mockito.PowerMockito.when; +import static org.testng.Assert.assertEquals; + +@PrepareForTest({IdentityDatabaseUtil.class, IdentityUtil.class, DefaultWorkflowEventRequestServiceTest.class}) +public class DefaultWorkflowEventRequestServiceTest extends PowerMockTestCase { + + DefaultWorkflowEventRequest defaultWorkflowEventRequest = new DefaultWorkflowEventRequestService(); + WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); + private static final Log log = LogFactory.getLog(DefaultWorkflowEventRequestServiceTest.class); + + @BeforeMethod + public void setUp() throws Exception { + + TestUtils.initiateH2Base(); + DataSource dataSource = mock(DataSource.class); + TestUtils.mockDataSource(dataSource); + mockStatic(IdentityUtil.class); + + try (Connection connection = TestUtils.getConnection()) { + Connection spyConnection = TestUtils.spyConnection(connection); + when(dataSource.getConnection()).thenReturn(spyConnection); + try { + workflowEventRequestDAO + .addApproversOfRequest("task1", "event1", "wf1", "role", + "Manager", "Reserved"); + workflowEventRequestDAO + .addApproversOfRequest("task2", "event2", "wf2", "role", + "Supervisor", null); + workflowEventRequestDAO + .addApproversOfRequest("task3", "event3", "wf3", "user", + "Senior manager", "Reserved"); + } catch (WorkflowEngineServerException e) { + log.error("Error while adding a task", e); + } + } + } + + @AfterMethod + public void tearDown() throws Exception { + + TestUtils.closeH2Base(); + } + + @DataProvider(name = "testGetApproversOfRequestsData") + public Object[][] testGetApproversOfRequestsData() { + + return new Object[][]{ + // eventId + // taskId + {"event4", "task4"}, + {"event5", "task5"}, + {"event6", "task6"} + }; + } + + @Test(dataProvider = "testGetApproversOfRequestsData") + public void testGetApprovalOfRequest(String eventId, String expected) { + + DataSource dataSource = mock(DataSource.class); + TestUtils.mockDataSource(dataSource); + try { + try (Connection connection = TestUtils.getConnection()) { + Connection spyConnection = TestUtils.spyConnection(connection); + when(dataSource.getConnection()).thenReturn(spyConnection); + try { + workflowEventRequestDAO + .addApproversOfRequest("task4", "event4", "wf4", "user", + "Bob", "Reserved"); + workflowEventRequestDAO + .addApproversOfRequest("task5", "event5", "wf5", "role", + "Supervisor", "Ready"); + workflowEventRequestDAO + .addApproversOfRequest("task6", "event6", "wf6", "user", + "Sam", "Reserved"); + String task = workflowEventRequestDAO.getApproversOfRequest(eventId); + assertEquals(task, expected); + } catch (WorkflowEngineServerException e) { + log.error(String.format("Error while selecting a task: %s", eventId), e); + } + } + } catch (SQLException e) { + log.error("SQL Exception", e); + } + } + + @DataProvider(name = "testDeleteApprovalOfRequestData") + public Object[][] testDeleteApprovalOfRequestData() { + + return new Object[][]{ + // taskId + // eventId + {"task1", "event1"}, + {"task2", "event2"}, + {"task3", "event3"} + }; + } + + @Test(dataProvider = "testDeleteApprovalOfRequestData") + public void testDeleteApprovalOfRequest(String taskId, String eventId) { + + DataSource dataSource = mock(DataSource.class); + TestUtils.mockDataSource(dataSource); + try { + try (Connection connection = TestUtils.getConnection()) { + Connection spyConnection = TestUtils.spyConnection(connection); + when(dataSource.getConnection()).thenReturn(spyConnection); + try { + defaultWorkflowEventRequest.deleteApprovalOfRequest(taskId); + String task = defaultWorkflowEventRequest.getApprovalOfRequest(eventId); + Assert.assertNull(task); + } catch (WorkflowEngineServerException e) { + log.error(String.format("Error while selecting a task: %s", taskId), e); + } + } + } catch (SQLException e) { + log.error("SQL Exception", e); + } + } + + @DataProvider(name = "testStatesOfRequest") + public Object[][] testCreateStatesOfRequestData() { + + return new Object[][]{ + // eventId + // workflowId + // currentStep + {"event1", "wf1", 1}, + {"event1", "wf1", 2}, + {"event2", "wf2", 2}, + {"event3", "wf3", 1} + }; + } + + @Test(dataProvider = "testStatesOfRequest") + public void testStatesOfRequest(String eventId, String workflowId, int currentStep) { + + DataSource dataSource = mock(DataSource.class); + TestUtils.mockDataSource(dataSource); + + try { + try (Connection connection = TestUtils.getConnection()) { + Connection spyConnection = TestUtils.spyConnection(connection); + when(dataSource.getConnection()).thenReturn(spyConnection); + try { + defaultWorkflowEventRequest.createStatesOfRequest(eventId, workflowId, currentStep); + int step = defaultWorkflowEventRequest.getStateOfRequest(eventId, workflowId); + assertEquals(step, currentStep); + } catch (WorkflowEngineServerException e) { + log.error(String.format("Error while selecting current step: %s", eventId), e); + } + } + } catch (SQLException e) { + log.error("SQL Exception", e); + } + } + + @Test(dataProvider = "testStatesOfRequest") + public void testUpdateStateOfRequest(String eventId, String workflowId, int currentStep) { + + DataSource dataSource = mock(DataSource.class); + TestUtils.mockDataSource(dataSource); + try { + try (Connection connection = TestUtils.getConnection()) { + Connection spyConnection = TestUtils.spyConnection(connection); + when(dataSource.getConnection()).thenReturn(spyConnection); + try { + defaultWorkflowEventRequest.createStatesOfRequest(eventId, workflowId, currentStep); + int step = defaultWorkflowEventRequest.getStateOfRequest(eventId, workflowId); + assertEquals(step, currentStep); + } catch (WorkflowEngineServerException e) { + log.error(String.format("Error while selecting current step: %s", eventId), e); + } + } + } catch (SQLException e) { + log.error("SQL Exception", e); + } + } + + @DataProvider(name = "testDeleteStateOfRequestData") + public Object[][] testDeleteStateOfRequestData() { + + return new Object[][]{ + // eventId + // workflowId + {"event1", "wf1"}, + {"event2", "wf2"}, + {"event3", "wf3"} + }; + } + + @Test(dataProvider = "testDeleteStateOfRequestData") + public void testDeleteStateOfRequest(String eventId, String workflowId) { + + DataSource dataSource = mock(DataSource.class); + TestUtils.mockDataSource(dataSource); + try { + try (Connection connection = TestUtils.getConnection()) { + Connection spyConnection = TestUtils.spyConnection(connection); + when(dataSource.getConnection()).thenReturn(spyConnection); + try { + defaultWorkflowEventRequest.deleteStateOfRequest(eventId); + int step = defaultWorkflowEventRequest.getStateOfRequest(eventId, workflowId); + + assertEquals(step, 0); + } catch (WorkflowEngineServerException e) { + log.error(String.format("Error while selecting current step: %s", eventId), e); + } + } + } catch (SQLException e) { + log.error("SQL Exception", e); + } + } +} \ No newline at end of file diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/test/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImplTest.java b/components/org.wso2.carbon.identity.workflow.engine/src/test/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImplTest.java new file mode 100644 index 0000000..5090985 --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/src/test/java/org/wso2/carbon/identity/workflow/engine/internal/dao/impl/WorkflowEventRequestDAOImplTest.java @@ -0,0 +1,368 @@ +package org.wso2.carbon.identity.workflow.engine.internal.dao.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.testng.PowerMockTestCase; +import org.testng.Assert; +import org.testng.IObjectFactory; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.DataProvider; +import org.testng.annotations.ObjectFactory; +import org.testng.annotations.Test; +import org.wso2.carbon.identity.core.util.IdentityDatabaseUtil; +import org.wso2.carbon.identity.workflow.engine.util.TestUtils; +import org.wso2.carbon.identity.workflow.engine.exception.WorkflowEngineServerException; +import org.wso2.carbon.identity.workflow.engine.internal.dao.WorkflowEventRequestDAO; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import javax.sql.DataSource; + +import static org.powermock.api.mockito.PowerMockito.mock; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.mockito.PowerMockito.when; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; +import static org.wso2.carbon.identity.workflow.engine.util.TestUtils.getConnection; +import static org.wso2.carbon.identity.workflow.engine.util.TestUtils.mockDataSource; +import static org.wso2.carbon.identity.workflow.engine.util.TestUtils.spyConnection; + +@PrepareForTest({IdentityDatabaseUtil.class}) +public class WorkflowEventRequestDAOImplTest extends PowerMockTestCase { + + private static final Log log = LogFactory.getLog(WorkflowEventRequestDAOImplTest.class); + private WorkflowEventRequestDAO workflowEventRequestDAO = new WorkflowEventRequestDAOImpl(); + + @BeforeMethod + public void setUp() throws Exception { + + TestUtils.initiateH2Base(); + mockStatic(IdentityDatabaseUtil.class); + } + + @AfterMethod + public void tearDown() throws Exception { + + TestUtils.closeH2Base(); + } + + @DataProvider(name = "testAddApproversOfRequestsData") + public Object[][] testAddApproversOfRequestsData() { + + return new Object[][]{ + // taskId + // eventId + // workflowId + // approverType + // approverName + // taskStatus + {"task1", "event1", "workflowId1", "role", "role1", "Reserved"}, + {"task2", "event2", "workflowId2", "role", "role2", "Reserved"}, + {"task3", "event3", "workflowId3", "user", "user2", "Ready"} + }; + } + + @Test(dataProvider = "testAddApproversOfRequestsData") + public void testAddApproversOfRequest(String taskId, String eventId, String workflowId, String approverType, + String approverName, String taskStatus) { + + { + DataSource dataSource = mock(DataSource.class); + TestUtils.mockDataSource(dataSource); + + try (Connection connection = TestUtils.getConnection()) { + Connection spyConnection = TestUtils.spyConnection(connection); + when(dataSource.getConnection()).thenReturn(spyConnection); + try { + workflowEventRequestDAO.addApproversOfRequest(taskId, eventId, workflowId, approverType, approverName, taskStatus); + String task = workflowEventRequestDAO.getApproversOfRequest(eventId); + assertNotNull(task); + Assert.assertEquals(workflowEventRequestDAO.getWorkflowID(taskId), workflowId); + Assert.assertEquals(workflowEventRequestDAO.getRequestID(taskId), eventId); + Assert.assertEquals(workflowEventRequestDAO.getApproversOfRequest(eventId), taskId); + Assert.assertEquals(workflowEventRequestDAO.getTaskStatusOfRequest(taskId), taskStatus); + } catch (WorkflowEngineServerException e) { + log.error(String.format("Error while adding task: %s", taskId), e); + } + } catch (SQLException e) { + //Mock behaviour. Hence, ignored. + } catch (WorkflowEngineServerException e) { + assertEquals(e.getMessage(), + String.format("Error occurred while adding the taskId: %s, for event: %s, for workflow" + + " id: %s, having the approver type: %s, with approver name " + + ": %s, task status: %s", + taskId, eventId, workflowId, approverType, approverName, taskStatus)); + } + } + } + + @DataProvider(name = "testApprovalOfRequestData") + public Object[][] testDeleteApprovalOfRequestData() { + + return new Object[][]{ + // taskId + // eventId + {"task1", "event1"}, + {"task2", "event2"} + }; + } + + @Test(dataProvider = "testApprovalOfRequestData") + public void testDeleteApproversOfRequest(String taskId, String eventId) { + + DataSource dataSource = mock(DataSource.class); + TestUtils.mockDataSource(dataSource); + + try (Connection connection = TestUtils.getConnection()) { + Connection spyConnection = TestUtils.spyConnection(connection); + when(dataSource.getConnection()).thenReturn(spyConnection); + try { + workflowEventRequestDAO.deleteApproversOfRequest(taskId); + String task = workflowEventRequestDAO.getApproversOfRequest(eventId); + Assert.assertNull(task); + } catch (WorkflowEngineServerException e) { + log.error(String.format("Error while adding task: %s", taskId), e); + } + } catch (SQLException e) { + //Mock behaviour. Hence, ignored. + } catch (WorkflowEngineServerException e) { + assertEquals(e.getMessage(), + String.format("Error occurred while adding the taskId: %s, for event: %s", + taskId, eventId)); + } + } + + @DataProvider(name = "testStatesOfRequestData") + public Object[][] testCreateStatesOfRequestData() { + + return new Object[][]{ + // eventId + // workflowId + // currentStep + {"event1", "wf1", 1}, + {"event2", "wf2", 2}, + {"event3", "wf3", 0} + }; + } + + @Test(dataProvider = "testStatesOfRequestData") + public void testCreateStatesOfRequest(String eventId, String workflowId, int currentStep) { + + DataSource dataSource = mock(DataSource.class); + mockDataSource(dataSource); + + try (Connection connection = getConnection()) { + Connection spyConnection = spyConnection(connection); + when(dataSource.getConnection()).thenReturn(spyConnection); + workflowEventRequestDAO.createStatesOfRequest(eventId, workflowId, currentStep); + int step = workflowEventRequestDAO.getStateOfRequest(eventId, workflowId); + assertEquals(step, currentStep); + } catch (SQLException | WorkflowEngineServerException e) { + //Mock behaviour. Hence, ignored. + } + } + + @Test(dataProvider = "testStatesOfRequestData") + public void testUpdateStateOfRequest(String eventId, String workflowId, int currentStep) { + + DataSource dataSource = mock(DataSource.class); + mockDataSource(dataSource); + + try (Connection connection = getConnection()) { + Connection spyConnection = spyConnection(connection); + when(dataSource.getConnection()).thenReturn(spyConnection); + workflowEventRequestDAO.createStatesOfRequest(eventId, workflowId, currentStep); + int step = workflowEventRequestDAO.getStateOfRequest(eventId, workflowId); + step += 1; + workflowEventRequestDAO.updateStateOfRequest(eventId, workflowId, step); + int updatedStep = workflowEventRequestDAO.getStateOfRequest(eventId, workflowId); + assertEquals(step, updatedStep); + } catch (SQLException | WorkflowEngineServerException e) { + //Mock behaviour. Hence, ignored. + } + } + + @DataProvider(name = "testDeleteStateOfRequestData") + public Object[][] testDeleteStateOfRequestData() { + + return new Object[][]{ + // eventId + // workflowId + {"event1", "wf1"}, + }; + } + + @Test(dataProvider = "testDeleteStateOfRequestData") + public void testDeleteCurrentStepOfRequest(String eventId, String workflowId) { + + DataSource dataSource = mock(DataSource.class); + mockDataSource(dataSource); + + try (Connection connection = getConnection()) { + Connection spyConnection = spyConnection(connection); + when(dataSource.getConnection()).thenReturn(spyConnection); + workflowEventRequestDAO.deleteCurrentStepOfRequest(eventId); + int step = workflowEventRequestDAO.getStateOfRequest(eventId, workflowId); + assertEquals(step, 0); + } catch (SQLException | WorkflowEngineServerException e) { + //Mock behaviour. Hence, ignored. + } + } + + @DataProvider(name = "testListApproversData") + public Object[][] testListApproversData() { + + List approverList = new ArrayList<>(); + approverList.add("Alex"); + approverList.add("Bob"); + + List approverList1 = new ArrayList<>(); + approverList1.add("Sam"); + + return new Object[][]{ + // eventId + // expected List + {"task4", approverList}, + {"task5", approverList1} + }; + } + + @Test(dataProvider = "testListApproversData") + public void testListApprovers(String taskId, List expectedList) { + + DataSource dataSource = mock(DataSource.class); + mockDataSource(dataSource); + + try (Connection connection = getConnection()) { + Connection spyConnection = spyConnection(connection); + when(dataSource.getConnection()).thenReturn(spyConnection); + workflowEventRequestDAO.addApproversOfRequest("task4", "event4", "wf1", + "user", "Alex", "Reserved"); + workflowEventRequestDAO.addApproversOfRequest("task4", "event4", "wf1", + "user", "Bob", "Reserved"); + workflowEventRequestDAO.addApproversOfRequest("task5", "event5", "wf2", + "user", "Sam", "Reserved"); + + List approvers = workflowEventRequestDAO.listApprovers(taskId); + Assert.assertEquals(approvers, expectedList); + + } catch (SQLException | WorkflowEngineServerException e) { + //Mock behaviour. Hence, ignored. + } + } + + @DataProvider(name = "testGetRequestIdFromApproverData") + public Object[][] testGetRequestIdFromApproverData() { + + return new Object[][]{ + // eventId + // workflowId + {"Alex", "event6"}, + {"Bob", "event5"} + }; + } + + @Test(dataProvider = "testGetRequestIdFromApproverData") + public void testGetRequestIdFromApprover(String approverName, String eventId) { + + DataSource dataSource = mock(DataSource.class); + mockDataSource(dataSource); + + try (Connection connection = getConnection()) { + Connection spyConnection = spyConnection(connection); + when(dataSource.getConnection()).thenReturn(spyConnection); + workflowEventRequestDAO.addApproversOfRequest("task6", "event6", "wf6", + "user", "Alex", "Reserved"); + Assert.assertFalse(approverName.isEmpty()); + + } catch (SQLException | WorkflowEngineServerException e) { + //Mock behaviour. Hence, ignored. + } + } + + @DataProvider(name = "testGetRequestsListData") + public Object[][] testGetRequestsListData() { + + //List approverList = Arrays.asList(new String[]{"Bob"}); + List requestList = new ArrayList<>(); + requestList.add("request1"); + requestList.add("request2"); + + List requestList1 = new ArrayList<>(); + requestList1.add("request3"); + + return new Object[][]{ + // eventId + // expected List + {"Alex", requestList}, + {"Sam", requestList1} + }; + } + + @Test(dataProvider = "testGetRequestsListData") + public void testGetRequestsList(String approverName, List exceptedList) { + + DataSource dataSource = mock(DataSource.class); + mockDataSource(dataSource); + + try (Connection connection = getConnection()) { + Connection spyConnection = spyConnection(connection); + when(dataSource.getConnection()).thenReturn(spyConnection); + workflowEventRequestDAO.addApproversOfRequest("task7", "request1", "wf1", + "user", "Alex", "Reserved"); + workflowEventRequestDAO.addApproversOfRequest("task8", "request2", "wf1", + "user", "Alex", "Reserved"); + workflowEventRequestDAO.addApproversOfRequest("task9", "request3", "wf2", + "user", "Sam", "Reserved"); + + List requests = workflowEventRequestDAO.getRequestsList(approverName); + Assert.assertEquals(requests, exceptedList); + + } catch (SQLException | WorkflowEngineServerException e) { + //Mock behaviour. Hence, ignored. + } + } + + @DataProvider(name = "testUpdateStatusOfRequestData") + public Object[][] testUpdateStatusOfRequestData() { + + return new Object[][]{ + // taskId + // taskStatus + {"task10", "Completed"}, + {"task11", "Reserved"} + }; + } + + @Test(dataProvider = "testUpdateStatusOfRequestData") + public void testUpdateStatusOfRequest(String taskId, String taskStatus) { + + DataSource dataSource = mock(DataSource.class); + mockDataSource(dataSource); + + try (Connection connection = getConnection()) { + Connection spyConnection = spyConnection(connection); + when(dataSource.getConnection()).thenReturn(spyConnection); + workflowEventRequestDAO.addApproversOfRequest("task10", "event4", "wf1", + "user", "Alex", "Reserved"); + workflowEventRequestDAO.addApproversOfRequest("task11", "event5", "wf2", + "role", "Bob", "Ready"); + workflowEventRequestDAO.updateStatusOfRequest(taskId, taskStatus); + String updatedStatus = workflowEventRequestDAO.getTaskStatusOfRequest(taskId); + Assert.assertEquals(updatedStatus, taskStatus); + + } catch (SQLException | WorkflowEngineServerException e) { + //Mock behaviour. Hence, ignored. + } + } + + @ObjectFactory + public IObjectFactory getObjectFactory() { + + return new org.powermock.modules.testng.PowerMockObjectFactory(); + } +} diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/test/java/org/wso2/carbon/identity/workflow/engine/util/TestUtils.java b/components/org.wso2.carbon.identity.workflow.engine/src/test/java/org/wso2/carbon/identity/workflow/engine/util/TestUtils.java new file mode 100644 index 0000000..2adb24d --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/src/test/java/org/wso2/carbon/identity/workflow/engine/util/TestUtils.java @@ -0,0 +1,82 @@ +package org.wso2.carbon.identity.workflow.engine.util; + +import org.apache.commons.dbcp.BasicDataSource; +import org.apache.commons.lang.StringUtils; +import org.wso2.carbon.identity.core.util.IdentityDatabaseUtil; + +import java.nio.file.Paths; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; +import javax.sql.DataSource; + +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; +import static org.powermock.api.mockito.PowerMockito.mockStatic; + +public class TestUtils { + + public static final String DB_NAME = "Test_db"; + public static final String H2_SCRIPT_NAME = "h2.sql"; + public static Map dataSourceMap = new HashMap<>(); + public static void mockDataSource(DataSource dataSource) { + + mockStatic(IdentityDatabaseUtil.class); + when(IdentityDatabaseUtil.getDataSource()).thenReturn(dataSource); + } + + public static Connection getConnection() throws SQLException { + + if (dataSourceMap.get(DB_NAME) != null) { + return dataSourceMap.get(DB_NAME).getConnection(); + } + throw new RuntimeException("No data source initiated for database: " + DB_NAME); + } + + public static Connection spyConnection(Connection connection) throws SQLException { + + Connection spy = spy(connection); + doNothing().when(spy).close(); + return spy; + } + public static Connection spyConnectionWithError(Connection connection) throws SQLException { + + Connection spy = spy(connection); + doThrow(new SQLException("Test Exception")).when(spy).prepareStatement(anyString()); + return spy; + } + + public static String getFilePath(String fileName) { + + if (StringUtils.isNotBlank(fileName)) { + return Paths.get(System.getProperty("user.dir"), "src", "test", "resources", "dbscripts", + fileName).toString(); + } + throw new IllegalArgumentException("DB Script file name cannot be empty."); + } + + public static void initiateH2Base() throws Exception { + + BasicDataSource dataSource = new BasicDataSource(); + dataSource.setDriverClassName("org.h2.Driver"); + dataSource.setUsername("username"); + dataSource.setPassword("password"); + dataSource.setUrl("jdbc:h2:mem:test" + DB_NAME); + try (Connection connection = dataSource.getConnection()) { + connection.createStatement().executeUpdate("RUNSCRIPT FROM '" + getFilePath(H2_SCRIPT_NAME) + "'"); + } + dataSourceMap.put(DB_NAME, dataSource); + } + + public static void closeH2Base() throws Exception { + + BasicDataSource dataSource = dataSourceMap.get(DB_NAME); + if (dataSource != null) { + dataSource.close(); + } + } +} diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/test/resources/dbscripts/h2.sql b/components/org.wso2.carbon.identity.workflow.engine/src/test/resources/dbscripts/h2.sql new file mode 100644 index 0000000..c2017dc --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/src/test/resources/dbscripts/h2.sql @@ -0,0 +1,39 @@ +CREATE TABLE IF NOT EXISTS WF_REQUEST ( + UUID VARCHAR (45), + CREATED_BY VARCHAR (255), + TENANT_ID INTEGER DEFAULT -1, + OPERATION_TYPE VARCHAR (50), + CREATED_AT TIMESTAMP, + UPDATED_AT TIMESTAMP, + STATUS VARCHAR (30), + REQUEST BLOB, + PRIMARY KEY (UUID) +); + +CREATE TABLE IF NOT EXISTS WF_WORKFLOW( + ID VARCHAR (45), + WF_NAME VARCHAR (45), + DESCRIPTION VARCHAR (255), + TEMPLATE_ID VARCHAR (45), + IMPL_ID VARCHAR (45), + TENANT_ID INTEGER DEFAULT -1, + PRIMARY KEY (ID) +); + +CREATE TABLE IF NOT EXISTS WF_WORKFLOW_APPROVAL_RELATION ( +TASK_ID VARCHAR(45) NOT NULL, +EVENT_ID VARCHAR(45) NOT NULL, +WORKFLOW_ID VARCHAR(45) NOT NULL, +APPROVER_TYPE VARCHAR(45) NOT NULL, +APPROVER_NAME VARCHAR(255) NOT NULL, +TASK_STATUS VARCHAR(255), +CONSTRAINT PK_WF_WORKFLOW_RELATION_CONSTRAINT PRIMARY KEY (TASK_ID, APPROVER_TYPE, APPROVER_NAME) +); + +CREATE TABLE IF NOT EXISTS WF_WORKFLOW_APPROVAL_STATE ( +EVENT_ID VARCHAR(45) NOT NULL, +WORKFLOW_ID VARCHAR(45) NOT NULL, +CURRENT_STEP INTEGER, +CONSTRAINT PK_WF_APPROVAL_STATE_CONSTRAINT PRIMARY KEY (EVENT_ID, WORKFLOW_ID) +); + diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/test/resources/testng.xml b/components/org.wso2.carbon.identity.workflow.engine/src/test/resources/testng.xml new file mode 100644 index 0000000..9a428e8 --- /dev/null +++ b/components/org.wso2.carbon.identity.workflow.engine/src/test/resources/testng.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From 065d15cab385d803f2022749bcc89a99bfeab35b Mon Sep 17 00:00:00 2001 From: IsharaSilva Date: Tue, 26 Jul 2022 20:25:57 +0530 Subject: [PATCH 16/16] Implement tase cases for workflows --- .../src/test/resources/testng.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/org.wso2.carbon.identity.workflow.engine/src/test/resources/testng.xml b/components/org.wso2.carbon.identity.workflow.engine/src/test/resources/testng.xml index 9a428e8..5ec564e 100644 --- a/components/org.wso2.carbon.identity.workflow.engine/src/test/resources/testng.xml +++ b/components/org.wso2.carbon.identity.workflow.engine/src/test/resources/testng.xml @@ -24,4 +24,4 @@ - \ No newline at end of file +