Search code examples
hl7hapihl7-v2terser

Unable to get OBR segment from HL7 REF^I12 message


I was working on HL7 (2.3.1) message of REF^I12 type. I'm trying to get the OBR segment from the message by using terser, but, I'm getting null whenever I try to get OBR or OBX segment values. Below is the hl7 message I'm trying to parse-

MSH|^~\&|Sample test hospital|TEST|||20191105154020||REF^I12|178038310|P|2.3.1
PID|1||179790^^^test||infection3^Test||19881128|F|||29 Mousehole Cresent^^Yanchep^WA^6035|||
PV1|1|O|29677|||||873250^Cailes^Jeremy^^^^^^test|muddu^Aung^Htun^TEST^^^^^
OBR|1|0175671960^test|0175671960^test|test123 Letter^Letter^testletter||20180725|20180719||20180725|||||||^Cailes^Jeremy^^^^^^testletter||||||||||||sunil^Shizaraj^Suma^(testpartner)^^^^^test~X0012622^Hess^Sally^(OPA)^^^^^test~I96766753^Doctor 1^Mail^^^^^^test~X20180713013100^Doctor 2^Mail^Business Name^^^^^test~FAX356^Doctor 3^Fax^(FAX)^^^^^test~||||||||20180719
OBX|1|FT|OLETTER^^test||Sample test hospital||||||F

I'm actually using terser to parse and get the value from OBR-28-1 segment. But my code is always returning null whenever I try to get the value. Below is the snippet of the code:

public void getReceipientFromOBRTest(Message messageobject) throws Exception {
        String provider;
        String id;
        Map<String, String> map = new HashMap<>();
        Terser terser = new Terser(messageObject);
        for (int i = 0; i <= 10; i++) {
            provider = terser.get("/.OBR-28(" + i + ")-4");
            id = terser.get("/.OBR-28(" + i + ")-1");
            if (provider != null && id != null) {
                map.put(id, provider);
            }
        }
        if (map.values().isEmpty()) {
            System.out.println(map);
        }
    }

When I remove the PV1 segment from the message, I am able to get the OBR segment value. But, if PV1 segment is present, terser is unable to recognise the OBR or even OBX segment. The HL7 file seems to be valid when I parsed it through online parser.

How to get the OBR segment from the message? Is there something I'm doing wrong? Would love it if anyone can help me out here.


Solution

  • I was able to get the OBR segment values and below is the working code:

    public void getReceipientFromOBRTest(Message messageobject) throws Exception {
            String provider;
            String id;
            Map<String, String> map = new HashMap<>();
            Terser terser = new Terser(messageObject);
            for (int i = 0; i <= 10; i++) {
                provider = terser.get("/.PV1PV2/OBR-28(" + i + ")-4");
                id = terser.get("/.PV1PV2/OBR-28(" + i + ")-1");
                if (provider != null && id != null) {
                    map.put(id, provider);
                }
            }
            if (map.values().isEmpty()) {
                System.out.println(map);
            }
        }