001/*
002 *  Copyright (c) 2022-2025, Mybatis-Flex (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 com.mybatisflex.core.keygen;
017
018import com.mybatisflex.annotation.KeyType;
019import com.mybatisflex.core.FlexConsts;
020import com.mybatisflex.core.FlexGlobalConfig;
021import com.mybatisflex.core.exception.FlexExceptions;
022import com.mybatisflex.core.table.IdInfo;
023import com.mybatisflex.core.table.TableInfo;
024import com.mybatisflex.core.util.ConvertUtil;
025import com.mybatisflex.core.util.StringUtil;
026import org.apache.ibatis.executor.Executor;
027import org.apache.ibatis.executor.ExecutorException;
028import org.apache.ibatis.executor.keygen.KeyGenerator;
029import org.apache.ibatis.mapping.MappedStatement;
030import org.apache.ibatis.reflection.MetaObject;
031import org.apache.ibatis.reflection.invoker.Invoker;
032import org.apache.ibatis.session.Configuration;
033
034import java.sql.Statement;
035import java.util.Map;
036
037/**
038 * 通过 java 编码的方式生成主键
039 * 当主键类型配置为 KeyType#Generator 时,使用此生成器生成
040 * {@link KeyType#Generator}
041 */
042public class CustomKeyGenerator implements KeyGenerator {
043
044    protected Configuration configuration;
045    protected IKeyGenerator keyGenerator;
046    protected TableInfo tableInfo;
047    protected IdInfo idInfo;
048
049
050    public CustomKeyGenerator(Configuration configuration, TableInfo tableInfo, IdInfo idInfo) {
051        this.configuration = configuration;
052        FlexGlobalConfig.KeyConfig globalKeyConfig = FlexGlobalConfig.getConfig(configuration).getKeyConfig();
053        String keyValue = MybatisKeyGeneratorUtil.getKeyValue(idInfo, globalKeyConfig);
054        this.keyGenerator = KeyGeneratorFactory.getKeyGenerator(keyValue);
055        this.tableInfo = tableInfo;
056        this.idInfo = idInfo;
057
058        ensuresKeyGeneratorNotNull();
059    }
060
061    private void ensuresKeyGeneratorNotNull() {
062        if (keyGenerator == null) {
063            throw FlexExceptions.wrap("The name of \"%s\" key generator not exist.\n" +
064                    "please check annotation @Id(value=\"%s\") at field: %s#%s"
065                , idInfo.getValue(), idInfo.getValue(), tableInfo.getEntityClass().getName(), idInfo.getProperty());
066        }
067    }
068
069
070    @Override
071    public void processBefore(Executor executor, MappedStatement ms, Statement stmt, Object parameter) {
072        Object entity = ((Map) parameter).get(FlexConsts.ENTITY);
073        try {
074            Object existId = tableInfo.getValue(entity, idInfo.getProperty());
075
076            // 若用户主动设置了主键,则使用用户自己设置的主键,不再生成主键
077            // 只有主键为 null 或者 空字符串时,对主键进行设置
078            if (existId == null || (existId instanceof String && StringUtil.isBlank((String) existId))) {
079                Configuration configuration = ms.getConfiguration();
080                MetaObject metaParam = configuration.newMetaObject(parameter);
081                Object generateId = keyGenerator.generate(entity, idInfo.getColumn());
082                MetaObject metaObjectForProperty = metaParam.metaObjectForProperty(FlexConsts.ENTITY);
083                // Invoker setInvoker = tableInfo.getReflector().getSetInvoker(idInfo.getProperty());
084                // Object id = ConvertUtil.convert(generateId, setInvoker.getType());
085                Class<?> setterType = tableInfo.getReflector().getSetterType(idInfo.getProperty());
086                Object id = ConvertUtil.convert(generateId, setterType);
087                this.setValue(metaObjectForProperty, this.idInfo.getProperty(), id);
088            }
089        } catch (Exception e) {
090            throw FlexExceptions.wrap(e);
091        }
092    }
093
094
095    @Override
096    public void processAfter(Executor executor, MappedStatement ms, Statement stmt, Object parameter) {
097        //do nothing
098    }
099
100    private void setValue(MetaObject metaParam, String property, Object value) {
101        if (!metaParam.hasSetter(property)) {
102            throw new ExecutorException("No setter found for the keyProperty '" + property + "' in " + metaParam.getOriginalObject().getClass().getName() + ".");
103        } else {
104            metaParam.setValue(property, value);
105        }
106    }
107
108}