I am looking for a blth where i need to create a user ID based on following conditions UserID=(First Character of First Name + First character of Last Name + 'a' + DepartmentNumber) For Example: First name: <FirstName> Last Name: <LastName> Department: 0200 UserID = <UserID> if the generated user id already exist then we can increment the character at 3 position to b. in that case new UserID will be abc0200
public class BLTHsetUID extends BLTHAdapter { public void handleValidation(BLTHContext blthContext) throws Exception { User user = blthContext.getUser();
if (FirstName.isEmpty() || LastName.isEmpty() || departmentNumber.isEmpty()) { // this message will be presented on the screen IMSException imsEx = new IMSException(); imsEx.addUserMessage("Failed to build an UID, first name, last name and department number are required"); throw imsEx; } String UID = FirstName.substring(0, 1) + LastName.substring(0, 1) + 'a' + departmentNumber; UserProvider userbis = blthContext.getUserProvider();
for (char c='a'; c<='z'; c++){ try { UID = FirstName.substring(0, 1) + LastName.substring(0, 1) + c + departmentNumber; userbis.findUser(UID, null); //UID already exists, search for next computed one if (c=='z') { // this message will be presented on the screen IMSException imsEx = new IMSException(); imsEx.addUserMessage("Failed to find an UID, all UIDs already exist"); throw imsEx; } } catch (NoSuchObjectException nso){break;} }
try { blthContext.getUser().setAttribute("%USER_ID%", UID); } catch (Exception ex) { // this message will be presented on the screen IMSException imsEx = new IMSException(); imsEx.addUserMessage("Failed to set an UID" + ex.getMessage()); throw imsEx; } } }
Additional Information
Adapt with your department Number attribute name or well known name and other specific requirements.