about 4 years ago

HAPI 是由 Java 開發的函式庫,能方便程式設計師產生和解析 HL7v2 的訊息

1. 下載 HAPI Libary

HAPI Download

2. 引用 HAPI 函式庫

2.1. Maven Dependency:This is the core HAPI libary

the Core HAPI libary
<dependency>
  <groupId>ca.uhn.hapi</groupId>
  <artifactId>hapi-base</artifactId>
  <version>2.1</version>
</dependency>

2.2. Maven Dependency:These are tructure JARs for HL7 2.3.1

the HL7 2.3.1 API
<dependency>
  <groupId>ca.uhn.hapi</groupId>
  <artifactId>hapi-structures-v231</artifactId>
  <version>2.1</version>
</dependency>

3. 產生 HL7v2 Message

HL7v2 Message Generator
try {
    // 訊息開頭符號
    char Start_Block = '\u000b';
    // 訊息結束符號
    char End_Block = '\u001c';
    // Segment 換行符號
    char Carriage_Return = 13;
    String SendingApplication = "Gaduo";
    String SendingFacility = "Gaduo";
    String ReceivingApplication = "NIST_RCVR_Gaduo_Chien";
    String ReceivingFacility = "NIST";
    String MessageControlID = "20130520164102";
    
    // 建構 ADT_A01 物件
    ADT_A01 adt = new ADT_A01();
    // 建構 MSH 物件
    MSH msh = adt.getMSH();
    // 建構 EVN 物件
    EVN evn = adt.getEVN();
    // 建構 PID 物件
    PID pid = adt.getPID();
    // 建構 PV1 物件
    PV1 pv1 = adt.getPV1();
    
    // 初始化 MSH(Message Header)
    adt.initQuickstart("ADT", "A01", "P");
    
    // 設置發送方應用程式名稱
    msh.getSendingApplication().getNamespaceID().setValue(SendingApplication);
    // 設置發送方設備名稱
    msh.getSendingFacility().getNamespaceID().setValue(SendingFacility);
    // 設置接收方應用程式名稱
    msh.getMsh5_ReceivingApplication().getHd1_NamespaceID().setValue(ReceivingApplication);
    // 設置接收方設備名稱
    msh.getMsh6_ReceivingFacility().getHd1_NamespaceID().setValue(ReceivingFacility);
    // 設置訊息產生時戳
    msh.getDateTimeOfMessage().getTimeOfAnEvent().setValue(System.currentTimeMillis()+"");
    // 設置訊息唯一識別碼
    msh.getMsh10_MessageControlID().setValue(MessageControlID);
    
    // 初始化 EVN (Event)
    TS ts = evn.getEvn2_RecordedDateTime(); 
    TSComponentOne componentOne = ts.getTimeOfAnEvent();
    componentOne.setValue(System.currentTimeMillis() + "");
    
    // 初始化 PID(Patient Identification)
    pid.getPid1_SetIDPID().parse("gaduo^^^IHENA&1.3.6.1.4.1.21367.2010.1.2.300&ISO");
    pid.getPid5_PatientName(0).parse("簡^嘎^都^^");
    pid.getPid7_DateTimeOfBirth().parse("20140122");
    pid.getPid8_Sex().parse("M");
    pid.getPid11_PatientAddress(0).parse("gaduo.chien@gmail.com");

        // 初始化PV1(Patient Visit)
    pv1 = adt.getPV1();
    IS is = pv1.getPatientClass();
    is.setValue("o");

        // 輸出訊息
    StringBuffer bs = new StringBuffer();
    try {
        bs.append(Start_Block);
        bs.append(msh.encode()).append(Carriage_Return);
        bs.append(evn.encode()).append(Carriage_Return);
        bs.append(pid.encode()).append(Carriage_Return);
        bs.append(pv1.encode()).append(Carriage_Return);
        bs.append(End_Block);
    } catch (HL7Exception e) {
        e.printStackTrace();
    }
    logger.info("\n" + bs.toString());

} catch (HL7Exception e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

4. HL7v2 Message:ADT^A01

ADT^A01:住院病患掛號

MSH|^~&|gaduo|Gaduo|NIST_RCVR_Gaduo_Chien|NIST|1398754506879||ADT^A01|20130520164102|P|2.3.1
EVN||1398754506880
PID|gaduo^^^IHENA||||簡^嘎^都||20140122|M|||gaduo.chien@gmail.com
PV1||o

5. 解析 HL7v2 Message

HL7v2 Message Parser
  Logger logger = Logger.getLogger(HAPITest.class);

  HapiContext context = new DefaultHapiContext();
  Parser p = context.getPipeParser();
  Message hapiMsg;
  try {
    // The parse method performs the actual parsing

    // 字串 msg 為
    hapiMsg = p.parse(msg);
  } catch (EncodingNotSupportedException e) {
    e.printStackTrace();
    return;
  } catch (HL7Exception e) {
    e.printStackTrace();
    return;
  }
  String version = hapiMsg.getVersion();
  if (version.equals("2.3.1")) {
    ADT_A01 adtMsg = (ADT_A01) hapiMsg;
    PID pid = adtMsg.getPID();

    try {
      // 取得病患唯一識別碼
      CX pid03 = pid.getPid3_PatientIdentifierList(0);
      ST id = pid03.getID();
      HD aa = pid03.getAssigningAuthority();
      logger.info(id);
      logger.info(aa.getNamespaceID());
      logger.info(aa.getUniversalID());

      // 取得病患姓名
      XPN pid05 = pid.getPid5_PatientName(0);
      logger.info(pid05.getXpn1_FamilyLastName().getFamilyName());
      logger.info(pid05.getXpn2_GivenName().getValue());
      logger.info(pid05.getXpn3_MiddleInitialOrName().getValue());

      // 取得病患出年月日
      TS pid07 = pid.getPid7_DateTimeOfBirth();
      logger.info(pid07.getTimeOfAnEvent());

      // 取得病患性別
      IS pid08 = pid.getPid8_Sex();
      logger.info(pid08);

      // 取得病患住址
      XAD pid11 = pid.getPid11_PatientAddress(0);
      logger.info(pid11.getComponent(0));
    } catch (DataTypeException e) {
      e.printStackTrace();
    }
  }
← Android - 基礎元件:各種 Button 比較 Android - Google Services - Maps →
 
comments powered by Disqus