Friday, October 2, 2015

OIM 11g R2: Creating Custom Schedule Task Using java code

In one of the scenario we have the requirement where we need to create user in OIM using scheduler.The scheduler will take the parameters from csv file and run after particular interval of time and create the user. In this lab we are going to create the custom scheduler using java code and we create the scheduler we will manually fill the required details in the scheduler and will run it to check whether it is creating user or not.

Note:  Here we are assuming that you have oim custom installer for jdeveloper. Also you know how to create the folder structure structure for the same.

1) Create the java project in jdeveloper and add the following jar as shown in figure.











2) Copy and Paste the below code.


  1. import java.util.HashMap;
  2. import oracle.iam.identity.usermgmt.api.UserManager;
  3. import oracle.iam.identity.usermgmt.vo.User;
  4. import oracle.iam.platform.Platform;
  5. import oracle.iam.scheduler.vo.TaskSupport;
  6. public class Test extends TaskSupport {
  7.     public void execute(HashMap hm) throws Exception {  
  8.         UserManager usrMgr = Platform.getService(UserManager.class);
  9.         String userKey = null; // USR_KEY
  10.         String firstName = (String) hm.get("First Name");
  11.         String lastName = (String) hm.get("Last Name"); 
  12.         String userLogin = (String) hm.get("User Login"); 
  13.         String organizationKey = "1";  
  14.         String userType = "End-User"; 
  15.         String role = "Full-Time"; 
  16.         User newUser = new User(userKey);
  17.         newUser.setLogin(userLogin);
  18.         newUser.setFirstName(firstName);
  19.         newUser.setLastName(lastName);
  20.         newUser.setOrganizationKey(organizationKey);
  21.         newUser.setUserType(userType);
  22.         newUser.setEmployeeType(role);
  23.          usrMgr.create(newUser);
  24.           System.out.println("User creation successfully done");
  25.            System.out.println("user display name is "+newUser.getLogin());
  26.             }
  27.     public HashMap getAttributes() {
  28.         return null;
  29.     }
  30.     public void setAttributes() {
  31.     }
  32. }
3) Create the "plugin.xml" with content as shown below
<oimplugins xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<plugins pluginpoint="oracle.iam.scheduler.vo.TaskSupport">
<plugin pluginclass="Test" version="2.0" name="SampleTestUserScheduledTask"/>    </plugins>
</oimplugins>

4) Create "metadata.xml" with content as shown below
<?xml version="1.0" encoding="UTF-8"?>
<scheduledTasks xmlns="http://xmlns.oracle.com/oim/scheduler">
    <task>
        <name>Create New User</name>
        <class> Test</class>
        <description>Create a new OIM User</description>
        <retry>5</retry>
        <parameters>
           <string-param required="true" encrypted="false" helpText="First Name">First Name</string-param>
            <string-param required="true" encrypted="false" helpText="Last Name">Last Name</string-param>
           <string-param required="true" encrypted="false" helpText="User Login">User Login</string-param>
        </parameters>
    </task>
</scheduledTasks>

5) If you see the metadata.xml file above, you will observer that we are going to create three fields on the scheduler namely First Name, Last Name, User Login. We will provide our parameters here and the default value for Organization, User Type are take from java code.

6) Paste the plugin.xml & metadata.xml file inside config folder.

7) Deploy the project.

8) Login to oim and create the custom scheduler.The only thing you need to take care here is while creating the custom scheduler make sure to select task name same the one you have given in plugin.xml file.

9) Once the scheduler gets created fill the required details and click on RunNow. The user will get created in OIM.


No comments:

Post a Comment

Other Posts