Search code examples
javahl7hapihl7-v2

Create correct HL7 message containing RXO segment in Java


I am trying to create an HL7 message in Java and then print the resulting message. I am faking basic patient information and then adding the Drug Prescription information. Then, I want to print the complete message but I wasn't able to use the API correctly. I am new at using HL7, so I know I'm probably missing some required segments and even using the wrong ones, can you please help? This is my current code:

    public RXO runDrugPrescriptionEvent(CMSGeneric cmsgen) {

         CMSDrugPrescriptionEvent cmsic = (CMSDrugPrescriptionEvent) cmsgen;

         ADT_A28 adt23 = new ADT_A28();

         try {
             adt23.initQuickstart("ADT", "A08", cmsic.getPDE_EVENT_ID());

             // We set the sex identity (male or female)
             if (cmsic.getBENE_SEX_IDENT_CD() == 1) {
                 adt23.getPID().getSex().setValue("Male");
             }
             else {
                 adt23.getPID().getSex().setValue("Female");
             }

             // We set a fake name and family name
             adt23.getPID().insertPatientName(0).getGivenName().setValue("CMS Name " + MainTest.NEXT_PATIENT_ID);
             adt23.getPID().insertPatientName(0).getFamilyName().setValue("CMS Family name " + MainTest.NEXT_PATIENT_ID);
             MainTest.NEXT_PATIENT_ID++;

             RXO rxo = new RXO(adt23, new DefaultModelClassFactory());
             rxo.getRxo1_RequestedGiveCode().getCe1_Identifier().setValue("" + cmsic.getPDE_DRUG_CD());
             rxo.getRxo18_RequestedGiveStrength().setValue("" + cmsic.getPDE_DRUG_STR_CD());
             rxo.getRxo19_RequestedGiveStrengthUnits().getCe1_Identifier().setValue("" + cmsic.getPDE_DRUG_STR_UNITS());
             rxo.getRxo5_RequestedDosageForm().getCe1_Identifier().setValue("" + cmsic.getPDE_DRUG_DOSE_CD());

             rxo.getRxo11_RequestedDispenseAmount().setValue("" + cmsic.getPDE_DRUG_QTY_DIS());

             HapiContext context = new DefaultHapiContext();
             Parser parser = context.getPipeParser();
             String encodedMessage =  adt23.getParser().encode(rxo.getMessage());

             logger.debug("Printing Message:");
             logger.debug(encodedMessage);

             return rxo;
        } catch (IOException e) {
             System.out.println("IOException creating HL7 message. " + e.getMessage());
             e.printStackTrace();
         } catch (HL7Exception e) {
             System.out.println("HL7Exception creating HL7 message. " + e.getMessage());
             e.printStackTrace();
         } 

         return null;
     }     

With this code, the logger prints the following message:

MSH|^~\&|||||20160331101349.8+0100||ADT^A08|110001|PDE-00001E6FADAD3F57|2.3 PID|||||CMS Family name 100~^CMS Name 100|||Female

But I was expecting to see the RXO segment as well. How can I achieve that?


Solution

  • I found that changing the message type from ADT_A28 to ORP_O10 would let me have all the fields I need, as ADT_A28 wasn't the appropriate message for the kind of information I needed. There's a complete example on how to set a great amount of segments in this type of message here. Then, I was able to print the complete message using the PipeParser:

            HapiContext context = new DefaultHapiContext();
            Parser parser = context.getPipeParser();
            String encodedMessage =  parser.encode(msg);
            logger.debug("Printing EREncoded Message:");
            logger.debug(encodedMessage);