Saturday, 24 March 2018

OIM API - Code Snippet to Set Manager of User.

public void setManagerOfUser(String userLogin, String userManagerLogin){
        final String logp = CN + " :: setManagerOfUser - ";
        LOGGER.log(Level.FINEST, logp + "START");
        LOGGER.log(Level.INFO, logp + "User Manager Login :: " + userManagerLogin);
        HashMap<String, Object> modifyMap = new HashMap<String, Object>();
       
        //get user manager service
        UserManager usrService = Platform.getService(UserManager.class);
       
        //get manager's user key
        Long userManagerKey = Long.valueOf(getUserKeyByUserLogin(userManagerLogin, usrService));
        modifyMap.put(UserManagerConstants.AttributeName.MANAGER_KEY.getId(), userManagerKey);

        //get user key of User for which manager needs to be set
        String userKey = getUserKeyByUserLogin(userLogin, usrService);
       
        try{
            User newUser = new User(userKey, modifyMap);
            LOGGER.log(Level.INFO, logp + "User to update :: " + userLogin);
            UserManagerResult result = usrService.modify(AttributeName.USER_KEY.getId(), userKey, newUser);
            LOGGER.log(Level.INFO, logp + "Operations Status :: " + result.getStatus());
        }catch(Exception e){
            LOGGER.log(Level.SEVERE, logp + "Exception occured :: " ,e);
        }
        Logger.log(Level.FINEST, logp + "END");
 }
  

      
public String getUserKeyByUserLogin(String userLogin, UserManager userService){
            HashSet<String> attrsToFetch = new HashSet<String>();
            attrsToFetch.add(UserManagerConstants.AttributeName.USER_KEY.getId());

            String usrKey = "";
            try{          
                User user = userService.getDetails(userLogin, attrsToFetch, true);
                usrKey = user.getEntityId();
            }catch(Exception e){
                e.printStackTrace();
            }
            return usrKey;
 }


Happy Learning!!!

No comments:

Post a Comment