Sunday, December 14, 2014

OIM Custom Validation Event Handler Example

Tested On: Oracle Identity Manager 11.1.2.2.0
Description: A custom validation event handler that validates if  the "Telephone Number" (USR_TELEPHONE_NUMBER) user attribute has the proper format. The event handler is triggered on modification of "Telephone Number" user attribute.


Referencehttp://docs.oracle.com/cd/E27559_01/dev.1112/e27150/oper.htm#OMDEV4778


Source Code
package com.blogspot.oraclestack.eventhandlers;

import java.io.Serializable;
import java.util.HashMap;
import oracle.core.ojdl.logging.ODLLevel;
import oracle.core.ojdl.logging.ODLLogger;
import oracle.iam.identity.usermgmt.api.UserManagerConstants;
import oracle.iam.platform.context.ContextAware;
import oracle.iam.platform.kernel.ValidationException;
import oracle.iam.platform.kernel.ValidationFailedException;
import oracle.iam.platform.kernel.spi.ValidationHandler;
import oracle.iam.platform.kernel.vo.BulkOrchestration;
import oracle.iam.platform.kernel.vo.Orchestration;

/**
 * Validates if "Telephone Number" user attribute has the correct format.
 * Triggers on modification of "Telephone Number" attribute.
 * @author rayedchan
 * @version 1.0
 */
public class TelephoneNumberValidationEH implements ValidationHandler
{
    private static final ODLLogger logger = ODLLogger.getODLLogger(TelephoneNumberValidationEH.class.getName());
    private static final String TELEPHONE_NUMBER_REGEX = "^(\\([0-9]{3}\\)|[0-9]{3}-)[0-9]{3}-[0-9]{4}$";
    
    public void validate(long l, long l1, Orchestration orchestration) 
    {
        logger.log(ODLLevel.NOTIFICATION, "Enter validate() with parameters: [Process Id: {0}], [Event Id: {1}]", new Object[]{l, l1});
        
        HashMap<String, Serializable> contextParams = orchestration.getParameters();
        logger.log(ODLLevel.NOTIFICATION, "Changed parameters: [{0}]", new Object[]{contextParams});
        
        String newTelephoneNumber = getParamaterValue(contextParams, UserManagerConstants.AttributeName.PHONE_NUMBER.getId());
        logger.log(ODLLevel.NOTIFICATION, "Telephone Number Regex: [{0}]", new Object[]{TELEPHONE_NUMBER_REGEX});
        logger.log(ODLLevel.NOTIFICATION, "New Telephone Number: [{0}]", new Object[]{newTelephoneNumber});
        
        // Perform validation on the new telephone number
        if (newTelephoneNumber != null && !newTelephoneNumber.equalsIgnoreCase("")) 
        {
            boolean isTelephoneNumberValidate = newTelephoneNumber.matches(TELEPHONE_NUMBER_REGEX);
            logger.log(ODLLevel.NOTIFICATION, "Validation Passed?: [{0}]", new Object[]{isTelephoneNumberValidate});
            
            // Throw Validation Exception since phone number is invalid
            if (!isTelephoneNumberValidate) 
            {
                // Error message is displayed in UI dialog box
                throw new ValidationFailedException("Format of Telephone Number must be: " + TELEPHONE_NUMBER_REGEX);
            }
        }
    }

    private String getParamaterValue(HashMap<String, Serializable> parameters, String key) 
    {
        if (parameters.containsKey(key)) 
        {
            String value = (parameters.get(key) instanceof ContextAware) ? (String)((ContextAware)parameters.get(key)).getObjectValue() : (String)parameters.get(key);
            return value;
        } 
        
        else 
        {
            return null;
        }
    }

    public void validate(long l, long l1, BulkOrchestration bulkOrchestration) throws ValidationException, ValidationFailedException
    {
    }

    public void initialize(HashMap<String, String> hashMap)
    {
    }
}

Plugin XML
<oimplugins xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <plugins pluginpoint="oracle.iam.platform.kernel.spi.EventHandler">
      <!--<plugin pluginclass="com.blogspot.oraclestack.eventhandlers.SetMiddleNamePreprocessEH" version="1.0" name="SetMiddleNamePreprocessEH"/>-->    
      <plugin name="TelephoneNumberValidationEH" pluginclass="com.blogspot.oraclestack.eventhandlers.TelephoneNumberValidationEH" version="1.0">
   </plugin></plugins>             
</oimplugins>


Custom Event Handlers 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">  
  <!--<action-handler 
   entity-type="User" 
   operation="CREATE" 
   class="com.blogspot.oraclestack.eventhandlers.SetMiddleNamePreprocessEH" 
   name="SetMiddleNamePreprocessEH" 
   stage="preprocess" order="1050" 
   sync="TRUE"/>-->  
  <validation-handler 
   class="com.blogspot.oraclestack.eventhandlers.TelephoneNumberValidationEH" 
   entity-type="User" 
   operation="MODIFY" 
   name="TelephoneNumberValidationEH" 
   order="1000"/>   
</eventhandlers>

4 comments:

  1. What about modifying that field, do we need to deploy another Eh fr that.

    ReplyDelete
  2. If your validation logic is same then no need to create new class but yes you have to create validation handler entry in eventhandlers.xml with same class but different operation in your case modify.

    ReplyDelete
  3. Is there any way to validate telephone number exists in DB or not?

    ReplyDelete