ballerina-platform / ballerina-library

The Ballerina Library
https://ballerina.io/learn/api-docs/ballerina/
Apache License 2.0
136 stars 58 forks source link

Can't access PID segment values of ballerinax/hl7v23:ORM_O01 message #6807

Open janihur opened 1 month ago

janihur commented 1 month ago

Description: Based on the ORM_O01 documentation, I can't figure out how to access the values of the PID segment. AFAICS the record access paths (e.g. inOrm.patient[0].pid.pid5[0].xpn1) are correct.

The MSH segment works as documented.

Steps to reproduce:

The program below outputs:

$ bal run test.bal
Compiling source
        test.bal

Running executable

message type: ORM^O01
PID.5 Patient Name:
  Family Name:
  Given Name:
  Middle Name:

When the following output is expected:

message type: ORM^O01
PID.5 Patient Name:
  Family Name: Lastname
  Given Name: Givenname
  Middle Name: Middlename

The program:

import ballerina/io;
import ballerinax/health.hl7v2;
import ballerinax/health.hl7v23;

string[] EXAMPLE_ARR = [
    string `MSH|^~\&|SENDING_APPLICATION|SENDING_FACILITY|RECEIVING_APPLICATION|RECEIVING_FACITILITY|20240718115723||ORM^O01|12345|P|2.3||||AL||8859/1`,
    string `PID|1|040404-0404^^^^SSN|||Lastname^Givenname^Middlename`,
    string `PV1|1||2039^Room 3^Bed 4^IMW||||||||||||||||||||||||||||||||||||9876^InternalMedicalWard|||||||||||1.2345.777.888`,
    string `PV2|1|||||||||||||||||1011|`,
    string `ORC|NW|20240718-1^FOO|||||^^^^^A||202407181157|||030303-0303^Smith^John^^^^^^^^^^SSN~987654^Smith^John^^^^^^^^^^SV`,
    string `OBR|1|20240718-1^FOO||4520^P-INR^LAB-KL-98|||202407201257||||L||info to sample taking||||||||||||||^^^^^A|||PORT`,
    string `NTE|1||Additional info to laboratory|`
];

public function main() returns error? {
    string EXAMPLE_STR = string:'join("\r", ...EXAMPLE_ARR);

    hl7v2:Message parsedMsg = check hl7v2:parse(EXAMPLE_STR);

    hl7v23:ORM_O01 inOrm = check parsedMsg.ensureType(hl7v23:ORM_O01);

    // https://central.ballerina.io/ballerinax/health.hl7v23/3.0.3#MSH
    io:println("message type: " + inOrm.msh.msh9.cm_msg1 + "^" + inOrm.msh.msh9.cm_msg2);

    // https://central.ballerina.io/ballerinax/health.hl7v23/3.0.3#ORM_O01
    io:println("PID.5 Patient Name:");
    // patient ORM_O01_PATIENT[]
    // pid PID
    // pid5 XPN[] - patient name
    // xpn1 ST - family name
    io:println("  Family Name: " + inOrm.patient[0].pid.pid5[0].xpn1);
    io:println("  Given Name: "  + inOrm.patient[0].pid.pid5[0].xpn2);
    io:println("  Middle Name: " + inOrm.patient[0].pid.pid5[0].xpn3);
}

Affected Versions:

ballerinax/health.hl7v23/3.0.3
$ bal --version
Ballerina 2201.9.2 (Swan Lake Update 9)
Language specification 2024R1
Update Tool 1.4.2
janihur commented 1 month ago

I also tried to access OBR-segment but that fails too in the same way than accessing PID-segment fails.

If I run the code with debugger I can see in debugger's local variables section that:

inOrm.pid[0].pid5[0].xpn1 = "Lastname"

With the knowledge from the debugging I made several trials that all just failed in a one way or other.

I'm also very confused with all those record types that seems to make no sense (or I just don't understand Ballerina enough).

isuruh15 commented 1 month ago

Looks like a bug in the hl7v23 parser.

Initial findings:

Conclusion

Need to investigate further and comeup with a fix

[1]https://github.com/ballerina-platform/module-ballerinax-health.hl7v2/blob/main/hl7v23/hl7_parser.bal#L103 [2]https://github.com/ballerina-platform/module-ballerinax-health.hl7v2/blob/main/hl7v23/message_orm_o01.bal#L36 [3]https://github.com/ballerina-platform/module-ballerinax-health.hl7v2/blob/main/hl7v23/hl7_parser.bal#L109 [4]https://github.com/ballerina-platform/module-ballerinax-health.hl7v2/blob/main/hl7v23/hl7_parser.bal#L135

sameeragunarathne commented 1 month ago

I also tried to access OBR-segment but that fails too in the same way than accessing PID-segment fails.

If I run the code with debugger I can see in debugger's local variables section that:

inOrm.pid[0].pid5[0].xpn1 = "Lastname"

With the knowledge from the debugging I made several trials that all just failed in a one way or other.

I'm also very confused with all those record types that seems to make no sense (or I just don't understand Ballerina enough). Hi @janihur this issue happens due to a parsing issue of the segment grouos. The mentioned segments in the issue "PID", "OBR" should reside on respective segment groups of the ORM message. Currently they are placed at the root level which is the errorneous behaviour. We are working on fixing this and release the packages soon. We'll update you on this ticket and the discord thread once done.

sameeragunarathne commented 1 month ago

Hi @janihur,

This issue has been fixed in the latest patch release. you should be able to access these segments(PV1, OBR) with the segment groups such as following,

    hl7v23:ORM_O01 inOrm = check parsedMsg.ensureType(hl7v23:ORM_O01);
    io:println(inOrm.patient[0].pid.pid1);
    io:println(inOrm.patient[0].orm_o01_patient_visit?.pv1?.pv11);
    io:println(inOrm.'order[0].orm_o01_order_detail?.orm_o01_order_detail_segment?.obr?.obr1);

Please let us know if you need further assistance or encounter any other issues.

Thanks,