Files
msgexchange-api/src/test/java/com/gzzn/omms/msgexchangeapi/tools/ToolTest.java
T

230 lines
6.3 KiB
Java
Raw Normal View History

2018-11-28 20:32:19 +08:00
package com.gzzn.omms.msgexchangeapi.tools;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
2018-11-30 11:30:08 +08:00
import java.util.ArrayList;
2018-12-03 10:44:14 +08:00
import java.util.Collections;
2018-11-30 11:30:08 +08:00
import java.util.HashMap;
2018-12-03 10:44:14 +08:00
import java.util.Iterator;
import java.util.LinkedHashMap;
2018-11-28 20:32:19 +08:00
import java.util.List;
2018-11-30 11:30:08 +08:00
import java.util.Map;
2018-12-03 10:44:14 +08:00
import java.util.Set;
import java.util.stream.Collector;
import java.util.stream.Collectors;
2018-11-28 20:32:19 +08:00
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
2018-12-03 10:44:14 +08:00
import org.springframework.util.StringUtils;
2018-11-28 20:32:19 +08:00
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.gzzn.omms.msgexchangeapi.domain.primary.dao.CminmsgDao;
import com.gzzn.omms.msgexchangeapi.domain.primary.entity.Cminmsg;
import com.gzzn.omms.msgexchangeapi.domain.primary.entity.msg.Msg;
import com.gzzn.omms.msgexchangeapi.exception.ExchangeServiceException;
import com.gzzn.omms.msgexchangeapi.service.IExchangeService;
import com.gzzn.omms.msgexchangeapi.utils.JsonUtil;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ToolTest {
private static Logger logger = LoggerFactory.getLogger(ToolTest.class);
@Autowired
private CminmsgDao cminmsgDao;
@Autowired
private IExchangeService exchangeService;
2018-11-30 11:30:08 +08:00
/**
* 每种消息类型导出一个xml文件即可
*/
2018-11-28 20:32:19 +08:00
@Test
2018-11-30 11:30:08 +08:00
public void exportOneTypeToJsonFiles() {
List<Cminmsg> lsCmin = (List<Cminmsg>) cminmsgDao.findAll();
Map<String, Cminmsg> mapCminmsg = new HashMap();
2018-11-28 20:32:19 +08:00
2018-12-03 10:44:14 +08:00
//每种类型提取一个
2018-11-30 11:30:08 +08:00
for(Cminmsg x : lsCmin)
{
Msg msg = exchangeService.xmlstrToObject(x.getCminmsgsClobMsg(),Msg.class);
mapCminmsg.putIfAbsent(msg.getMeta().getType() + "_" + msg.getMeta().getStyp(), x);
}
List<Cminmsg> ls = new ArrayList(mapCminmsg.values());
toJsonFiles(ls);
2018-11-28 20:32:19 +08:00
}
2018-11-30 11:30:08 +08:00
/**
* 导出CMINMSGS数据中的消息到单独的Xml文件
* @throws JsonParseException
* @throws JsonMappingException
* @throws IOException
*/
2018-12-03 10:44:14 +08:00
@Test
2018-11-28 20:32:19 +08:00
public void exportToXmlFiles() throws JsonParseException, JsonMappingException, IOException
{
List<Cminmsg> lsCmin = (List<Cminmsg>) cminmsgDao.findAll();
2018-12-03 10:44:14 +08:00
//提取某个航班的信息
List<Cminmsg> lsFlopTypeCmin = lsCmin.parallelStream()
.filter(node->{
Msg msg = exchangeService.xmlstrToObject(node.getCminmsgsClobMsg(), Msg.class);
return msg.getMeta().getType().equals("FLOP");
})
.collect(Collectors.toList());
flopMsgToXmlFiles(lsFlopTypeCmin);
2018-11-28 20:32:19 +08:00
} //end function
2018-11-30 11:30:08 +08:00
/**
2018-12-03 10:44:14 +08:00
* 生成json文件
2018-11-30 11:30:08 +08:00
* @param lsCmin
*/
private void toJsonFiles(List<Cminmsg> lsCmin)
{
try {
for(Cminmsg x : lsCmin)
{
String json = exchangeService.xmlToJson(x.getCminmsgsClobMsg());
Msg msg;
msg = JsonUtil.getObject(json,Msg.class);
String Dttm = msg.getMeta().getDttm();
String dir = "json";
StringBuilder path = new StringBuilder();
path.append(dir);
path.append("/");
path.append(msg.getMeta().getType());//Type
path.append("-"+msg.getMeta().getStyp());//Subtype
path.append(".json");
//父目录是否存在
File fileDir = new File(dir);
if(fileDir.exists() == false)
{
fileDir.mkdirs();
}
//
File f = new File(path.toString());
try (FileOutputStream fop = new FileOutputStream(f)) {
String jsonString = exchangeService.xmlToJson(x.getCminmsgsClobMsg());
jsonString = jsonString.replace("\"noNamespaceSchemaLocation\":\"unisysaodbsis.xsd\",", "");
byte[] contentInBytes = jsonString.getBytes("UTF-8");
fop.write(contentInBytes);
fop.flush();
fop.close();
}
catch (ExchangeServiceException e) {
logger.error(e.getMessage());
}
catch (IOException e) {
logger.error(e.getMessage());
}
}
} catch (JsonParseException e1) {
throw new RuntimeException(e1.getMessage());
} catch (JsonMappingException e1) {
throw new RuntimeException(e1);
} catch (IOException e1) {
throw new RuntimeException(e1);
}
2018-12-03 10:44:14 +08:00
} //end function
private void flopMsgToXmlFiles(List<Cminmsg> lsCmin)
{
FileOutputStream fop = null;
try {
for(Cminmsg x : lsCmin)
{
Msg msg = exchangeService.xmlstrToObject(x.getCminmsgsClobMsg(), Msg.class);
Map msgMap = exchangeService.xmlstrToObject(x.getCminmsgsClobMsg(), Map.class);
Map mapFlop = (Map)msgMap.get("FLOP");
String flid = String.valueOf(mapFlop.get("FLID"));
if(null == flid || flid.equalsIgnoreCase("null") || flid.equals("")) {
logger.warn("没有flid的消息:"+x.getCminmsgsClobMsg());
continue;
}
String type = "xml";
String path = getPathGroupByFlid(msg,type,flid);
//父目录是否存在
File fileDir = new File(path);
if(fileDir.getParentFile().exists() == false)
{
fileDir.getParentFile().mkdirs();
}
//
byte[] contentInBytes = x.getCminmsgsClobMsg().getBytes("UTF-8");
File f = new File(path.toString());
fop = new FileOutputStream(f);
fop.write(contentInBytes);
fop.flush();
fop.close();
}//end for
}
catch (Exception e) {
logger.error(""+JsonUtil.getString(e));
}
} //end function
private String getPath(Msg msg,String type)
{
String dir = type;
StringBuilder path = new StringBuilder();
path.append(dir);
path.append("/");
path.append(msg.getMeta().getType());//Type
path.append("-"+msg.getMeta().getStyp());//Subtype
path.append("."+dir);
return path.toString();
2018-11-30 11:30:08 +08:00
}
2018-12-03 10:44:14 +08:00
private String getPathGroupByFlid(Msg msg,String type,String flid)
{
String dir = type;
StringBuilder path = new StringBuilder();
path.append(dir);
path.append("/");
path.append(flid);
path.append("/");
path.append(msg.getMeta().getDttm());//dttm
path.append("-"+msg.getMeta().getType());//Type
path.append("-"+msg.getMeta().getStyp());//Subtype
path.append("."+dir);
return path.toString();
}
2018-11-28 20:32:19 +08:00
}