Transform – Complex foreach - Velocity Script “foreach” transform
This portion of the transform comes from a static transform type showing the value portion of the script where the velocity logic resides. The transform is ingesting multiple departments in one format, converting it to a different format and removing duplicates.
"value": "#set($uniqueDepts = [])#if($persona != 'Student - X' && $persona != 'Student - Y' && $psDept != 'none')$psDept#elseif(($persona == 'X' || $persona == 'Y') && $studentDept != 'none')#set($studentDepts = $studentDept.replaceAll('\\{\\}', '').replaceAll('\\}\\{', ', ').replaceAll('[{}]','').split(', '))#foreach($dept in $studentDepts)#if(!$uniqueDepts.contains($dept))#set($ignore = $uniqueDepts.add($dept))#end#end#set($output = $uniqueDepts.toString().replace('[', '').replace(']', ''))$output#end"
Let’s first format the velocity script so that it is a little bit more readable:
#set($uniqueDepts = [])
#if($persona != 'Student - X' && $persona != 'Student - Y' && $psDept != 'none')$psDept
#elseif(($persona == 'X' || $persona == 'Y') && $studentDept != 'none')
#set($studentDepts = $studentDept.replaceAll('\\{\\}', '').replaceAll('\\}\\{', ', ').replaceAll('[{}]','').split(', '))
#foreach($dept in $studentDepts)
#if(!$uniqueDepts.contains($dept))
#set($ignore = $uniqueDepts.add($dept))
#end
#end
#set($output = $uniqueDepts.toString().replace('[', '').replace(']', ''))
$output
#end"
Sample Input Data
- Input: {blue}{orange}{green}{blue}{}{}
Step-by-Step Explanation
- Initial Setup :
#set($uniqueDepts = [])
This initializes an empty list unique Titles to store unique departments.
- Checking Conditions :
#if($persona != 'Student - X' && $persona != 'Student - Y' && $psDept != 'none')$psDept
#elseif(($persona == 'Student - X' || $persona == 'Student - Y') && $studentDept != 'none')
The script first checks if the persona is not ‘Student - X’ or ‘Student - Y’ and if $psDept is not ‘none’. If this condition is true, it directly outputs $psDept. The PS Department represents a department from a different source that would be written out if they are not a Student X or Y.
If the persona is either ‘Student - X’ or ‘Student - Y’ and $studentDept is not ‘none’, it proceeds to the next step.
- Processing Student Departments :
#set($studentDepts = $studentDept.replaceAll('\\{\\}', '').replaceAll('\\}\\{', ', ').replaceAll('[{}]','').split(', '))
Here, the script processes the $studentDept string by:
- Removing empty braces {}.
- Replacing the closing and opening brace }{ with a comma and a space “, “.
- Removing any remaining braces {}.
- Splitting the cleaned string by , to create an array of departments.
For the given sample data, the processed result will be: [“blue”, “orange”, “green”, “blue”].
- Iterating Over Departments to remove duplicate values :
#foreach($dept in $studentDepts)
#if(!$uniqueDepts.contains($dept))
#set($ignore = $uniqueDepts.add($dept))
#end
#end
The script iterates over each department in studentDepts:
- For each department, it checks if uniqueDepts already contains the department.
- If it doesn’t contain the department, it adds the department to uniqueDepts.
After processing the sample data, uniqueDepts will be:
[“blue”, “orange”, “green”].
- Formatting the Output :
#set($output = $uniqueDepts.toString().replace('[', '').replace(']', ''))
$output
#end
The script converts uniqueDepts to a string and removes the square brackets , creating the final output.
Final Output
For the given sample data {blue}{orange}{green}{blue}{}{}, the final output will be:
blue, orange, green
This output contains the unique departments separated by a comma and a space.