package com.codvision.workflow.config;

import com.codvision.workflow.listener.GlobalWorkFlowMultiInstanceListener;
import com.codvision.workflow.listener.GlobalWorkFlowStatusChangeListener;
import com.codvision.workflow.listener.GlobalWorkFlowVariableListener;
import lombok.RequiredArgsConstructor;
import org.flowable.engine.RuntimeService;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;

import static org.flowable.common.engine.api.delegate.event.FlowableEngineEventType.*;


/**
 * 流程全局监听配置
 * @author hxl
 */
@Configuration
@RequiredArgsConstructor
public class WorkFlowGlobalEventListenerConfig implements ApplicationListener<ContextRefreshedEvent> {

    private final GlobalWorkFlowStatusChangeListener globalWorkFlowStatusChangeListener;
    private final GlobalWorkFlowVariableListener globalWorkFlowVariableListener;
    private final GlobalWorkFlowMultiInstanceListener globalWorkFlowMultiInstanceListener;
    private final RuntimeService runtimeService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        runtimeService.addEventListener(globalWorkFlowStatusChangeListener, TASK_CREATED, ACTIVITY_COMPLETED);
        runtimeService.addEventListener(globalWorkFlowVariableListener, VARIABLE_CREATED);
        runtimeService.addEventListener(globalWorkFlowMultiInstanceListener, MULTI_INSTANCE_ACTIVITY_COMPLETED_WITH_CONDITION);
    }
}
