package com.gzzn.omms.msgexchangeapi.tools; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.stream.Collector; import java.util.stream.Collectors; 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; import org.springframework.util.StringUtils; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; import com.gzzn.omms.msgexchangeapi.dao.CminmsgDao; import com.gzzn.omms.msgexchangeapi.entity.Cminmsg; import com.gzzn.omms.msgexchangeapi.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; /** * 每种消息类型导出一个xml文件即可 */ @Test public void exportOneTypeToJsonFiles() { List lsCmin = (List) cminmsgDao.findAll(); Map mapCminmsg = new HashMap(); //每种类型提取一个 for(Cminmsg x : lsCmin) { Msg msg = exchangeService.xmlstrToObject(x.getCminmsgsClobMsg(),Msg.class); mapCminmsg.putIfAbsent(msg.getMeta().getType() + "_" + msg.getMeta().getStyp(), x); } List ls = new ArrayList(mapCminmsg.values()); toJsonFiles(ls); } /** * 导出CMINMSGS数据中的消息到单独的Xml文件 * @throws JsonParseException * @throws JsonMappingException * @throws IOException */ @Test public void exportToXmlFiles() throws JsonParseException, JsonMappingException, IOException { List lsCmin = (List) cminmsgDao.findAll(); //提取某个航班的信息 List lsFlopTypeCmin = lsCmin.parallelStream() .filter(node->{ Msg msg = exchangeService.xmlstrToObject(node.getCminmsgsClobMsg(), Msg.class); return msg.getMeta().getType().equals("FLOP"); }) .collect(Collectors.toList()); flopMsgToXmlFiles(lsFlopTypeCmin); } //end function /** * 生成json文件 * @param lsCmin */ private void toJsonFiles(List 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); } } //end function private void flopMsgToXmlFiles(List 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(); } 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(); } }