123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- package com.yihu.hos.crawler.service;
- import com.fasterxml.jackson.databind.JsonNode;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.yihu.hos.core.compress.Zipper;
- import com.yihu.hos.core.encrypt.MD5;
- import com.yihu.hos.core.encrypt.RSA;
- import com.yihu.hos.core.file.FileUtil;
- import com.yihu.hos.core.http.HTTPResponse;
- import com.yihu.hos.core.http.HttpClientKit;
- import com.yihu.hos.core.log.Logger;
- import com.yihu.hos.core.log.LoggerFactory;
- import com.yihu.hos.crawler.model.config.SysConfig;
- import com.yihu.hos.crawler.model.patient.Patient;
- import java.io.File;
- import java.security.Key;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.UUID;
- /**
- * 档案上传
- *
- * @author Air
- * @version 1.0
- * @created 2015.07.06 15:58
- */
- public class OldPatientCDAUpload {
- public static String uploadMethod;
- private static Logger logger = LoggerFactory.getLogger(OldPatientCDAUpload.class);
- /**
- * @param patient
- * @return
- * @modify 2015.09.15 airhead 修订删除目录
- * @modify 2015.09.19 airhead 修复无文档问题及错误信息
- */
- public boolean upload(Patient patient, String token) {
- ZipFile zipFile = zip(patient);
- try {
- if (zipFile == null || zipFile.file == null) {
- logger.info("压缩病人档案失败,病人文档未生成,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
- return false;
- }
- boolean result = upload(patient, zipFile, token);
- if (!result) {
- logger.info("上传病人档案失败,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
- FileUtil.deleteDirectory(new File(zipFile.directory));
- return false;
- }
- result = FileUtil.deleteDirectory(new File(zipFile.directory));
- if (!result) {
- logger.info("删除临时文件失败,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
- }
- } catch (Exception e) {
- FileUtil.deleteDirectory(new File(zipFile.directory));
- }
- return true;
- }
- /**
- * @param patient
- * @return
- * @modify 从data目录生成zip数据
- */
- public ZipFile zip(Patient patient) {
- try {
- PatientCDAIndex patientCDAIndex = new PatientCDAIndex(patient);
- String dataDirectory = patientCDAIndex.getDataDirectory();
- String filePath = patientCDAIndex.createIndex(PatientCDAIndex.IndexType.ZIP, PatientCDAIndex.FileType.ZIP);
- UUID uuidPwd = UUID.randomUUID();
- String pwd = uuidPwd.toString();
- Key key = RSA.genPublicKey(SysConfig.getInstance().getPublicKeyMap().get(patient.getOrgCode()));
- if (key == null) {
- logger.info("压缩文件错误,无公钥信息.");
- FileUtil.deleteDirectory(new File(patientCDAIndex.getDirectory()));
- return null;
- }
- ZipFile zipFile = new ZipFile();
- zipFile.encryptPwd = RSA.encrypt(pwd, key);
- Zipper zipper = new Zipper();
- zipFile.file = zipper.zipFile(new File(dataDirectory), filePath, pwd);
- zipFile.dataDirectory = dataDirectory;
- zipFile.directory = patientCDAIndex.getDirectory();
- return zipFile;
- } catch (Exception e) {
- e.printStackTrace();
- logger.info("从data目录生成zip数据时,压缩文件异常", e);
- }
- return null;
- }
- private boolean upload(Patient patient, ZipFile zipFile, String token) {
- try {
- String uploadMethod = EsbHttp.defaultHttpUrl + "/packages";
- String fileMd5 = MD5.getMd5ByFile(zipFile.file);
- Map<String, String> formParams = new HashMap<>();
- formParams.put("md5", fileMd5);
- formParams.put("package_crypto", zipFile.encryptPwd);
- formParams.put("org_code", patient.getOrgCode());
- formParams.put("token", token);
- Map<String, String> header = new HashMap<>();
- header.put("Authorization", "Basic " + EsbHttp.clientKey);
- HTTPResponse response = HttpClientKit.postFile(uploadMethod, zipFile.file.getAbsolutePath(), formParams, header);
- if (response == null) {
- logger.info("上传病人档案请求失败,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
- return false;
- }
- if (response.getStatusCode() != 200) {
- logger.info("上传病人档案请求失败,错误代码:" + response.getStatusCode() + ",patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
- return false;
- }
- ObjectMapper mapper = new ObjectMapper();
- JsonNode rootNode = mapper.readValue(response.getBody(), JsonNode.class);
- JsonNode codeNode = rootNode.get("code");
- String result = codeNode.asText();
- if (!result.equals("0")) {
- logger.info("上传病人档案失败,错误代码:" + result + ",patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
- return false;
- } else {
- logger.info("上传病人档案成功,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
- return true;
- }
- } catch (Exception e) {
- e.printStackTrace();
- logger.info("上传病人档案异常,patient_id:" + patient.getPatientId() + ",event_no:" + patient.getEventNo());
- return false;
- }
- }
- private class ZipFile {
- public File file;
- public String encryptPwd;
- public String directory;
- public String dataDirectory;
- }
- }
|