
package org.jeecg.modules.workFlow.flowlong.engine.handler.impl;

import org.jeecg.modules.workFlow.flowlong.engine.core.Execution;
import org.jeecg.modules.workFlow.flowlong.engine.core.FlowLongContext;
import org.jeecg.modules.workFlow.flowlong.engine.entity.FlwTask;
import org.jeecg.modules.workFlow.flowlong.engine.handler.CreateTaskHandler;
import org.jeecg.modules.workFlow.flowlong.engine.model.NodeModel;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 默认流程任务创建处理器
 *

 */
@Slf4j
public class SimpleCreateTaskHandler implements CreateTaskHandler {
    private static SimpleCreateTaskHandler createTaskHandler;

    public static SimpleCreateTaskHandler getInstance() {
        if (null == createTaskHandler) {
            synchronized (SimpleCreateTaskHandler.class) {
                createTaskHandler = new SimpleCreateTaskHandler();
            }
        }
        return createTaskHandler;
    }

    /**
     * 根据任务模型、执行对象，创建下一个任务，并添加到execution对象的tasks集合中
     */
    public boolean handle(FlowLongContext flowLongContext, Execution execution, NodeModel nodeModel) {
        try {
            List<FlwTask> flwTasks = execution.getEngine().taskService().createTask(nodeModel, execution);
            if (null != flwTasks) {
                execution.addTasks(flwTasks);
            }
            return true;
        } catch (Exception e) {
            log.error("SimpleCreateTaskHandler createTask failed. {}", e.getMessage());
            throw e;
        }
    }
}
