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

import org.jeecg.modules.workFlow.flowlong.engine.ProcessModelParser;
import org.jeecg.modules.workFlow.flowlong.engine.assist.Assert;
import org.jeecg.modules.workFlow.flowlong.engine.cache.FlowCache;
import org.jeecg.modules.workFlow.flowlong.engine.cache.FlowSimpleCache;
import org.jeecg.modules.workFlow.flowlong.engine.core.FlowLongContext;
import org.jeecg.modules.workFlow.flowlong.engine.model.ProcessModel;
import lombok.Getter;
import lombok.Setter;

/**
 * FlowLong 默认流程模型解析器
 *

 */
@Getter
@Setter
public class DefaultProcessModelParser implements ProcessModelParser {

    /**
     * 流程缓存处理类，默认 ConcurrentHashMap 实现
     * 使用其它缓存框架可在初始化时赋值该静态属性
     */
    private FlowCache flowCache;

    public DefaultProcessModelParser(FlowCache flowCache) {
        if (null == flowCache) {
            this.flowCache = new FlowSimpleCache();
        } else {
            this.flowCache = flowCache;
        }
    }

    @Override
    public ProcessModel parse(String content, String cacheKey, boolean redeploy) {
        // 缓存解析逻辑
        if (null != cacheKey) {
            FlowCache flowCache = this.getFlowCache();
            ProcessModel processModel = flowCache.get(cacheKey);
            if (null != content && (null == processModel || redeploy)) {
                processModel = parseProcessModel(content);
                flowCache.put(cacheKey, processModel);
            }
            return processModel;
        }

        // 未缓存解析逻辑
        return parseProcessModel(content);
    }

    private ProcessModel parseProcessModel(String content) {
        ProcessModel processModel = FlowLongContext.fromJson(content, ProcessModel.class);
        Assert.isNull(processModel, "process model json parser error");
        processModel.buildParentNode(processModel.getNodeConfig());
        return processModel;
    }

    @Override
    public void invalidate(String cacheKey) {
        this.getFlowCache().remove(cacheKey);
    }

    @Override
    public FlowCache getFlowCache() {
        return flowCache;
    }
}
