Search code examples
javaparsingmessagesegmenthapi

How to parse DFT_P03 message with ZPM segment


I am coding a server application that will receive DFT_P03 messages with an added ZPM segment (which i have created a class for as per the HAPI documentation). Currently i am able to access this field as a generic segment when doing the following :

@Override
public Message processMessage(Message t, Map map) throws ReceivingApplicationException, HL7Exception 
{
    String encodedMessage = new DefaultHapiContext().getPipeParser().encode(t);

    logEntryService.logDebug(LogEntry.CONNECTIVITY, "Received message:\n" + encodedMessage + "\n\n");

    try 
    {
        InboundMessage inboundMessage = new InboundMessage();
        inboundMessage.setMessageTime(new Date());
        inboundMessage.setMessageType("Usage");

        DFT_P03 usageMessage = (DFT_P03) t;
        Segment ZPMSegment = (Segment)usageMessage.get("ZPM");

        inboundMessage.setMessage(usageMessage.toString());
        Facility facility = facilityService.findByCode(usageMessage.getMSH().getReceivingFacility().getNamespaceID().getValue());
        inboundMessage.setTargetFacility(facility);
        String controlID = usageMessage.getMSH().getMessageControlID().encode();
        controlID = controlID.substring(controlID.indexOf("^") + 1, controlID.length());
        inboundMessage.setControlId(controlID);

        Message response;

        try
        {
            inboundMessageService.save(inboundMessage);
            response = t.generateACK();
            logEntryService.logDebug(LogEntry.CONNECTIVITY, "Message ACKed");
        }
        catch (Exception ex)
        {
            response = t.generateACK(AcknowledgmentCode.AE, new HL7Exception(ex));
            logEntryService.logDebug(LogEntry.CONNECTIVITY, "Message NACKed");
        }

        return response;
    } 
    catch (IOException e) 
    {
        logEntryService.logDebug(LogEntry.CONNECTIVITY, "Message rejected");

        throw new HL7Exception(e);
    }
}

I have created a DFT_P03_Custom class as following :

public class DFT_P03_Custom extends DFT_P03
{
    public DFT_P03_Custom() throws HL7Exception
    {
        this(new DefaultModelClassFactory());
    }

    public DFT_P03_Custom(ModelClassFactory factory) throws HL7Exception
    {
        super(factory);
        String[] segmentNames = getNames();
        int indexOfPid = Arrays.asList(segmentNames).indexOf("FT1");
        int index = indexOfPid + 1;
        Class<ZPM> type = ZPM.class;
        boolean required = true;
        boolean repeating = false;
        this.add(type, required, repeating, index);
    }

    public ZPM getZPM()
    {
        return getTyped("ZPM", ZPM.class);
    }
}

When trying to typecast the message to a DFT_P03_Custom instance i get a ClassCastException. As per their documentation, i did create the CustomModelClassFactory class but using this i just get tons of validation errors on the controlId field.

I am already using an identical logic to send custom MFN_M01 messages with an added ZFX segment and that works flawlessly. I understand there is some automatic typecasting being done by HAPI when it receives a DFT_P03 message and that is likely what i need to somehow override for it to be able to give me a DFT_P03_Custom instance instead.

If you have some insight on how i can achieve this without having to use a generic segment instance please help!

Thank you!


Solution

  • I finally figured this out. The only way i got this to work was to generate a conformance profile XML file (using an example message from our application as a base) with the messaging workbench on the HAPI site and use the maven plugin to generate the message and segment classes. Only with these classes am i able to correctly parse a message to my custom class. One thing to note is that it DOES NOT work if i try to use the MSH, PID, PV1 or FT1 classes provided by HAPI and use my Z-segment class. It only works if all the segments are the classes generated by the conformance plugin. This combined with a CustomModelClassFactory class (as shown on the HAPI website) and the proper package structure finally allowed me to access my Z-segment.