OldPatientCDAUpload.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package com.yihu.hos.crawler.service;
  2. import com.fasterxml.jackson.databind.JsonNode;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import com.yihu.hos.core.compress.Zipper;
  5. import com.yihu.hos.core.encrypt.MD5;
  6. import com.yihu.hos.core.encrypt.RSA;
  7. import com.yihu.hos.core.file.FileUtil;
  8. import com.yihu.hos.core.http.HTTPResponse;
  9. import com.yihu.hos.core.http.HttpClientKit;
  10. import com.yihu.hos.core.log.Logger;
  11. import com.yihu.hos.core.log.LoggerFactory;
  12. import com.yihu.hos.crawler.model.config.SysConfig;
  13. import com.yihu.hos.crawler.model.patient.Patient;
  14. import java.io.File;
  15. import java.security.Key;
  16. import java.util.HashMap;
  17. import java.util.Map;
  18. import java.util.UUID;
  19. /**
  20. * 档案上传
  21. *
  22. * @author Air
  23. * @version 1.0
  24. * @created 2015.07.06 15:58
  25. */
  26. public class OldPatientCDAUpload {
  27. public static String uploadMethod;
  28. private static Logger logger = LoggerFactory.getLogger(OldPatientCDAUpload.class);
  29. /**
  30. * @param patient
  31. * @return
  32. * @modify 2015.09.15 airhead 修订删除目录
  33. * @modify 2015.09.19 airhead 修复无文档问题及错误信息
  34. */
  35. public boolean upload(Patient patient, String token) {
  36. ZipFile zipFile = zip(patient);
  37. try {
  38. if (zipFile == null || zipFile.file == null) {
  39. logger.info("压缩病人档案失败,病人文档未生成,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
  40. return false;
  41. }
  42. boolean result = upload(patient, zipFile, token);
  43. if (!result) {
  44. logger.info("上传病人档案失败,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
  45. FileUtil.deleteDirectory(new File(zipFile.directory));
  46. return false;
  47. }
  48. result = FileUtil.deleteDirectory(new File(zipFile.directory));
  49. if (!result) {
  50. logger.info("删除临时文件失败,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
  51. }
  52. } catch (Exception e) {
  53. FileUtil.deleteDirectory(new File(zipFile.directory));
  54. }
  55. return true;
  56. }
  57. /**
  58. * @param patient
  59. * @return
  60. * @modify 从data目录生成zip数据
  61. */
  62. public ZipFile zip(Patient patient) {
  63. try {
  64. PatientCDAIndex patientCDAIndex = new PatientCDAIndex(patient);
  65. String dataDirectory = patientCDAIndex.getDataDirectory();
  66. String filePath = patientCDAIndex.createIndex(PatientCDAIndex.IndexType.ZIP, PatientCDAIndex.FileType.ZIP);
  67. UUID uuidPwd = UUID.randomUUID();
  68. String pwd = uuidPwd.toString();
  69. Key key = RSA.genPublicKey(SysConfig.getInstance().getPublicKeyMap().get(patient.getOrgCode()));
  70. if (key == null) {
  71. logger.info("压缩文件错误,无公钥信息.");
  72. FileUtil.deleteDirectory(new File(patientCDAIndex.getDirectory()));
  73. return null;
  74. }
  75. ZipFile zipFile = new ZipFile();
  76. zipFile.encryptPwd = RSA.encrypt(pwd, key);
  77. Zipper zipper = new Zipper();
  78. zipFile.file = zipper.zipFile(new File(dataDirectory), filePath, pwd);
  79. zipFile.dataDirectory = dataDirectory;
  80. zipFile.directory = patientCDAIndex.getDirectory();
  81. return zipFile;
  82. } catch (Exception e) {
  83. e.printStackTrace();
  84. logger.info("从data目录生成zip数据时,压缩文件异常", e);
  85. }
  86. return null;
  87. }
  88. private boolean upload(Patient patient, ZipFile zipFile, String token) {
  89. try {
  90. String uploadMethod = EsbHttp.defaultHttpUrl + "/packages";
  91. String fileMd5 = MD5.getMd5ByFile(zipFile.file);
  92. Map<String, String> formParams = new HashMap<>();
  93. formParams.put("md5", fileMd5);
  94. formParams.put("package_crypto", zipFile.encryptPwd);
  95. formParams.put("org_code", patient.getOrgCode());
  96. formParams.put("token", token);
  97. Map<String, String> header = new HashMap<>();
  98. header.put("Authorization", "Basic " + EsbHttp.clientKey);
  99. HTTPResponse response = HttpClientKit.postFile(uploadMethod, zipFile.file.getAbsolutePath(), formParams, header);
  100. if (response == null) {
  101. logger.info("上传病人档案请求失败,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
  102. return false;
  103. }
  104. if (response.getStatusCode() != 200) {
  105. logger.info("上传病人档案请求失败,错误代码:" + response.getStatusCode() + ",patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
  106. return false;
  107. }
  108. ObjectMapper mapper = new ObjectMapper();
  109. JsonNode rootNode = mapper.readValue(response.getBody(), JsonNode.class);
  110. JsonNode codeNode = rootNode.get("code");
  111. String result = codeNode.asText();
  112. if (!result.equals("0")) {
  113. logger.info("上传病人档案失败,错误代码:" + result + ",patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
  114. return false;
  115. } else {
  116. logger.info("上传病人档案成功,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
  117. return true;
  118. }
  119. } catch (Exception e) {
  120. e.printStackTrace();
  121. logger.info("上传病人档案异常,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
  122. return false;
  123. }
  124. }
  125. private class ZipFile {
  126. public File file;
  127. public String encryptPwd;
  128. public String directory;
  129. public String dataDirectory;
  130. }
  131. }