A Post-Process Event handler are consequent operations related to the current operation taking place. Eg: In our case we want E-Mail ID to be created for the user in OIM as soon as the user is successfully created.
Now to create the post-process event handler we need to implement PostProcessHandler interface.
Below are the 2 ways in which a user can be created in OIM:
1. Using Create option from OIM Identity Console
2. Using Bulk Upload like Flat-File
Now to handle these 2 cases, we use below 2 methods as described by PostProcessHandler interface:
1. public EventResult execute(long l, long l1, Orchestration orchestration)
2. public BulkEventResult execute(long l, long l1,BulkOrchestration bulkOrchestration)
Below is the complete code to implement the logic:
package com.oimacademy.eventhandlers;
import com.oimacademy.db.DBUtil;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import oracle.core.ojdl.logging.ODLLogger;
import oracle.iam.platform.Platform;
import oracle.iam.platform.kernel.spi.PostProcessHandler;
import oracle.iam.platform.context.ContextAware;
import oracle.iam.platform.entitymgr.EntityManager;
import oracle.iam.platform.kernel.vo.AbstractGenericOrchestration;
import oracle.iam.platform.kernel.vo.BulkEventResult;
import oracle.iam.platform.kernel.vo.BulkOrchestration;
import oracle.iam.platform.kernel.vo.EventResult;
import oracle.iam.platform.kernel.vo.Orchestration;
public class generateEmailid implements PostProcessHandler{
private ODLLogger logger = ODLLogger.getODLLogger(Constant.LOGGER_NAME);
private static final String CLASS_NAME="com.oimacademy.eventhandlers.generateEmailid";
/**
* This method is used to remove any spaces present in the string
* @param userName
* @return
*/
private String removeSpaces(String userName){
userName=userName.replaceAll("-","");
userName=userName.replaceAll(" ","");
userName=userName.replaceAll("","");
return userName;
}
/**
* Thsi method is used to fetch the User Details like FirstName, MiddleName, LastName etc.
* @param parameters
* @param key
* @return
*/
private String getParamaterValue(HashMap<String, Serializable> parameters,
String key) {
String value = (parameters.get(key) instanceof ContextAware)
? (String) ((ContextAware) parameters.get(key)).getObjectValue()
: (String) parameters.get(key);
return value;
}
/**
* This method will generate the E-Mail Address based on the logic, like FirstName.LastName@xyz.com or FirstNameMiddleName.LastName@xyz.com
* @param FN
* @param MN
* @param LN
* @param counter
* @return
*/
private String generateEmailID(String FN, String MN, String LN, int counter) {
String methodName= "generateEmailid";
logger.entering(CLASS_NAME, methodName);
String address = "@xyz.com";
String emailID = "";
String count = "";
if(counter > 0){
count = Integer.toString(counter);
}
if(MN.equalsIgnoreCase("Null")){
emailID = ((FN.concat(".")).concat(LN)).concat(count).concat(address);
logger.info("Generate Email ID: "+emailID);
}
else{
emailID = (((FN.concat(MN)).concat(".")).concat(LN)).concat(count).concat(address);
logger.info("Generate Email ID: "+emailID);
}
logger.exiting(CLASS_NAME, methodName);
return emailID;
}
/**
* This method will check for the uniqueness of the generated Email ID by validting against OIM DB
* @param email
* @param Conn
* @return
*/
private String checkEmailID(String email, Connection Conn) {
String methodName = "checkEmailID";
logger.entering(CLASS_NAME, methodName);
String flag = "Unique";
int Count = 0;
PreparedStatement ps = null;
ResultSet rs = null;
try{
ps = Conn.prepareStatement(Constant.SQL_SELECT_EMAIL_ID);
ps.setString(1, email);
rs = ps.executeQuery();
while(rs.next()){
Count = rs.getInt(1);
logger.info("Count: "+Count);
}
if(Count>0){
flag = "Duplicate";
logger.info("Generated Email ID already exists: "+email);
}
else{
logger.info("Generate Email ID is Unique: "+email);
}
logger.exiting(CLASS_NAME, methodName);
return flag;
}
catch (Exception e){
logger.severe("Exception inside get checkEmailID"+e.getMessage());
}finally{
try{
if (ps != null){
ps.close();
}
if(rs!=null){
rs.close();
}
}catch(Exception e){
logger.severe("Exception while closing the ps and rs"+e.getMessage());
}
}
return flag;
}
/**
* Main Class that executes the PostProcess Event Handler
* @param parameterHashMap
* @param targetType
* @param targetId
*/
private void executeEvent(HashMap parameterHashMap, String targetType, String targetId) {
Connection oimConnection=DBUtil.getOIMConnection();
if(targetId!=null){
try{
EntityManager mgr = Platform.getService(EntityManager.class);
String flag = "Duplicate";
int i = 0;
//Fetching FirstName, MiddleName and LastName of User to generate Email ID
String FirstName = getParamaterValue(parameterHashMap, "First Name");
String MiddleName = getParamaterValue(parameterHashMap, "Middle Name");
String LastName = getParamaterValue(parameterHashMap, "Last Name");
logger.info("First Name: "+FirstName);
logger.info("Middle Name: "+MiddleName);
logger.info("Last Name: "+LastName);
HashMap<String, Object> modParams = new HashMap<String, Object>();
if((FirstName == null || (FirstName.length() == 0))&&(LastName == null || (LastName.length() == 0))){
logger.info("Invalid First Name or Last Name");
System.exit(0);
}
else{
if(MiddleName == null || (MiddleName.length() == 0)){
MiddleName= "Null";
logger.info("Middle Name is NULL!!!!");
}
FirstName = removeSpaces(FirstName);
MiddleName = removeSpaces(MiddleName);
LastName = removeSpaces(LastName);
String genEmail = generateEmailID(FirstName, MiddleName, LastName, i);
//Check if generated Email ID is unique
flag = checkEmailID(genEmail, oimConnection);
while (flag.equalsIgnoreCase("Duplicate")){
i = i+1;
genEmail = generateEmailID(FirstName, MiddleName, LastName, i);
flag = checkEmailID(genEmail, oimConnection);
}
if(flag.equalsIgnoreCase("Unique")){
modParams.put("Email", genEmail);
mgr.modifyEntity(targetType, targetId, modParams);
logger.info("USER Modified SUCCESSFULLY WITH EMAIL ID: "+genEmail);
}
}
}catch(Exception e){
logger.severe("Exception inside executeEvent()"+e.getMessage());
}finally{
try{
if(oimConnection!=null){
oimConnection.close();
}
}catch(Exception e){
logger.severe("Exception occured while closing Connection"+e.getMessage());
}
}
}
}
/**
* This method is called to when a user is created using Identity Console
* @param l
* @param l1
* @param orchestration
* @return
*/
public EventResult execute(long l, long l1, Orchestration orchestration) {
String methodName = "EventResult()";
logger.entering(CLASS_NAME, methodName);
HashMap<String, Serializable> parameters = orchestration.getParameters();
logger.info(String.format("Parameters: ", parameters));
String targetType = orchestration.getTarget().getType();
String entityID = orchestration.getTarget().getEntityId();
logger.info("Target Type: "+targetType);
logger.info("Entity ID: "+entityID);
try {
executeEvent(parameters,orchestration.getTarget().getType(), orchestration.getTarget().getEntityId());
} catch (Exception e) {
e.printStackTrace();
}
logger.exiting(CLASS_NAME, methodName);
return new EventResult();
}
/**
* This method is called to when a users are created using Flat File or Bulk Upload Utility
* @param l
* @param l1
* @param bulkOrchestration
* @return
*/
public BulkEventResult execute(long l, long l1, BulkOrchestration bulkOrchestration) {
logger.info("Executing BULK operation");
HashMap<String, Serializable>[] bulkParameters = bulkOrchestration.getBulkParameters();
String[] entityIds = bulkOrchestration.getTarget().getAllEntityId();
for (int i = 0; i < bulkParameters.length; i++) {
try {
executeEvent(bulkParameters[i],bulkOrchestration.getTarget().getType(), entityIds[i]);
} catch (Exception e) {
e.printStackTrace();
}
}
return new BulkEventResult();
}
public boolean cancel(long l, long l1, AbstractGenericOrchestration abstractGenericOrchestration) {
return false;
}
public void compensate(long l, long l1, AbstractGenericOrchestration abstractGenericOrchestration) {
}
public void initialize(HashMap<String, String> hashMap) {
}
}
In my case I have 2 more files that need to be used to make this code work. I have defined a separate class to establish OIM DB Connection which is as below and the same has been referred from above code:
package com.oimacademy.db;
import java.sql.Connection;
import oracle.iam.platform.Platform;
public class DBUtil {
public static Connection getOIMConnection() {
Connection conn = null; try {
conn = Platform.getOperationalDS().getConnection();
return conn;
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}
Last class is just to define the final constants and thus I have named it as Constant.java
package com.oimacademy.eventhandlers;
public static final String LOGGER_NAME = "com.oimacademy.eventhandlers.generateEmailid";
public static final String SQL_SELECT_EMAIL_ID="select count(*) from usr where usr_email = ?";
}
Now to create the post-process event handler we need to implement PostProcessHandler interface.
Below are the 2 ways in which a user can be created in OIM:
1. Using Create option from OIM Identity Console
2. Using Bulk Upload like Flat-File
Now to handle these 2 cases, we use below 2 methods as described by PostProcessHandler interface:
1. public EventResult execute(long l, long l1, Orchestration orchestration)
2. public BulkEventResult execute(long l, long l1,BulkOrchestration bulkOrchestration)
Below is the complete code to implement the logic:
package com.oimacademy.eventhandlers;
import com.oimacademy.db.DBUtil;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import oracle.core.ojdl.logging.ODLLogger;
import oracle.iam.platform.Platform;
import oracle.iam.platform.kernel.spi.PostProcessHandler;
import oracle.iam.platform.context.ContextAware;
import oracle.iam.platform.entitymgr.EntityManager;
import oracle.iam.platform.kernel.vo.AbstractGenericOrchestration;
import oracle.iam.platform.kernel.vo.BulkEventResult;
import oracle.iam.platform.kernel.vo.BulkOrchestration;
import oracle.iam.platform.kernel.vo.EventResult;
import oracle.iam.platform.kernel.vo.Orchestration;
public class generateEmailid implements PostProcessHandler{
private ODLLogger logger = ODLLogger.getODLLogger(Constant.LOGGER_NAME);
private static final String CLASS_NAME="com.oimacademy.eventhandlers.generateEmailid";
/**
* This method is used to remove any spaces present in the string
* @param userName
* @return
*/
private String removeSpaces(String userName){
userName=userName.replaceAll("-","");
userName=userName.replaceAll(" ","");
userName=userName.replaceAll("","");
return userName;
}
/**
* Thsi method is used to fetch the User Details like FirstName, MiddleName, LastName etc.
* @param parameters
* @param key
* @return
*/
private String getParamaterValue(HashMap<String, Serializable> parameters,
String key) {
String value = (parameters.get(key) instanceof ContextAware)
? (String) ((ContextAware) parameters.get(key)).getObjectValue()
: (String) parameters.get(key);
return value;
}
/**
* This method will generate the E-Mail Address based on the logic, like FirstName.LastName@xyz.com or FirstNameMiddleName.LastName@xyz.com
* @param FN
* @param MN
* @param LN
* @param counter
* @return
*/
private String generateEmailID(String FN, String MN, String LN, int counter) {
String methodName= "generateEmailid";
logger.entering(CLASS_NAME, methodName);
String address = "@xyz.com";
String emailID = "";
String count = "";
if(counter > 0){
count = Integer.toString(counter);
}
if(MN.equalsIgnoreCase("Null")){
emailID = ((FN.concat(".")).concat(LN)).concat(count).concat(address);
logger.info("Generate Email ID: "+emailID);
}
else{
emailID = (((FN.concat(MN)).concat(".")).concat(LN)).concat(count).concat(address);
logger.info("Generate Email ID: "+emailID);
}
logger.exiting(CLASS_NAME, methodName);
return emailID;
}
/**
* This method will check for the uniqueness of the generated Email ID by validting against OIM DB
* @param email
* @param Conn
* @return
*/
private String checkEmailID(String email, Connection Conn) {
String methodName = "checkEmailID";
logger.entering(CLASS_NAME, methodName);
String flag = "Unique";
int Count = 0;
PreparedStatement ps = null;
ResultSet rs = null;
try{
ps = Conn.prepareStatement(Constant.SQL_SELECT_EMAIL_ID);
ps.setString(1, email);
rs = ps.executeQuery();
while(rs.next()){
Count = rs.getInt(1);
logger.info("Count: "+Count);
}
if(Count>0){
flag = "Duplicate";
logger.info("Generated Email ID already exists: "+email);
}
else{
logger.info("Generate Email ID is Unique: "+email);
}
logger.exiting(CLASS_NAME, methodName);
return flag;
}
catch (Exception e){
logger.severe("Exception inside get checkEmailID"+e.getMessage());
}finally{
try{
if (ps != null){
ps.close();
}
if(rs!=null){
rs.close();
}
}catch(Exception e){
logger.severe("Exception while closing the ps and rs"+e.getMessage());
}
}
return flag;
}
/**
* Main Class that executes the PostProcess Event Handler
* @param parameterHashMap
* @param targetType
* @param targetId
*/
private void executeEvent(HashMap parameterHashMap, String targetType, String targetId) {
Connection oimConnection=DBUtil.getOIMConnection();
if(targetId!=null){
try{
EntityManager mgr = Platform.getService(EntityManager.class);
String flag = "Duplicate";
int i = 0;
//Fetching FirstName, MiddleName and LastName of User to generate Email ID
String FirstName = getParamaterValue(parameterHashMap, "First Name");
String MiddleName = getParamaterValue(parameterHashMap, "Middle Name");
String LastName = getParamaterValue(parameterHashMap, "Last Name");
logger.info("First Name: "+FirstName);
logger.info("Middle Name: "+MiddleName);
logger.info("Last Name: "+LastName);
HashMap<String, Object> modParams = new HashMap<String, Object>();
if((FirstName == null || (FirstName.length() == 0))&&(LastName == null || (LastName.length() == 0))){
logger.info("Invalid First Name or Last Name");
System.exit(0);
}
else{
if(MiddleName == null || (MiddleName.length() == 0)){
MiddleName= "Null";
logger.info("Middle Name is NULL!!!!");
}
FirstName = removeSpaces(FirstName);
MiddleName = removeSpaces(MiddleName);
LastName = removeSpaces(LastName);
String genEmail = generateEmailID(FirstName, MiddleName, LastName, i);
//Check if generated Email ID is unique
flag = checkEmailID(genEmail, oimConnection);
while (flag.equalsIgnoreCase("Duplicate")){
i = i+1;
genEmail = generateEmailID(FirstName, MiddleName, LastName, i);
flag = checkEmailID(genEmail, oimConnection);
}
if(flag.equalsIgnoreCase("Unique")){
modParams.put("Email", genEmail);
mgr.modifyEntity(targetType, targetId, modParams);
logger.info("USER Modified SUCCESSFULLY WITH EMAIL ID: "+genEmail);
}
}
}catch(Exception e){
logger.severe("Exception inside executeEvent()"+e.getMessage());
}finally{
try{
if(oimConnection!=null){
oimConnection.close();
}
}catch(Exception e){
logger.severe("Exception occured while closing Connection"+e.getMessage());
}
}
}
}
/**
* This method is called to when a user is created using Identity Console
* @param l
* @param l1
* @param orchestration
* @return
*/
public EventResult execute(long l, long l1, Orchestration orchestration) {
String methodName = "EventResult()";
logger.entering(CLASS_NAME, methodName);
HashMap<String, Serializable> parameters = orchestration.getParameters();
logger.info(String.format("Parameters: ", parameters));
String targetType = orchestration.getTarget().getType();
String entityID = orchestration.getTarget().getEntityId();
logger.info("Target Type: "+targetType);
logger.info("Entity ID: "+entityID);
try {
executeEvent(parameters,orchestration.getTarget().getType(), orchestration.getTarget().getEntityId());
} catch (Exception e) {
e.printStackTrace();
}
logger.exiting(CLASS_NAME, methodName);
return new EventResult();
}
/**
* This method is called to when a users are created using Flat File or Bulk Upload Utility
* @param l
* @param l1
* @param bulkOrchestration
* @return
*/
public BulkEventResult execute(long l, long l1, BulkOrchestration bulkOrchestration) {
logger.info("Executing BULK operation");
HashMap<String, Serializable>[] bulkParameters = bulkOrchestration.getBulkParameters();
String[] entityIds = bulkOrchestration.getTarget().getAllEntityId();
for (int i = 0; i < bulkParameters.length; i++) {
try {
executeEvent(bulkParameters[i],bulkOrchestration.getTarget().getType(), entityIds[i]);
} catch (Exception e) {
e.printStackTrace();
}
}
return new BulkEventResult();
}
public boolean cancel(long l, long l1, AbstractGenericOrchestration abstractGenericOrchestration) {
return false;
}
public void compensate(long l, long l1, AbstractGenericOrchestration abstractGenericOrchestration) {
}
public void initialize(HashMap<String, String> hashMap) {
}
}
In my case I have 2 more files that need to be used to make this code work. I have defined a separate class to establish OIM DB Connection which is as below and the same has been referred from above code:
package com.oimacademy.db;
import java.sql.Connection;
import oracle.iam.platform.Platform;
public class DBUtil {
public static Connection getOIMConnection() {
Connection conn = null; try {
conn = Platform.getOperationalDS().getConnection();
return conn;
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}
Last class is just to define the final constants and thus I have named it as Constant.java
package com.oimacademy.eventhandlers;
public static final String LOGGER_NAME = "com.oimacademy.eventhandlers.generateEmailid";
public static final String SQL_SELECT_EMAIL_ID="select count(*) from usr where usr_email = ?";
}
This comment has been removed by the author.
ReplyDelete