2

I'm getting started to Hl7 using HAPI API. I have an Hl7 test message (picked up from a real case) and I want to read the field of the various segment (MSH, PID, PV1, etc.) The problem is that the Parser do not split the message in segments, but put all the message in the MSH segment. I tried both the PipeParser and the GenericParser. The message is an ORU_R01 message, version 2.3.1 (I used the debug and the getVersion() method to get those information)

Here is the test class

    import java.io.IOException;

import ca.uhn.hl7v2.DefaultHapiContext;
import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.HapiContext;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v231.group.ORU_R01_PIDPD1NK1NTEPV1PV2;
import ca.uhn.hl7v2.model.v231.group.ORU_R01_PIDPD1NK1NTEPV1PV2ORCOBRNTEOBXNTECTI;
import ca.uhn.hl7v2.model.v231.message.ORU_R01;
import ca.uhn.hl7v2.model.v231.segment.MSH;
import ca.uhn.hl7v2.model.v231.segment.PID;
import ca.uhn.hl7v2.parser.Parser;

public class Hl7EncodeTester 
{
    public static void main (String[] args) throws HL7Exception, IOException
    {
        String msg = "MSH|^~\\&|REPO|BCS|THE|Theorema|201412170140||ORU^R01|20140012620415|P|2.3.1||||||||PID|||711697^^^^PI^PRIAMO~RSSMHL69B15B081W^^^^NN||ROSSINI^MICHELE||19690215|F|||VIA^POMPEI^6^ASSO^CO^22063||031700239|||M||RSSMHL69B15B081W|300CP102||||ASSO|||100|||||PV1|||265^INRCA Ospedale^^959^^^0310^^||||||||||||||||||||||||||||||||||||||||||||||||||OBR|1||2014047190|69^RADIOLOGIA||||201412162247||||||||||||0310692014047190||201412162247|||||||||||||||OBX|1|FT|20140001988995||REFERTO FORMATO P7M BASE64 ||||||F|||20141216||CFmedicorefertante^CognomeMedicoRefertante^NomeMedicoRefertante||||TXA|1||multipart|||20141216|||||CFmedicorefertante^^^^^^^^^^^^COMPILATORE||||2014047190||AU|||||||NTE|1|O||";
        HapiContext context = new DefaultHapiContext();
//      Parser p = context.getPipeParser();
        Parser p = context.getGenericParser();
        Message adt = p.parse(msg);
        ORU_R01 oruMsg = (ORU_R01) adt;
        System.out.println("ORU_RO1 message = " + oruMsg.encode());
        MSH msh = oruMsg.getMSH();
        System.out.println("MSH segment = " + msh.encode());
        ORU_R01_PIDPD1NK1NTEPV1PV2ORCOBRNTEOBXNTECTI segment1 = 
                            oruMsg.getPIDPD1NK1NTEPV1PV2ORCOBRNTEOBXNTECTI();
        ORU_R01_PIDPD1NK1NTEPV1PV2 segment2= segment1.getPIDPD1NK1NTEPV1PV2();
        PID pid = segment2.getPID();
        System.out.println("Patient Name = " + pid.getPatientName(0).getXpn2_GivenName().getValue());
    }
}

This is the console output:

ORU_RO1 message = MSH|^~\&|REPO|BCS|THE|Theorema|201412170140||ORU^R01|20140012620415|P|2.3.1||||||||PID|||711697^^^^PI^PRIAMO~RSSMHL69B15B081W^^^^NN||ROSSINI^MICHELE||19690215|F|||VIA^POMPEI^6^ASSO^CO^22063||031700239|||M||RSSMHL69B15B081W|300CP102||||ASSO|||100|||||PV1|||265^INRCA Ospedale^^959^^^0310||||||||||||||||||||||||||||||||||||||||||||||||||OBR|1||2014047190|69^RADIOLOGIA||||201412162247||||||||||||0310692014047190||201412162247|||||||||||||||OBX|1|FT|20140001988995||REFERTO FORMATO P7M BASE64 ||||||F|||20141216||CFmedicorefertante^CognomeMedicoRefertante^NomeMedicoRefertante||||TXA|1||multipart|||20141216|||||CFmedicorefertante^^^^^^^^^^^^COMPILATORE||||2014047190||AU|||||||NTE|1|O

MSH segment = MSH|^~\&|REPO|BCS|THE|Theorema|201412170140||ORU^R01|20140012620415|P|2.3.1||||||||PID|||711697^^^^PI^PRIAMO~RSSMHL69B15B081W^^^^NN||ROSSINI^MICHELE||19690215|F|||VIA^POMPEI^6^ASSO^CO^22063||031700239|||M||RSSMHL69B15B081W|300CP102||||ASSO|||100|||||PV1|||265^INRCA Ospedale^^959^^^0310||||||||||||||||||||||||||||||||||||||||||||||||||OBR|1||2014047190|69^RADIOLOGIA||||201412162247||||||||||||0310692014047190||201412162247|||||||||||||||OBX|1|FT|20140001988995||REFERTO FORMATO P7M BASE64 ||||||F|||20141216||CFmedicorefertante^CognomeMedicoRefertante^NomeMedicoRefertante||||TXA|1||multipart|||20141216|||||CFmedicorefertante^^^^^^^^^^^^COMPILATORE||||2014047190||AU|||||||NTE|1|O
Patient Name = null

The whole message and the MSH segment I obtained are the same, I cannot split the message in segments (there are supposed to be: MSH, PID, PV1, OBR, OBX, TXA and NTE) Someone knows a solution to my problem?

Luca89
  • 99
  • 1
  • 4
  • 14

1 Answers1

3

There's no segment terminator in your message. So the parser can't find it. Note that this terminator is binary and hard-coded in the standard

HL7 Messaging Standard Version 2.5 → Control → Message Framework → 2.5.4 Message delimiters

...The segment terminator is always a carriage return (in ASCII, a hex 0D). The other delimiters are defined in the MSH segment, with the field delimiter in the 4th character position...

See also:

Community
  • 1
  • 1
xmojmr
  • 8,073
  • 5
  • 31
  • 54
  • Thank you for the reply. So is the message wrong? This is true even with a ORU message version 2.3.1? – Luca89 Mar 27 '15 at 13:45
  • 1
    @Luca89 yes, your test string message is missing the `\r` segment terminators. The basic encoding scheme for `hl7 v2.x` did not change since decades(?) ago. If you'd load the test message from a **binary** file containing the real message or if you'd receive the messages through the `MLLP` protocol from some testing engine, the parser should behave correctly – xmojmr Mar 27 '15 at 14:42
  • Ok thank you. In this test case I use a hard-coded String, but if add the \r manually (for example, before the |PID|) it throws EncodingNotSupportedException. I have to test with loading the test message from a binary file – Luca89 Mar 27 '15 at 15:32
  • 1
    @Luca89 `PID` is segment name. It's 1st thing in the segment. The hard-coded string should go like "`...\rPID...`". `hl7 v2.x` is binary protocol. It's quite important to get it right and standards compliant, if you're going to use your code outside your testing sandbox. Test against several reference implementations. Some of the important tools that I've added to my tool-belt when working with `hl7` are listed in question [Need sample message for HL7 V2.7](http://stackoverflow.com/a/25235685/2626313) – xmojmr Mar 27 '15 at 15:43