package com.yanzhu.flowable.config;

import com.yanzhu.flowable.listener.GlobalEventListener;
import lombok.RequiredArgsConstructor;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEventDispatcher;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;

/**
 * Flowable添加全局监听器
 *
 * @author JiangYuQi
 */
@Configuration
@RequiredArgsConstructor
public class FlowableGlobalListenerConfig implements ApplicationListener<ContextRefreshedEvent> {

    private final SpringProcessEngineConfiguration configuration;

    private final GlobalEventListener globalEventListener;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        FlowableEventDispatcher dispatcher = configuration.getEventDispatcher();
        /**
         * 任务创建全局监听-待办消息发送
         * PROCESS_CREATED 流程创建
         * TASK_CREATED 任务创建
         * TASK_COMPLETED 任务完成
         * PROCESS_COMPLETED 流程完成
         * 流程创建、任务创建、任务完成、流程完成
         */
        dispatcher.addEventListener(globalEventListener,FlowableEngineEventType.TASK_CREATED);
        dispatcher.addEventListener(globalEventListener,FlowableEngineEventType.PROCESS_COMPLETED);
    }

}

