HTTP sender in Javascript

This blog post is about developing HTTP sender in Mirth, without having to send your data via destination.

Suppose, consider a situation when we need to loop through the incoming message and we have to send/post the message multiple time. Mirth do not have the capability to store the loop content variable and pass that to the destination end. So, if you want to loop through the incoming message and send them to HTTP sender, we need to write the Javascript code in the source transformer.

Imagine, that “output” is the variable that contains the looped contents of information in HL7v2 format, now use the below code to invoke HTTTP sender via javascript.

do {
try {
var data = output;
//Destination URL
destURL = ‘http://localhost:8088/’;
//URL
var url = new java.net.URL(destURL);
var conn = url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod(“POST”);
conn.setRequestProperty(“Content-length”, data.length());
conn.setRequestProperty(“Content-type”, “text/plain”);
var outStream = conn.getOutputStream();
var outWriter = new java.io.OutputStreamWriter(outStream);
outWriter.write(data);
outWriter.close();
// Get response Code (200, 500 etc.)
var respCode = conn.getResponseCode();
// get response body
//getResponse();
var response = new XML($(‘RESPONSE’)); // XML response message
var status = response[‘Status’].toString();

if (respCode != 200) {
// Write error to error folder
var stringData = response.toString() + ‘\n’;
FileUtil.write(‘C:/Outbox/Errors/’ + $(‘originalFilename’) + ‘.ERROR_RESPONSE’, false, stringData);
// Return Error to Mirth to move the file to the error folder
return ERROR;
}
errorCond = ‘false’;
break;
}
catch(err) {
retry++;
if(retry > 10) {
channelMap.put(‘RESPONSE’, err);
responseMap.put(‘WEBSVC’, ResponseFactory.getErrorResponse(err))
throw(err);
// Can return ERROR, QUEUED, SENT
// This re-queues the message on a fatal error. I’m doing this since any fatal message may be
// caused by HTTPS connect errors etc. The message will be re-queued
return QUEUED; // Re-queue the message
}
java.lang.Thread.sleep(6000); // 6 seconds * 10

errorCond = ‘true’;
}
}
while (errorCond == ‘true’);

Note : This code should be present inside the for loop or the while loop, so that the specific HL7v2 message will be sent/posted to the destination

 

 

Parse SOAP response & send HL7V2 ADT

This channel will explain how to parse the SOAP response which will be received from the webservice endpoint which will be a SOAP XML.

Mirth will fetch this XML, parse the SOAP data and convert it into HL7V2 ADT message and send it to the sender.

Mirth : 3.2.1.7650

Have the below HL7v2 available in the POSTMAN of your system. This message will be received by the Mirth as HTTP listener.

MSH|^~\&|TEST|DH|ADT|DH|201301011226||ADT^A08|HG201001|P|2.3|EVN|A08|201301011223||PID|||MRN04160^7^M12||TEST^JACK^R^||19900903|M||2106-3|No5 AskVAsk Street^^Beta^WH^11136-1020|US|(414)222-9595^^^AxeData@test.com|(414)123-4597|EN|S||MRN27485987^2^M10|986410430|978974^NC||HIN1|Medicare|CD430677||1|||||||IN2|Medicaid|B025546788||1|||||||PD1|02997173|Data|Data2^20161011^20161111^Enrolled^Other||||02693680|Y|Enrolled|||

We will transform this data to the webservice specified format (XML), and the webservice will send a response like the below:

<S:Envelope xmlns:S=”http://test.xmlsoap.org/soap/envelope/&#8221; xmlns:SOAP-ENV=”http://test.xmlsoap.org/soap/envelope/”&gt;
<SOAP-ENV:Header/>
<S:Body>
<ns1:addInformation xmlns:ns2=”http://Getdatawebservice/”&gt;
<send>
<MrnNumber>MRN5023150</MrnNumber>
<isError>True</isError>
<ErrorReason>Patient deceased</ErrorReason>
</send>
</ns1:addInformation>
</S:Body>
</S:Envelope>

Now, we wanted to take the error reason and send it to the sender as HL7V2 response. To do that, go the the Edit Response tab of this destination, and type as below:

var updateMsg= new XML (msg).toString().replace(/:/g,”)
var newMsg= new XML (updateMsg);

var checkError = newmsg[‘SBody’][‘ns1addInformation ‘][‘send’][‘isError’].toString();
if(checkError==”false”)
{
var errorMessage = “Message Sent Successfully”;
channelMap.put(“errorMessage”,errorMessage);
}else{
var errorMessage = newmsg[‘SBody’][‘ns1addInformation ‘][‘send’][‘ErrorReason’].toString();
channelMap.put(“errorMessage”,errorMessage);
}

Now create another destination and create a MSA segment in the outbound template as follows:

MSA|||||||

Inside the transformer do the following:

tmp[‘MSH’] = $(‘MSH’);
tmp[‘MSA’][‘MSA.2’][‘MSA.2.1’] = $(‘errorMessage’);

Make the destination as Javascript Writer and copy the following code in the code area part

return connectorMessage.getEncodedData();

Deploy the channel, and send the message from the postman and you will get the response like this

MSH|^~\&|TEST|DH|ADT|DH|20141125094031||ADT^A08|c6e86566-41c3-468c-8b35-0573cee6f2e0|T|2.6
MSA||Patient deceased|||

 

Happy Integrating !!!!!!!!!!!

Bi-directional Channel (JSON-HL7V2):

Let’s create a channel in Mirth that will consume the incoming JSON message and convert that into an equivalent HL7v2 message in Mirth (i.e) we are converting JSON format of message to the pipe-delimiter format message with Mirth interoperability.

Pre-requisite:

  • MirthConnect : 3.4.1.8057 (latest version)

Channel setup :

  1. Create a New channel name it as your wish Here I have named JSON to HL7V2
  2. Click on Set Data Types button set the source connector inbound to JSON
  3. Outbound and Destination 1 to HL7V2.x
     – that’s it the channel setup is done for it.

Source Transformer :

  • Go to the source transformer and right click to create a new step
  • Name the step as JSONto HL7V2 
  • Select the drop-down of  the step that you have created and select Javascript

Code : 

//Get the incoming JSON Data

var input = JSON.parse(connectorMessage.getRawData());

// parse MSH content
var patienId = input.header.patient_id;
var patientName = input.header.patient_name;
var idCard = input.header.id;
var messagetType = input.header.type;
var subType = input.header.subType;
var ctrlNum = input.header.controlNumber;
var pId = input.header.processingId;
var version = input.header.version;

//parse patientInfo
var internalId = input.patientInfo.internalId;
var firstName = input.patientInfo.nameDetails.firstName;
var lastName = input.patientInfo.nameDetails.lastName;
var initial = input.patientInfo.nameDetails.initial;
var suffix = input.patientInfo.nameDetails.suffix;
var motherName = input.patientInfo.motherName;
var DateOfBirth = input.patientInfo.DOB;
var gender = input.patientInfo.sex;
var race = input.patientInfo.race;
var status = input.patientInfo.maritalStatus;
var religion = input.patientInfo.Religion;
var ssn = input.patientInfo.SSN;
var birthPlace = input.patientInfo.birthPlace;
var orderOfBirth = input.patientInfo.birthOrder;
var citizenship =input.patientInfo.citizenship;

//NextOfKin Details
var nk1Len=msg[‘kinDetails’].length;
for(var nk1Counter=0;nk1Counter<nk1Len;nk1Counter++)
{
var kinFirstName=input.kinDetails[nk1Counter].firstName;
var kinLastName=input.kinDetails[nk1Counter].lastName;
var relationShip=input.kinDetails[nk1Counter].relationship;
var phNumber=input.kinDetails[nk1Counter].phoneNumber;
var contactRole=input.kinDetails[nk1Counter].contactRole;
}

//Equal msg=tmp
msg=tmp;

//Generate HL7v2 Message
//MSH
createSegment(‘MSH’, msg);
msg[‘MSH’][‘MSH.1’]= “|”;
msg[‘MSH’][‘MSH.2’]= “^~\\&”;
msg[‘MSH’][‘MSH.3’]= patienId;
msg[‘MSH’][‘MSH.4’]= patientName;
msg[‘MSH’][‘MSH.5’]= idCard;
msg[‘MSH’][‘MSH.9’][‘MSH.9.1’]=messagetType;
msg[‘MSH’][‘MSH.10’][‘MSH.10.1’]=ctrlNum;
msg[‘MSH’][‘MSH.11’][‘MSH.11.1’]=pId;
msg[‘MSH’][‘MSH.12’][‘MSH.12.1’]=version;

//PID
createSegment(‘PID’, msg);
msg[‘PID’][‘PID.3’][‘PID.3.1’]=internalId;
msg[‘PID’][‘PID.5’][‘PID.5.1’]=firstName;
msg[‘PID’][‘PID.5’][‘PID.5.2’]=lastName;
msg[‘PID’][‘PID.5’][‘PID.5.3’]=initial;
msg[‘PID’][‘PID.5’][‘PID.5.4’]=suffix;
msg[‘PID’][‘PID.6’][‘PID.6.1’]=motherName;
msg[‘PID’][‘PID.7’][‘PID.7.1’]=DateOfBirth;
msg[‘PID’][‘PID.8’][‘PID.8.1’]=gender;
msg[‘PID’][‘PID.10’][‘PID.10.1’]=race;
msg[‘PID’][‘PID.16’][‘PID.16.1’]=status;
msg[‘PID’][‘PID.17’][‘PID.17.1’]=religion;
msg[‘PID’][‘PID.19’][‘PID.19.1’]=ssn;
msg[‘PID’][‘PID.23’][‘PID.23.1’]=birthPlace;
msg[‘PID’][‘PID.25’][‘PID.25.1’]=orderOfBirth;
msg[‘PID’][‘PID.26’][‘PID.26.1’]=citizenship;

//NK1
var nk1Counter=0;
var NK1Segment = createSegment(‘NK1’, msg);
msg[‘NK1’][nk1Counter][‘NK1.2’][‘NK1.2.1’]=kinFirstName
msg[‘NK1’][nk1Counter][‘NK1.2’][‘NK1.2.2’]=kinLastName;
msg[‘NK1’][nk1Counter][‘NK1.3’][‘NK1.3.1’]=relationShip;
msg[‘NK1’][nk1Counter][‘NK1.5’][‘NK1.5.1’]=phNumber;
msg[‘NK1’][nk1Counter][‘NK1.7’][‘NK1.7.1’]=contactRole;
nk1Counter++;

//Convert Mirth based XMl to HL7v2
var output = SerializerFactory.getSerializer(‘HL7V2’).fromXML(msg);
logger.info(“output : “+output);

Consider you are sending an JSON message like this provided below:

{
“header”: {
“patient_id”: “XXXX”,
“patient_name”: “John”,
“id”: “454141541”,
“type”: “ADT”,
“subType”: “A04”,
“controlNumber”: “12345678”,
“processingId”: “T”,
“version”: “2.8”
},
“patientInfo”: {
“internalId”: “12345684”,
“nameDetails”: {
“firstName”: “XXXXX”,
“lastName”: “YYYY”,
“initial”: “Z”,
“suffix”: “III”
},
“motherName”: “YYYYYYYY”,
“DOB”: “19940401”,
“sex”: “M”,
“race”: “white”,
“maritalStatus”: “Single”,
“Religion”: “Jew”,
“SSN”: “112-546-878”,
“birthPlace”: “Minnesota”,
“birthOrder”: “test”,
“citizenship”: “US”
},
“kinDetails”: [{
“firstName”: “WWWWWW”,
“lastName”: “ZZZZZ”,
“relationship”: “XXXXX”,
“phoneNumber”: “(216)123-4567”,
“contactRole”: “EC”
}]
}

Note : In the above JSON message you can see that the data being transmitted is primarily a ADT message. specifically an A04 message which is basically a Patient Registration message format.

Here MSH and PID segments are mandatory. That’s why you can see that In the JSON structure I have not used array (‘[]’) for the key values ‘header‘ and ‘patientInfo‘. If there is a possibility that multiple values may occur in future then we will be having a array structure specified there as in kinDetails. That’s why I have looped the kin details in the transformer code.

If everything goes well upon deploying the channel you will get an output message like this show below.

MSH|^~\&|XXXX|John|454141541||||ADT|12345678|T|2.8
PID|||12345684||XXXXX^YYYY^Z^III|YYYYYYYY|19940401|M||white||||||Single|Jew||112-546-878||||Minnesota||test|US
NK1||WWWWWW^ZZZZZ|XXXXX||(216)123-4567||EC

In case of NK1 segment there can be multiple next of kin available then in that case you will have a JSON like this:

“kinDetails”: [{
“firstName”: “WWWWWW”,
“lastName”: “ZZZZZ”,
“relationship”: “XXXXX”,
“phoneNumber”: “(216)123-4567”,
“contactRole”: “EC”
},{
“firstName”: “WWWWWW”,
“lastName”: “ZZZZZ”,
“relationship”: “XXXXX”,
“phoneNumber”: “(216)123-4567”,
“contactRole”: “EC”
}]

If you print the data inside the loop of the transformer code then you can see both the data are logged. Hence the output should also have multiple NK1 segment available in it.

NOTE :  HIPPA policies are strictly followed in this BLOG so I will never reveal any patient name, other relation name etc here. I will only build a dummy data set, don’t look in specific to message logic.

 

HL7V2 to JSON in Mirth

Bi-directional Channel (HL7v2-JSON) : 

Let’s learn to make the conversion from HL7V2 to JSON in Mith tool. Need for this conversion is high in demand, the end-users may be in need to consume the JSON data for their business use, from the HL7V2 which is basically  a pipe-delimiter message.

Pre-requisites :

  • MirthConnect : 3.4.1.8057 (latest version)

Channel setup :

  1. Create a New channel name it as your wish Here I have named HL7V2 to JSON
  2. Click on Set Data Types button set the source connector inbound to HL7V2.x
  3. Outbound and Destination 1 to JSON
     – that’s it the channel setup is done for it

Source Transformer :

  • Go to the source transformer and right click to create a new step
  • Name the step as HL7V2 to JSON
  • Select the drop-down of  the step that you have created and select Javascript

Code :

var hl7V2Array = [];
var hl7V2Obj = {};
hl7V2Obj.messageHeader ={};

hl7V2Obj.messageHeader.fieldSeparator = msg[‘MSH’][‘MSH.1’]
hl7V2Obj.messageHeader.encodingCharacter=msg[‘MSH’][‘MSH.2’]
hl7V2Obj.messageHeader.sendingApplication=msg[‘MSH’][‘MSH.3’][‘MSH.3.1’]
hl7V2Obj.messageHeader.sendingFacility=msg[‘MSH’][‘MSH.4’][‘MSH.4.1’]
hl7V2Obj.messageHeader.receivingApplication=msg[‘MSH’][‘MSH.5’][‘MSH.5.1’]
hl7V2Obj.messageHeader.receivingFacility=msg[‘MSH’][‘MSH.6’][‘MSH.6.1’]
hl7V2Obj.messageHeader.dateTimeMessage=msg[‘MSH’][‘MSH.7’][‘MSH.7.1’]
hl7V2Obj.messageHeader.security=msg[‘MSH’][‘MSH.8’][‘MSH.8.1’]
hl7V2Obj.messageHeader.messageType=msg[‘MSH’][‘MSH.9’][‘MSH.9.1’]
hl7V2Obj.messageHeader.eventType=msg[‘MSH’][‘MSH.9’][‘MSH.9.2’]
hl7V2Obj.messageHeader.controlID=msg[‘MSH’][‘MSH.10’][‘MSH.10.1’]
hl7V2Obj.messageHeader.processingID=msg[‘MSH’][‘MSH.11’][‘MSH.11.1’]
hl7V2Obj.messageHeader.versionID=msg[‘MSH’][‘MSH.12’][‘MSH.12.1’]
hl7V2Obj.messageHeader.sequenceNumber=msg[‘MSH’][‘MSH.13’][‘MSH.13.1’]

hl7V2Obj.patientIdentification ={};
hl7V2Obj.patientIdentification.setId=msg[‘PID’][‘PID.1’][‘PID.1.1’]
hl7V2Obj.patientIdentification.patientIdExternal=msg[‘PID’][‘PID.2’][‘PID.2.1’]
hl7V2Obj.patientIdentification.patientIdInternal=msg[‘PID’][‘PID.3’][‘PID.3.1’]
hl7V2Obj.patientIdentification.alternateId=msg[‘PID’][‘PID.4’][‘PID.4.1’]
hl7V2Obj.patientIdentification.patientName={};
hl7V2Obj.patientIdentification.patientName.familyName=msg[‘PID’][‘PID.5’][‘PID.5.1’]
hl7V2Obj.patientIdentification.patientName.givenName=msg[‘PID’][‘PID.5’][‘PID.5.2’]
hl7V2Obj.patientIdentification.motherName=msg[‘PID’][‘PID.6’][‘PID.6.1’]
hl7V2Obj.patientIdentification.dateTimeBirth=msg[‘PID’][‘PID.7’][‘PID.7.1’]
hl7V2Obj.patientIdentification.sex=msg[‘PID’][‘PID.8’][‘PID.8.1’]
hl7V2Obj.patientIdentification.patientAlias=msg[‘PID’][‘PID.9’][‘PID.9.1’]
hl7V2Obj.patientIdentification.race=msg[‘PID’][‘PID.10’][‘PID.10.1’]
hl7V2Obj.patientIdentification.patientAddress=msg[‘PID’][‘PID.11’][‘PID.11.1’]
hl7V2Obj.patientIdentification.countryCode=msg[‘PID’][‘PID.12’][‘PID.12.1’]
hl7V2Obj.patientIdentification.phoneHome=msg[‘PID’][‘PID.13’][‘PID.13.1’]
hl7V2Obj.patientIdentification.phoneBusiness=msg[‘PID’][‘PID.14’][‘PID.14.1’]
hl7V2Obj.patientIdentification.primaryLanguage=msg[‘PID’][‘PID.15’][‘PID.15.1’]
hl7V2Obj.patientIdentification.maritalStatus=msg[‘PID’][‘PID.16’][‘PID.16.1’]
hl7V2Obj.patientIdentification.religion=msg[‘PID’][‘PID.17’][‘PID.17.1’]
hl7V2Obj.patientIdentification.accountNumber=msg[‘PID’][‘PID.18’][‘PID.18.1’]
hl7V2Obj.patientIdentification.ssnNumber=msg[‘PID’][‘PID.19’][‘PID.19.1’]
hl7V2Obj.patientIdentification.driverLicense=msg[‘PID’][‘PID.20’][‘PID.20.1’]

hl7V2Array.push(hl7V2Obj);

var output = JSON.stringify(hl7V2Array);
logger.info(output);
channelMap.put(‘JSON’,output);

Consider you send a HL7V2 message like this:

MSH|^~\&|CERNER||PriorityHealth||||ORU^R01|Q479004375T431430612|P|2.3|
PID|||001677980||SMITH^CURTIS||19680219|M||||||||||929645156318|123456789|
PD1||||1234567890^LAST^FIRST^M^^^^^NPI|
OBR|1|341856649^HNAM_ORDERID|000002006326002362|648088^Basic Metabolic Panel|||20061122151600|||||||||1620^Hooker^Robert^L||||||20061122154733|||F|||||||||||20061122140000|
OBX|1|NM|GLU^Glucose Lvl|59|mg/dL|65-99^65^99|L|||F|||20061122154733|

You will get a JSON like this as output:

[{
“messageHeader”: {
“fieldSeparator”: “|”,
“encodingCharacter”: “^~\\&”,
“sendingApplication”: “CERNER”,
“sendingFacility”: “”,
“receivingApplication”: “PriorityHealth”,
“receivingFacility”: “”,
“dateTimeMessage”: “”,
“security”: “”,
“messageType”: “ORU”,
“eventType”: “R01”,
“controlID”: “Q479004375T431430612”,
“processingID”: “P”,
“versionID”: “2.3”,
“sequenceNumber”: “”
},
“patientIdentification”: {
“setId”: “”,
“patientIdExternal”: “”,
“patientIdInternal”: “001677980”,
“alternateId”: “”,
“patientName”: {
“familyName”: “SMITH”,
“givenName”: “CURTIS”
},
“motherName”: “”,
“dateTimeBirth”: “19680219”,
“sex”: “M”,
“patientAlias”: “”,
“race”: “”,
“patientAddress”: “”,
“countryCode”: “”,
“phoneHome”: “”,
“phoneBusiness”: “”,
“primaryLanguage”: “”,
“maritalStatus”: “”,
“religion”: “”,
“accountNumber”: “929645156318”,
“ssnNumber”: “123456789”,
“driverLicense”: “”
}
}]

Blog at WordPress.com.

Up ↑