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