0

I am trying to encode an HL7 message of the type ORU_R01 using the HAPI 2.0 library for an OpenMRS module. I have followed the tutorials given in the HAPI documentation and according to that, I have populated the required fields of the ORU_R01 message. Now, I want to post this message using the following link:

http://localhost:8080/openmrs/remotecommunication/postHl7.form

I am using the following message for testing:

MSH|^~\&|||||20140713154042||ORU^R01|20140713154042|P|2.5|1
PID|||1
OBR|1||1234^SensorReading|88304
OBX|0|NM|1||45
OBX|1|NM|2||34
OBX|2|NM|3||23

I have properly ensured that all the parameters are correct. Once I have posted the HL7 message, I start the HL7 task from the scheduler. Then I go to the admin page and click on "Manage HL7 errors" in order to see if the message arrives there. I get the following stack trace:

 ca.uhn.hl7v2.HL7Exception: HL7 encoding not supported
...


 Caused by: ca.uhn.hl7v2.parser.EncodingNotSupportedException: Can't parse message beginning MSH|^~\
 at ca.uhn.hl7v2.parser.Parser.parse(Parser.java:140)

The full stack trace is here: http://pastebin.com/ZnbFqfWC. I have written the following code to encode the HL7 message (using the HAPI library):

public String createHL7Message(int p_id, int concept_id[], String val[])
            throws HL7Exception {

    ORU_R01 message = new ORU_R01();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss",
            Locale.ENGLISH);
    MSH msh = message.getMSH();
    msh.getFieldSeparator().setValue("|");
    msh.getEncodingCharacters().setValue("^~\\&");
    msh.getProcessingID().getProcessingID().setValue("P");
    msh.getSequenceNumber().setValue("1");
    msh.getMessageType().getTriggerEvent().setValue("R01");
    msh.getMessageType().getMessageCode().setValue("ORU");
    msh.getVersionID().getVersionID().setValue("2.5");
    msh.getMessageControlID().setValue(
            sdf.format(Calendar.getInstance().getTime()));
    msh.getDateTimeOfMessage().getTime()
            .setValue(sdf.format(Calendar.getInstance().getTime()));


    ORU_R01_ORDER_OBSERVATION orderObservation = message
            .getPATIENT_RESULT().getORDER_OBSERVATION();
    ca.uhn.hl7v2.model.v25.segment.PID pid = message.getPATIENT_RESULT()
            .getPATIENT().getPID();

    Patient patient = (Patient) Context.getPatientService()
            .getPatient(p_id);
    System.out.println(String.valueOf(p_id) + "  " + patient.getGivenName()
            + "  " + patient.getFamilyName());

    pid.getPatientName(0).getFamilyName().getSurname()
            .setValue(patient.getFamilyName());
    pid.getPatientName(0).getGivenName().setValue(patient.getGivenName());
    pid.getPatientIdentifierList(0).getIDNumber()
            .setValue(String.valueOf(p_id));

    System.out.println();
    // Parser parser = new PipeParser();
    // String encodedMessage = null;
    // encodedMessage = parser.encode(message);
    // System.out.println(encodedMessage);

    // Populate the OBR
    OBR obr = orderObservation.getOBR();
    obr.getSetIDOBR().setValue("1");
    obr.getFillerOrderNumber().getEntityIdentifier().setValue("1234");
    obr.getFillerOrderNumber().getNamespaceID().setValue("SensorReading");
    obr.getUniversalServiceIdentifier().getIdentifier().setValue("88304");
    Varies value = null;
    // Varies value[] = new Varies[4];
    for (int i = 0; i < concept_id.length; i++) {
        ORU_R01_OBSERVATION observation = orderObservation
                .getOBSERVATION(i);
        OBX obx2 = observation.getOBX();
        obx2.getSetIDOBX().setValue(String.valueOf(i));
        obx2.getObservationIdentifier().getIdentifier()
                .setValue(String.valueOf(concept_id[i]));
        obx2.getValueType().setValue("NM");
        NM nm = new NM(message);
        nm.setValue(val[i]);

        value = obx2.getObservationValue(0);
        value.setData(nm);
    }
    Parser parser = new PipeParser();
    String encodedMessage = null;
    encodedMessage = parser.encode(message);
    return encodedMessage;

}

In all likelihood, something is wrong with the MSH segment of the message, but I cannot seem to figure out what it is. What can I do to correct this error?

Octavarium
  • 1
  • 1
  • 1

1 Answers1

1

Why do you declare the Encoding Characters using double backslashes?

msh.getEncodingCharacters().setValue("^~\\&");

Shouldn't it be:

msh.getEncodingCharacters().setValue("^~\&");

...and because your message is using the default encoding characters maybe you don't even need to declare them at all? Extract from HAPI MSH Class reference

getENCODINGCHARACTERS

public ST getENCODINGCHARACTERS()
Returns MSH-2: "ENCODING CHARACTERS" - creates it if necessary

Update I have no previous experience with HAPI. A quick google found an ORU example. Could you try initializing your MSH with initQuickstart("ORU", "R01", "P");

According to the comments in the example-code the initQuickstart method populates all of the mandatory fields in the MSH segment of the message, including the message type, the timestamp, and the control ID. (...and hopefully the default encoding chars as well :-)

Lars
  • 1,428
  • 1
  • 13
  • 22
  • If you write msh.getEncodingCharacters().setValue("^~\&");, you get the error "Invalid escape sequence". If you don't set the encoding characters, you get an HL7 exception telling you that the encoding characters have not been set, even if you are only using the default encoding characters. – Octavarium Jul 14 '14 at 08:55
  • @Octavarium I've updated my answer with additional information. Hope it helps. – Lars Aug 04 '14 at 12:01