Friday, 9 June 2017

OIM DB Connection in Code

Some times you may need to connect to OIM DB inside the code, below is the sample code which helps you on the same..


private Connection getOIMConnection() {
             Connection oimConnection = null;
             try {
                    oimConnection = Platform.getOperationalDS().getConnection();
             } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
             }
             return oimConnection;


       }

OIM Weblogic Data Source Connection

Sometimes we need in our code to connect to OIM Database , we can use the Platform related methods to do this if it is plugin but in ADF codes of OIM , Platform API's doesn't support in such cases, we can use the Weblogic Data sources to get the DB Connection.

Below is the sample code for the same.

private Connection getConnectWLSDataSource(){

    Connection conn=null;

try {
Properties prop = new Properties();



prop.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");

String NameProviderUrl="hostName url"; // Basically the URL of your OIM, better to get from a Lookup as this will change from env to env, so better not to hard code in code
Context ctx = new InitialContext(prop);

prop.put(Context.PROVIDER_URL, NameProviderUrl);

Object obj = ctx.lookup("jdbc/ApplicationDBDS");
System.out.println("Data Source Found");

DataSource ds = (DataSource) obj;
conn = ds.getConnection();
System.out.println("Data Source User Name::"+conn.getMetaData().getUserName());




} catch (Exception e) {
e.printStackTrace();
}

return conn;

}

Wednesday, 7 June 2017

Setting the context to Admin when running the OIM API in code

OIM API's run with logged-in users context and not the Admin user context but there are situations where this logged-in user may not have enough capabilities to perform our desired operations.

In such cases We can add below steps and perform our code to run in Admin context... FYI I have used this in the request Data Validator for one of our requirement.

String ContextUserbeforeUpdate=ContextManager.getOIMUser();
         
       System.out.println("Context OIM user Before Updating with Admin Context:  " +ContextManager.getOIMUser());
        
HashMap<String,String> map = new HashMap<String,String>();
               map.put(UserManagerConstants.AttributeName.USER_KEY.getId(), "1");//Passing the User Key of XELSYSADM i.e. 1 , need to pass it as hardcoded as existing context User is not having the search capability to get this key based on Login(XELSYSADM) but anyhow 1 is usrkey for XELSYSADM always.
               ContextManager.setOIMUser("XELSYSADM");
               ContextManager.setUserDetails(map); //Sets the Context Details with XELSYSADM Details
               ContextManager.pushContext(null, ContextManager.ContextTypes.ADMIN"");
      
       System.out.println("Context OIM user after setting with Admin Context:  " +ContextManager.getOIMUser());
       
               //Perform your operations start ------   
RoleManager roleManager= Platform.getService(RoleManager.class);
Set<String> retKey = new HashSet<String>();
SearchCriteria criteria;
criteria = new SearchCriteria(RoleAttributeName.NAME.getId(), roleName, SearchCriteria.Operator.EQUAL);
List<Role> role=roleManager.search(criteriaretKeynull);
//Perform your operations end -------
              ContextManager.popContext();
            
             System.out.println("Context OIM user after popup:  " +ContextManager.getOIMUser());
            
             ContextManager.setOIMUser(ContextUserbeforeUpdate);
              System.out.println("Context All Values after Setting Back the User:  "+ContextManager.getAllValuesFromCurrentContext());
               System.out.println("Context OIM user after Setting Back the User: " +ContextManager.getOIMUser());

Get All the Child Organizations of a given Organization including sub hierarchies

public static List getChildorganizations(String orgkey) {

             List<String> childOrgList = new ArrayList<>();
             RoleManager roleMgr = oimClient.getService(RoleManager.class);
             tcAccessPolicyOperationsIntf accessPolicyIntf = oimClient.getService(tcAccessPolicyOperationsIntf.class);
             ApplicationInstanceService AppInstSer = oimClient.getService(ApplicationInstanceService.class);
             EntityPublicationService entityPubService = oimClient.getService(EntityPublicationService.class);

             OrganizationManager orgManager = oimClient.getService(OrganizationManager.class);

             try {

                    HashMap<String, Object> configParams = new HashMap<String, Object>();
                    Set<String> retAttrs = new HashSet<String>();
                    List<Organization> ChildOrg = new ArrayList<Organization>();
                    ChildOrg = orgManager.getChildOrganizations(orgkeyretAttrsconfigParams);

                    System.out.println("Child Organizations Details : " + ChildOrg);

                    if (ChildOrg != null) {

                           for (int s = 0; s < ChildOrg.size(); s++) {

                                 String childorgkey = ChildOrg.get(s).getEntityId();
                                 System.out.println("Child Org Key : " + childorgkey);

                                 childOrgList.add(childorgkey);

                                 // System.out.println("Child Org List : " +childOrgList);

                                 List<Organization> grandChildOrg = new ArrayList<Organization>();
                                 grandChildOrg = orgManager.getChildOrganizations(childorgkeyretAttrsconfigParams);
                                 if (grandChildOrg != null) {

                                        for (int a = 0; a < grandChildOrg.size(); a++) {

                                               String grandchildorgkey = grandChildOrg.get(a).getEntityId();
                                               System.out.println("Grand Child Org Key : " + grandchildorgkey);

                                               childOrgList.add(grandchildorgkey);

                                               List<Organization> garndgrandChildOrg = new ArrayList<Organization>();
                                               garndgrandChildOrg = orgManager.getChildOrganizations(grandchildorgkeyretAttrs,
                                                            configParams);
                                               if (garndgrandChildOrg != null) {

                                                     for (int b = 0; b < garndgrandChildOrg.size(); b++) {

                                                            String garndgrandchildorgkey =garndgrandChildOrg.get(b).getEntityId();
                                                            System.out.println("Grand Grand Child Org Key : " +garndgrandchildorgkey);

                                                            childOrgList.add(garndgrandchildorgkey);

                                                     }

                                               }

                                        }

                                 }

                           }

                    }

                    System.out.println("Child Org List outside while: " + childOrgList);

             } catch (Exception e) {
                    e.printStackTrace();
             }

             return childOrgList;

       }