Saturday, 24 March 2018

OIM API - Stand Alone Code to add Process Task for Bulk Users.

To execute below code you have to add following jars in classpath:

  • commons-logging.jar

  • eclipselink.jar

  • jrf-api.jar

  • oimclient.jar

  • spring.jar

  • wlfullclient.jar

 

Stand Alone Code:

 

import Thor.API.Exceptions.tcAPIException;
import Thor.API.Exceptions.tcColumnNotFoundException;
import Thor.API.Operations.TaskDefinitionOperationsIntf;       
import Thor.API.tcResultSet;
import Thor.API.Operations.tcProvisioningOperationsIntf;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import javax.security.auth.login.LoginException;
import oracle.iam.identity.usermgmt.api.UserManager;
import oracle.iam.identity.usermgmt.vo.User;
import oracle.iam.platform.OIMClient;
import oracle.iam.provisioning.api.ProvisioningService;
import oracle.iam.provisioning.vo.Account;

public class AddProcessTask { 

    OIMClient oimClient = null;
   
    //identity self service details
    String username = "xelsysadm";
    String password = "<password>"; //xelsysadm password
    String t3url = "t3://<hostname>:<port>"; //OIM HostName and Port
    String authwl_location = "<location of authwl.conf file in your local machine>"; //eg. D:\\authwl.conf
   
 public void getOIMConnection(){
            System.out.println("getOIMConnection() : Start");
            //set system properties
            System.setProperty("java.security.auth.login.config", authwl_location);
            System.setProperty("OIM.AppServerType", "wls");
            System.setProperty("APPSERVER_TYPE", "wls");
   
            Hashtable oimenv = new Hashtable();
            oimenv.put(OIMClient.JAVA_NAMING_FACTORY_INITIAL, "weblogic.jndi.WLInitialContextFactory");
            oimenv.put(OIMClient.JAVA_NAMING_PROVIDER_URL,t3url);
            oimClient = new OIMClient(oimenv);
            try {
                oimClient.login(username, password.toCharArray());
                System.out.println("Connected");
            } catch (LoginException e) {
                e.printStackTrace();
            }
            System.out.println("getOIMConnection() : End");
    }

public void addProcessTask(List<String> userList, String appInst, String taskName, String accountStatus){
      try{
        ProvisioningService provService = oimClient.getService(ProvisioningService.class);
        tcProvisioningOperationsIntf provisioningOperationsIntf = oimClient.getService(tcProvisioningOperationsIntf.class);
        UserManager userManager = oimClient.getService(UserManager.class);
       
        for(String userLogin : userList){
            System.out.println("User Login - " + userLogin);
            User user = userManager.getDetails(userLogin, null, true);
 
            List<Account> provList = provService.getAccountsProvisionedToUser(user.getEntityId());
            System.out.println("No of accounts : " + provList.size() + " for user : " + user.getLogin());
           
            for (Account account : provList){
              System.out.println(account.getAppInstance().getApplicationInstanceName());
              if ((appInst.equals(account.getAppInstance().getApplicationInstanceName())) &&
                (account.getAccountStatus().equals(accountStatus))){
                long pInstKey = Long.parseLong(account.getProcessInstanceKey());
                long taskKey = getTaskKey(pInstKey, taskName);
                if(taskKey != 0L){
                  provisioningOperationsIntf.addProcessTaskInstance(taskKey, pInstKey);
                }
                System.out.println("Successfully added task " + taskName + " to " + appInst + "!!!");
              }else{
                System.out.println("None of the accounts match the app instance");
              }
            }
        }
      }catch (Exception e){
          System.out.println("Error occured while adding process task " + e.getMessage());
      }
 }
   
 public long getTaskKey(long prcInstKey, String taskName) throws tcAPIException,
                                                   tcAPIException, tcColumnNotFoundException{
          TaskDefinitionOperationsIntf taskDefnOpsIntf = oimClient.getService(TaskDefinitionOperationsIntf.class);
         
          if((taskName == null) || (taskName.length() == 0)){
            System.out.println("Task Name not found");
          }
         
          Map<String, String> tskSrchFilter = new HashMap<String, String>();
          tskSrchFilter.put("Process Definition.Tasks.Task Name", taskName);
         
          tcResultSet tskSrchResSet = taskDefnOpsIntf.getTaskDetail(prcInstKey, tskSrchFilter);
         
          long retValue = 0L;
          if ((tskSrchResSet != null) && (tskSrchResSet.getTotalRowCount() != 0)){
            if(tskSrchResSet.getTotalRowCount() == 1){
              tskSrchResSet.goToRow(0);
              retValue = tskSrchResSet.getLongValue("Process Definition.Tasks.Key");
              System.out.println("retValue :: " + retValue);
            }else{
              System.out.println("Multiple Tasks found for Task Name :: " + taskName);
            }
          }else{
            System.out.println("No Task found for task Name :: " + taskName);
          }
          return retValue;
 }
   
 public List<String> getUsersFromFile(String sourceFile){
          System.out.println("Reading the data from CSV file :" + sourceFile);
          String userLogin = "";
          List<String> users = null;
          try{
            BufferedReader br = new BufferedReader(new FileReader(sourceFile));
            users = new ArrayList<String>();
             
            br.readLine();
            while(null != (userLogin = br.readLine())){
              users.add(userLogin.trim());
            }
            br.close();
          }catch (FileNotFoundException e){
            System.out.println("CSV file not found " + e.getMessage());
          }catch (IOException e){
            System.out.println("IO exception occured" + e.getMessage());
          }catch (Exception e){
            System.out.println("Error occured while reading file" + e.getMessage());
          }
          return users;
 }
   
 public static void main(String[] args){
        AddProcessTask obj = new AddProcessTask();
       
        //Users for which process task needs to be added
        String sourceFile = "D:/AddProcessTask/Users.csv";
       
        //Application Instance
        String appInstName = "ActiveDirectory";

        //Task Name
        String taskName = "Email Updated";

        //Account Status
        String accountStatus = "Provisioned";
       
        //get OIM Client
        obj.
getOIMConnection();
       
        //get list of users for which process task needs to be added
        List<String> userList = obj.getUsersFromFile(sourceFile);
       
        //add process task for users
        obj.addProcessTask(userList, appInstName, taskName, accountStatus);
   } 
}


Sample Users.csv file as shown below:


 
























Happy Learning!!!

2 comments:

  1. Very interesting, good job and thanks for sharing such a good blog. Zohno provide offboard user account provisioning procedures by following your IT policies. The pre-built task ensures that each user is offboarded, same way proper procedures are followed.

    ReplyDelete
  2. Very interesting and thank you so much Man, really much much appreciated

    ReplyDelete