Wednesday, January 23, 2008

Creating Scheduled Task Event

If you need to do create a schedule task event that occurs periodically in OIM, here is a code snippet that you can modify to put your custom task in.

In my example here, I am emailing all the members of a OIM Group "Blank Email ID Notification" (configurable - passed from task scheduler itself) to notify that there are Employees in OIM with Blank Employee IDs. Someone should log on to OIM and add employee user ids. This can be a task that can run daily, weekly, monthly or however you want to configure it within OIM. Just create the jar file from the code and drop it in ScheduledTask folder under xellerate application.

======================================
Here is how to configure the task scheduler and OIM group:
======================================






======================================
Here is how the end result looks :
======================================




======================================
BlankEIDEmail.java
======================================

import java.util.HashMap;

import com.thortech.xl.scheduler.tasks.SchedulerBaseTask;
import Thor.API.tcResultSet;
import Thor.API.Operations.tcUserOperationsIntf;
import Thor.API.Operations.tcGroupOperationsIntf;
import com.thortech.xl.dataobj.util.tcEmailNotificationUtil;

/**
*
* Class to determine the OIM users having a blank EID
*
*/

public class BlankEIDEmail extends SchedulerBaseTask{

private tcUserOperationsIntf userAPI;
private String eid;
private String userID;
private String firstName;
private String middleName;
private String lastName;
private String dept;
private String manager;
private String mailBody;
private String mailTemp;
public static String newline = "\n\r";
private String grpName;
private tcGroupOperationsIntf grpAPI;

public void init() {
//Fetch the attributes of the scheduled task and initialize the APIs
try{
grpName = getAttribute("Group Name");
mailTemp = getAttribute("Email Template");
userAPI = (tcUserOperationsIntf)getUtility("Thor.API.Operations.tcUserOperationsIntf");
grpAPI = (tcGroupOperationsIntf)getUtility("Thor.API.Operations.tcGroupOperationsIntf");
}
catch(Exception e){
e.printStackTrace();
}
}//end of init method

public void execute() {
try{
HashMap hashmap = new HashMap();
hashmap.put("Users.User ID","*");
//hashmap.put("USR_UDF_EID", "");
tcResultSet rset = userAPI.findAllUsers(hashmap);
System.out.println("** Filtered ** Number of Users:"+rset.getRowCount());
rset.sort("USR_UDF_EID",true);
if(rset.getRowCount()>0){
mailBody = "The following users have a blank EID:"+newline;
mailBody+= "(First Name, Middle Name, Last Name, User ID, Manager, Department)"+newline;
for(int count=0;count rset.goToRow(count);
eid = rset.getStringValue("USR_UDF_EID");
System.out.println("Processing User:"+rset.getStringValue("Users.User ID")+";USR_UDF_EID="+eid);
//filter users who have a blank EID
if(eid =="" eid==null){
userID = rset.getStringValue("Users.User ID");
firstName = rset.getStringValue("Users.First Name");
middleName = rset.getStringValue("Users.Middle Name");
lastName = rset.getStringValue("Users.Last Name");
manager = rset.getStringValue("Users.Manager Login");
dept = rset.getStringValue("USR_UDF_DEPARTMENT");
mailBody+= firstName+", ";
mailBody+= middleName+", ";
mailBody+= lastName+", ";
mailBody+= userID+", ";
mailBody+= manager+",";
mailBody+= dept;
mailBody+= newline;
}///end of inner if
else{
break;
}//end of inner else
}//end of for loop */
mailBody+=newline;
mailBody+="Thank You."+newline;
mailBody+="System Administrator."+newline;
mailTo(mailBody);
}//end of outer if
else{
}//end of else
}//end of try block
catch(Exception e){
e.printStackTrace();
}//end of catch block
}//end of execute method


public void mailTo(String mailBody)
{
try
{
tcEmailNotificationUtil sendMail = new tcEmailNotificationUtil(getDataBase());
//construct the email text
sendMail.constructEmail(mailTemp);
sendMail.setBody(mailBody);
tcResultSet memberSet = getGroupMembers(grpName);
for(int i=0;i memberSet.goToRow(i);
String usrID = memberSet.getStringValue("Users.User ID");
HashMap findUsr = new HashMap();
findUsr.put("Users.User ID", usrID);
tcResultSet userSet = userAPI.findUsers(findUsr);
String reqMailID = userSet.getStringValue("Users.Email");
//send email to the respective user ids
if(reqMailID =="" reqMailID==null){
System.out.println("Blank email for UserID:"+usrID);
} else {
System.out.println("Mailing BlankEID List to:"+reqMailID);
sendMail.sendEmail(reqMailID);
}
}//end of for loop
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* This method is called to find one the members of a group
* @param name of the group
* @return returns resultset containing members of the group
*/

public tcResultSet getGroupMembers(String groupName){
tcResultSet grpMembers = null;
try{
HashMap grp = new HashMap();
grp.put("Groups.Group Name", grpName);
tcResultSet groupSet = grpAPI.findGroups(grp);
groupSet = grpAPI.findGroups(grp);
long grpKey = groupSet.getLongValue("Groups.Key");
grpMembers = grpAPI.getAllMemberUsers(grpKey);
}
catch(Exception e){
e.printStackTrace();
}
return grpMembers;
}
}

3 comments:

cj said...

Hi Rajnish,

Thanks for your sample code, but I keep getting this error.

Microsoft Windows [Version 5.2.3790]
(C) Copyright 1985-2003 Microsoft Corp.

C:\Program Files\Notepad++>javac BlankEIDEmail.java
error: compiler message file broken: key=compiler.err.file com\thortech\xl\dataa
ccess\tcDataProvider.class not found arguments=null, null, null, null, null, nul
l, null
1 error

Rahul Sudhakar Pawar said...

If you get the "compiler message file broken: key=compiler.err.file" error, it means the tcDataProvider.class is not found in the classpath. To resolve the error, just do the following steps:

1. Build with "-d" option: ant -d -f build.xml (TARGET_NAME). This will tell the name of the Java file that is accessing the tcDataProvider.class.
2. Now for the Java file check if there is an import for the tcDataProvider.class
3. Add a entry in classpath.xml that points to the Jar containing the tcDataProvider.class

sujata said...

Hi Rajnish,

Could you provide me with a code to search for AD Groups which are saved as Resource Profile in any particular Organization in OIM. Then look for the users in this AD Group and find out their email ids from OIM and return these email IDs.