Monday, June 29, 2015

OIM 11G : End to End Process of sending custom emails using notification templates

Step 1

Define the Event. Let me call it
"MyCustomEvent"



 <EventType name="MyCustomEvent">
    <StaticData>
      <Attribute DataType="X2-Entity" EntityName="User" Name="User Login" />
    </StaticData>
    <Resolver class="com.deepak.dubey.notification.resolver.MyCustomEventResolver">
        <Param DataType="X2-Entity" EntityName="User" Name="usr_key" />
        <Param DataType="X2-Entity" EntityName="User" Name="MyCustomAttributeToBePassed" />
    </Resolver>
  </EventType>


Import this in MDS using weblogicImportMetaData.sh

Step 2

Write the Event Resolver Class

package com.deepak.dubey.notification.resolver;

import static oracle.iam.identity.utils.Constants.DISPLAYNAME;
import static oracle.iam.identity.utils.Constants.MLS_BASE_VALUE;
import static oracle.iam.identity.utils.Constants.USERKEY;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import oracle.iam.identity.usermgmt.api.UserManager;
import oracle.iam.identity.vo.Identity;
import oracle.iam.notification.api.NotificationService;
import oracle.iam.notification.impl.NotificationEventResolver;
import oracle.iam.notification.vo.NotificationAttribute;
import oracle.iam.platform.Platform;

public class MyCustomEventResolver implements NotificationEventResolver{

   
    @Override
    public List<NotificationAttribute> getAvailableData(String eventType,
        Map<String, Object> params) throws Exception {
        List<NotificationAttribute> list = new ArrayList<NotificationAttribute>();
        return list;
    }

    @Override
    public HashMap<String, Object> getReplacedData(String eventType,
        Map<String, Object> params) throws Exception {

        HashMap<String, Object> resolvedData = new HashMap<String, Object>();
        UserManager usrMgr = Platform.getService(UserManager.class);
        String userKey = (String) params.get(USERKEY);

        // Mapping token with their actual value for user attributes.
        if (userKey != null) {
         
            NotificationService notificationService =
                Platform.getService(NotificationService.class);
            List<NotificationAttribute> notificationAttributes =
                notificationService.getStaticData(eventType);
         
            /*
             * Configuring user attributes required to be returned in the search
             * aligning with attributes being showed as 'Available Data' in the
             * notification template for this event.
                   */
            Set<String> userRetAttrs = new HashSet<String>();
            for (NotificationAttribute notificationAttribute :
                notificationAttributes.get(0).getSubtree()) {
                userRetAttrs.add(notificationAttribute.getName());
            }
         
            //Getting values for the attributes using userKey
            Identity user = usrMgr.getDetails(userKey, userRetAttrs ,false);
            HashMap<String, Object> userAttributes = user.getAttributes();
         
            /*
             * Creating map containing mapping between tokens available for template
             * to their actual values
             */
            String key = null;
            for (Map.Entry<String, Object>  entry : userAttributes.entrySet()) {
                key = entry.getKey();
                if (key != null) {
                    if ((entry.getValue() instanceof java.util.Map) &&
                        (key.equalsIgnoreCase(DISPLAYNAME))) {
                        key = key.replace(' ', '_');
                        resolvedData.put(key,
                            ((HashMap)entry.getValue()).get(MLS_BASE_VALUE));
                    } else {
                        key = key.replace(' ', '_');
                        resolvedData.put(key, entry.getValue());
                    }
                }
            }
        }
       

        return resolvedData;
    }
}


Step 3

Create plugin.xml

<?xml version="1.0" encoding="UTF-8"?>
<oimplugins xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <plugins pluginpoint="oracle.iam.notification.impl.NotificationEventResolver">
        <plugin pluginclass= "com.deepak.dubey.notification.resolver.MyCustomEventResolver"
            version="1.0" name="My Custom Event Resolver"/>    
    </plugins>
</oimplugins>

Step 4

Create the Notification Template "My Custom Email Template"
$MyCustomAttributeToBePassed
format for custom attributes

Step 5

Write the java code to invoke the notification template

oracle.iam.notification.api.NotificationService notsvc = oimClient
                .getService(oracle.iam.notification.api.NotificationService.class);
        oracle.iam.notification.vo.NotificationEvent notevent = new oracle.iam.notification.vo.NotificationEvent();
        String[] receiverUserIds = { managerLogin, roleApprover };
        notevent.setUserIds(receiverUserIds);
        notevent.setTemplateName("Custom Email Template");
        java.util.HashMap templateParams = new java.util.HashMap();
        templateParams.put("usr_key", beneficiaryID);
        templateParams.put("request_id", reqId);
        templateParams.put("role_name", roleName);
        templateParams.put("requester_name", requesterDisplayName);
templateParams.put("requester_name", MyCustomAttributeToBePassed);
        notevent.setSender(null);
        notevent.setParams(templateParams);
        System.out.println("Sending Notification");
        notsvc.notify(notevent);


Package the java code and plugin.xml as a zip file and register using
ant -f pluginregistration.xml register