1

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
    }      

}
David Garcia
  • 3,056
  • 18
  • 55
  • 90

2 Answers2

2

A lot of repeated code with a few substiutions makes me think using a separate function with a map.

function updateSubscription(val, label, recipient) {
    if (val) {
        nms.subscription.Unsubscribe(label, recipient);
    } else {
        nms.subscription.Subscribe(label, recipient, false);
    }
}

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

    updateSubscription(parseInt(i.@fun), 'uosFunStuff', recipient);    
    updateSubscription(parseInt(i.@news), 'uosUniversityNews', recipient);    
    updateSubscription(parseInt(i.@events), 'uosEvents', recipient);    
    updateSubscription(parseInt(i.@student), 'uosStudentLife', recipient);
}

As a sidenote, it's generally better to use if-else over a switch until you have about 5 options. There are plenty of caveats to that, but for just zero or one, an if-else is a better idea.

Carson
  • 2,700
  • 11
  • 24
1

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);
        }           
    });
}
Menelaos
  • 23,508
  • 18
  • 90
  • 155