最近因为项目需要,所以研究了一下工作流,然而,我们都知道,国外的工作流框架,是不支持驳回问题的!这是其一!第二!项目的工作流要实现任意节点的流转,这也是国外工作流框架所不具备的!因此,国外的工作流框架是没有一个可以真正满足我们的业务要求的,然而国内的工作流架构,我也没有听说过哪个可以支持中国式工作流!因此,在技术选型上面选择了相对比较火的activiti框架!当时我们的原则就是尽可能的不改变框架,而实现我们的业务逻辑,因为,工作流这一大模块是本屌全权负责,就研究了一通!本项目实现了工作流节点的任意流转,工作流回退(包含会签),等等一系列操作,至于研究会签问题的辛酸泪就不在多说,本节也不多说其他功能,主要说工作流回退,特别强调会签回退,以及网上一些会签回退的问题!
本屌在研究工作流的时候,曾参考csdn上某一位同志的博客(点击蓝色文字可以进入),但是在使用的时候,发现会签是有一些问题的,所以对代码进行了一些调整,验证完美实现会签驳回
另外,在上代码之前一定声明一点,大家一定注意我在完成任务的时候,进行了一系类的逻辑判断,此判断尤其重要,关系到会签是否真正的没有问题,曾经我出现过会签驳回之后审批会出现多条记录的情况,因此,希望大家一定要看仔细代码,最后:声明一点,前面已经说了,本屌参考了一篇博客,说明出处,因此有雷同代码,不要疑惑和纠结!废话不在多少,直接上代码!
下面为核心代码,但是会签的时候在审批要注意,在核心代码之后我会贴出我对会签审批的处理
/**
*
* @param taskId 任务id
* @param msg 批注
* @param endActivityId 结束节点的activitiyId
* @throws Exception
*/
public void turnBackNew(String taskId, String msg, String endActivityId) throws Exception {
Map variables;
HistoricTaskInstance currTask = historyService
.createHistoricTaskInstanceQuery().taskId(taskId)
.singleResult();
ProcessInstance instance = runtimeService
.createProcessInstanceQuery()
.processInstanceId(currTask.getProcessInstanceId())
.singleResult();
if (instance == null) {
throw new RuntimeException("流程已结束");
}
variables = instance.getProcessVariables();
ProcessDefinitionEntity definition = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService)
.getDeployedProcessDefinition(currTask
.getProcessDefinitionId());
if (definition == null) {
throw new RuntimeException("流程定义未找到");
}
ActivityImpl currActivity = ((ProcessDefinitionImpl) definition)
.findActivity(currTask.getTaskDefinitionKey());
List rtnList = new ArrayList<>();
List tempList = new ArrayList<>();
List activities = iteratorBackActivity(
taskId,
currActivity,
rtnList,
tempList
);
if (activities == null || activities.size() <= 0) throw new RuntimeException("没有可以选择的驳回节点!");
List list = taskService.createTaskQuery().processInstanceId(instance.getId()).list();
for (Task task : list) {
if (!task.getId().equals(taskId)) {
task.setAssignee("排除标记");
commitProcess(task.getId(), null, endActivityId);
}
}
turnTransition(taskId, activities.get(0).getId(), null);
}
/**
* @param taskId 当前任务ID
* @param variables 流程变量
* @param activityId 流程转向执行任务节点ID
* 此参数为空,默认为提交操作
* @throws Exception
*/
private void commitProcess(String taskId, Map variables,
String activityId) throws Exception {
if (variables == null) {
variables = new HashMap();
}
if (activityId == null || activityId.equals("")) {
taskService.complete(taskId, variables);
} else {
turnTransition(taskId, activityId, variables);
}
}
/**
* 清空指定活动节点流向
*
* @param activityImpl 活动节点
* @return 节点流向集合
*/
private List clearTransition(ActivityImpl activityImpl) {
List oriPvmTransitionList = new ArrayList();
List pvmTransitionList = activityImpl
.getOutgoingTransitions();
for (PvmTransition pvmTransition : pvmTransitionList) {
oriPvmTransitionList.add(pvmTransition);
}
pvmTransitionList.clear();
return oriPvmTransitionList;
}
/**
* 还原指定活动节点流向
*
* @param activityImpl 活动节点
* @param oriPvmTransitionList 原有节点流向集合
*/
private void restoreTransition(ActivityImpl activityImpl,
List oriPvmTransitionList) {
List pvmTransitionList = activityImpl
.getOutgoingTransitions();
pvmTransitionList.clear();
for (PvmTransition pvmTransition : oriPvmTransitionList) {
pvmTransitionList.add(pvmTransition);
}
}
/**
* 流程转向操作
*
* @param taskId 当前任务ID
* @param activityId 目标节点任务ID
* @param variables 流程变量
* @throws Exception
*/
private void turnTransition(String taskId, String activityId,
Map variables) throws Exception {
ActivityImpl currActivity = findActivitiImpl(taskId, null);
List oriPvmTransitionList = clearTransition(currActivity);
TransitionImpl newTransition = currActivity.createOutgoingTransition();
ActivityImpl pointActivity = findActivitiImpl(taskId, activityId);
newTransition.setDestination(pointActivity);
taskService.complete(taskId, variables);
pointActivity.getIncomingTransitions().remove(newTransition);
restoreTransition(currActivity, oriPvmTransitionList);
}
/**
* 迭代循环流程树结构,查询当前节点可驳回的任务节点
*
* @param taskId 当前任务ID
* @param currActivity 当前活动节点
* @param rtnList 存储回退节点集合
* @param tempList 临时存储节点集合(存储一次迭代过程中的同级userTask节点)
* @return 回退节点集合
*/
private List iteratorBackActivity(String taskId,
ActivityImpl currActivity, List rtnList,
List tempList) throws Exception {
ProcessInstance processInstance = findProcessInstanceByTaskId(taskId);
List incomingTransitions = currActivity
.getIncomingTransitions();
List exclusiveGateways = new ArrayList();
List parallelGateways = new ArrayList();
for (PvmTransition pvmTransition : incomingTransitions) {
TransitionImpl transitionImpl = (TransitionImpl) pvmTransition;
ActivityImpl activityImpl = transitionImpl.getSource();
String type = (String) activityImpl.getProperty("type");
/**
* 并行节点配置要求:
* 必须成对出现,且要求分别配置节点ID为:XXX_start(开始),XXX_end(结束)
*/
if ("parallelGateway".equals(type)) {
String gatewayId = activityImpl.getId();
String gatewayType = gatewayId.substring(gatewayId
.lastIndexOf("_") + 1);
if ("START".equals(gatewayType.toUpperCase())) {
return rtnList;
} else {
parallelGateways.add(activityImpl);
}
} else if ("startEvent".equals(type)) {
return rtnList;
} else if ("userTask".equals(type)) {
tempList.add(activityImpl);
} else if ("exclusiveGateway".equals(type)) {
currActivity = transitionImpl.getSource();
exclusiveGateways.add(currActivity);
}
}
/**
* 迭代条件分支集合,查询对应的userTask节点
*/
for (ActivityImpl activityImpl : exclusiveGateways) {
iteratorBackActivity(taskId, activityImpl, rtnList, tempList);
}
/**
* 迭代并行集合,查询对应的userTask节点
*/
for (ActivityImpl activityImpl : parallelGateways) {
iteratorBackActivity(taskId, activityImpl, rtnList, tempList);
}
/**
* 根据同级userTask集合,过滤最近发生的节点
*/
currActivity = filterNewestActivity(processInstance, tempList);
if (currActivity != null) {
String id = findParallelGatewayId(currActivity);
if (id == null || id.equals("")) {
rtnList.add(currActivity);
} else {
currActivity = findActivitiImpl(taskId, id);
}
tempList.clear();
iteratorBackActivity(taskId, currActivity, rtnList, tempList);
}
return rtnList;
}
/**
* 根据当前节点,查询输出流向是否为并行终点,如果为并行终点,则拼装对应的并行起点ID
*
* @param activityImpl 当前节点
* @return
*/
private String findParallelGatewayId(ActivityImpl activityImpl) {
List incomingTransitions = activityImpl
.getOutgoingTransitions();
for (PvmTransition pvmTransition : incomingTransitions) {
TransitionImpl transitionImpl = (TransitionImpl) pvmTransition;
activityImpl = transitionImpl.getDestination();
String type = (String) activityImpl.getProperty("type");
if ("parallelGateway".equals(type)) {
String gatewayId = activityImpl.getId();
String gatewayType = gatewayId.substring(gatewayId
.lastIndexOf("_") + 1);
if ("END".equals(gatewayType.toUpperCase())) {
return gatewayId.substring(0, gatewayId.lastIndexOf("_"))
+ "_start";
}
}
}
return null;
}
/**
* 根据流入任务集合,查询最近一次的流入任务节点
*
* @param processInstance 流程实例
* @param tempList 流入任务集合
* @return
*/
private ActivityImpl filterNewestActivity(ProcessInstance processInstance,
List tempList) {
while (tempList.size() > 0) {
ActivityImpl activity_1 = tempList.get(0);
HistoricActivityInstance activityInstance_1 = findHistoricUserTask(
processInstance, activity_1.getId());
if (activityInstance_1 == null) {
tempList.remove(activity_1);
continue;
}
if (tempList.size() > 1) {
ActivityImpl activity_2 = tempList.get(1);
HistoricActivityInstance activityInstance_2 = findHistoricUserTask(
processInstance, activity_2.getId());
if (activityInstance_2 == null) {
tempList.remove(activity_2);
continue;
}
if (activityInstance_1.getEndTime().before(
activityInstance_2.getEndTime())) {
tempList.remove(activity_1);
} else {
tempList.remove(activity_2);
}
} else {
break;
}
}
if (tempList.size() > 0) {
return tempList.get(0);
}
return null;
}
/**
* 查询指定任务节点的最新记录
*
* @param processInstance 流程实例
* @param activityId
* @return
*/
private HistoricActivityInstance findHistoricUserTask(
ProcessInstance processInstance, String activityId) {
HistoricActivityInstance rtnVal = null;
List historicActivityInstances = historyService
.createHistoricActivityInstanceQuery().activityType("userTask")
.processInstanceId(processInstance.getId()).activityId(
activityId).finished()
.orderByHistoricActivityInstanceEndTime().desc().list();
if (historicActivityInstances.size() > 0) {
rtnVal = historicActivityInstances.get(0);
}
return rtnVal;
}
/**
* 根据任务ID和节点ID获取活动节点
*
* @param taskId 任务ID
* @param activityId 活动节点ID
* 如果为null或"",则默认查询当前活动节点
* 如果为"end",则查询结束节点
* @return
* @throws Exception
*/
private ActivityImpl findActivitiImpl(String taskId, String activityId)
throws Exception {
ProcessDefinitionEntity processDefinition = findProcessDefinitionEntityByTaskId(taskId);
if (activityId == null || "".equals(activityId)) {
activityId = findTaskById(taskId).getTaskDefinitionKey();
}
if (activityId.toUpperCase().equals("END")) {
for (ActivityImpl activityImpl : processDefinition.getActivities()) {
List pvmTransitionList = activityImpl
.getOutgoingTransitions();
if (pvmTransitionList.isEmpty()) {
return activityImpl;
}
}
}
ActivityImpl activityImpl = ((ProcessDefinitionImpl) processDefinition)
.findActivity(activityId);
return activityImpl;
}
/**
* 根据任务ID获取流程定义
*
* @param taskId 任务ID
* @return
* @throws Exception
*/
private ProcessDefinitionEntity findProcessDefinitionEntityByTaskId(
String taskId) throws Exception {
ProcessDefinitionEntity processDefinition = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService)
.getDeployedProcessDefinition(findTaskById(taskId)
.getProcessDefinitionId());
if (processDefinition == null) {
throw new Exception("流程定义未找到!");
}
return processDefinition;
}
/**
* 根据任务ID获得任务实例
*
* @param taskId 任务ID
* @return
* @throws Exception
*/
private TaskEntity findTaskById(String taskId) throws Exception {
TaskEntity task = (TaskEntity) taskService.createTaskQuery().taskId(
taskId).singleResult();
if (task == null) {
throw new Exception("任务实例未找到!");
}
return task;
}
/**
* 根据任务ID获取对应的流程实例
*
* @param taskId 任务ID
* @return
* @throws Exception
*/
private ProcessInstance findProcessInstanceByTaskId(String taskId)
throws Exception {
ProcessInstance processInstance = runtimeService
.createProcessInstanceQuery().processInstanceId(
findTaskById(taskId).getProcessInstanceId())
.singleResult();
if (processInstance == null) {
throw new Exception("流程实例未找到!");
}
return processInstance;
}
下面尤其重要的说明一点,会签在提交的时候,一定要如我之判定,否则,会在多次驳回的时候,出现多条记录,流程混乱的问题
public void submitTask(ProjectWorkflowRequest param, String userCode, String username, User user) throws Exception {
String s = WorkflowReflectionLog.refectCompare(projectService.getProjectByProjectId(projectService.getNewestProjectIdByApplicationId(param.getProject().getTab1().getApplicationProjectId())), param.getProject())
// if (true) throw new RuntimeException()
projectService.applicationSubmit(param.getProject(), user)
Task task = taskService.createTaskQuery().taskId(param.getTaskId()).singleResult()
String processInstanceId = task.getProcessInstanceId()
taskService.addComment(param.getTaskId(), processInstanceId, param.getMsg())
taskService.setVariableLocal(param.getTaskId(), "projectId", param.getProject().getTab1().getProjectId())
if (s != null) taskService.setVariableLocal(param.getTaskId(), "log", s)
runtimeService.setVariables(task.getExecutionId(), new HashMap() {{
this.put("LAST_USER", userCode)
this.put("LAST_USER_NAME", username)
}})
runtimeService.setVariable(processInstanceId, "LAST_USER", username)
// 因为前台标记驳回还是通过还是终止流程,分别用0,1,2标识,所以,你就当是三种情况,通过驳回,终止
if ("1".equals(param.getFlag())) {// 通过
switch (param.getType()) {
case 1: {
String sid = task.getTaskDefinitionKey()
List list = taskService.createTaskQuery().processInstanceId(task.getProcessInstanceId()).list()
if (list.size() == 1) {// 如果只剩一个任务,可能是普通任务,可能是会签任务
if (
"sid-71E0BFA1-AD56-45D6-907E-D4B61F445D84".equals(sid) ||
"sid-9795199D-77F0-4F34-A0D6-E388525FA8BE".equals(sid) ||
"sid-6340E7E8-2BA1-4B2F-B7CF-19FE31412E58".equals(sid)) {// 如果是会签,则提交到下一个审批节点(并行网关之后的第一个节点)
//工作流任意流转,流转到下一个审批节点,因为我的会签里面包含三个节点,所以经过activitiyId来判定是不是这三个节点,如果是,则任意流转节点,如果不是,则正常完成任务,注意:工作流程图确定之后,每个节点的activityId是永恒不变的!因此,才可以做此判定
commitProcess(task.getId(), null, "sid-C6334E47-F8A8-430C-9A35-FA98CFD0C405")
} else if (
"sid-62D73895-5AB3-4818-8795-D8CCCDB59CE6".equals(sid) ||
"sid-CFDED207-B694-478A-AE94-3DFFD486E77F".equals(sid) ||
"sid-7A523F96-9F43-4C2C-979F-06B7BC9656B6".equals(sid)
) else taskService.complete(task.getId())
} else {
commitProcess(task.getId(), null, "sid-404150FC-8E2D-4582-9CFB-BC9FF43F09E1")
}
}
break
case 2: {
Map variables = new HashMap<>()
variables.put("a", param.getU1())
variables.put("b", param.getU2())
taskService.complete(param.getTaskId(), variables)
}
break
case 3: {
Map variables = new HashMap<>()
variables.put("flag", param.getFlag1())
taskService.complete(param.getTaskId(), variables)
}
break
}
} else if ("0".equals(param.getFlag())) {// 拒绝 // // TODO: 2017/11/8 0008
turnBackNew(param.getTaskId(), param.getMsg(), "sid-404150FC-8E2D-4582-9CFB-BC9FF43F09E1")
} else if ("2".equals(param.getFlag())) {// 终止流程
endProcess(param.getTaskId(), "sid-404150FC-8E2D-4582-9CFB-BC9FF43F09E1")
}
}