The messages are hospital patient event messages, and are made up of segments, some of which may be repeated. In the example test message below, the NK1 (next of kin) segment is repeated, with two entries in the set (for the patient's father and mother).
It is easy to map and insert the non-repeating segments, but much harder to map and insert the repeating blocks. As I said, it took me a while to work out how to do it from the code snippets I found on the internet.
I had previously set up my Channel with a source to listen for the messages...
I then added a database writer destination specifically for testing my Javascript code. I added a step of type Javascript called NextofKin in the destination transformer and wrote the code shown to iterate through the NK1 segments and write the contents to an array called NokArray containing a set of objects called NoK.
$c('NoKArray',[]);
for each (seg in msg.NK1)
{
var NoK = {};
NoK.SetID = seg['NK1.1']['NK1.1.1'].toString();
NoK.Surname = seg['NK1.2']['NK1.2.1'].toString();
NoK.Firstname = seg['NK1.2']['NK1.2.2'].toString();
NoK.Relationship = seg['NK1.3']['NK1.3.1'].toString();
$c('NoKArray').push(NoK);
}
The code makes the NoKArray visible to the Channel Map (see it in the Destination Mappings on the right), so I can access it in the DatabaseWriter destination as follows...
// INSERT into NK1 repeating segment table
var sql3 = "INSERT INTO HL7_NK1_JAVA (source_message_id, source_message_type, set_id, nok_first_name, nok_surname, nok_relationship) VALUES (?, ?, ?, ?, ?, ?)";
for each (NoK in $('NoKArray'))
{
//logger.info('SetID: ['+NoK.SetID+'] Firstname: ['+NoK.Firstname+'] Surname: ['+NoK.Surname+'] Relationship: ['+NoK.Relationship+']');
var params3 = new Packages.java.util.ArrayList();
params3.add(connectorMessage.getMessageId());
params3.add($('mirth_type'));
params3.add(NoK.SetID);
params3.add(NoK.Firstname);
params3.add(NoK.Surname);
params3.add(NoK.Relationship);
var result3 = dbConn.executeUpdate(sql3, params3);
}
This will write one record for each next of kin for every message I filter through the channel.
No comments:
Post a Comment