Friday, May 2, 2014

OIM 11g R2 : Set Last Disable Date

This post covers a sample event handler code that will set a custom attribute to the date whenever the user is last disabled.
If user is enabled the custom attribute is cleared.

It runs on a single event as well as bulk event like trusted recon for user entity.
It listens for Disable and Enable Event.

  1 package com.dubey.deepak.iam.oim.event.handler;
  2 
  3 import java.io.Serializable;
  4 import java.text.DateFormat;
  5 import java.text.SimpleDateFormat;
  6 import java.util.Date;
  7 import java.util.HashMap;
  8 import java.util.Map;
  9 import java.util.Set;
 10 
 11 import oracle.iam.platform.Platform;
 12 import oracle.iam.platform.context.ContextAware;
 13 import oracle.iam.platform.kernel.spi.PostProcessHandler;
 14 import oracle.iam.platform.kernel.vo.AbstractGenericOrchestration;
 15 import oracle.iam.platform.kernel.vo.BulkEventResult;
 16 import oracle.iam.platform.kernel.vo.BulkOrchestration;
 17 import oracle.iam.platform.kernel.vo.EventResult;
 18 import oracle.iam.platform.kernel.vo.Orchestration;
 19 import Thor.API.tcResultSet;
 20 import Thor.API.Exceptions.tcAPIException;
 21 import Thor.API.Exceptions.tcStaleDataUpdateException;
 22 import Thor.API.Exceptions.tcUserNotFoundException;
 23 import Thor.API.Operations.tcLookupOperationsIntf;
 24 import Thor.API.Operations.tcUserOperationsIntf;
 25 
 26 public class LastDisabledDate implements PostProcessHandler {
 27 
 28  private String getTodaysDate() {
 29   Date today = new Date();
 30   System.out.println("today ->" + today);
 31   DateFormat formatter = new SimpleDateFormat("yyyyMMdd");
 32   String todayFormatted = formatter.format(today);
 33   System.out.println("todayFormatted ->" + todayFormatted);
 34   return todayFormatted;
 35  }
 36 
 37  public EventResult execute(long processId, long eventId,
 38    Orchestration orchestration) {
 39   System.out
 40     .println("Entering  EventResult of LastDisabledDate");
 41   System.out.println("processId ->"+processId);
 42   System.out.println("eventId ->"+eventId);
 43   System.out.println("orchestration.getOperation() ->"
 44     + orchestration.getOperation());
 45   HashMap<String, Serializable> parameters = orchestration
 46     .getParameters();
 47   Set<String> paramKeys = parameters.keySet();
 48   for (String paramKey : paramKeys) {
 49    System.out.println("paramKey ->" + paramKey);
 50    System.out.println("paramValue ->" + parameters.get(paramKey));
 51   }
 52   String userKey = orchestration.getTarget().getEntityId();
 53   System.out.println("userKey ->" + userKey);
 54   if (orchestration.getOperation().equalsIgnoreCase("DISABLE")) {
 55    System.out.println("Is Disabled");
 56    addLastDisabledDate(userKey, getTodaysDate());
 57    System.out.println("Last Disabled Date - yyyymmdd added correctly");
 58   } else if (orchestration.getOperation().equalsIgnoreCase("ENABLE")) {
 59    addLastDisabledDate(userKey, "");
 60    System.out.println("Last Disabled Date - yyyymmdd added correctly");
 61   }
 62   System.out
 63     .println("Exiting  EventResult of LastDisabledDate");
 64   return new EventResult();
 65  }
 66 
 67  private String addLastDisabledDate(String userKey, String lastDisabledDate) {
 68   System.out.println("Entering  addLastDisabledDate");
 69   tcUserOperationsIntf userOp = ((tcUserOperationsIntf) Platform
 70     .getService(tcUserOperationsIntf.class));
 71   HashMap<String, String> usrMap = new HashMap<String, String>();
 72   usrMap.put("Users.Key", userKey);
 73   tcResultSet resultSet;
 74   String response = null;
 75   try {
 76    resultSet = userOp.findUsers(usrMap);
 77    for (int i = 0; i < resultSet.getRowCount(); i++) {
 78     resultSet.goToRow(i);
 79     Map<String, String> updateUser = new HashMap<String, String>();
 80     updateUser.put("Users.LastDisabledDate", lastDisabledDate);
 81     userOp.updateUser(resultSet, updateUser);
 82     response = "Last Disabled Date Added";
 83     System.out.println("response ->" + response);
 84    }
 85   } catch (tcAPIException e) {
 86    
 87    e.printStackTrace();
 88   } catch (tcUserNotFoundException e) {
 89    
 90    e.printStackTrace();
 91   } catch (tcStaleDataUpdateException e) {
 92    
 93    e.printStackTrace();
 94   } catch (Exception e) {
 95    
 96    e.printStackTrace();
 97   }
 98   System.out.println("Exiting  addLastDisabledDate");
 99   return response;
100  }
101 
102  private String addLastDisabledDate2(String usrLogin, String lastDisabledDate) {
103   System.out.println("Entering  addLastDisabledDate2 lastDisabledDate  --> "+lastDisabledDate);
104   System.out.println("usrLogin ->"+usrLogin);
105   tcUserOperationsIntf userOp = ((tcUserOperationsIntf) Platform
106     .getService(tcUserOperationsIntf.class));
107   Map<String, String> usrMap = new HashMap<String, String>();
108   usrMap.put("Users.User ID", usrLogin);
109   tcResultSet resultSet;
110   String response = null;
111   try {
112    resultSet = userOp.findUsers(usrMap);
113    int count = resultSet.getRowCount();
114    System.out.println("count ->"+count);
115    for (int i = 0; i < count ; i++) {
116     System.out.println("for loop  ->"+i);
117     resultSet.goToRow(i);
118     Map<String, String> updateUser = new HashMap<String, String>();
119     updateUser.put("Users.LastDisabledDate", lastDisabledDate);
120     userOp.updateUser(resultSet, updateUser);
121     response = "Last Disabled Date Added";
122     System.out.println("response ->" + response);
123    }
124   } catch (tcAPIException e) {
125    System.out.println("e ->"+e);
126    e.printStackTrace();
127   } catch (tcUserNotFoundException e) {
128    System.out.println("e ->"+e);
129    e.printStackTrace();
130   } catch (tcStaleDataUpdateException e) {
131    System.out.println("e ->"+e);
132    e.printStackTrace();
133   } catch (Exception e) {
134    System.out.println("e ->"+e);
135    e.printStackTrace();
136   }
137   System.out.println("Exiting  addLastDisabledDate2");
138   return response;
139  }
140 
141  public BulkEventResult execute(long l, long l1,
142    BulkOrchestration bulkOrch) {
143   System.out
144     .println("Entering BulkEventResult of LastDisabledDate");
145   System.out.println("l ->"+l);
146   System.out.println("l1 ->"+l1);
147   String oprType = bulkOrch.getOperation();
148   System.out.println("oprType ->" + oprType);
149   HashMap<String, Serializable>[] bulkParams = bulkOrch
150     .getBulkParameters();
151   for (HashMap<String, Serializable> bulkParam : bulkParams) {
152    System.out.println("bulkParam ->" + bulkParam);
153    Set<String> bulkKeySet = bulkParam.keySet();
154    System.out.println("bulkKeySet ->" + bulkKeySet);
155    String usrLogin = null;
156    String status = null;
157    for (String key : bulkKeySet) {
158     System.out.println("key ->" + key);
159     Serializable serializable = bulkParam.get(key);
160     System.out.println("serializable ->" + serializable);    
161     if (key.equalsIgnoreCase("User Login")) {
162      usrLogin = serializable.toString();
163      System.out.println("usrLogin ->" + usrLogin);
164     }    
165     if (key.equalsIgnoreCase("EMPSTATUSPS")) {
166      status = serializable.toString();
167      System.out.println("status ->" + status);
168     }    
169    }
170    if ((status.equalsIgnoreCase("A"))
171      || (status.equalsIgnoreCase("L"))
172      || (status.equalsIgnoreCase("P"))) {
173     addLastDisabledDate2(usrLogin, "");
174    } else {
175     addLastDisabledDate2(usrLogin, getTodaysDate());
176    }
177   }
178   System.out
179     .println("Exiting BulkEventResult of LastDisabledDate");
180   return new BulkEventResult();
181  }
182 
183  public void compensate(long l, long l1,
184    AbstractGenericOrchestration abstractGenericOrchestration) {
185  }
186 
187  public boolean cancel(long l, long l1,
188    AbstractGenericOrchestration abstractGenericOrchestration) {
189   return false;
190  }
191 
192  public void initialize(HashMap<String, String> hashMap) {
193  }
194 
195  private String getParameterValue(HashMap<String, Serializable> parameters,
196    String key) {
197   String value = (parameters.get(key) instanceof ContextAware) ? (String) ((ContextAware) parameters
198     .get(key)).getObjectValue()
199     : (String) parameters.get(key);
200   return value;
201  }
202  private String getStatusValue(String empStatus) {
203   System.out.println("Entering  getStatusValue empStatus---->"
204     + empStatus);
205   String status = null;
206   if ((empStatus != null) && (!empStatus.trim().equalsIgnoreCase(""))) {
207    tcLookupOperationsIntf lookOpr = null;
208    lookOpr = (tcLookupOperationsIntf) Platform
209      .getService(tcLookupOperationsIntf.class);
210    try {
211     status = lookOpr.getDecodedValueForEncodedValue(
212       "Lookup.MyOrg.Empstatus.to.Status", empStatus);
213    } catch (tcAPIException e) {
214     System.out.println("Exception in  getStatusValue->"
215       + e.getMessage());
216     e.printStackTrace();
217    } catch (Exception e) {
218     System.out.println("Exception in  getStatusValue->"
219       + e.getMessage());
220     e.printStackTrace();
221    }
222   }
223   System.out.println("Exiting  getStatusValue status--->" + status);
224   return status;
225  }
226 
227 }
228 



Plugin.xml

<?xml version="1.0" encoding="UTF-8"?>
<oimplugins>
  <plugins pluginpoint="oracle.iam.platform.kernel.spi.EventHandler">
    <plugin pluginclass=
        "com.dubey.deepak.iam.oim.event.handler.
LastDisabledDate"
         version="1.0"
         name="
LastDisabledDate ">
    </plugin>
 </plugins>
</oimplugins>
Go to OIM_HOME/plugin_utility/ to register the plugin

ant -f  pluginregistration.xml register 
 
Package the plugin in a zip file for it to be registered correctly.