web stats
Using JavaScript Objects for Transformations - Mirth Community

Go Back   Mirth Community > Mirth Connect > General Discussion

Reply
 
Thread Tools Display Modes
  #1  
Old 01-09-2019, 08:05 AM
sphelan sphelan is offline
What's HL7?
 
Join Date: Mar 2017
Posts: 3
sphelan is on a distinguished road
Default Using JavaScript Objects for Transformations

I'm attempting to cleanup a bunch of our channels that have duplicate code and consolidate our logic.

Most of our interfaces are HL7 which we transform into a standard JSON message that gets passed to our application for processing. The logic for 95% of the channels is the same (map PID.5.1 to first name, etc ...).

We have moved a large portion of our code base to code template functions. While this has reduced a large portion of duplicate code, I'm not completely satisfied with the results. Because there are some interfaces that do something different, we have to account for it by adding some if/then logic in the functions or duplicate the function to be use just by that channel.

I'm looking to move to a more object oriented approach and was wondering if anyone has done anything similar. I'm looking to move our logic to a JavaScript object stored as code template. You create this object in your channel transformation, pass the message and call a method to return the transformed message.

For those interfaces that like to be different, I could override the particular method that maps the field which is different. No duplicate code is create.

In my sample code, I have a base object PatientStay which has a method to create my standard JSON message. The PatientStayHL7 message inherits from PatientStay and provides methods to populate the JSON (map first name to PID.5.1).
Code:
// Base class (used by HL7, delimiter, etc ..)
function PatientStay() {};

PatientStay.prototype.toJSON = function() {
    var patientStayJSON = {
        patientFirstName: this.patientFirstName(),
        patientLastName: this.patientLastName(),
        mrn: this.getMedicalRecordNumber()
    }
    return JSON.stringify(patientStayJSON);
}

PatientStay.prototype.patientFirstName = function() {
    return null;
}
PatientStay.prototype.patientLastName = function() {
    return null;
}
PatientStay.prototype.getMedicalRecordNumber = function() {
    return null;
}

// PatientStay HL7
function PatientStayHL7(hl7Message) {
    this.hl7Message = hl7Message.copy();
}

PatientStayHL7.prototype = Object.create(PatientStay.prototype);

PatientStayHL7.prototype.patientFirstName = function() {
    return this.hl7Message['PID']['PID.5']['PID.5.2'].toString();
}

PatientStayHL7.prototype.patientLastName = function() {
    return this.hl7Message['PID']['PID.5']['PID.5.1'].toString();
}

PatientStayHL7.prototype.getMedicalRecordNumber = function() {
    return this.hl7Message['PID']['PID.3']['PID.3.1'].toString()
}
If an interface follows our normal HL7 mappings (Hospital A), I can just create a channel, create a PatientStayH7 Message, pass the msg, and generate the new JSON payload. Very little code would be added to the channel's transformation.
Code:
var patientStay = new PatientStayHL7(msg);
msg = patientStay.toJSON();
When an interface does something slightly different (Hospital B), I can create a new object and override the method that populates that field.
In this example they put the MRN in 2.1 instead of 2.3.
Code:
// Create HospitalB object that inherits from PatientStayHL7
function HospitalB(hl7Message) {
   PatientStayHL7.call(this, hl7Message);
}
HospitalB.prototype = Object.create(PatientStayHL7.prototype);

// Override getMedicalRecordNumber to pull from PID.2.1 instead of PID.2.1
HospitalB.prototype.getMedicalRecordNumber = function() {
	return this.hl7Message['PID']['PID.2']['PID.2.1'].toString()
}

var patientStay = new HospitalB(msg);
msg = patientStay.toJSON();

Thoughts? Any downsides? An alternative that you are using successfully
Attached Files
File Type: zip javascript_object.zip (26.6 KB, 6 views)
Reply With Quote
  #2  
Old 01-10-2019, 11:16 PM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 802
agermano is on a distinguished road
Default

I like the approach.

I would change your toJSON function to return an object instead of a string.

Code:
PatientStay.prototype.toJSON = function() {
    return {
        patientFirstName: this.patientFirstName(),
        patientLastName: this.patientLastName(),
        mrn: this.getMedicalRecordNumber()
    };
}
That should reduce your hospitalA transformer to
Code:
msg = new PatientStayHL7(msg);
as setting the outbound type to JSON will automatically call JSON.stringify(msg) when your transformer finishes (which will in turn call your toJSON function.)

I also wouldn't bother subclassing unless you plan on putting the derived class in the code template to share between multiple hospitals. If I was overriding in the transformer I would do it like this (assuming the above change was also made:)
Code:
msg = new PatientStayHL7(msg);

// Override getMedicalRecordNumber to pull from PID.2.1 instead of PID.2.1
msg.getMedicalRecordNumber = function() {
	return this.hl7Message['PID']['PID.2']['PID.2.1'].toString()
}
Reply With Quote
  #3  
Old 01-11-2019, 04:46 AM
sphelan sphelan is offline
What's HL7?
 
Join Date: Mar 2017
Posts: 3
sphelan is on a distinguished road
Default

Much appreciated. Both good suggestions.
Reply With Quote
  #4  
Old 06-27-2019, 02:40 AM
shishirakram shishirakram is offline
Mirth Newb
 
Join Date: Jun 2018
Posts: 12
shishirakram is on a distinguished road
Default

Adding to this, is it possible to store the mappings in the database and use it in the channel?

So that I do not have to make any changes to the channel when an interface follows a different mappings???
Reply With Quote
  #5  
Old 06-27-2019, 05:35 AM
cory_cole cory_cole is offline
Mirth Guru
 
Join Date: Mar 2012
Posts: 1,228
cory_cole is on a distinguished road
Default

Yes it is. Which database are you using?
Reply With Quote
  #6  
Old 06-27-2019, 08:52 PM
shishirakram shishirakram is offline
Mirth Newb
 
Join Date: Jun 2018
Posts: 12
shishirakram is on a distinguished road
Default

I'm planning to use SQL DB. Can you tell me how. I'm able to store all the mapping and read it in mirth, but I'm not able to use those as it is.
Reply With Quote
  #7  
Old 06-28-2019, 06:03 AM
cory_cole cory_cole is offline
Mirth Guru
 
Join Date: Mar 2012
Posts: 1,228
cory_cole is on a distinguished road
Default

I assume that when you say a SQL DB, you mean SQL Server?

You will need to fill in the specific information for your DB.
var dbConn;
var query = 'SELECT FIELD1 FROM TABLE WHERE FIELD2 = VALUE;
var resultValue = '';
try {
dbConn = DatabaseConnectionFactory.createDatabaseConnection ('net.sourceforge.jtds.jdbc.Driver','dblocation',' username','pwd');
result = dbConn.executeCachedQuery(query);
while(result.next())
{
resultValue = result.getString(1);//assuming it is a string.
}
// You may access this result below with $('column_name')
return result;
} finally {
if (dbConn) {
dbConn.close();
}
}
Reply With Quote
  #8  
Old 06-28-2019, 06:51 AM
shishirakram shishirakram is offline
Mirth Newb
 
Join Date: Jun 2018
Posts: 12
shishirakram is on a distinguished road
Default

Sorry I meant SQL Server. I think I was not clear with the question.

What I was asking is I'm storing all the mapping details in the database.
Ex: FirstName(Column) - msg['PID']['PID.5']['PID.5.2'](Value).

Now suppose my channel is say HL7 to CSV converter. And I'm mapping first name in HL7 message to a particular field in csv, I would ask mirth to go to database and find out what is the location of firstname in HL7 message and get the value and map it to a particular field.

so if I write something like tmp['somepath'] = result of the database query(To fetch the mapping of the firstname) it will not work right....

How do I make this work???

Should I store those Xpath(msg['PID']['PID.5']['PID.5.2']) in DB as I mentioned in the above example or should I use a different approach altogether?

Or is there any way of telling mirth that the result you got from database is not the value that you have to map, but it is the path in HL7 message where you can find the actual value of First name and map that to the tmp???
Reply With Quote
  #9  
Old 07-02-2019, 08:42 PM
shishirakram shishirakram is offline
Mirth Newb
 
Join Date: Jun 2018
Posts: 12
shishirakram is on a distinguished road
Default Help

Can someone please help me on this???
Reply With Quote
Reply

Tags
mapping, xpath

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 11:56 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Mirth Corporation