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;
017
018import org.dromara.warm.flow.core.config.WarmFlow;
019import org.dromara.warm.flow.core.entity.*;
020import org.dromara.warm.flow.core.handler.DataFillHandler;
021import org.dromara.warm.flow.core.handler.TenantHandler;
022import org.dromara.warm.flow.core.invoker.FrameInvoker;
023import org.dromara.warm.flow.core.json.JsonConvert;
024import org.dromara.warm.flow.core.service.*;
025import org.dromara.warm.flow.core.utils.ClassUtil;
026import org.dromara.warm.flow.core.utils.ObjectUtil;
027import org.dromara.warm.flow.core.utils.StringUtils;
028
029import java.util.function.Supplier;
030
031/**
032 * 流程定义工程
033 *
034 * @author warm
035 */
036public class FlowFactory {
037
038    private static DefService defService = null;
039    private static HisTaskService hisTaskService = null;
040    private static InsService insService = null;
041
042    private static NodeService nodeService = null;
043    private static SkipService skipService = null;
044    private static TaskService taskService = null;
045    private static UserService UserService = null;
046
047
048    private static Supplier<Definition> defSupplier;
049    private static Supplier<HisTask> hisTaskSupplier;
050    private static Supplier<Instance> insSupplier;
051    private static Supplier<Node> nodeSupplier;
052    private static Supplier<Skip> skipSupplier;
053    private static Supplier<Task> taskSupplier;
054    private static Supplier<User> userSupplier;
055
056    private static WarmFlow flowConfig;
057
058    private static DataFillHandler dataFillHandler;
059
060    private static boolean tenantHandlerFlag;
061
062    private static TenantHandler tenantHandler;
063
064    public static JsonConvert jsonConvert;
065
066    public static DefService defService() {
067        if (ObjectUtil.isNotNull(defService)) {
068            return defService;
069        }
070        return defService = FrameInvoker.getBean(DefService.class);
071    }
072
073    public static HisTaskService hisTaskService() {
074        if (ObjectUtil.isNotNull(hisTaskService)) {
075            return hisTaskService;
076        }
077        return hisTaskService = FrameInvoker.getBean(HisTaskService.class);
078    }
079
080    public static InsService insService() {
081        if (ObjectUtil.isNotNull(insService)) {
082            return insService;
083        }
084        return insService = FrameInvoker.getBean(InsService.class);
085    }
086
087    public static NodeService nodeService() {
088        if (ObjectUtil.isNotNull(nodeService)) {
089            return nodeService;
090        }
091        return nodeService = FrameInvoker.getBean(NodeService.class);
092    }
093
094    public static SkipService skipService() {
095        if (ObjectUtil.isNotNull(skipService)) {
096            return skipService;
097        }
098        return skipService = FrameInvoker.getBean(SkipService.class);
099    }
100
101    public static TaskService taskService() {
102        if (ObjectUtil.isNotNull(taskService)) {
103            return taskService;
104        }
105        return taskService = FrameInvoker.getBean(TaskService.class);
106    }
107
108    public static UserService userService() {
109        if (ObjectUtil.isNotNull(UserService)) {
110            return UserService;
111        }
112        return UserService = FrameInvoker.getBean(UserService.class);
113    }
114
115    public static void setNewDef(Supplier<Definition> supplier) {
116        FlowFactory.defSupplier = supplier;
117    }
118
119    public static Definition newDef() {
120        return defSupplier.get();
121    }
122
123    public static void setNewHisTask(Supplier<HisTask> supplier) {
124        FlowFactory.hisTaskSupplier = supplier;
125    }
126
127    public static HisTask newHisTask() {
128        return hisTaskSupplier.get();
129    }
130
131    public static void setNewIns(Supplier<Instance> supplier) {
132        FlowFactory.insSupplier = supplier;
133    }
134
135    public static Instance newIns() {
136        return insSupplier.get();
137    }
138
139    public static void setNewNode(Supplier<Node> supplier) {
140        FlowFactory.nodeSupplier = supplier;
141    }
142
143    public static Node newNode() {
144        return nodeSupplier.get();
145    }
146
147    public static void setNewSkip(Supplier<Skip> supplier) {
148        FlowFactory.skipSupplier = supplier;
149    }
150
151    public static Skip newSkip() {
152        return skipSupplier.get();
153    }
154
155    public static void setNewTask(Supplier<Task> supplier) {
156        FlowFactory.taskSupplier = supplier;
157    }
158
159    public static Task newTask() {
160        return taskSupplier.get();
161    }
162
163    public static void setNewUser(Supplier<User> supplier) {
164        FlowFactory.userSupplier = supplier;
165    }
166
167    public static User newUser() {
168        return userSupplier.get();
169    }
170
171    public static WarmFlow getFlowConfig() {
172        return FlowFactory.flowConfig;
173    }
174
175    public static void setFlowConfig(WarmFlow flowConfig) {
176        FlowFactory.flowConfig = flowConfig;
177    }
178
179    public static boolean isLogicDelete() {
180        return FlowFactory.flowConfig.isLogicDelete();
181    }
182
183    /**
184     * 获取填充类
185     */
186    public static DataFillHandler dataFillHandler() {
187        if (ObjectUtil.isNotNull(FlowFactory.dataFillHandler)) {
188            return FlowFactory.dataFillHandler;
189        }
190        DataFillHandler o = null;
191        try {
192            String dataFillHandlerPath = flowConfig.getDataFillHandlerPath();
193            if (!StringUtils.isEmpty(dataFillHandlerPath)) {
194                Class<?> clazz = ClassUtil.getClazz(dataFillHandlerPath);
195                if (clazz != null) {
196                    return FlowFactory.dataFillHandler = o = (DataFillHandler) clazz.newInstance();
197                }
198            }
199            o = FrameInvoker.getBean(DataFillHandler.class);
200        } catch (Exception ignored) {
201        }
202        if (ObjectUtil.isNull(o)) {
203            return FlowFactory.dataFillHandler = new DataFillHandler() {};
204        }
205        return FlowFactory.dataFillHandler = o;
206    }
207
208    /**
209     * 获取租户数据
210     */
211    public static TenantHandler tenantHandler() {
212        if (ObjectUtil.isNotNull(FlowFactory.tenantHandler) || tenantHandlerFlag) {
213            return FlowFactory.tenantHandler;
214        }
215        TenantHandler o = null;
216        try {
217            String tenantHandlerPath = flowConfig.getTenantHandlerPath();
218            if (!StringUtils.isEmpty(tenantHandlerPath)) {
219                Class<?> clazz = ClassUtil.getClazz(tenantHandlerPath);
220                if (clazz != null) {
221                    return FlowFactory.tenantHandler = o = (TenantHandler) clazz.newInstance();
222                }
223            }
224            o = FrameInvoker.getBean(TenantHandler.class);
225        } catch (Exception ignored) {
226        }
227        tenantHandlerFlag = true;
228        return FlowFactory.tenantHandler = o;
229    }
230
231    /**
232     * 获取租户数据
233     */
234    public static void jsonConvert(JsonConvert jsonConvert) {
235        FlowFactory.jsonConvert = jsonConvert;
236    }
237
238    /**
239     * 获取数据库类型
240     */
241    public static String dataSourceType() {
242        return flowConfig.getDataSourceType();
243    }
244}