Search code examples
javahapihl7-v2

Read Patient Information from HL7 Standard Message


For HL7(Health Level 7) I'm using Hapi

Write Operation :-

     ADT_A01 adt = new ADT_A01();
     adt.initQuickstart("ADT", "A01", "P");

      // Populate the MSH Segment
      MSH mshSegment = adt.getMSH();
      mshSegment.getSendingApplication().getNamespaceID().setValue("TestSendingSystem");
      mshSegment.getSequenceNumber().setValue("123");

      // Populate the PID Segment
      PID pid = adt.getPID(); 
      pid.getPatientName(0).getFamilyName().getSurname().setValue("Doe");
      pid.getPatientName(0).getGivenName().setValue("John");
      pid.getPatientIdentifierList(0).getID().setValue("123456");

      pid.getAdministrativeSex().setValue("M");

      pid.getPhoneNumberHome(0).getPhoneNumber().setValue("90000000000");

      pid.getPatientAddress(0).getStreetAddress().getStreetName().setValue("B201, Abc street");
      pid.getPatientAddress(0).getCity().setValue("Noida");
      pid.getPatientAddress(0).getStateOrProvince().setValue("UP");
      pid.getPatientAddress(0).getCountry().setValue("India");

      // Now, let's encode the message and look at the output
      HapiContext context = new DefaultHapiContext();
      Parser parser = context.getPipeParser();
      String encodedMessage = parser.encode(adt);
      System.out.println("Printing ER7 Encoded Message:");
      System.out.println(encodedMessage);

Ouput :

Printing ER7 Encoded Message: MSH|^~\&|TestSendingSystem||||20170227154106.754+0530||ADT^A01^ADT_A01|1301|P|2.4|123 PID|||123456||Doe^John|||M|||&B201, Abc street^^Noida^UP^^India||^^^^^^90000000000

Read Operation:

       HapiContext context2 = new DefaultHapiContext();
       Parser p = context2.getGenericParser();
       Message hapiMsg;

        try {
          hapiMsg = p.parse(encodedMessage);
        } catch (EncodingNotSupportedException e) {
          return;
        } catch (HL7Exception e) {
          return;
        }

      ADT_A01 adtMsg = (ADT_A01)hapiMsg;
      MSH msh = adtMsg.getMSH();

     // Retrieve some data from the MSH segment
     String msgType = msh.getMessageType().getMessageType().getValue();
     String msgTrigger = msh.getMessageType().getTriggerEvent().getValue();

     System.out.println("Decode : ");
     System.out.println(msgType + " " + msgTrigger);

Output:

Decode : ADT A01

How I get patient Information from this message. please suggest me !! Thanks


Solution

  • I Solved it !!!
    Here is my solution :

    PID pid1 = adtMsg.getPID();
    String id = pid.getPatientIdentifierList(0).getID().getValue();
    System.out.println("PID : "+id);
    String last = pid1.getPatientName(0).getFamilyName().getSurname().getValue();
    String first = pid1.getPatientName(0).getGivenName().getValue();
    System.out.println("Patient Name : "+first+" "+last);
    

    output

    Decode : 
    ------------------
    Message Type : ADT
    Message Trigger : A01
    PID : 123456
    Patient Name : John Doe