All Downloads are FREE. Search and download functionalities are using the official Maven repository.

artoria.jdbc.mybatisplus.handler.AbstractMetaObjectHandler Maven / Gradle / Ivy

The newest version!
package artoria.jdbc.mybatisplus.handler;

import artoria.exception.ExceptionUtils;
import artoria.util.Assert;
import artoria.util.ObjectUtils;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;

import static artoria.convert.ConversionUtils.convert;

/**
 * The abstract meta object field fill handler.
 * @author Kahle
 */
public abstract class AbstractMetaObjectHandler implements MetaObjectHandler {
    private static final Logger log = LoggerFactory.getLogger(AbstractMetaObjectHandler.class);
    private final Map> commands = new ConcurrentHashMap>();
    private final Map configs = new ConcurrentHashMap();

    protected Map> getCommands() { return commands; }

    protected Map getConfigs() { return configs; }

    /**
     * Register a command.
     * @param command The name of the command that is registered
     * @param callable The command logic that is registered
     */
    protected void registerCommand(String command, Callable callable) {
        Assert.notBlank(command, "Parameter \"command\" must not blank. ");
        Assert.notNull(callable, "Parameter \"callable\" must not null. ");
        this.commands.put(command, callable);
    }

    /**
     * Register a configuration.
     * @param fieldName The name of the field to be filled
     * @param command The command name corresponding to the field name
     */
    protected void registerConfig(String fieldName, String command) {
        Assert.notBlank(fieldName, "Parameter \"fieldName\" must not blank. ");
        Assert.notBlank(command, "Parameter \"command\" must not blank. ");
        this.configs.put(fieldName, command);
    }

    /**
     * Invoke a command.
     * @param command The name of the command to be invoked
     * @return The command invoke result
     */
    protected Object invokeCommand(String command) {
        Assert.notBlank(command, "Parameter \"command\" must not blank. ");
        Callable callable = commands.get(command);
        Assert.notNull(callable, "The command \"" + command + "\" is not supported. ");
        try {
            return callable.call();
        }
        catch (Exception e) {
            throw ExceptionUtils.wrap(e);
        }
    }

    /**
     * Initialize the entire object.
     * It will be called at the constructor.
     */
    protected void init() {
        // creatorId、creatorName、modifierId、modifierName、
        registerCommand("nowDate", new Callable() {
            @Override
            public Object call() { return new Date(); }
        });
        registerCommand("int1", new Callable() {
            @Override
            public Object call() { return 1; }
        });
        registerCommand("int0", new Callable() {
            @Override
            public Object call() { return 0; }
        });
    }

    public AbstractMetaObjectHandler() { init(); }

    @Override
    public void insertFill(MetaObject metaObject) {
        for (Map.Entry entry : getConfigs().entrySet()) {
            // Invoke command.
            Object fieldVal = invokeCommand(entry.getValue());
            if (ObjectUtils.isNull(fieldVal)) { continue; }
            // Whether the setter exists?
            if (!metaObject.hasSetter(entry.getKey())) { continue; }
            // Convert type and fill.
            @SuppressWarnings("rawtypes")
            Class clazz = metaObject.getSetterType(entry.getKey());
            strictInsertFill(metaObject, entry.getKey(), clazz, convert(fieldVal, clazz));
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        for (Map.Entry entry : getConfigs().entrySet()) {
            // Invoke command.
            Object fieldVal = invokeCommand(entry.getValue());
            if (ObjectUtils.isNull(fieldVal)) { continue; }
            // Whether the setter exists?
            if (!metaObject.hasSetter(entry.getKey())) { continue; }
            // Convert type and fill.
            @SuppressWarnings("rawtypes")
            Class clazz = metaObject.getSetterType(entry.getKey());
            strictUpdateFill(metaObject, entry.getKey(), clazz, convert(fieldVal, clazz));
        }
    }

}