001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
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 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
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 io.jboot.db.model;
017
018import io.jboot.Jboot;
019import io.jboot.app.config.annotation.ConfigModel;
020import io.jboot.components.cache.JbootCache;
021import io.jboot.components.cache.JbootCacheManager;
022import io.jboot.utils.ClassUtil;
023import io.jboot.utils.StrUtil;
024
025/**
026 * @author Michael Yang 杨福海 (fuhai999@gmail.com)
027 * @version V1.0
028 */
029@ConfigModel(prefix = "jboot.model")
030public class JbootModelConfig {
031
032    private String scanPackage;
033    private String unscanPackage;
034
035    private String columnCreated = "created";
036    private String columnModified = "modified";
037
038    /**
039     * id 缓存的时间,默认为 1 个小时,单位:秒
040     */
041    private int idCacheTime = 60 * 60;
042
043    /**
044     * Model 过滤器,可以通过这个配置来防止 xss 等问题
045     * filter 会在 save 和 update 的时候被执行
046     */
047    private String filterClass;
048
049    /**
050     * 主键的值的生成器,可以通过配置这个来自定义主键的生成策略
051     */
052    private String primarykeyValueGeneratorClass;
053
054
055    /**
056     * 是否启用 id 缓存,如果启用,当根据 id 查询的时候,会自动存入缓存
057     * 下次再通过 id 查询的时候,直接从缓存中获取 Model
058     */
059    private boolean idCacheEnable = true;
060
061    /**
062     * 从缓存获取数据的时候,是复制一个返回,这样保证前端在修改的时候不修改到缓存数据
063     */
064    private boolean idCacheByCopyEnable = true;
065
066
067    private String idCacheName = "default";
068
069
070    public JbootModelConfig() {
071    }
072
073    public String getScanPackage() {
074        return scanPackage;
075    }
076
077    public void setScanPackage(String scanPackage) {
078        this.scanPackage = scanPackage;
079    }
080
081    public String getUnscanPackage() {
082        return unscanPackage;
083    }
084
085    public void setUnscanPackage(String unscanPackage) {
086        this.unscanPackage = unscanPackage;
087    }
088
089    public String getColumnCreated() {
090        return columnCreated;
091    }
092
093    public void setColumnCreated(String columnCreated) {
094        this.columnCreated = columnCreated;
095    }
096
097    public String getColumnModified() {
098        return columnModified;
099    }
100
101    public void setColumnModified(String columnModified) {
102        this.columnModified = columnModified;
103    }
104
105    public int getIdCacheTime() {
106        return idCacheTime;
107    }
108
109    public void setIdCacheTime(int idCacheTime) {
110        this.idCacheTime = idCacheTime;
111    }
112
113    public String getFilterClass() {
114        return filterClass;
115    }
116
117    public void setFilterClass(String filterClass) {
118        this.filterClass = filterClass;
119    }
120
121    public String getPrimarykeyValueGeneratorClass() {
122        return primarykeyValueGeneratorClass;
123    }
124
125    public void setPrimarykeyValueGeneratorClass(String primarykeyValueGeneratorClass) {
126        this.primarykeyValueGeneratorClass = primarykeyValueGeneratorClass;
127    }
128
129    public boolean isIdCacheEnable() {
130        return idCacheEnable;
131    }
132
133    public void setIdCacheEnable(boolean idCacheEnable) {
134        this.idCacheEnable = idCacheEnable;
135    }
136
137    public boolean isIdCacheByCopyEnable() {
138        return idCacheByCopyEnable;
139    }
140
141    public void setIdCacheByCopyEnable(boolean idCacheByCopyEnable) {
142        this.idCacheByCopyEnable = idCacheByCopyEnable;
143    }
144
145
146    public JbootModelConfig(String idCacheName) {
147        this.idCacheName = idCacheName;
148    }
149
150    private JbootModelFilter filter;
151
152    public JbootModelFilter getFilter() {
153        if (filter == null) {
154            if (StrUtil.isNotBlank(filterClass)) {
155                filter = ClassUtil.newInstance(filterClass);
156            } else {
157                filter = JbootModelFilter.DEFAULT;
158            }
159        }
160        return filter;
161    }
162
163    public void setFilter(JbootModelFilter filter) {
164        this.filter = filter;
165    }
166
167
168    private PrimarykeyValueGenerator primarykeyValueGenerator;
169
170    public PrimarykeyValueGenerator getPrimarykeyValueGenerator() {
171        if (primarykeyValueGenerator == null) {
172            if (StrUtil.isNotBlank(primarykeyValueGeneratorClass)) {
173                primarykeyValueGenerator = ClassUtil.newInstance(primarykeyValueGeneratorClass);
174            } else {
175                primarykeyValueGenerator = PrimarykeyValueGenerator.DEFAULT;
176            }
177        }
178        return primarykeyValueGenerator;
179    }
180
181
182    public void setPrimarykeyValueGenerator(PrimarykeyValueGenerator primarykeyValueGenerator) {
183        this.primarykeyValueGenerator = primarykeyValueGenerator;
184    }
185
186
187    private static JbootModelConfig config;
188
189    public static JbootModelConfig getConfig() {
190        if (config == null) {
191            config = Jboot.config(JbootModelConfig.class);
192        }
193        return config;
194    }
195
196    private JbootCache idCache;
197
198    public JbootCache getIdCache() {
199        if (idCache == null) {
200            idCache = JbootCacheManager.me().getCache(idCacheName);
201        }
202        return idCache;
203    }
204
205    public void setIdCache(JbootCache idCache) {
206        this.idCache = idCache;
207    }
208
209
210}