Reputation: 5746
I have this ugly code below for hl7 message seen here. The code runs for varied versions of HL7 listeners. For one system I receive in v2.3 but for the other one I get in v2.5
As you can notice the job is almost same for all versions. But the base of ACK is message
and I can't call getMsa1_AcknowledgementCode
method without explicitly casting to exact HL7 version,and there isn't a common ACK
class among them. By this dummy diagram I tried to present the problem. (I know there are some other elements ,such as AbstractMessage,Group...)
And for first two the method name is getMsa1_AcknowledgementCode
but for v2.5 it has missing e
letter and seems like getMsa1_AcknowledgmentCode
.
Should I use reflection and find a method starts with the name getMsa_Ack...
to check an Acknowledge message or is there any type safe and beautiful way for this purpose in hapi project?
Do not constrain yourself for ACK message while answering I wonder a better way for others as well.
MSH|^~\&|Vendor|VandorApp|Receiver|RCApp|201504241154||ACK|187718704|T|2.3|||AL|AL|TR
MSA|AA|187718704
String result="AA";
ca.uhn.hl7v2.model.Message hl7 = initiator.sendAndReceive(msg);
if(hl7 instanceof ca.uhn.hl7v2.model.v231.message.ACK)
{
ca.uhn.hl7v2.model.v231.datatype.ID id= ((ca.uhn.hl7v2.model.v231.message.ACK)hl7).getMSA().getMsa1_AcknowledgementCode();
result=id.getValue();
}else if (hl7 instanceof ca.uhn.hl7v2.model.v23.message.ACK)
{
ca.uhn.hl7v2.model.v23.datatype.ID id= ((ca.uhn.hl7v2.model.v23.message.ACK)hl7).getMSA().getMsa1_AcknowledgementCode();
result=id.getValue();
}else if( hl7 instanceof ca.uhn.hl7v2.model.v25.message.ACK)
{
ca.uhn.hl7v2.model.v25.datatype.ID id= ((ca.uhn.hl7v2.model.v25.message.ACK)hl7).getMSA().getMsa1_AcknowledgmentCode();
result=id.getValue();
}
Upvotes: 1
Views: 1633
Reputation: 2318
If you want to just get the ack code (or any other field) while ignoring all the issues with HAPI's object model, you can use the Terser
public String getAckCode(Message message) throws HL7Exception {
Segment msaSegment = (Segment)message.get("MSA");
return Terser.get(msaSegment, 1, 0, 1, 1);
}
Upvotes: 0
Reputation: 86
With C#, using nHAPI, I would do something similar to the following
PipeParser parser = new PipeParser();
IMessage hl7Message = parser.Parse(hl7);
IStructure msa = hl7Message.GetStructure("MSA");
IType ackCode = ((ISegment)msa).GetField(1)[0];
MessageBox.Show(((AbstractPrimitive)ackCode).Value);
You have to use a lot of the base structures, and I have assumed a number of the castings - as we are looking for a specific field (that we know are defined the same in the HL7 specs).
Upvotes: 0