001/*
002 *    Copyright 2024-2025, Warm-Flow (290631660@qq.com).
003 *
004 *    Licensed under the Apache License, Version 2.0 (the "License");
005 *    you may not use this file except in compliance with the License.
006 *    You may obtain a copy of the License at
007 *
008 *       https://www.apache.org/licenses/LICENSE-2.0
009 *
010 *    Unless required by applicable law or agreed to in writing, software
011 *    distributed under the License is distributed on an "AS IS" BASIS,
012 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *    See the License for the specific language governing permissions and
014 *    limitations under the License.
015 */
016package org.dromara.warm.flow.core.config;
017
018import org.dromara.warm.flow.core.FlowFactory;
019import org.dromara.warm.flow.core.constant.FlowConfigCons;
020import org.dromara.warm.flow.core.expression.ExpressionStrategy;
021import org.dromara.warm.flow.core.invoker.FrameInvoker;
022import org.dromara.warm.flow.core.json.JsonConvert;
023import org.dromara.warm.flow.core.utils.*;
024import org.dromara.warm.flow.core.variable.VariableStrategy;
025
026import java.io.Serializable;
027
028/**
029 * WarmFlow属性配置文件
030 *
031 * @author warm
032 */
033public class WarmFlow implements Serializable {
034
035    /**
036     * 开关
037     */
038    private boolean enabled = true;
039
040    /**
041     * 启动banner
042     */
043    private boolean banner = true;
044
045    /**
046     * id生成器类型, 不填默认为orm扩展自带生成器或者warm-flow内置的19位雪花算法, SnowId14:14位,SnowId15:15位, SnowFlake19:19位
047     */
048    private String keyType;
049
050    /**
051     * 是否开启逻辑删除
052     */
053    private boolean logicDelete = false;
054
055    /**
056     * 逻辑删除字段值
057     */
058    private String logicDeleteValue = "2";
059
060    /**
061     * 逻辑未删除字段
062     */
063    private String logicNotDeleteValue = "0";
064
065    /**
066     * 数据填充处理类路径
067     */
068    private String dataFillHandlerPath;
069
070    /**
071     * 租户模式处理类路径
072     */
073    private String tenantHandlerPath;
074    /**
075     * 数据源类型, mybatis模块对orm进一步的封装, 由于各数据库分页语句存在差异,
076     * 当配置此参数时, 以此参数结果为基准, 未配置时, 取DataSource中数据源类型,
077     * 兜底为mysql数据库
078     */
079    private String dataSourceType;
080
081    /**
082     * ui开关
083     */
084    private boolean ui = true;
085
086    public static WarmFlow init() {
087        WarmFlow flowConfig = new WarmFlow();
088        // 设置banner
089        String banner = FrameInvoker.getCfg(FlowConfigCons.BANNER);
090        if (StringUtils.isNotEmpty(banner)) {
091            flowConfig.setBanner(ObjectUtil.isStrTrue(banner));
092        }
093
094        // 设置ui开关
095        String ui = FrameInvoker.getCfg(FlowConfigCons.ui);
096        if (StringUtils.isNotEmpty(ui)) {
097            flowConfig.setUi(ObjectUtil.isStrTrue(ui));
098        }
099
100        // 设置id生成器类型
101        String keyType = FrameInvoker.getCfg(FlowConfigCons.KEYTYPE);
102        if (StringUtils.isNotEmpty(keyType)) {
103            flowConfig.setKeyType(keyType);
104        }
105
106        // 设置逻辑删除
107        setLogicDelete(flowConfig);
108
109        // 设置租户模式
110        flowConfig.setTenantHandlerPath(FrameInvoker.getCfg(FlowConfigCons.TENANTHANDLERPATH));
111
112        // 设置数据填充处理类
113        flowConfig.setDataFillHandlerPath(FrameInvoker.getCfg(FlowConfigCons.DATAFILLHANDLEPATH));
114
115        // 设置数据源类型
116        flowConfig.setDataSourceType(FrameInvoker.getCfg(FlowConfigCons.DATA_SOURCE_TYPE));
117        printBanner(flowConfig);
118
119        // 通过SPI机制
120        spiLoad();
121        return flowConfig;
122    }
123    public static void spiLoad() {
124        // 通过SPI机制加载条件表达式策略实现类
125        ServiceLoaderUtil.loadList(ExpressionStrategy.class).forEach(ExpressionUtil::setExpression);
126
127        // 通过SPI机制加载办理人变量表达式策略实现类
128        ServiceLoaderUtil.loadList(VariableStrategy.class).forEach(VariableUtil::setExpression);
129
130        // 通过SPI机制加载json转换策略实现类
131        FlowFactory.jsonConvert(ServiceLoaderUtil.loadFirst(JsonConvert.class));
132    }
133
134    private static void setLogicDelete(WarmFlow flowConfig) {
135        String logicDelete = FrameInvoker.getCfg(FlowConfigCons.LOGICDELETE);
136        if (ObjectUtil.isStrTrue(logicDelete)) {
137            flowConfig.setLogicDelete(ObjectUtil.isStrTrue(logicDelete));
138            String logicDeleteValue = FrameInvoker.getCfg(FlowConfigCons.LOGICDELETEVALUE);
139            if (StringUtils.isNotEmpty(logicDeleteValue)) {
140                flowConfig.setLogicDeleteValue(logicDeleteValue);
141            }
142            String logicNotDeleteValue = FrameInvoker.getCfg(FlowConfigCons.LOGICNOTDELETEVALUE);
143            if (StringUtils.isNotEmpty(logicNotDeleteValue)) {
144                flowConfig.setLogicNotDeleteValue(logicNotDeleteValue);
145            }
146        }
147    }
148
149    private static void printBanner(WarmFlow flowConfig) {
150        if (flowConfig.isBanner()) {
151            System.out.println("\n" +
152                    "   ▄     ▄                      ▄▄▄▄▄▄   ▄                     \n" +
153                    "   █  █  █  ▄▄▄    ▄ ▄▄  ▄▄▄▄▄  █        █     ▄▄▄  ▄     ▄    \n" +
154                    "   ▀ █▀█ █ ▀   █   █▀  ▀ █ █ █  █▄▄▄▄▄   █    █▀ ▀█ ▀▄ ▄ ▄▀    \n" +
155                    "    ██ ██▀ ▄▀▀▀█   █     █ █ █  █        █    █   █  █▄█▄█     \n" +
156                    "    █   █  ▀▄▄▀█   █     █ █ █  █        ▀▄▄  ▀█▄█▀   █ █      \n" +
157                    "\n" +
158                    "\033[32m   :: Warm-Flow ::     (v" + WarmFlow.class.getPackage()
159                    .getImplementationVersion() + ")\033[0m\n");
160        }
161    }
162
163    public boolean isEnabled() {
164        return enabled;
165    }
166
167    public void setEnabled(boolean enabled) {
168        this.enabled = enabled;
169    }
170
171    public boolean isBanner() {
172        return banner;
173    }
174
175    public void setBanner(boolean banner) {
176        this.banner = banner;
177    }
178
179    public String getKeyType() {
180        return keyType;
181    }
182
183    public void setKeyType(String keyType) {
184        this.keyType = keyType;
185    }
186
187    public boolean isLogicDelete() {
188        return logicDelete;
189    }
190
191    public void setLogicDelete(boolean logicDelete) {
192        this.logicDelete = logicDelete;
193    }
194
195    public String getLogicDeleteValue() {
196        return logicDeleteValue;
197    }
198
199    public void setLogicDeleteValue(String logicDeleteValue) {
200        this.logicDeleteValue = logicDeleteValue;
201    }
202
203    public String getLogicNotDeleteValue() {
204        return logicNotDeleteValue;
205    }
206
207    public void setLogicNotDeleteValue(String logicNotDeleteValue) {
208        this.logicNotDeleteValue = logicNotDeleteValue;
209    }
210
211    public String getDataFillHandlerPath() {
212        return dataFillHandlerPath;
213    }
214
215    public void setDataFillHandlerPath(String dataFillHandlerPath) {
216        this.dataFillHandlerPath = dataFillHandlerPath;
217    }
218
219    public String getTenantHandlerPath() {
220        return tenantHandlerPath;
221    }
222
223    public void setTenantHandlerPath(String tenantHandlerPath) {
224        this.tenantHandlerPath = tenantHandlerPath;
225    }
226
227    public String getDataSourceType() {
228        return dataSourceType;
229    }
230
231    public void setDataSourceType(String dataSourceType) {
232        this.dataSourceType = dataSourceType;
233    }
234
235    public boolean isUi() {
236        return ui;
237    }
238
239    public void setUi(boolean ui) {
240        this.ui = ui;
241    }
242}