节点完成后有两个方法获取到节点表单的控件值,一个是在节点加任务监听器,一个是用脚本,然后加全局任务监听器。
任务监听器:
@Slf4j
public class LYOverListener implements TaskListener {
@Override
public void notify(DelegateTask delegateTask) {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = processEngine.getTaskService();
Map<String, Object> variables = taskService.getVariables(delegateTask.getId());// 可获取节点表单
Task task = taskService.createTaskQuery().taskId(delegateTask.getId()).singleResult();
List<Task> taskList = taskService.createTaskQuery().processInstanceId(delegateTask.getProcessInstanceId()).list();//只能取到当前节点,要修改下个节点的候选人,需在下个节点开始前
String formKey = delegateTask.getFormKey();
String assignee = delegateTask.getAssignee();
String category = delegateTask.getCategory();
String processDefinitionId = delegateTask.getProcessDefinitionId();
String processInstanceId = delegateTask.getProcessInstanceId();
log.info("processDefinitionId={},processInstanceId={},category={},assignee={},formKey={}",
processDefinitionId,processInstanceId,category,assignee,formKey);
}
}
全局任务监听器:
public class GlobalTaskCompletedListener implements FlowableEventListener {
private static final Log log = Log.get();
@Override
public void onEvent(FlowableEvent flowableEvent) {
FlowableEntityEventImpl flowableEntityEvent = (FlowableEntityEventImpl) flowableEvent;
DelegateExecution execution = flowableEntityEvent.getExecution();
String eventType = flowableEntityEvent.getType().name();
//根据事件类型获取脚本并执行
BpmEventUtil.getAndExecuteScript(execution, eventType);
log.info(">>> 任务完成了........");
}
@Override
public boolean isFailOnException() {
return false;
}
@Override
public boolean isFireOnTransactionLifecycleEvent() {
return false;
}
@Override
public String getOnTransaction() {
return null;
}
}
需要配置一下才能起作用:
@Configuration
public class FlowableConfig implements EngineConfigurationConfigurer<SpringProcessEngineConfiguration> {
@Override
public void configure(SpringProcessEngineConfiguration springProcessEngineConfiguration) {
//使用mybatis-plus的主键生成器,注意:不会影响act_de开头的表主键生成,因为这是流程设计器的,不是工作流引擎的
springProcessEngineConfiguration.setIdGenerator(() -> Convert.toStr(new DefaultIdentifierGenerator().nextId(null)));
//创建EventDispatcher
FlowableEventDispatcher eventDispatcher = new FlowableEventDispatcherImpl();
//添加全局流程启动监听器
eventDispatcher.addEventListener(new GlobalProcessStartedListener(), FlowableEngineEventType.PROCESS_STARTED);
//添加全局流程完成监听器
eventDispatcher.addEventListener(new GlobalProcessCompletedListener(), FlowableEngineEventType.PROCESS_COMPLETED);
//添加全局流程取消监听器
eventDispatcher.addEventListener(new GlobalProcessCancelledListener(), FlowableEngineEventType.PROCESS_CANCELLED);
//添加全局活动开始监听器
eventDispatcher.addEventListener(new GlobalActStartedListener(), FlowableEngineEventType.ACTIVITY_STARTED);
//添加全局活动完成监听器
eventDispatcher.addEventListener(new GlobalActCompletedListener(), FlowableEngineEventType.ACTIVITY_COMPLETED);
//添加全局活动取消监听器
eventDispatcher.addEventListener(new GlobalActCancelledListener(), FlowableEngineEventType.ACTIVITY_CANCELLED);
//添加全局任务分配监听器
eventDispatcher.addEventListener(new GlobalTaskAssignedListener(), FlowableEngineEventType.TASK_ASSIGNED);
//添加全局任务创建监听器
eventDispatcher.addEventListener(new GlobalTaskCreatedListener(), FlowableEngineEventType.TASK_CREATED);
//添加全局任务完成监听器
eventDispatcher.addEventListener(new GlobalTaskCompletedListener(), FlowableEngineEventType.TASK_COMPLETED);
//添加全局连接线监听器
eventDispatcher.addEventListener(new GlobalSequenceflowTakenListener(), FlowableEngineEventType.SEQUENCEFLOW_TAKEN);
//设置EventDispatcher
springProcessEngineConfiguration.setEventDispatcher(eventDispatcher);
}
}
这个里面执行脚本
= flowableEntityEvent.getExecution();
String eventType = flowableEntityEvent.getType().name();
//根据事件类型获取脚本并执行
BpmEventUtil.getAndExecuteScript(execution, eventType);
然后写个脚本(groovy):
import cn.hutool.json.JSONObject;
Object formData = execution.getParent().getVariables().get("formData");
if (null != formData) {
JSONObject jsonObject = new JSONObject(formData);
String s = jsonObject.get("#myName").toString();
System.out.println("#myName 控件的值是:"+s);
}
主要为:
execution.getParent().getVariables().get("formData");
在流程节点配置中,给节点设置脚本
然后节点完成后也能获取到控件值,就可以根据控件值做些具体业务操作了,比如学生转专业时根据目标专业找具体负责人来进行下个节点。
在整个流程完成后的监听器中,可以获取到流程表单与流程数据,通过一些映射配置,可以将流程表单数据添加到数据表中,以便业务的统计查询使用。
public class GlobalProcessCompletedListener implements FlowableEventListener {
private static final Log log = Log.get();
@Override
public void onEvent(FlowableEvent flowableEvent) {
FlowableProcessEventImpl flowableProcessEvent = (FlowableProcessEventImpl) flowableEvent;
DelegateExecution execution = flowableProcessEvent.getExecution();
String eventType = flowableProcessEvent.getType().name();
//根据事件类型获取脚本并执行
BpmEventUtil.getAndExecuteScript(execution, eventType);
String processInstanceId = flowableProcessEvent.getProcessInstanceId();
//流程实例bean,@Autowired 注入是个null ,需自行获取
FlowableInstanceService flowableInstanceService = SpringContextHolder.getBean(FlowableInstanceService.class);
//流程表单bean
FlowableFormService flowableFormService = SpringContextHolder.getBean(FlowableFormService.class);
//流程表单保存到数据表的bean
IFormSaveTableService formSaveTableService = SpringContextHolder.getBean(IFormSaveTableService.class);
//取流程实例详情
FlowableInstanceResult detail = flowableInstanceService.detail(processInstanceId);
FlowableFormParam flowableFormParam = new FlowableFormParam();
flowableFormParam.setProcessDefinitionId(detail.getProcDef().getId());
//取全局表单
FlowableFormResource globalForm = null;
try {
globalForm = flowableFormService.getGlobalForm(flowableFormParam);
} catch (Exception e) {
log.info("保存到数据表时出现错误:"+e.getMessage());
}
//取流程表单数据
Map<String, Object> stringObjectMap = flowableInstanceService.formData(new FlowableInstanceParam() {{
setId(processInstanceId);
}});
//保存到数据表
formSaveTableService.saveToTable(processInstanceId,globalForm.getCode(),
globalForm.getFormJson(),stringObjectMap.get("formData").toString(),
detail.getStartUserId(),detail.getStartTime());
}
@Override
public boolean isFailOnException() {
return false;
}
@Override
public boolean isFireOnTransactionLifecycleEvent() {
return false;
}
@Override
public String getOnTransaction() {
return null;
}
}