/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 大恒泰山系统
 * 创建日期:2021/12/16
 */
package org.ts.ddcs.dataStream;


import lombok.Data;

import org.ts.ddcs.datatemplate.DataTemplate;


/***
 * Date:2021/12/16
 * Title:数据匹配器模块
 * Description：每个适配器处理器
 * @author dylan
 * @mender：（文件的修改者，文件创建者之外的人）
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@Data
public abstract class DataStreamProcessor {
    private DataStreamProcessor preProcessor;
    private DataStreamProcessor nextProcessor;
    private DataStreamLineManager context;
    private DataTemplate dataTemplate;
    private boolean isForward = false;

    public void init(DataStreamLineManager context, DataTemplate dataTemplate) {
        this.context = context;
        this.dataTemplate = dataTemplate;
    }

    public void reset() {
        this.isForward = false;
    }

    public void forward() {
        this.isForward = true;
    }

    public void setPreProcessor(DataStreamProcessor processor) {
        this.preProcessor = processor;
    }

    public void setNextProcessor(DataStreamProcessor processor) {
        this.nextProcessor = processor;
    }

    public DataStreamProcessor getPreProcessor() {
        return this.preProcessor;
    }

    public DataStreamProcessor getNextProcessor() {
        return this.nextProcessor;
    }

    public abstract void process(DataStreamCache dataStreamCache);

    public abstract void batchComplete(DataStreamCache dataStreamCache);

    public void stream(DataStreamCache dataStreamCache) {
        try {
            this.process(dataStreamCache);
            if (this.isForward) {
                if (null != nextProcessor) {
                    nextProcessor.stream(dataStreamCache);
                }
            } else {
                this.backStream(dataStreamCache);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    private void backStream(DataStreamCache dataStreamCache) {
        this.batchComplete(dataStreamCache);
        if (null != preProcessor) {
            preProcessor.backStream(dataStreamCache);
        }
    }

}
