Announcement

Collapse
No announcement yet.

Using JavaScript Objects for Transformations

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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

  • #2
    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()
    }

    Comment


    • #3
      Much appreciated. Both good suggestions.

      Comment


      • #4
        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???

        Comment


        • #5
          Yes it is. Which database are you using?

          Comment


          • #6
            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.

            Comment


            • #7
              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();
              }
              }

              Comment


              • #8
                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???

                Comment


                • #9
                  Help

                  Can someone please help me on this???

                  Comment

                  Working...
                  X