PatientCDAUpload.java 3.6 KB

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