Thursday, 29 March 2018

OIM API - Code Snippet to get ProcessInstanceKey by ApplicationInstanceName and UserKey.

  public String getProcessInstanceKey(String userKey, String appInstName){
       final String logp = CN + " :: getProcessInstanceKey - ";
       logger.info(logp + "START");

       ProvisioningService provServOps = Platform.getService(ProvisioningService.class);
      
       String procInstKey = null;
       List<Account> accountList = null;
       try{
           accountList = provServOps.getAccountsProvisionedToUser(userKey);
          
           for(Account account : accountList){
           if ((account.getAppInstance().getApplicationInstanceName().equalsIgnoreCase(appInstName)) &&
               ((account.getAccountStatus().equalsIgnoreCase("Provisioned")) ||
                account.getAccountStatus().equalsIgnoreCase("Enabled"))){
                   logger.info(logp + "Account is Provisioned/Enabled");
                   if((Account.ACCOUNT_TYPE.Primary).equals(accountprovisioned.getAccountType())){
                       logger.info(logp + "Account is Primary");
                       procInstKey = account.getProcessInstanceKey();


                       logger.info(logp + "procInstKey : " + procInstKey);
                   }
               }
           }
       }catch(Exception e){
           logger.error(logp + "Exception while getting IT Process Instance Key for - " + e, e);
       }
       logger.info(logp + "END");
       return procInstKey;
  }


Happy Learning!!!

Saturday, 24 March 2018

OIM API - Code Snippet to Set Display Name of User.

public void setDisplayNameOfUser(String userKey, String displayName){
        final String logp = CN + " :: setDisplayNameOfUser - ";
        LOGGER.log(Level.FINEST, logp + "START");
        HashMap<String, Object> modifyMap = new HashMap<String, Object>();
       
        //get user manager service
        UserManager usrService = Platform.getService(UserManager.class);
       
        //create display name map
        HashMap displayNameMap = new HashMap();
        displayNameMap.put("base", displayName);
        modifyMap.put(UserManagerConstants.AttributeName.DISPLAYNAME.getId(), displayNameMap);
       
        try{
            User newUser = new User(userKey, modifyMap);
            LOGGER.log(Level.INFO, logp + "User to update :: " + userKey);
            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.getMessage());
        }
        Logger.log(Level.FINEST, logp + "END");
 } 


Happy Learning!!!!

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!!!

OIM - How to get SOA Database Connection?

public void getSOADatabaseConnection(){
        Connection connection = null;
        DataSource soaDataSource = null;

        try {
            Context ctx = null;
            Hashtable ht = new Hashtable();
            ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
            ht.put(Context.PROVIDER_URL,"t3://<REPLACE_SOA_HOST_NAME>:<REPLACE_PORT>"); //eg- t3://10.22.34.56:8001
           
            ctx = new InitialContext(ht);
            soaDataSource = (javax.sql.DataSource)ctx.lookup("jdbc/SOADataSource");
            connection = soaDataSource.getConnection();
            System.out.println("Connected to SOA Database...");
        } catch(Exception e) {
            System.out.println("Exception occured while getting SOA DB connection " + e);
        }finally{
            if(null != connection){
                try{
                    connection.close();
                }catch(SQLException e){
                    System.out.println("Exception occured closing connection " + e);
                }
            } 
        }
 }


Happy Learning!!!

Code Snippet to Send Email Notification using Java Transport API.

public static void main(String[] args){
        final String username = "<SMTP Server User Name>";
        final String password = "<SMTP Server Password>";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", "<SMTP Server Host Name>");
        props.put("mail.smtp.port", "<SMTP Server Port>"); //generally it is 25.

        Session session = Session.getInstance(props,
          new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                }
          });

        try{
            Message message = new MimeMessage(session);
           
            //set from email address
            message.setFrom(new InternetAddress("anandrajbadal@gmail.com"));
           
            //set TO Recipients
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("anandrajbadal@ymail.com"));
           
            //set CC Recipients
            message.setRecipients(Message.RecipientType.CC, InternetAddress.parse("anandrajbadal@rediff.com"));
           
            //set subject
            message.setSubject("Test Mail");
           
            //set body
            message.setText("This is test email");
   
            Transport.send(message);
            System.out.println("Email sent successfully");
        }catch(MessagingException e){
            throw new RuntimeException(e);
        }
 }


Happy Learning!!!

OIM API - Code Snippet to get User Key by User Login.

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

        String usrKey = "";
        try{     
            //get user manager service
            UserManager userService = Platform.getService(UserManager.class);
           
            User user = userService.getDetails(userLogin, attrsToFetch, true);
            usrKey = user.getEntityId();
        }catch(NoSuchUserException e){
            e.printStackTrace();
        }catch(UserLookupException e){
            e.printStackTrace();
        }catch(AccessDeniedException e){
            e.printStackTrace();
        }
        return usrKey;
 }


Happy Learning!!!

OIM API - Code Snippet to get Entitlement from OIM for the given Entitlement Name/Code.

 public Entitlement getEntitlement(String entName){
        final String logp = CN + ":: getEntitlementDetails - ";
        LOGGER.debug(logp + "START");

        if(null == entName || entName.trim().length() == 0){
                LOGGER.error(logp + "Entitmement name is null or empty");
                return null;
        }
        entName = entName.trim();

        Entitlement ent = null;
        try{
            SearchCriteria criteria = new SearchCriteria(Entitlement.ENTITLEMENT_NAME, entName, SearchCriteria.Operator.EQUAL);
           
            //get entitlement service
            EntitlementService entServ = Platform.getService(EntitlementService.class);
            List<Entitlement> entList = entServ.findEntitlements(criteria, null);

            if (entList.size() == 0 || entList.size() > 1) {
                LOGGER.error(logp + "Improper number of entitlements found for entitlement name " + entName + " - " + entList.size());
            }else{
                    ent = entList.get(0);
                    LOGGER.info(logp + "Successfully obtained entitlement - " + ent);
                    LOGGER.info(logp + "Entitlement Key - " + ent.getEntitlementKey());
                    LOGGER.info(logp + "Entitlement Display Name  - " + ent.getDisplayName());
                    LOGGER.info(logp + "IT Resource Key  - " + ent.getItResourceKey());
            }
        }catch (Exception e){
                LOGGER.error(logp + "Exception while fetching entitlement for entitlement name " + entName + " - " + e, e);
        }

        LOGGER.debug(logp + "END");
        return ent;
 }


Happy Learning!!!