Wednesday, September 16, 2015

Validation Event Handler in OIM

Sometimes there might be scenario where we need to verify certain fields using event handler. Suppose we have to verify whether the telephone number you have entered is valid or not. For this we are going to write the validation event handler. Please see below the steps for the same.

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) Develop the project in jdeveloper as shown in figure below.



















2) Copy paste the below code.


  1. package validationevent;

  2. import java.io.Serializable;
  3. import java.util.HashMap;
  4. import oracle.iam.identity.usermgmt.api.UserManagerConstants;
  5. import oracle.iam.platform.context.ContextAware;
  6. import oracle.iam.platform.kernel.ValidationException;
  7. import oracle.iam.platform.kernel.ValidationFailedException;
  8. import oracle.iam.platform.kernel.spi.ValidationHandler;
  9. import oracle.iam.platform.kernel.vo.BulkOrchestration;
  10. import oracle.iam.platform.kernel.vo.Orchestration;
  11.  

  12. public class TelephoneNumberValidationEH implements ValidationHandler
  13. {
  14.    private static final String TELEPHONE_NUMBER_REGEX = "^((\\+){0,1}91(\\s){0,1}(\\-){0,1}(\\s){0,1}){0,1}9[0-9](\\s){0,1}(\\-){0,1}(\\s){0,1}[1-9]{1}[0-9]{7}$";
  15.     private static final String TELEPHONE_NUMBER_REGEX_1 = "Please provide 10 digits number only";
  16.      
  17.     public void validate(long l, long l1, Orchestration orchestration) 
  18.     {
  19.               
  20.         HashMap<String, Serializable> contextParams = orchestration.getParameters();                
  21.         String newTelephoneNumber = getParamaterValue(contextParams, UserManagerConstants.AttributeName.PHONE_NUMBER.getId());        
  22.        
  23.         if (newTelephoneNumber != null && !newTelephoneNumber.equalsIgnoreCase("")) 
  24.         {
  25.             boolean isTelephoneNumberValidate = newTelephoneNumber.matches(TELEPHONE_NUMBER_REGEX);
  26.                         
  27.             if (!isTelephoneNumberValidate) 
  28.             {
  29.                 throw new ValidationFailedException("Format of Telephone Number must be: " + TELEPHONE_NUMBER_REGEX_1);
  30.             }
  31.         }
  32.     }
  33.  
  34.     private String getParamaterValue(HashMap<String, Serializable> parameters, String key) 
  35.     {
  36.         if (parameters.containsKey(key)) 
  37.         {
  38.             String value = (parameters.get(key) instanceof ContextAware) ? (String)((ContextAware)parameters.get(key)).getObjectValue() : (String)parameters.get(key);
  39.             return value;
  40.         } 
  41.          
  42.         else
  43.         {
  44.             return null;
  45.         }
  46.     }
  47.  
  48.     public void validate(long l, long l1, BulkOrchestration bulkOrchestration) throws ValidationException, ValidationFailedException
  49.     {
  50.     }
  51.  
  52.     public void initialize(HashMap<String, String> hashMap)
  53.     {
  54.     }
  55. }


3) Create the proper folder structure and paste the metadata.xml  & plugin.xml file inside config
folder.

metadata.xml

<?xml version="1.0" encoding="UTF-8"?>
<eventhandlers xmlns="http://www.oracle.com/schema/oim/platform/kernel" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.oracle.com/schema/oim/platform/kernel orchestration-handlers.xsd">
<validation-handler class="validationevent.TelephoneNumberValidationEH" entity-type="User" operation="CREATE" name="TelephoneNumberValidationEH" order="1000"/>

</eventhandlers>

plugin.xml

<oimplugins xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <plugins pluginpoint="oracle.iam.platform.kernel.spi.EventHandler">
      <plugin name="TelephoneNumberValidationEH" pluginclass="validationevent.TelephoneNumberValidationEH" version="1.0">
      </plugin>
   </plugins>          

</oimplugins>

4) Go to jdeveloper, select you project.Go to Tools-->OIM Customization installer-->Deploy as shown in figure below.


















5) Once deploy successfully It will show you the success message as shown in figure below.













6) Now go to oim and create user. Enter the invalid value for telephone number. You will get error message on the console and it wont allow you create user in OIM as shown in figure below.



7) In this way we have developed an validation event handler.

No comments:

Post a Comment

Other Posts