Commit b6b07855 sunke

打包时自动从243服务器拉取最新的翻译数据,服务器启动时,将其初始化到数据库中

1 个父辈 c5e34def
......@@ -242,6 +242,12 @@
<artifactId>easyexcel</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
......
......@@ -94,6 +94,21 @@ public class LanguageMsgController {
return ResultBean.newOkResult(resultMsg);
}
@RequestMapping(value = "/getLanguageMsgList")
@AnonymousAccess
public List<LanguageMsg> getMsgList(String type) throws Exception {
List<LanguageMsg> results = new ArrayList<>();
Criteria criteria = Criteria.where("type").is(type);
Query query = Query.query(criteria);
List<LanguageMsg> list = languageMsgManager.findByQuery(query);
for (LanguageMsg languageMsg : list) {
if(!languageMsg.getCreateDate().equals(languageMsg.getUpdateDate())){
languageMsg.setId(null);//不需要返回ID
results.add(languageMsg);
}
}
return results;
}
@ApiOperation("导出资源文件")
@GetMapping(value = "/download")
......
......@@ -18,7 +18,8 @@ import java.util.Map;
@Data
@Document
public class LanguageMsg extends BasePo implements Serializable {
public class
LanguageMsg extends BasePo implements Serializable {
public LanguageMsg(){
......
......@@ -2,6 +2,8 @@ package com.neotel.smfcore.core.language.util;
import cn.hutool.core.util.ObjectUtil;
import com.neotel.smfcore.common.csv.CsvReader;
import com.neotel.smfcore.common.utils.FileUtil;
import com.neotel.smfcore.common.utils.JsonUtil;
import com.neotel.smfcore.core.device.util.DataCache;
import com.neotel.smfcore.core.language.service.bean.Content;
import com.neotel.smfcore.core.language.service.bean.LanguageInfo;
......@@ -9,7 +11,6 @@ import com.neotel.smfcore.core.language.service.nanager.ILanguageMsgManager;
import com.neotel.smfcore.core.language.service.po.LanguageMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
......@@ -20,14 +21,14 @@ import java.util.*;
@Service
public class LanguageMsgService {
private static MyMessageSource messageSource;
@Autowired
ILanguageMsgManager languageMsgManager;
@Autowired
DataCache dataCache;
private static MyMessageSource messageSource;
public LanguageMsgService(MyMessageSource messageSource) {
this.messageSource = messageSource;
LanguageMsgService.messageSource = messageSource;
// if(messageSource instanceof ResourceBundleMessageSource){
//
// Map<String,String> map1=messageSource.getMessageMap(Locale.SIMPLIFIED_CHINESE);
......@@ -37,31 +38,31 @@ public class LanguageMsgService {
// Map<String,String> map5=messageSource.getMessageMap(new Locale(""));
// }
}
public Map<String,Map<String, LanguageMsg>> loadMsgMap() {
int hasCount=1;
Map<String,Map<String, LanguageMsg>> resourceMap=new HashMap<>();
public Map<String, Map<String, LanguageMsg>> loadMsgMap() {
int hasCount = 1;
Map<String, Map<String, LanguageMsg>> resourceMap = new HashMap<>();
List<LanguageMsg> msgs = languageMsgManager.findByQuery(new Query());
for (LanguageMsg msg :
msgs) {
if(msg.getCode().startsWith("smfcode.")||msg.getType().equals("smfcode")){
String newCode=msg.getCode().replace("smfcode.","smfcore.");
for (LanguageMsg msg : msgs) {
if (msg.getCode().startsWith("smfcode.") || msg.getType().equals("smfcode")) {
String newCode = msg.getCode().replace("smfcode.", "smfcore.");
msg.setCode(newCode);
msg.setType("smfcore");
languageMsgManager.save(msg);
}
Map<String, LanguageMsg> typeMap = resourceMap.get(msg.getType());
if(typeMap==null){
typeMap=new HashMap<>();
if (typeMap == null) {
typeMap = new HashMap<>();
}
if(typeMap.containsKey(msg.getCode())){
String delId=msg.getId();
if (typeMap.containsKey(msg.getCode())) {
String delId = msg.getId();
// log.info("资源创建时间["+typeMap.get(msg.getCode()).getCreateDate()+"]["+ msg.getCreateDate()+"]ID["+typeMap.get(msg.getCode()).getId()+"]["+ msg.getId()+"],code=["+msg.getCode()+"]已存在,["+hasCount+"]");
log.info("资源 ID["+typeMap.get(msg.getCode()).getId()+"]["+ msg.getId()+"],code=["+msg.getCode()+"]已存在,删除资源["+delId+"]["+hasCount+"]");
log.info("资源 ID[" + typeMap.get(msg.getCode()).getId() + "][" + msg.getId() + "],code=[" + msg.getCode() + "]已存在,删除资源[" + delId + "][" + hasCount + "]");
languageMsgManager.delete(msg);
hasCount++;
}else{
typeMap.put(msg.getCode(),msg);
} else {
typeMap.put(msg.getCode(), msg);
resourceMap.put(msg.getType(), typeMap);
}
}
......@@ -69,24 +70,33 @@ public class LanguageMsgService {
return resourceMap;
}
// public String getResourcesPath() {
// String filePath = System.getProperty("user.dir");
// log.info("user.dir=" + filePath);
// // "\\WEB-INF\\classes\\META-INF\\resources";
//// user.dir=C:\Program Files\Apache Software Foundation\Tomcat 9.0
//// C:\Program Files\Apache Software Foundation\Tomcat 9.0\webapps\smf-core\WEB-INF\classes\META-INF\resources
// if(filePath.toLowerCase().contains("tomcat")){
// return filePath+"\\webapps\\smf-core\\WEB-INF\\classes\\META-INF\\resources";
// }else{
// return filePath + "\\src\\main\\resources";
// }
// }
public void autoImportJsonFile() {
String classesPath = this.getClass().getResource("/").getPath();
File classDir = new File(classesPath); //需要获取的文件的路径
if (classDir.exists() && classDir.isDirectory()) {
for (File file : classDir.listFiles()) {
//只读取后缀名为json的
if (file.isFile()) {
String fileName = file.getName();
if (fileName.endsWith(".json")) {
try {
String jsonStr = FileUtil.readUtf8String(file);
List<LanguageMsg> languageMsgs = JsonUtil.toList(jsonStr, LanguageMsg.class);
msgListUpload(file.getName(),languageMsgs,false);
} catch (Exception ex) {
log.info("autoImportCsvFile 读取文件" + fileName + "错误:" + ex.toString());
}
}
}
}
}
}
public void autoImportCsvFile(){
public void autoImportCsvFile() {
// String filePath = getResourcesPath();
String filePath = "D:\\resources";
File file = new File(filePath ); //需要获取的文件的路径
List<LanguageInfo> languageInfoList=dataCache.getLanguageList();
File file = new File(filePath); //需要获取的文件的路径
List<LanguageInfo> languageInfoList = dataCache.getLanguageList();
if (file.exists() && file.isDirectory()) {
String[] fileNameLists = file.list(); //存储文件名的String数组
File[] filePathLists = file.listFiles(); //存储文件路径的String数组
......@@ -98,12 +108,12 @@ public class LanguageMsgService {
String fileName = filePathLists[i].getName();
if (fileName.endsWith(".csv")) {
try {
List<LanguageMsg> msgList=readCsvFile(filePathLists[i].getAbsolutePath(),languageInfoList);
if(msgList.size()>0){
msgListUpload(fileName, msgList,false);
List<LanguageMsg> msgList = readCsvFile(filePathLists[i].getAbsolutePath(), languageInfoList);
if (msgList.size() > 0) {
msgListUpload(fileName, msgList, false);
}
}catch (Exception ex){
log.info("autoImportCsvFile 读取文件"+fileName+"错误:"+ex.toString());
} catch (Exception ex) {
log.info("autoImportCsvFile 读取文件" + fileName + "错误:" + ex.toString());
}
}
}
......@@ -138,12 +148,10 @@ public class LanguageMsgService {
List<LanguageMsg> languageMsgs = new ArrayList<>();
for (String code :
defaultLanMap.keySet()) {
for (String code : defaultLanMap.keySet()) {
String defMsg = defaultLanMap.get(code);
LanguageMsg msg = new LanguageMsg(code, defMsg, "");
for (String lan :
lanMsgMap.keySet()) {
for (String lan : lanMsgMap.keySet()) {
if (lanMsgMap.get(lan).containsKey(code)) {
String lanMsg = lanMsgMap.get(lan).get(code);
msg.setContent(lan, lanMsg);
......@@ -216,49 +224,44 @@ public class LanguageMsgService {
// }
public List<String> getAllLanList() {
public List<String> getAllLanList() {
List<LanguageInfo> lanList = dataCache.getLanguageList();
List<String> list = new ArrayList<>();
for (LanguageInfo lan : lanList
) {
for (LanguageInfo lan : lanList) {
list.add(lan.getLanCode());
}
return list;
}
public String resourceUpload(String orgFileName, Map<String, String> proMap , String lanCode, String type) {
public String resourceUpload(String orgFileName, Map<String, String> proMap, String lanCode, String type) {
List<LanguageMsg> newLanguageList = new ArrayList<>();
List<LanguageMsg> updateLanguageList = new ArrayList<>();
if (proMap != null && proMap.size() > 0) {
for (String key :
proMap.keySet()) {
for (String key : proMap.keySet()) {
//如果Key爲空,直接返回
if(ObjectUtil.isEmpty(key))
{
if (ObjectUtil.isEmpty(key)) {
continue;
}
String msgStr = proMap.get(key);
LanguageMsg msg = MessageUtils.getMsg(type,key);
LanguageMsg msg = MessageUtils.getMsg(type, key);
//导入时只能新增不能修改
if (msg == null) {
msg = new LanguageMsg(key, msgStr,type);
msg = new LanguageMsg(key, msgStr, type);
msg.setContent(lanCode, msgStr);
newLanguageList.add(msg);
} else {
String oldMsg = msg.getContent(lanCode);
// if (!oldMsg.equals(msgStr)) {
if(ObjectUtil.isEmpty(oldMsg)){
if (ObjectUtil.isEmpty(oldMsg)) {
msg.setContent(lanCode, msgStr);
//内容默认更改为中文
if(lanCode.equals(MessageUtils.ZH_CN)){
if(!msg.getMsg().equals(msgStr)){
if (lanCode.equals(MessageUtils.ZH_CN)) {
if (!msg.getMsg().equals(msgStr)) {
msg.setMsg(msgStr);
}
}
......@@ -272,41 +275,39 @@ public class LanguageMsgService {
if (newLanguageList.size() > 0) {
languageMsgManager.insertAll(newLanguageList);
for (LanguageMsg msg :
newLanguageList) {
for (LanguageMsg msg : newLanguageList) {
MessageUtils.updateMsg(msg);
}
}
if(updateLanguageList.size() >0||newLanguageList.size()>0){
log.info("导入资源[" + orgFileName + "]["+type+"]["+lanCode+"],共["+proMap.size()+"]条数据,共更新[" + updateLanguageList.size() + "]条资源,新增[" + newLanguageList.size() + "]条资源");
}else{
log.debug("导入资源[" + orgFileName + "]["+type+"]["+lanCode+"],共["+proMap.size()+"]条数据,共更新[" + updateLanguageList.size() + "]条资源,新增[" + newLanguageList.size() + "]条资源");
if (updateLanguageList.size() > 0 || newLanguageList.size() > 0) {
log.info("导入资源[" + orgFileName + "][" + type + "][" + lanCode + "],共[" + proMap.size() + "]条数据,共更新[" + updateLanguageList.size() + "]条资源,新增[" + newLanguageList.size() + "]条资源");
} else {
log.debug("导入资源[" + orgFileName + "][" + type + "][" + lanCode + "],共[" + proMap.size() + "]条数据,共更新[" + updateLanguageList.size() + "]条资源,新增[" + newLanguageList.size() + "]条资源");
}
return "ok";
}
public void msgListUpload(String fileName, List<LanguageMsg> msgList,boolean isNeedUpdate) {
public void msgListUpload(String fileName, List<LanguageMsg> msgList, boolean isNeedUpdate) {
List<LanguageMsg> newLanguageList = new ArrayList<>();
List<LanguageMsg> updateLanguageList = new ArrayList<>();
for (LanguageMsg msg :
msgList) {
for (LanguageMsg msg : msgList) {
if(ObjectUtil.isEmpty(msg.getCode())) {
if (ObjectUtil.isEmpty(msg.getCode())) {
//code不能为空
continue;
}
if(ObjectUtil.isEmpty(msg.getMsg())){
if (ObjectUtil.isEmpty(msg.getMsg())) {
//内容不能为空
continue;
} if(ObjectUtil.isEmpty(msg.getType())){
}
if (ObjectUtil.isEmpty(msg.getType())) {
//内容不能为空
continue;
}
List<Content> contentList = new ArrayList<>();
for (Content con :
msg.getContentList()) {
for (Content con : msg.getContentList()) {
if (ObjectUtil.isEmpty(con.getLanCode()) || ObjectUtil.isEmpty(con.getMsg())) {
continue;
}
......@@ -315,19 +316,17 @@ public class LanguageMsgService {
//判断是否是新增
LanguageMsg oldMsg=MessageUtils.getMsg(msg.getType(), msg.getCode());
if(oldMsg==null){
LanguageMsg oldMsg = MessageUtils.getMsg(msg.getType(), msg.getCode());
if (oldMsg == null) {
newLanguageList.add(msg);
}
else{
boolean isUpdate=false;
} else {
boolean isUpdate = false;
//只新增不修改
List<String> allLanList=getAllLanList();
for (String lanType :
allLanList) {
List<String> allLanList = getAllLanList();
for (String lanType : allLanList) {
String oldValue = oldMsg.getContent(lanType);
String newValue=msg.getContent(lanType);
if(ObjectUtil.isNotEmpty(newValue)){
String newValue = msg.getContent(lanType);
if (ObjectUtil.isNotEmpty(newValue)) {
if (isNeedUpdate || ObjectUtil.isEmpty(oldValue)) {
oldMsg.setContent(lanType, newValue);
isUpdate = true;
......@@ -341,7 +340,7 @@ public class LanguageMsgService {
}
}
if(isUpdate){
if (isUpdate) {
updateLanguageList.add(oldMsg);
languageMsgManager.save(oldMsg);
MessageUtils.updateMsg(oldMsg);
......@@ -351,26 +350,20 @@ public class LanguageMsgService {
}
if (newLanguageList.size() > 0) {
languageMsgManager.insertAll(newLanguageList);
for (LanguageMsg msg :
newLanguageList) {
for (LanguageMsg msg : newLanguageList) {
MessageUtils.updateMsg(msg);
}
}
log.info("导入csv["+fileName+"]资源,共["+msgList.size()+"]条数据,共更新[" + updateLanguageList.size() + "]条资源,新增[" + newLanguageList.size() + "]条资源");
log.info("导入csv[" + fileName + "]资源,共[" + msgList.size() + "]条数据,共更新[" + updateLanguageList.size() + "]条资源,新增[" + newLanguageList.size() + "]条资源");
}
public Map<String, String> readPropertiesFile(File file) {
public Map<String, String> readPropertiesFile(File file) {
Map<String, String> map = new HashMap<>();
try (FileInputStream fis = new FileInputStream(file.getPath());
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(isr)
) {
try (FileInputStream fis = new FileInputStream(file.getPath()); InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); BufferedReader br = new BufferedReader(isr)) {
String line;
while ((line = br.readLine()) != null) {
......@@ -389,31 +382,30 @@ public class LanguageMsgService {
}
public List<LanguageMsg> readCsvFile(String fileURL , List<LanguageInfo> languageInfos) throws Exception{
CsvReader csvRead = CsvReader.newReader(fileURL,"编号","code");
int codeIndex = csvRead.getCsvIndex("编号","code");
int typeIndex = csvRead.getCsvIndex("类型","type");
int msgIndex = csvRead.getCsvIndex("内容","msg");
public List<LanguageMsg> readCsvFile(String fileURL, List<LanguageInfo> languageInfos) throws Exception {
CsvReader csvRead = CsvReader.newReader(fileURL, "编号", "code");
int codeIndex = csvRead.getCsvIndex("编号", "code");
int typeIndex = csvRead.getCsvIndex("类型", "type");
int msgIndex = csvRead.getCsvIndex("内容", "msg");
Map<String,Integer> lanCodeIndex=new HashMap<>() ;
Map<String, Integer> lanCodeIndex = new HashMap<>();
// List<LanguageInfo> languageInfos=dataCache.getLanguageList();
for (LanguageInfo lan:languageInfos
) {
int lanIndex = csvRead.getCsvIndex(lan.getLanCode(),lan.getLanName());
if(lanIndex>=0){
lanCodeIndex.put(lan.getLanCode(),lanIndex);
for (LanguageInfo lan : languageInfos) {
int lanIndex = csvRead.getCsvIndex(lan.getLanCode(), lan.getLanName());
if (lanIndex >= 0) {
lanCodeIndex.put(lan.getLanCode(), lanIndex);
}
}
int row = 1;
int newRowCount = 0;
int updateRowCount = 0;
String msg="";
List<LanguageMsg> list=new ArrayList<>();
while(csvRead.readRecord()) {
String msg = "";
List<LanguageMsg> list = new ArrayList<>();
while (csvRead.readRecord()) {
row++;
String[] lineValues = csvRead.getValues();
LanguageMsg languageMsg=new LanguageMsg();
LanguageMsg languageMsg = new LanguageMsg();
String code = lineValues[codeIndex];
String type = lineValues[typeIndex];
String msgStr = lineValues[msgIndex];
......@@ -425,13 +417,12 @@ public class LanguageMsgService {
languageMsg.setCode(code);
languageMsg.setType(type);
for (String lan :
lanCodeIndex.keySet()) {
int lanIndex=lanCodeIndex.get(lan);
if(lineValues.length>lanIndex){
String lanMsg=lineValues[lanIndex];
if(!lanMsg.isEmpty()){
languageMsg.setContent(lan,lanMsg);
for (String lan : lanCodeIndex.keySet()) {
int lanIndex = lanCodeIndex.get(lan);
if (lineValues.length > lanIndex) {
String lanMsg = lineValues[lanIndex];
if (!lanMsg.isEmpty()) {
languageMsg.setContent(lan, lanMsg);
}
}
}
......@@ -442,17 +433,14 @@ public class LanguageMsgService {
}
public Map<String,String> readJsFile(File file){
Map<String,String> map=new HashMap<>();
public Map<String, String> readJsFile(File file) {
Map<String, String> map = new HashMap<>();
try (FileInputStream fis = new FileInputStream(file.getPath());
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(isr)
) {
try (FileInputStream fis = new FileInputStream(file.getPath()); InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); BufferedReader br = new BufferedReader(isr)) {
String line;
//网友推荐更加简洁的写法
int lineIndex=-1;
String currNodeName="";
int lineIndex = -1;
String currNodeName = "";
while ((line = br.readLine()) != null) {
lineIndex++;
......@@ -460,49 +448,48 @@ public class LanguageMsgService {
// continue;
// }
if(ObjectUtil.isEmpty(line)){
if (ObjectUtil.isEmpty(line)) {
continue;
}
if (line.contains("{")){
if(line.contains("=")){
currNodeName="";
}else{
int dendIndex=line.indexOf(':');
String lineNode=line.substring(0,dendIndex).trim();
if(ObjectUtil.isEmpty(currNodeName)){
currNodeName=lineNode;
}else{
if (line.contains("{")) {
if (line.contains("=")) {
currNodeName = "";
} else {
int dendIndex = line.indexOf(':');
String lineNode = line.substring(0, dendIndex).trim();
if (ObjectUtil.isEmpty(currNodeName)) {
currNodeName = lineNode;
} else {
//节点名称+
currNodeName=currNodeName+"."+lineNode;
currNodeName = currNodeName + "." + lineNode;
}
}
}else if(line.contains("}")){
} else if (line.contains("}")) {
//节点名称-
if(currNodeName.contains(".")){
int endIndex=currNodeName.lastIndexOf(".");
currNodeName=currNodeName.substring(0,endIndex );
}else{
currNodeName="";
if (currNodeName.contains(".")) {
int endIndex = currNodeName.lastIndexOf(".");
currNodeName = currNodeName.substring(0, endIndex);
} else {
currNodeName = "";
}
}
else if(line .contains(":")){
if(line.endsWith(",")){
line=line.substring(0,line.length()-1);
} else if (line.contains(":")) {
if (line.endsWith(",")) {
line = line.substring(0, line.length() - 1);
}
String[] array=line.replace("'","").replace("\"","").split(":");
if(array.length==2){
String linekey=array[0].trim();
String lineValue=array[1].trim();
map.put(currNodeName+"."+linekey,lineValue);
String[] array = line.replace("'", "").replace("\"", "").split(":");
if (array.length == 2) {
String linekey = array[0].trim();
String lineValue = array[1].trim();
map.put(currNodeName + "." + linekey, lineValue);
}
}
}
} catch (IOException e) {
log.error("readJsFile 出错:"+e.toString());
} catch (IOException e) {
log.error("readJsFile 出错:" + e.toString());
e.printStackTrace();
}
return map;
......
......@@ -92,8 +92,8 @@ public class MessageUtils {
private void initLanguageMsgList() {
msgMap = messageService.loadMsgMap();
//自动导入CSV内容,只新增不修改
messageService.autoImportCsvFile();
//自动导入JSON内容
messageService.autoImportJsonFile();
//只新增不修改
messageService.autoImportResources();
if (msgMap.size() > 0) {
......
......@@ -2,7 +2,7 @@ server:
port: 8800
api:
name: Neotel
name:
inCheckUrl:
outNotifyUrl:
inNotifyUrl:
......
package com.neotel.smfcore;
import com.neotel.smfcore.common.exception.ApiException;
import com.neotel.smfcore.common.utils.FileUtil;
import com.neotel.smfcore.common.utils.HttpHelper;
import com.neotel.smfcore.common.utils.JsonUtil;
import org.junit.jupiter.api.Test;
import org.springframework.util.FileCopyUtils;
import java.io.File;
import java.io.FileWriter;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ApplicationTests {
@Test
void contextLoads() {
saveLanguageFile("smfcore");
saveLanguageFile("smf");
}
private void saveLanguageFile(String type){
try {
String url = "http://192.168.1.243/smf-core/api/translation/getLanguageMsgList";
Map<String,Object> params = new HashMap<>();
params.put("type",type);
System.out.println("开始获取最新["+type+"]翻译资源...");
String jsonTxt = HttpHelper.postParam(url,params);
Map<String, Object> resultMap = JsonUtil.toMap(jsonTxt);
if(resultMap.get("status") != null){
System.out.println(jsonTxt);
return;
}
// String jsonTxt = type + "+++++";
String testClassDirPath = this.getClass().getResource("/").getPath();
File testClassDir = new File(testClassDirPath);
File projectDir = testClassDir.getParentFile().getParentFile();
String filename = type+".json";
File projectJsonFile = new File(projectDir,"src/main/resources/"+filename);
System.out.println("生成翻译资源文件到" + projectJsonFile.getAbsolutePath());
try(FileWriter fw = new FileWriter(projectJsonFile)){
fw.write(jsonTxt);
}
File targetJsonFile = new File(projectDir,"target/classes/"+filename);
if(targetJsonFile.exists()){
targetJsonFile.delete();
}
System.out.println("复制翻译资源文件到" + targetJsonFile.getAbsolutePath());
FileCopyUtils.copy(projectJsonFile,targetJsonFile);
} catch (Exception e) {
System.out.println("获取["+type+"]翻译资源出错"+e.getMessage());
}
}
}
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!