Daily using/supporting

Get Firefox browser!
Get Thunderbird!
Get Opera browser!
Get The Gimp!
Get Inkscape!
Get LibreOffice!
Get Videolan!
Get Linux!
Get Mandriva!
Get Joomla!
Hacker Emblem

Archives

Which topics would you like us to cover more?

Latest comments

Latest tweets

about 1 day ago Using REDIPS.drag to add drag and drop to your .Net webapplication #li #dib0 http://t.co/n8zY3s7d
about 7 days ago http://t.co/cknQcDbo #Kindle
about 15 days ago Freedom isn't the ability to choose what to do or say, but the ability to choose what not to do or say #freedom
about 29 days ago http://t.co/61KTQknI #Kindle
12 Apr 2012 Force the use of a networking adapter using C# #li #dib0 http://t.co/ZTJOPzOz
9 Apr 2012 Mandriva 2010.2 and USB devices in Virtualbox http://t.co/fwq9gbHB
9 Apr 2012 Execute a http request to you own site with PHP http://t.co/DIvWPrpd
Home Architecture, security and coding Creating a HL7 Ack/Nack message in .Net using nHapi
Creating a HL7 Ack/Nack message in .Net using nHapi
Written by Division by Zero   
Wednesday, 16 December 2009 10:14
While I was working on a HL7 implementation using nHapi (http://nhapi.sourceforge.net/home.php). I needed to create an acknowledge message. After a bit of trying and a lot of searching on the internet if found a java implementation (http://sites.google.com/site/rogersearjeant/healthcare-and-hl7/hapi-ack-nak-message-helper). Based on this java implementation I created a .Net version.

/// 
/// Create an Ack message based on a received message
/// 
///
 
 received message
///  Acknowlegde code
///  Message to be created
/// Created message
public static IMessage MakeACK(IMessage inboundMessage, string ackCode, IMessage ackMessage)
{
 Terser t = new Terser(inboundMessage);
 ISegment inboundHeader = null;
 try
 {
 inboundHeader = t.getSegment("MSH");
 }
 catch (NHapi.Base.HL7Exception)
 {
 throw new NHapi.Base.HL7Exception("Need an MSH segment to create a response ACK");
 }
 return MakeACK(inboundHeader, ackCode, ackMessage);
}

/// 
/// Create an Ack message based on a received message
/// 
/// received message
/// Acknowlegde code
/// Message to be created
/// Created message
public static IMessage MakeACK(ISegment inboundHeader, string ackCode, IMessage ackMessage)
{
 if (!inboundHeader.GetStructureName().Equals("MSH"))
 throw new NHapi.Base.HL7Exception(
 "Need an MSH segment to create a response ACK (got " + inboundHeader.GetStructureName() + ")");

 // Find the HL7 version of the inbound message:
 string version = null;
 try
 {
 version = Terser.Get(inboundHeader, 12, 0, 1, 1);
 }
 catch (NHapi.Base.HL7Exception)
 {
 // I'm not happy to proceed if we can't identify the inbound
 // message version.
 throw new NHapi.Base.HL7Exception("Failed to get valid HL7 version from inbound MSH-12-1");
 }

 // Create a Terser instance for the outbound message (the ACK).
 Terser terser = new Terser(ackMessage);

 // Populate outbound MSH fields using data from inbound message
 ISegment outHeader = (ISegment)terser.getSegment("MSH");
 DeepCopy.copy(inboundHeader, outHeader);

 // Now set the message type, HL7 version number, acknowledgement code
 // and message control ID fields:
 string sendingApp = terser.Get("/MSH-3");
 string sendingEnv = terser.Get("/MSH-4");
 terser.Set("/MSH-3", CommunicationNameOfThisApplication);
 terser.Set("/MSH-4", EnvironmentIdentifierOfThisApplication);
 terser.Set("/MSH-5", SendingApp);
 terser.Set("/MSH-6", sendingEnv);
 terser.Set("/MSH-7", DateTime.Now.ToString("yyyyMMddmmhh"));
 terser.Set("/MSH-9", "ACK");
 terser.Set("/MSH-12", version);
 // The ackCode should be "AA" if the message was correctly handled and "AE" if there was an error
 terser.Set("/MSA-1", ackCode == null ? "AA" : ackCode);
 terser.Set("/MSA-2", Terser.Get(inboundHeader, 10, 0, 1, 1));

 return ackMessage;
}

 

Let me know if this is helpful or if you have any more comments or questions on the matter. You can use the comments on this article or the forum.

 

Comments  

 
0 # Bas Groeneveld 2010-12-14 03:47
How do you get access to the Terser object in VS C#. What reference/using clause needs to be added?

Thanks
Bas
Reply | Reply with quote | Quote
 
 
0 # Bas 2010-12-14 09:20
Hi Bas,

Thank you for your question. I realize that I omitted this in this post. The Terser is located in the namespace NHapi.Base.Util (from the NHapi.Base assembly).

In another post I added an example solution. The reference to the Terser is in the /nHapiExampleApp lication/Service/HL7MessageHelpe r.cs directory from the zip-file I added. You can find it here: dib0.nl/.../132-nhapi-example.
Reply | Reply with quote | Quote
 
 
0 # Sean 2011-03-30 23:04
Can you guys point me to any other general examples on using nHapi? I tried to follow the link (dib0.nl/.../132-nhapi-example) but the example apparently is no longer located there.

Thanks,


Sean
Reply | Reply with quote | Quote
 
 
0 # Bas 2011-03-31 09:12
Hi Sean,

Thank you for your comment. I'm quite curious which problem you ran into. The link seems to work fine. If you could tell me, I can try to fix it.

Maybe this post (dib0.nl/.../...) will help you. There are some explanations, some example code and a link to a example solution to get you going (dib0.nl/.../...).

If you have any questions on HL7 and nHapi, feel free to ask.
Reply | Reply with quote | Quote
 
 
0 # Chatir 2011-08-31 16:35
Hi guys,

First i whould to Thank you for the examples.
But i have a probleme with the ACK message:
how to include the ERR segment in the ACK message?

Thank's.
Reply | Reply with quote | Quote
 
 
+1 # Bas 2011-08-31 19:34
Hi Chatir,

You're welcome! It's good to hear that my examples are usefull! :-)

To add an ERR segment to an ACK message you'll need to get the ErrorCodeAndLoc ation datatype (ELD datatype).

Let's say you have an message object like this (the MakeACK code is shown in the post above):
NHapi.Model.V24.Message.ACK message = (ACK)MakeACK(ms g, "AE", message);

The string "AE" designates an NACK. Now you want to add an ERR Segment. You can do this with the following code:
message.ERR.GetErrorCodeAnd Loc ation(0).CodeIdentifying Error.AlternateIdenti fier.Value = "1";
message.ERR.GetErrorCodeAnd Loc ation(0).CodeIdentifying Error.AlternateText.Value = "Something went wrong.";

A shorter notation would be:
NHapi.Model.V24.Datatype.ELD eld = message.ERR.GetErrorCodeAnd Loc ation(0);
eld.CodeIdentifying Error.AlternateIdenti fier.Value = "1";
eld.CodeIdentifying Error.AlternateText.Value = "Something went wrong.";

In this example I only added one ELD datatype. If you want to add more than one ELD element, just increase the repetition value you give as an argument to GetErrorCodeAnd Loc ation.

So the following code will create a second ELD element:
NHapi.Model.V24.Datatype.ELD eld = message.ERR.GetErrorCodeAnd Loc ation(1);
eld.CodeIdentifying Error.AlternateIdenti fier.Value = "1";
eld.CodeIdentifying Error.AlternateText.Value = "Something went wrong.";

Does this help you?
Reply | Reply with quote | Quote
 
 
0 # Patrick 2011-11-10 12:11
Thank you for the samples. This sounds really dumb, but now I can't work out for the life of me, how to output the ack message to a string...

It should be something really simple, but I just can't work it out.
Reply | Reply with quote | Quote
 
 
0 # Bas 2011-11-10 12:50
Hi Patrick,

You're welcome! I'm glad to hear the the examples are helpful!

An Ack message is (as all nHapi messages) of the base type IMessage. To output this to a string is easy with the parses provided by nHapi:

IMessage message = GetAckMessage() ; // this method will return an HL7 Ack message
PipeParser parser = new PipeParser();
string result = parser.Encode(message) ;
Reply | Reply with quote | Quote
 
 
0 # Phil 2012-01-11 22:03
I am getting this error message:
System.TypeInitializat ionException
{"The type initializer for 'NHapi.Base.EventMapper' threw an exception."}
at this point in the HL7 code

I am using the nHapi in a WPF dashboard app. what is strange to me is that I have run a test project using the same data and it works. I am reading files in a directory (where we store the 'failed trsnsmission' files.) maybe you have seent his before or have some idea?

// here is our problem.
catch (System.Exception e)
{
throw new HL7Exception("Couldn't create Message object of type " + theName, HL7Exception.UNSUPPORTED_MES SAGE_TYPE, e);
}

result.ValidationConte xt = _validationCont e xt;

return result;
}

w
Reply | Reply with quote | Quote
 
 
0 # Bas 2012-01-13 14:55
Hi!

I've seen this error before. To determine the right class to load and to parse the message, nHapi uses reflection.

So nHapi takes the event definition in the MSH segment, loads the right assembly (with the HL7 version) and tries to initialize the class with the same name as the event.

If the class doesn't exist (an invalid event name is given in the MSH segment) this error will be thrown.

I hope this answer will help you!
Reply | Reply with quote | Quote
 
 
0 # Arvid 2012-01-24 14:49
Hi,
I'm currently having problem with a system that sends HL7 messages to my integration node containing linefeeds in the middle of a text. This of course gives HAPI problems when trying to parse the message.
The problem is that I cannot send a NACK since I cannot parse out the MSH segment. Therefore the sender will timeout waiting for the ACK and then resend the same message again, effectively blocking his own outbound queue.

Is there anyway to deal with this, or should I just blame the sending system?

/Arvid
Reply | Reply with quote | Quote
 
 
0 # Bas 2012-01-24 15:32
Hi Arvid,

It depends on how the sending system is sending the linefeeds. Of course you can solve the problem on your side by filtering the linefeeds, but if the system is sending the hex value of the linefeed (10 or 13) there is a possiblity to do encoding. The sending application can send these hexadecimal values like this: X000d (ASCII 13). nHapi will interpret this as a linefeed.

Linefeeds are only allowed in text blocks. Is this other system sending them in the right places?

Maybe this link will help you:
dib0.nl/.../...
Reply | Reply with quote | Quote
 

Add comment


Security code
Refresh

Its name is Public Opinion. It is held in reverence. It settles everything. Some think it is the voice of God. - Mark Twain


© 2009 - 2012, Division by Zero

Template based on the empire template by joomlashack 

Valid XHTML 1.0 Strict  Valid CSS!  Creative Commons License
This work by Division by Zero is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Netherlands License.