04/12/2022 01:14 PM
Hi,
I need to transform text that comes from HR, this includes special characters and accents (to meet AD requirements).
I have a script that works in 'pure' groovy using this IDE:
https://www.tutorialspoint.com/execute_groovy_online.php
import java.text.Normalizer import java.text.Normalizer.Form private String camelCase(String name){ List list = new ArrayList(); for (String part in name.split(' ')){ list.add(part[0].toUpperCase() + part.substring(1,part.length()).toLowerCase()); } return Normalizer.normalize(list.join(' '), Form.NFD).replaceAll(/\p{InCombiningDiacriticalMarks}+/, '').replaceAll('[^a-zA-Z ]', ''); }
This is working perfectly for me (with some limitations that we can live with) some unit tests:
CHRIS JEFFERS VAN DER RENSBURG becomes: Chris Jeffers Van Der Rensburg Ugly UNCLE TOM becomes: Ugly Uncle Tom SANDY-JANE MCWILLIAMS becomes: Sandyjane Mcwilliams JERRY O'BRIAN becomes: Jerry Obrian éôü êùéàç ÍËÇ becomes: Eou Eueac Iec ł € œ becomes:
You can see the limitations on the last line, it does not like those characters.
How can I now integrate this into the ACCOUNTNAME JSON? I have tried to use it, but I get the following error (hope you can read it):
Can I create a function and then use it like I intend? Or is there another way I need to do 'advanced' transformations?
Here is a snippet of my JSON:
CN=${ private String camelCase(String name){ List list = new ArrayList(); for (String part in name.split(' ')){ list.add(part[0].toUpperCase() + part.substring(1,part.length()).toLowerCase()); } return Normalizer.normalize(list.join(' '), Form.NFD).replaceAll(/\p{InCombiningDiacriticalMarks}+/, '').replaceAll('[^a-zA-Z ]', ''); } camelCase(user.firstname + ' ' + user.lastname); }${ if(role.equalsIgnoreCase('Contractor or Vendor') || role.equalsIgnoreCase('Contractor or Vendor - No Mailbox')){ if(company.equalsIgnoreCase('Adapt IT')){ ',OU=Adapt IT,OU=User Accounts,OU=Administrative Container' } else if(company.equalsIgnoreCase('Adcorp BLU')){ ',OU=Adcorp BLU,OU=User Accounts,OU=Administrative Container' } else if(company.equalsIgnoreCase('Advansys')){ ',OU=Advansys,OU=User Accounts,OU=Administrative Container' } else if(company.equalsIgnoreCase('African Business Software (ABS)')){ ',OU=African Business Software (ABS),OU=User Accounts,OU=Administrative Container' } else if(company.equalsIgnoreCase('Altron Karabina')){ ',OU=Altron Karabina,OU=User Accounts,OU=Administrative Container' } else if(company.equalsIgnoreCase('Bidvest Mobility')){ ',OU=Bidvest Mobility,OU=User Accounts,OU=Administrative Container' }
Thanks in advance!!
Solved! Go to Solution.
04/12/2022 02:41 PM
Hi Craig,
It does not seem to be liking the method definition on your json. Can you rather build this into a jar and then call it fetch your values and try?
Regards,
Sahaj
04/12/2022 02:41 PM
Thanks Sahaj,
I will implement a Jar
04/12/2022 02:41 PM
For the sake of anyone else looking to do something similar, this is what I found.
I created a class called camelCase (not really the right name, but close enough) that's input was a String, however that didnt work as it seems that when passing a String into a class through the config, it actually passes a String Array, so in my class, instead of expecting a String, I now expect an String Array and that works.
package com.altron.normalizeName; import java.text.Normalizer; import java.text.Normalizer.Form; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class NormalizeName { //this does not work as what comes in from the ACCOUNTNAME rule is a String[] public static String camelCase(String name){ List<String> list = new ArrayList<String>(); String[] splitName = name.split(" "); for (String part : splitName) { list.add(part.substring(0,1).toUpperCase() + part.substring(1,part.length()).toLowerCase()); } String joined = String.join(" ", list); String normalized = Normalizer.normalize(joined, Form.NFD); String replaced = normalized.replaceAll("[^a-zA-Z ]", ""); return replaced; } //This works. Since I only ever expect 1 string, I can do what I do on line 25 public static String camelCase(String[] names){ List<String> list = Arrays.asList(names); String name = names[0]; String[] splitName = name.split(" "); for (String part : splitName) { list.add(part.substring(0,1).toUpperCase() + part.substring(1,part.length()).toLowerCase()); } String joined = String.join(" ", list); String normalized = Normalizer.normalize(joined, Form.NFD); String replaced = normalized.replaceAll("[^a-zA-Z ]", ""); return replaced; } }
Then I call it by using:
CN=${def name = com.altron.normalizeName.NormalizeName.camelCase(user.firstname + ' ' + user.lastname);return name;},///rest of the DN here