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

com.lq.cli.other.CreateRedisConfigTask Maven / Gradle / Ivy

package com.lq.cli.other;

import com.lq.cli.comment.CreateTask;
import com.lq.comment.util.FileUtil;
import com.lq.comment.util.StringUtil;
import com.lq.cli.comment.TaskArgs;
import com.lq.comment.jdbc.TableInfo;

import java.io.File;
import java.util.List;

public class CreateRedisConfigTask extends CreateTask {

    private List tableInfos;

    public CreateRedisConfigTask(TaskArgs taskArgs, List tableInfos) {
        super(taskArgs);
        this.tableInfos = tableInfos;
        setPackageName("config");
    }

    @Override
    public Boolean call() throws Exception {
        if (checkDir()) {
            File file = new File(taskArgs.rootPackagePath + getPackageName() + File.separator + "RedisConfig.java");
            if (file.exists()) {
                StringBuilder fileContentSb = FileUtil.readFileContent(file);
                if (fileContentSb!=null&&fileContentSb.length()>0){
                    int index = fileContentSb.indexOf("@Configuration");
                    StringBuilder redisConfigPrefix = new StringBuilder(fileContentSb.substring(0, index));
                    StringBuilder redisConfigContent = new StringBuilder(fileContentSb.substring(index, fileContentSb.lastIndexOf("}")));
                    for (TableInfo tableInfo : tableInfos) {
                        TableInfo transformTableInfo = tableInfo.getTransformTableInfo();
                        if (!redisConfigContent.toString().contains(StringUtil.firstToLowerCase(transformTableInfo.getTableName())+"RedisTemplate")){
                            writeRedis(redisConfigPrefix,redisConfigContent,transformTableInfo);
                        }
                    }
                    redisConfigContent.append("\n}");
                    redisConfigPrefix.append("\n").append(redisConfigContent);
                    FileUtil.createWriteFile(file,redisConfigPrefix.toString());
                }
            } else {
                StringBuilder redisConfigPrefix = new StringBuilder();
                redisConfigPrefix.append("package ").append(taskArgs.packageName).append(".").append(getPackageName())
                        .append(";\n\nimport org.springframework.context.annotation.Configuration;\n")
                        .append("import org.springframework.context.annotation.Bean;\n")
                        .append("import org.springframework.data.redis.connection.RedisConnectionFactory;\n")
                        .append("import org.springframework.data.redis.core.RedisTemplate;\n")
                        .append("import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;\n");

                StringBuilder redisConfigContent = new StringBuilder("\n@Configuration\npublic class RedisConfig {\n\n");
                for (TableInfo tableInfo : tableInfos) {
                    TableInfo transformTableInfo = tableInfo.getTransformTableInfo();
                    writeRedis(redisConfigPrefix,redisConfigContent,transformTableInfo);
                }
                redisConfigContent.append("\n}");
                redisConfigPrefix.append(redisConfigContent);
                createFile("RedisConfig.java", redisConfigPrefix.toString());
            }

        }
        return true;
    }

    private void writeRedis(StringBuilder redisConfigPrefix,StringBuilder redisConfigContent,TableInfo transformTableInfo){
        redisConfigPrefix.append("import ").append(taskArgs.packageName).append(".entity.")
                .append(transformTableInfo.getTableName()).append(";\n");
        redisConfigContent.append("\t@Bean\n\tpublic RedisTemplate  ")
                .append(StringUtil.firstToLowerCase(transformTableInfo.getTableName()))
                .append("RedisTemplate(\n\t\t\tRedisConnectionFactory redisConnectionFactory) {\n\t\tRedisTemplate template = new RedisTemplate<>();\n\t\ttemplate.setConnectionFactory(redisConnectionFactory);\n\t\tJackson2JsonRedisSerializer<")
                .append(transformTableInfo.getTableName())
                .append("> ser = new Jackson2JsonRedisSerializer<>(")
                .append(transformTableInfo.getTableName()).append(".class);\n\t\ttemplate.setDefaultSerializer(ser);\n\t\treturn template;\n\t}\n\n");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy