Solved

Is there a way to optimise this code?

Posted on 2016-10-20
3
56 Views
Last Modified: 2016-10-20
/**
Is there a way to optimise this code by
1. looping instead of the long repetition.
2. Also, to display nothing (instead of string 'NHS Number') when 'NHSIDNumber' is null
----
When NHS is 'not null':
|abc9874^^^MRN|1234567890^^^NHS Number|

----

When NHS is null:
Correct Display Should be:
|abc9874^^^MRN||  

Wrong Display:
abc9874^^^MRN|^^^NHS Number|
**/


var next = output.append(input[0]);

var body = next.text;

var type;

if(body.indexOf("PV1|") > 0)
{
      next.setProperty("PatientClass",next.getField("PV1.PatientClass"));
}

//*
//Start SWAP NHS + PASID
//*

var sPID = "PID."; //default to 'normal'



// Get the number of items in the array
var count = next.getRepeatCount(sPID + "PatientIdentifierList");

next.setProperty("pCount",count);

var NHSIDNumber = "";
var PASIDNumber = "";
//var PASAA = "";
//var PASAF = "";

if (count > 0)
{
      for(var i = 0; i < count; i ++)
      {            
            
            // Get the field's current value       
            if (next.getField(sPID + "PatientIdentifierList["+ i +"].IdentifierTypeCode") == "NHS")
            {
                  NHSIDNumber = next.getField(sPID + "PatientIdentifierList["+ i +"].IDNumber")            
                  next.setProperty("NHSID", NHSIDNumber);
            }
            else if (next.getField(sPID + "PatientIdentifierList["+ i +"].IdentifierTypeCode") == "FAH")
            {
                  PASIDNumber = next.getField(sPID + "PatientIdentifierList["+ i +"].IDNumber")      
                  //PASAA = next.getField(sPID + "PatientIdentifierList["+ i +"].AssigningAuthority.NamespaceID");
                  //PASAF = next.getField(sPID + "PatientIdentifierList["+ i +"].AssigningFacility.NamespaceID");
                  next.setProperty("PASID", PASIDNumber);
            }                  
      }            
      
            
      next.setField(sPID + "PatientIdentifierList[0].IDNumber",PASIDNumber);
      next.setField(sPID + "PatientIdentifierList[0].AssigningAuthority.NamespaceID", "MRN");
      next.setField(sPID + "PatientIdentifierList[0].IdentifierTypeCode", '');
      //next.setField(sPID + "PatientIdentifierList[0].AssigningAuthority.NamespaceID",PASAA);
      //next.setField(sPID + "PatientIdentifierList[0].AssigningFacility.NamespaceID",PASAF);
            
      
      next.setField(sPID + "AlternatePatientID[0].IDNumber",NHSIDNumber);
      next.setField(sPID + "AlternatePatientID[0].AssigningAuthority.NamespaceID", "NHS Number");
      next.setField(sPID + "AlternatePatientID[0].IdentifierTypeCode", '');
      //next.setField(sPID + "AlternatePatientID[0].IdentifierTypeCode.NamespaceID",'');
      //next.setField(sPID + "AlternatePatientID[0].IdentifierTypeCode.NamespaceID",'');
      
      next.setField(sPID + "PatientIdentifierList[1].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[1].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[1].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[1].AssigningFacility.NamespaceID", '');
      
      next.setField(sPID + "PatientIdentifierList[2].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[2].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[2].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[2].AssigningFacility.NamespaceID", '');
      
      next.setField(sPID + "PatientIdentifierList[3].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[3].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[3].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[3].AssigningFacility.NamespaceID", '');
      
      next.setField(sPID + "PatientIdentifierList[4].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[4].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[4].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[4].AssigningFacility.NamespaceID", '');
      
      next.setField(sPID + "PatientIdentifierList[5].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[5].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[5].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[5].AssigningFacility.NamespaceID", '');
      
      next.setField(sPID + "PatientIdentifierList[6].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[6].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[6].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[6].AssigningFacility.NamespaceID", '');
      
      next.setField(sPID + "PatientIdentifierList[7].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[7].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[7].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[7].AssigningFacility.NamespaceID", '');
      
      next.setField(sPID + "PatientIdentifierList[8].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[8].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[8].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[8].AssigningFacility.NamespaceID", '');
      
      next.setField(sPID + "PatientIdentifierList[9].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[9].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[9].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[9].AssigningFacility.NamespaceID", '');
      
      next.setField(sPID + "PatientIdentifierList[10].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[10].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[10].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[10].AssigningFacility.NamespaceID", '');
}      
//**
//*End SWAP      NHS + PASID
//**
0
Comment
Question by:Olugbnega Oyeneye
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 22

Accepted Solution

by:
Kim Walker earned 500 total points
ID: 41852318
Be sure to always select all your code and click the CODE button above the comment/question window so it will be displayed in a format that is easy to parse.

It's difficult to say if anything can be optimized without knowing what is returned by output.append(input[0]);. I'm guessing output is a custom object.

But the following long repeat code is easily replaced with a loop
      next.setField(sPID + "PatientIdentifierList[1].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[1].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[1].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[1].AssigningFacility.NamespaceID", '');
      
      ... 

      next.setField(sPID + "PatientIdentifierList[10].IDNumber",'');
      next.setField(sPID + "PatientIdentifierList[10].IdentifierTypeCode", '');
      next.setField(sPID + "PatientIdentifierList[10].AssigningAuthority.NamespaceID", '');
      next.setField(sPID + "PatientIdentifierList[10].AssigningFacility.NamespaceID", '');

Open in new window

Can be reduced to
      var ii = 1;
      while (ii <= 10) {
          next.setField(sPID + "PatientIdentifierList[" + ii +"].IDNumber",'');
          next.setField(sPID + "PatientIdentifierList[" + ii +"].IdentifierTypeCode", '');
          next.setField(sPID + "PatientIdentifierList[" + ii +"].AssigningAuthority.NamespaceID", '');
          next.setField(sPID + "PatientIdentifierList[" + ii +"].AssigningFacility.NamespaceID", '');
          ii++;
      }

Open in new window

2. I don't see anything in this code that generates any output -- certainly not the string that ends with "NHS Number."
0
 

Author Comment

by:Olugbnega Oyeneye
ID: 41852417
Yeah, that solved that bit. Thank you.
It is HL7 messaging., using java script to transform it and the message is in transit.

 var ii = 1;
      while (ii <= i) {
          next.setField(sPID + "PatientIdentifierList[" + ii +"].IDNumber",'');
          next.setField(sPID + "PatientIdentifierList[" + ii +"].IdentifierTypeCode", '');
          next.setField(sPID + "PatientIdentifierList[" + ii +"].AssigningAuthority.NamespaceID", '');
          next.setField(sPID + "PatientIdentifierList[" + ii +"].AssigningFacility.NamespaceID", '');
          ii++;
      }
0
 

Author Comment

by:Olugbnega Oyeneye
ID: 41852505
//This is solved:
//=============


/* Single input message case */
// Create the output message
var next = output.append(input[0]);

var body = next.text;

var type;

if(body.indexOf("PV1|") > 0)
{
      next.setProperty("PatientClass",next.getField("PV1.PatientClass"));
}

//*
//Start SWAP NHS + PASID
//*

var sPID = "PID."; //default to 'normal'

//if (next.getField("MSH.MessageType.TriggerEvent") == "A40")
//{
//      sPID = "PatientInformation[0].PID."
//}

// Get the number of items in the array
var count = next.getRepeatCount(sPID + "PatientIdentifierList");

next.setProperty("pCount",count);

var NHSIDNumber = "";
var PASIDNumber = "";
//var PASAA = "";
//var PASAF = "";

if (count > 0)
{
      for(var i = 0; i < count; i ++)
      {            
            
            // Get the field's current value       
            if (next.getField(sPID + "PatientIdentifierList["+ i +"].IdentifierTypeCode") == "NHS")
            {
                  NHSIDNumber = next.getField(sPID + "PatientIdentifierList["+ i +"].IDNumber")            
                  next.setProperty("NHSID", NHSIDNumber);
            }
            else if (next.getField(sPID + "PatientIdentifierList["+ i +"].IdentifierTypeCode") == "FACIL")
            {
                  PASIDNumber = next.getField(sPID + "PatientIdentifierList["+ i +"].IDNumber")      
                  //PASAA = next.getField(sPID + "PatientIdentifierList["+ i +"].AssigningAuthority.NamespaceID");
                  //PASAF = next.getField(sPID + "PatientIdentifierList["+ i +"].AssigningFacility.NamespaceID");
                  next.setProperty("PASID", PASIDNumber);
            }                  
      }            
      
            
      next.setField(sPID + "PatientIdentifierList[0].IDNumber",PASIDNumber);
      next.setField(sPID + "PatientIdentifierList[0].AssigningAuthority.NamespaceID", "MRN");
      next.setField(sPID + "PatientIdentifierList[0].IdentifierTypeCode", '');
      //next.setField(sPID + "PatientIdentifierList[0].AssigningAuthority.NamespaceID",PASAA);
      //next.setField(sPID + "PatientIdentifierList[0].AssigningFacility.NamespaceID",PASAF);
            
      
      
            if (NHSIDNumber == null)
      {      
      next.setField(sPID + "AlternatePatientID[0].IDNumber",'');
      next.setField(sPID + "AlternatePatientID[0].AssigningAuthority.NamespaceID",'' );
      next.setField(sPID + "AlternatePatientID[0].IdentifierTypeCode", '');
      //next.setField(sPID + "AlternatePatientID[0].IdentifierTypeCode.NamespaceID",'');
      //next.setField(sPID + "AlternatePatientID[0].IdentifierTypeCode.NamespaceID",'');
      }
      else
      {
      next.setField(sPID + "AlternatePatientID[0].IDNumber",NHSIDNumber);
      next.setField(sPID + "AlternatePatientID[0].AssigningAuthority.NamespaceID", "NHS Number");
      next.setField(sPID + "AlternatePatientID[0].IdentifierTypeCode", '');
      //next.setField(sPID + "AlternatePatientID[0].IdentifierTypeCode.NamespaceID",'');
      //next.setField(sPID + "AlternatePatientID[0].IdentifierTypeCode.NamespaceID",'');
      }
      


 var ii = 1;
      while (ii <= i) {
          next.setField(sPID + "PatientIdentifierList[" + ii +"].IDNumber",'');
          next.setField(sPID + "PatientIdentifierList[" + ii +"].IdentifierTypeCode", '');
          next.setField(sPID + "PatientIdentifierList[" + ii +"].AssigningAuthority.NamespaceID", '');
          next.setField(sPID + "PatientIdentifierList[" + ii +"].AssigningFacility.NamespaceID", '');
          ii++;
      }
      

      
//**
//*End SWAP      NHS + PASID
//**
}
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question