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;
|
|
|
|
|
import java.util.HashMap;
|
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-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;
|
|
|
|
|
|
|
|
|
|
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-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-11-28 20:32:19 +08:00
|
|
|
public void exportToXmlFiles() throws JsonParseException, JsonMappingException, IOException
|
|
|
|
|
{
|
|
|
|
|
List<Cminmsg> lsCmin = (List<Cminmsg>) cminmsgDao.findAll();
|
|
|
|
|
|
2018-11-30 11:30:08 +08:00
|
|
|
toJsonFiles(lsCmin);
|
2018-11-28 20:32:19 +08:00
|
|
|
|
|
|
|
|
} //end function
|
2018-11-30 11:30:08 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成xml文件
|
|
|
|
|
* @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-11-28 20:32:19 +08:00
|
|
|
}
|