PatientCDAUpload.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package com.yihu.hos.crawler.service;
  2. import com.yihu.hos.core.compress.Zipper;
  3. import com.yihu.hos.core.encrypt.RSA;
  4. import com.yihu.hos.core.file.FileUtil;
  5. import com.yihu.hos.core.log.Logger;
  6. import com.yihu.hos.core.log.LoggerFactory;
  7. import com.yihu.hos.crawler.model.config.SysConfig;
  8. import com.yihu.hos.crawler.model.patient.Patient;
  9. import java.io.File;
  10. import java.security.Key;
  11. import java.util.UUID;
  12. /**
  13. * 档案上传
  14. *
  15. * @author Air
  16. * @version 1.0
  17. * @created 2015.07.06 15:58
  18. */
  19. public class PatientCDAUpload {
  20. private static Logger logger = LoggerFactory.getLogger(PatientCDAUpload.class);
  21. public static String uploadMethod;
  22. /**
  23. * @param patient
  24. * @return
  25. * @modify 2015.09.15 airhead 修订删除目录
  26. * @modify 2015.09.19 airhead 修复无文档问题及错误信息
  27. */
  28. public Boolean upload(Patient patient, String token) {
  29. ZipFile zipFile = zip(patient);
  30. try {
  31. if (zipFile == null || zipFile.file == null) {
  32. logger.info("压缩病人档案失败,病人文档未生成,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
  33. return false;
  34. }
  35. boolean result = upload(patient, zipFile, token);
  36. if (!result) {
  37. logger.info("上传病人档案失败,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
  38. return result;
  39. }
  40. logger.trace(zipFile.directory);
  41. result = FileUtil.deleteDirectory(new File(zipFile.directory));
  42. if (!result) {
  43. logger.info("删除临时文件失败,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
  44. return result;
  45. }
  46. } catch (Exception e) {
  47. FileUtil.deleteDirectory(new File(zipFile.directory));
  48. return false;
  49. }
  50. return true;
  51. }
  52. /**
  53. * @param patient
  54. * @return
  55. * @modify 从data目录生成zip数据
  56. */
  57. public ZipFile zip(Patient patient) {
  58. try {
  59. PatientCDAIndex patientCDAIndex = new PatientCDAIndex(patient);
  60. String dataDirectory = patientCDAIndex.getDataDirectory();
  61. String filePath = patientCDAIndex.createIndex(PatientCDAIndex.IndexType.ZIP, PatientCDAIndex.FileType.ZIP);
  62. UUID uuidPwd = UUID.randomUUID();
  63. String pwd = uuidPwd.toString();
  64. String publicKey = SysConfig.getInstance().getPublicKeyMap().get(patient.getOrgCode());
  65. if(publicKey== null || publicKey.length() == 0) {
  66. publicKey = EsbHttp.getPublicKey(patient.getOrgCode());
  67. SysConfig.getInstance().getPublicKeyMap().put(patient.getOrgCode(), publicKey);
  68. }
  69. Key key = RSA.genPublicKey(publicKey);
  70. if (key == null) {
  71. logger.info("压缩文件错误,获取公钥错误.");
  72. return null;
  73. }
  74. ZipFile zipFile = new ZipFile();
  75. zipFile.encryptPwd = RSA.encrypt(pwd, key);
  76. Zipper zipper = new Zipper();
  77. zipFile.file = zipper.zipFileForAll(new File(dataDirectory), filePath, pwd);
  78. zipFile.dataDirectory = dataDirectory;
  79. zipFile.directory = patientCDAIndex.getDirectory();
  80. return zipFile;
  81. } catch (Exception e) {
  82. logger.error("从data目录生成zip数据时,压缩文件异常");
  83. logger.error(e.getCause().toString());
  84. }
  85. return null;
  86. }
  87. private boolean upload(Patient patient, ZipFile zipFile, String token) {
  88. return EsbHttp.upload(patient, zipFile.file, zipFile.encryptPwd, token);
  89. }
  90. private class ZipFile {
  91. public File file;
  92. public String encryptPwd;
  93. public String directory;
  94. public String dataDirectory;
  95. }
  96. }