javascriptxmlxml-parsinge4xadobe-campaign-classic

Adobe Campaign: Javascript XML E4X Switch Case


Given the following table data model in Adobe Campaign Data Model

I have developed the following script, which is a mix of javascript, xml, e4x (used by adobe campaign). The script basically iterates through each line and executes for each switch the code inside the cases. I am looking for a way to simplify the switches/cases as they are a bit redundant? can anyone suggest a better approach?

  var query = xtk.queryDef.create(
               <queryDef schema="temp:enrich" operation="select">
                  <select>                                       
                    <node expr="@id"/>
                    <node expr="@fun"/>
                    <node expr="@news"/>
                    <node expr="@events"/>
                    <node expr="@student"/>                       
                  </select> 
               </queryDef>)    
  var result = query.ExecuteQuery();  

for each(var i in result.enrich)
{
    //Debug: logInfo(i.@id+ " "+i.@fun+" " +i.@news+" " +i.@student);
    var recipient = <recipient _key = "@id" id = {i.@id} />;
    
    var fun     = parseInt(i.@fun);
    var news    = parseInt(i.@news);
    var events  = parseInt(i.@events);
    var student = parseInt(i.@student);    

    switch(fun) {
      case 0:
        nms.subscription.Unsubscribe("uosFunStuff", recipient);
        break;
      case 1:
        nms.subscription.Subscribe("uosFunStuff", recipient,false);
        break;
      default:
        // donothing
    }        
    switch(news) {
      case 0:
        nms.subscription.Unsubscribe("uosUniversityNews", recipient);
        break;
      case 1:
        nms.subscription.Subscribe("uosUniversityNews", recipient,false);
        break;
      default:
        // donothing
    }         
    switch(events) {
      case 0:
        nms.subscription.Unsubscribe("uosEvents", recipient);
        break;
      case 1:
        nms.subscription.Subscribe("uosEvents", recipient,false);
        break;
      default:
        // donothing
    }     
    switch(student) {
      case 0:
        nms.subscription.Unsubscribe("uosStudentLife", recipient);
        break;
      case 1:
        nms.subscription.Subscribe("uosStudentLife", recipient,false);
        break;
      default:
        // donothing
    }      

}

Solution

  • Intro

    The code could be optimized in 3 ways. Since I tested only in javascript you will have to see what works for you.

    Array of Labels and Functions

    var labels = ["uosFunStuff", "uosUniversityNews", "uosEvents","uosStudentLife"];
    var functionArray = [
        function(input){return parseInt(input.@fun)  },
        function(input){return parseInt(input.@news)  },
        function(input){return parseInt(input.@events)  },
        function(input){return parseInt(input.@student)  }
    ];
    
    for each(var i in results.enrich()) {
    
        var recipient = <recipient _key = "@id" id = {i.@id} />;
    
        var  functionIndex = 0;
        for each(var label in labels) { 
            if(functionArray[functionIndex++](i) == 1){
                  nms.subscription.Subscribe(label, recipient,false);
            }else{          
                 nms.subscription.Unsubscribe(label, recipient);
            }           
        });
    

    }

    Array of Labels, Array of Keys

    var labels = ["uosFunStuff", "uosUniversityNews", "uosEvents","uosStudentLife"];
    var keys = [ "@fun","@news","@events","@student"];
    
    for each(var i in results.enrich()) {
        var recipient = <recipient _key = "@id" id = {i.@id} />;
    
        var  functionIndex = 0;
        for each(var label in labels) { 
            if(parseInt(i[keys[functionIndex++]])== 1){
                  nms.subscription.Subscribe(label, recipient,false);
            }else{          
                 nms.subscription.Unsubscribe(label, recipient);
            }           
        });
    }
    

    Object (This one might not work)

    var labelsAndKeys = {
    uosFunStuff:"@fun",
    uosUniversityNews:"@news",
    uosEvents:"@events",
    uosStudentLife:"@student"
    };
    
    for each(var i in results.enrich()) {
        var recipient = <recipient _key = "@id" id = {i.@id} />;
    
        for each(var label in labels) { 
            if(parseInt(i[labelsAndKeys[labels])== 1){
                  nms.subscription.Subscribe(label, recipient,false);
            }else{          
                 nms.subscription.Unsubscribe(label, recipient);
            }           
        });
    }