OIM API To Remove Org User Membership Relations.

package com.orgs;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import oracle.iam.identity.orgmgmt.api.OrganizationManager;
import oracle.iam.identity.orgmgmt.api.OrganizationManagerConstants;
import oracle.iam.identity.orgmgmt.vo.Organization;
import oracle.iam.platform.entitymgr.spi.relation.RelationshipBulkResult;
import oracle.iam.platform.entitymgr.vo.SearchCriteria;
import oracle.sql.ARRAY;
import oracle.sql.ArrayDescriptor;

import com.connection.DataSource;
import com.connection.Platform;

public class RemoveUserMemberShips {

 /**
  * @param args
  * @throws Exception
  */
 public static void main(String[] args) throws Exception {

  //Search for HOME Organization
  String orgName = "people";
  OrganizationManager orgManager = Platform.getService(OrganizationManager.class);
  final Set<String> returnMap = new HashSet<String>();
  System.out.println("Org Constant "+ OrganizationManagerConstants.AttributeName.ORG_NAME.getId());
  System.out.println("Org Name is " + orgName);  
  SearchCriteria criteria = new SearchCriteria(OrganizationManagerConstants.AttributeName.ORG_NAME.getId(),
    "people", SearchCriteria.Operator.CONTAINS);
  List<Organization> orgs  =orgManager.search(criteria, null, null);
  
  
  List<String> entity1List = new ArrayList<String>();
  List<String> entity2List = new ArrayList<String>();
  
  if(!orgs.isEmpty()){
//   Doing only for 1 org key.
   String orgKey = orgs.get(0).getEntityId();
   //Get User Key using Org Key from ORG_USER_MEMBERSHIPS table.
   entity2List = getUserKeyFromOrgUsrMembershipTable(orgKey);
   //Populate OrgKey same as size of Users List.
   if(!entity2List.isEmpty()){
    for(String value : entity2List){      
     entity1List.add(orgKey);   
    }
   }
  
  System.out.println(" Invoking Remove Method ");
  RelationshipBulkResult rbr =remove(entity1List,entity2List);
  System.out.println(" Completed with status "+rbr.getStatus());
  }else{
   System.out.println(" Organization Search resulted with Null info ");
  }
 }

 

 public static RelationshipBulkResult remove(List<String> entity1List, List<String> entity2List) {

  // for now ignoring timestamps

  if (entity1List.size() != entity2List.size()) {
   // they should have the same size
   return new RelationshipBulkResult(
     RelationshipBulkResult.Status.FAILURE);
  }

  Object[] orgKeys = new Object[entity1List.size()];
  for (int i = 0; i < entity1List.size(); i++) {
   orgKeys[i] = new Long(entity1List.get(i));
  }

  Object[] userKeys = new Object[entity2List.size()];
  for (int i = 0; i < entity2List.size(); i++) {
   userKeys[i] = entity2List.get(i);
  }

  RelationshipBulkResult rbr = new RelationshipBulkResult(RelationshipBulkResult.Status.FAILURE);
  Connection conn = null;
  CallableStatement cstmt = null;

  try {   
   conn = DataSource.getConnection();

   ARRAY orgKeysArray = getARRAY(conn, "OIM_TYP_NUMBERARR", orgKeys);
   ARRAY userKeysArray = getARRAY(conn, "OIM_TYP_CHARARR", userKeys);  
   
   cstmt = conn.prepareCall("{call OIM_ORGANIZATION_MGMT.DeleteOrgMemberships(?,?,?,?,?,?)}");
   int index = 0;
   cstmt.setArray(++index, orgKeysArray);
   cstmt.setArray(++index, userKeysArray);
   cstmt.setString(++index, "1");
   cstmt.setString(++index, "0");
   // out params:
   cstmt.registerOutParameter(++index, java.sql.Types.BIGINT); // param
                  // 5
   cstmt.registerOutParameter(++index, java.sql.Types.VARCHAR); // param
                   // 6

   try {
    cstmt.execute();
   } catch (SQLException ee) {
    ee.printStackTrace();
   }

   if (cstmt != null) {
    rbr = new RelationshipBulkResult(RelationshipBulkResult.Status.SUCCESS);
    System.out.println(" Removing Home Org Status is  " +rbr.getStatus());    
   }
  } catch (Exception e) {
   e.printStackTrace();
   rbr.setError(e.getMessage());
  } finally {   
   try {
    if (cstmt != null) {
     cstmt.close();
     cstmt = null;
    }
    if (conn != null) {
      DataSource.closeConnection();
    }
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
  return rbr;

 }

 protected static ARRAY getARRAY(Connection conn, String type,
   Object[] arrayValues) throws Exception {
  Connection metaDataConn = conn
    .unwrap(oracle.jdbc.OracleConnection.class);
  if (metaDataConn == null) {
   throw new Exception("Could not get DB connection from repository");
  }

  ArrayDescriptor arrayDescriptor = ArrayDescriptor.createDescriptor(type
    .toUpperCase(), metaDataConn);
  ARRAY array = new ARRAY(arrayDescriptor, metaDataConn, arrayValues);

  return array;
 }
 
 
 public static List<String> getUserKeyFromOrgUsrMembershipTable(String orgKey) throws SQLException{ 
  Connection con = DataSource.getConnection();
  List<String> usrList = new ArrayList<String>();
  Statement stmt = con.createStatement();
  String query = " select usr_key from org_user_memberships where act_key="+orgKey;
  ResultSet rs = stmt.executeQuery(query);
  while (rs.next()) {
    String usrKey = rs.getString("usr_key");
   System.out.println(" User Key -> : "+ usrKey);
   usrList.add(usrKey);
   
  } 
  if(usrList.isEmpty()){
   System.out.println("There is no data in org_user_memberships for the given AUD_JMS_KEY "+ orgKey);
  }
  return usrList;
 }
 
}

No comments:

Post a Comment

About OIM

Oracle Identity Management enables organizations to effectively manage the end - to - end life - cycle of user ide...

Popular Posts