提交 47afe3db authored 作者: 黄夏豪's avatar 黄夏豪

[工作流引擎] 修复了流程结束后无法调用回调函数的BUG

上级 555b6ecc
package com.tykj.workflowcore.workflow_editer.listener; package com.tykj.workflowcore.workflow_editer.listener;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.tykj.workflowcore.api.controller.ApiController;
import com.tykj.workflowcore.api.entity.InvokeRequest; import com.tykj.workflowcore.api.entity.InvokeRequest;
import com.tykj.workflowcore.api.entity.Parameter; import com.tykj.workflowcore.api.entity.Parameter;
import com.tykj.workflowcore.workflow_editer.entity.CommandGetValue; import com.tykj.workflowcore.workflow_editer.entity.CommandGetValue;
...@@ -8,11 +9,14 @@ import com.tykj.workflowcore.workflow_editer.entity.VariableStorage; ...@@ -8,11 +9,14 @@ import com.tykj.workflowcore.workflow_editer.entity.VariableStorage;
import com.tykj.workflowcore.workflow_editer.entity.vo.InvokeRequestVo; import com.tykj.workflowcore.workflow_editer.entity.vo.InvokeRequestVo;
import com.tykj.workflowcore.workflow_editer.entity.vo.SearchVariableStorageVo; import com.tykj.workflowcore.workflow_editer.entity.vo.SearchVariableStorageVo;
import com.tykj.workflowcore.workflow_editer.service.VariableStorageService; import com.tykj.workflowcore.workflow_editer.service.VariableStorageService;
import liquibase.pro.packaged.A;
import org.flowable.common.engine.api.delegate.Expression; import org.flowable.common.engine.api.delegate.Expression;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent; import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.engine.*; import org.flowable.engine.*;
import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener; import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener;
import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl; import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.flowable.engine.impl.persistence.entity.ExecutionEntity;
import org.flowable.engine.runtime.ProcessInstance; import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -46,25 +50,25 @@ public class ProcessEndListener extends AbstractFlowableEngineEventListener { ...@@ -46,25 +50,25 @@ public class ProcessEndListener extends AbstractFlowableEngineEventListener {
ProcessEngineConfigurationImpl processEngineConfiguration; ProcessEngineConfigurationImpl processEngineConfiguration;
@Autowired @Autowired
ProcessEngine processEngine; ApiController apiController;
@Override @Override
protected void processCompleted(FlowableEngineEntityEvent event) { protected void processCompleted(FlowableEngineEntityEvent event) {
//流程结束了
if (event.getEntity() instanceof ProcessInstance){ if (event.getEntity() instanceof ProcessInstance){
ProcessInstance processInstance = (ProcessInstance) event.getEntity(); ProcessInstance processInstance = (ProcessInstance) event.getEntity();
String processInstanceId = processInstance.getProcessInstanceId();
String processDefinitionId = processInstance.getProcessDefinitionId(); String processDefinitionId = processInstance.getProcessDefinitionId();
String flowKey = processDefinitionId.substring(0,processDefinitionId.indexOf(":")); String flowKey = processDefinitionId.substring(0,processDefinitionId.indexOf(":"));
List<VariableStorage> variableStorageList = variableStorageService.searchVariableStorageList(new SearchVariableStorageVo(flowKey)); List<VariableStorage> variableStorageList = variableStorageService.searchVariableStorageList(new SearchVariableStorageVo(flowKey));
for (VariableStorage variableStorage : variableStorageList) { for (VariableStorage variableStorage : variableStorageList) {
InvokeRequestVo variableInfo = variableStorage.getInvokeRequest(); InvokeRequestVo variableInfo = variableStorage.getInvokeRequest();
variableInfo.setProcessInstance(processInstance);
//调用服务接口 //调用服务接口
variableInfo.setProcessInstanceId(processInstanceId); //1. 获取调用的具体数值
getApiInvokeParam(variableInfo); apiController.invoke(getApiInvokeParam(variableInfo));
} }
} }
// throw new ApiException("强行报错"); // throw new ApiException("强行报错");
} }
@Override @Override
...@@ -72,6 +76,12 @@ public class ProcessEndListener extends AbstractFlowableEngineEventListener { ...@@ -72,6 +76,12 @@ public class ProcessEndListener extends AbstractFlowableEngineEventListener {
super.taskCompleted(event); super.taskCompleted(event);
} }
@Override
public void onEvent(FlowableEvent flowableEvent) {
System.out.println(flowableEvent.getType());
super.onEvent(flowableEvent);
}
/** /**
* 获取调用Api用的参数 * 获取调用Api用的参数
* *
...@@ -80,37 +90,34 @@ public class ProcessEndListener extends AbstractFlowableEngineEventListener { ...@@ -80,37 +90,34 @@ public class ProcessEndListener extends AbstractFlowableEngineEventListener {
*/ */
public InvokeRequest getApiInvokeParam(InvokeRequestVo invokeRequestVo) { public InvokeRequest getApiInvokeParam(InvokeRequestVo invokeRequestVo) {
//拿出taskId //拿出taskId
String processInstanceId = invokeRequestVo.getProcessInstanceId(); ProcessInstance processInstance = invokeRequestVo.getProcessInstance();
List<Parameter> parameterList = invokeRequestVo.getParameterList(); List<Parameter> parameterList = invokeRequestVo.getParams();
List<Parameter> newParameterList = new ArrayList<>(); List<Parameter> newParameterList = new ArrayList<>();
for (int i = 0; i < parameterList.size(); i++) { for (int i = 0; i < parameterList.size(); i++) {
Parameter parameter = parameterList.get(i); Parameter parameter = parameterList.get(i);
if (!StringUtils.isEmpty(parameter.getExp())) { if (!StringUtils.isEmpty(parameter.getExp())) {
//如果是表示式 如:${people} //如果是表示式 如:${people}
parameter.setInstance((Map<String, Object>) getProcessValue(processInstanceId, parameter.getExp())); parameter.setInstance((Map<String, Object>) getProcessValue(processInstance, parameter.getExp()));
} else { } else {
Map<String, Object> instance = parameter.getInstance(); Map<String, Object> instance = parameter.getInstance();
JSONObject newInstance = new JSONObject(); JSONObject newInstance = new JSONObject();
//遍历param的key //遍历param的key
Set<String> oldInstanceKey = instance.keySet(); Set<String> oldInstanceKey = instance.keySet();
for (String key : oldInstanceKey) { for (String key : oldInstanceKey) {
newInstance.put(key, getProcessValue(processInstanceId, (String) instance.get(key))); newInstance.put(key, getProcessValue(processInstance, (String) instance.get(key)));
} }
parameter.setInstance(newInstance); parameter.setInstance(newInstance);
} }
newParameterList.add(i, parameter); newParameterList.add(i, parameter);
} }
invokeRequestVo.setParameterList(newParameterList); invokeRequestVo.setParams(newParameterList);
return JSONObject.parseObject(JSONObject.toJSONString(invokeRequestVo), InvokeRequest.class); return JSONObject.parseObject(JSONObject.toJSONString(invokeRequestVo), InvokeRequest.class);
} }
public Object getProcessValue(String processInstanceId, String exp) { public Object getProcessValue(ProcessInstance processInstance, String exp) {
// processInstance -> ExecutionEntity // processInstance -> ExecutionEntity
Expression expression = processEngineConfiguration.getExpressionManager().createExpression(exp); Expression expression = processEngineConfiguration.getExpressionManager().createExpression(exp);
ManagementService managementService = processEngine.getManagementService(); return expression.getValue((ExecutionEntity)processInstance);
CommandGetValue test = new CommandGetValue(processInstanceId, expression);
Object o = managementService.executeCommand(test);
return o;
} }
} }
...@@ -33,6 +33,7 @@ import org.flowable.image.ProcessDiagramGenerator; ...@@ -33,6 +33,7 @@ import org.flowable.image.ProcessDiagramGenerator;
import org.flowable.task.api.Task; import org.flowable.task.api.Task;
import org.flowable.task.api.TaskQuery; import org.flowable.task.api.TaskQuery;
import org.flowable.task.api.history.HistoricTaskInstance; import org.flowable.task.api.history.HistoricTaskInstance;
import org.hibernate.engine.transaction.internal.TransactionImpl;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -57,6 +58,7 @@ import java.util.regex.Pattern; ...@@ -57,6 +58,7 @@ import java.util.regex.Pattern;
@Service @Service
public class WorkFlowServiceImpl implements WorkFlowService { public class WorkFlowServiceImpl implements WorkFlowService {
private final RepositoryService repositoryService; private final RepositoryService repositoryService;
private final RuntimeService runtimeService; private final RuntimeService runtimeService;
private final TaskService taskService; private final TaskService taskService;
...@@ -285,63 +287,16 @@ public class WorkFlowServiceImpl implements WorkFlowService { ...@@ -285,63 +287,16 @@ public class WorkFlowServiceImpl implements WorkFlowService {
if (taskVo.getComments() != null) { if (taskVo.getComments() != null) {
taskService.addComment(task.getId(), processInstanceId, taskVo.getComments()); taskService.addComment(task.getId(), processInstanceId, taskVo.getComments());
} }
// //先判断是不是表达式 //先判断是不是表达式
// if (taskVo.getConditionalExpression() != null) { if (taskVo.getConditionalExpression() != null) {
// claimTask(task.getId(), userService.getCurrentUser().getId()); claimTask(task.getId(), userService.getCurrentUser().getId());
// taskService.complete(task.getId()); taskService.complete(task.getId());
//
// } else {
// map.put("handlingOpinion", taskVo.getHandlingOpinion());
// claimTask(task.getId(), userService.getCurrentUser().getId());
// taskService.complete(task.getId(), map);
// }
taskService.complete(task.getId());
// 流程结束了
// ProcessInstance processInstance =
// runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
// if (processInstance == null){
// //通过processInstanceId查询出flowKey
// FlowsInfo flowsInfo = flowsInfoMapper.findByProcessInstanceId(processInstanceId);
// //查询调用接口的配置
// List<VariableStorage> variableStorageList = variableStorageService.searchVariableStorageList(new
// SearchVariableStorageVo(flowsInfo.getFlowKey()));
// for (VariableStorage variableStorage : variableStorageList) {
// InvokeRequestVo variableInfo = variableStorage.getInvokeRequest();
// //调用服务接口
// getApiInvokeParam(variableInfo);
// }
// }
//查询是否为最后一个节点
// ExecutionEntity ee = (ExecutionEntity) processEngine.getRuntimeService().createExecutionQuery()
// .executionId(task.getExecutionId()).singleResult();
// BpmnModel bpmnModel = processEngine.getRepositoryService().getBpmnModel(task.getProcessDefinitionId());
// FlowNode flowNode = (FlowNode) bpmnModel.getFlowElement(ee.getActivityId());
// List<SequenceFlow> outFlows = flowNode.getOutgoingFlows();
// for (SequenceFlow sequenceFlow : outFlows) {
// FlowElement targetFlow = sequenceFlow.getTargetFlowElement();
// // 如果下个审批节点为结束节点
// if (targetFlow instanceof EndEvent) {
// System.out.println("下一节点为结束节点:id=" + targetFlow.getId() + ",name=" + targetFlow.getName());
// }
// }
} else {
// if (taskVo.getTaskId() == null){ map.put("handlingOpinion", taskVo.getHandlingOpinion());
// //流程结束了 claimTask(task.getId(), userService.getCurrentUser().getId());
// //通过processInstanceId查询出flowKey taskService.complete(task.getId(), map);
// FlowsInfo flowsInfo = flowsInfoMapper.findByProcessInstanceId(processInstanceId); }
// //查询调用接口的配置
// List<VariableStorage> variableStorageList = variableStorageService.searchVariableStorageList(new SearchVariableStorageVo(flowsInfo.getFlowKey()));
// for (VariableStorage variableStorage : variableStorageList) {
// InvokeRequestVo variableInfo = variableStorage.getInvokeRequest();
// //调用服务接口
// getApiInvokeParam(variableInfo);
// }
// }
} }
...@@ -464,47 +419,6 @@ public class WorkFlowServiceImpl implements WorkFlowService { ...@@ -464,47 +419,6 @@ public class WorkFlowServiceImpl implements WorkFlowService {
return null; return null;
} }
/**
* 获取调用Api用的参数
*
* @param invokeRequestVo 调用服务接口vo
* @return 调用服务接口实体
*/
public InvokeRequest getApiInvokeParam(InvokeRequestVo invokeRequestVo) {
//拿出taskId
String processInstanceId = invokeRequestVo.getProcessInstanceId();
List<Parameter> parameterList = invokeRequestVo.getParameterList();
List<Parameter> newParameterList = new ArrayList<>();
for (int i = 0; i < parameterList.size(); i++) {
Parameter parameter = parameterList.get(i);
if (!StringUtils.isEmpty(parameter.getExp())) {
//如果是表示式 如:${people}
parameter.setInstance((Map<String, Object>) getProcessValue(processInstanceId, parameter.getExp()));
} else {
Map<String, Object> instance = parameter.getInstance();
JSONObject newInstance = new JSONObject();
//遍历param的key
Set<String> oldInstanceKey = instance.keySet();
for (String key : oldInstanceKey) {
newInstance.put(key, getProcessValue(processInstanceId, (String) instance.get(key)));
}
parameter.setInstance(newInstance);
}
newParameterList.add(i, parameter);
}
invokeRequestVo.setParameterList(newParameterList);
return JSONObject.parseObject(JSONObject.toJSONString(invokeRequestVo), InvokeRequest.class);
}
public Object getProcessValue(String processInstanceId, String exp) {
Expression expression = processEngineConfiguration.getExpressionManager().createExpression(exp);
ManagementService managementService = processEngine.getManagementService();
CommandGetValue test = new CommandGetValue(processInstanceId, expression);
Object o = managementService.executeCommand(test);
return o;
}
@Override @Override
public Map<String, Object> setVariables(@RequestBody Map<String, Object> maxMap) { public Map<String, Object> setVariables(@RequestBody Map<String, Object> maxMap) {
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论