<commit_before>/**
 * Copyright (c) 2015-2019, <NAME> 杨福海 (<EMAIL>).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jboot.db.model;

import com.jfinal.core.JFinal;
import com.jfinal.plugin.activerecord.Config;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Table;
import io.jboot.db.dialect.IJbootModelDialect;
import io.jboot.exception.JbootException;
import io.jboot.utils.StrUtil;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Set;


@SuppressWarnings("serial")
public class JbootModel<M extends JbootModel<M>> extends Model<M> {

    public static final String AUTO_COPY_MODEL = "_auto_copy_model_";

    private static JbootModelConfig config = JbootModelConfig.getConfig();
    private static String column_created = config.getColumnCreated();
    private static String column_modified = config.getColumnModified();
    private static boolean idCacheEnable = config.isIdCacheEnable();


    /**
     * copy new model with all attrs
     *
     * @return
     */
    public M copy() {
        M m = null;
        try {
            m = (M) _getUsefulClass().newInstance();
            m.put(_getAttrs());
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return m;
    }

    /**
     * copy new model with db attrs and fill modifyFlag
     *
     * @return
     */
    public M copyModel() {
        M m = null;
        try {
            m = (M) _getUsefulClass().newInstance();
            Table table = _getTable(true);
            Set<String> attrKeys = table.getColumnTypeMap().keySet();
            for (String attrKey : attrKeys) {
                Object o = this.get(attrKey);
                if (o != null) {
                    m.set(attrKey, o);
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return m;
    }


    /**
     * 修复 jfinal use 可能造成的线程安全问题
     *
     * @param configName
     * @return
     */
    @Override
    public M use(String configName) {
        M m = this.get("__ds__" + configName);
        if (m == null) {
            synchronized (configName) {
                m = this.get("__ds__" + configName);
                if (m != null) return m;
                m = this.copy().superUse(configName);
                this.put("__ds__" + configName, m);
            }
        }
        return m;
    }


    M superUse(String configName) {
        return super.use(configName);
    }


    public boolean saveOrUpdate() {
        if (null == getIdValue()) {
            return this.save();
        }
        return this.update();
    }


    @Override
    public boolean save() {
        if (_hasColumn(column_created) && get(column_created) == null) {
            set(column_created, new Date());
        }

        boolean needInitPrimaryKey = (String.class == _getPrimaryType() && null == get(_getPrimaryKey()));

        if (needInitPrimaryKey) {
            set(_getPrimaryKey(), generatePrimaryValue());
        }

        boolean saveSuccess = false;

        Boolean autoCopyModel = get(AUTO_COPY_MODEL);
        if (autoCopyModel != null && autoCopyModel == true) {
            M copyModel = copyModel();
            saveSuccess = copyModel.superSave();

            if (saveSuccess && !needInitPrimaryKey) {
                this.set(_getPrimaryKey(), copyModel.get(_getPrimaryKey()));
            }
        } else {
            saveSuccess = this.superSave();
        }

        return saveSuccess;
    }


    protected boolean superSave() {
        return super.save();
    }


    protected String generatePrimaryValue() {
        return StrUtil.uuid();
    }

    @Override
    public M findById(Object idValue) {
        if (idValue == null) {
            throw new IllegalArgumentException("idValue can not be null");
        }
        return idCacheEnable ? loadByCache(idValue) : super.findById(idValue);
    }

    @Override
    public M findByIds(Object... idValues) {
        if (idValues == null || idValues.length != _getPrimaryKeys().length) {
            throw new IllegalArgumentException("primary key nubmer must equals id value number and can not be null");
        }
        return idCacheEnable ? loadByCache(idValues) : super.findByIds(idValues);
    }

    protected M loadByCache(Object... idValues) {
        return config.getCache().get(_getTableName()
                , buildCacheKey(idValues)
                , () -> JbootModel.super.findById(idValues)
                , config.getIdCacheTime());
    }

<commit_msg>v2.0-rc.4<commit_after>/**
 * Copyright (c) 2015-2019, <NAME> 杨福海 (<EMAIL>).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jboot.db.model;

import com.jfinal.core.JFinal;
import com.jfinal.plugin.activerecord.Config;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Table;
import io.jboot.db.dialect.IJbootModelDialect;
import io.jboot.exception.JbootException;
import io.jboot.utils.StrUtil;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Set;


@SuppressWarnings("serial")
public class JbootModel<M extends JbootModel<M>> extends Model<M> {

    public static final String AUTO_COPY_MODEL = "_auto_copy_model_";

    private static JbootModelConfig config = JbootModelConfig.getConfig();
    private static String column_created = config.getColumnCreated();
    private static String column_modified = config.getColumnModified();
    private static boolean idCacheEnable = config.isIdCacheEnable();


    /**
     * copy new model with all attrs
     *
     * @return
     */
    public M copy() {
        M m = null;
        try {
            m = (M) _getUsefulClass().newInstance();
            m.put(_getAttrs());
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return m;
    }

    /**
     * copy new model with db attrs and fill modifyFlag
     *
     * @return
     */
    public M copyModel() {
        M m = null;
        try {
            m = (M) _getUsefulClass().newInstance();
            Table table = _getTable(true);
            Set<String> attrKeys = table.getColumnTypeMap().keySet();
            for (String attrKey : attrKeys) {
                Object o = this.get(attrKey);
                if (o != null) {
                    m.set(attrKey, o);
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return m;
    }


    /**
     * 修复 jfinal use 可能造成的线程安全问题
     *
     * @param configName
     * @return
     */
    @Override
    public M use(String configName) {
        M m = this.get("__ds__" + configName);
        if (m == null) {
            synchronized (configName) {
                m = this.get("__ds__" + configName);
                if (m != null) return m;
                m = this.copy().superUse(configName);
                this.put("__ds__" + configName, m);
            }
        }
        return m;
    }


    M superUse(String configName) {
        return super.use(configName);
    }


    public boolean saveOrUpdate() {
        if (null == getIdValue()) {
            return this.save();
        }
        return this.update();
    }


    @Override
    public boolean save() {
        if (_hasColumn(column_created) && get(column_created) == null) {
            set(column_created, new Date());
        }

        boolean needInitPrimaryKey = (String.class == _getPrimaryType() && null == get(_getPrimaryKey()));

        if (needInitPrimaryKey) {
            set(_getPrimaryKey(), generatePrimaryValue());
        }

        boolean saveSuccess = false;

        Boolean autoCopyModel = get(AUTO_COPY_MODEL);
        if (autoCopyModel != null && autoCopyModel == true) {
            M copyModel = copyModel();
            saveSuccess = copyModel.superSave();

            if (saveSuccess && !needInitPrimaryKey) {
                this.set(_getPrimaryKey(), copyModel.get(_getPrimaryKey()));
            }
        } else {
            saveSuccess = this.superSave();
        }

        return saveSuccess;
    }


    protected boolean superSave() {
        return super.save();
    }


    protected String generatePrimaryValue() {
        return StrUtil.uuid();
    }

    @Override
    public M findById(Object idValue) {
        if (idValue == null) {
            throw new IllegalArgumentException("idValue can not be null");
        }
        return idCacheEnable ? loadByCache(idValue) : super.findByIds(idValue);
    }

    @Override
    public M findByIds(Object... idValues) {
        if (idValues == null || idValues.length != _getPrimaryKeys().length) {
            throw new IllegalArgumentException("primary key nubmer must equals id value number and can not be null");
        }
        return idCacheEnable ? loadByCache(idValues) : super.findByIds(idValues);
    }

    protected M loadByCache(Object... idValues) {
        return config.getCache().get(_getTableName()
                , buildCacheKey(idValues)
                , () -> JbootModel.super.findByIds(idValues)
                , config.getIdCacheTime());
    }


{'fraction_non_alphanumeric': 0.1101711396343835, 'fraction_numerical': 0.0026254375729288216, 'mean_word_length': 2.1685150955021566, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2972372', 'n_tokens_mistral': 2980, 'n_tokens_neox': 2888, 'n_words': 762}