OIM API To Remove Org User Membership Relations


  1. package com.oimacademy.orgs;
  2. import java.sql.CallableStatement;
  3. import java.sql.Connection;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.util.ArrayList;
  8. import java.util.HashSet;
  9. import java.util.List;
  10. import java.util.Set;
  11. import oracle.iam.identity.orgmgmt.api.OrganizationManager;
  12. import oracle.iam.identity.orgmgmt.api.OrganizationManagerConstants;
  13. import oracle.iam.identity.orgmgmt.vo.Organization;
  14. import oracle.iam.platform.entitymgr.spi.relation.RelationshipBulkResult;
  15. import oracle.iam.platform.entitymgr.vo.SearchCriteria;
  16. import oracle.sql.ARRAY;
  17. import oracle.sql.ArrayDescriptor;
  18. import com.oimacademy.connection.DataSource;
  19. import com.oimacademy.connection.Platform;
  20. public class RemoveUserMemberShips {
  21.  /**
  22.   * @param args
  23.   * @throws Exception
  24.   */
  25.  public static void main(String[] args) throws Exception {
  26.   //Search for HOME Organization
  27.   String orgName = "people";
  28.   OrganizationManager orgManager = Platform.getService(OrganizationManager.class);
  29.   final Set<String> returnMap = new HashSet<String>();
  30.   System.out.println("Org Constant "+ OrganizationManagerConstants.AttributeName.ORG_NAME.getId());
  31.   System.out.println("Org Name is " + orgName);  
  32.   SearchCriteria criteria = new SearchCriteria(OrganizationManagerConstants.AttributeName.ORG_NAME.getId(),
  33.     "people", SearchCriteria.Operator.CONTAINS);
  34.   List<Organization> orgs  =orgManager.search(criteria, null, null);  
  35.   List<String> entity1List = new ArrayList<String>();
  36.   List<String> entity2List = new ArrayList<String>();  
  37.   if(!orgs.isEmpty()){
  38. //   Doing only for 1 org key.
  39.    String orgKey = orgs.get(0).getEntityId();
  40.    //Get User Key using Org Key from ORG_USER_MEMBERSHIPS table.
  41.    entity2List = getUserKeyFromOrgUsrMembershipTable(orgKey);
  42.    //Populate OrgKey same as size of Users List.
  43.    if(!entity2List.isEmpty()){
  44.     for(String value : entity2List){      
  45.      entity1List.add(orgKey);   
  46.     }
  47.    }  
  48.   System.out.println(" Invoking Remove Method ");
  49.   RelationshipBulkResult rbr =remove(entity1List,entity2List);
  50.   System.out.println(" Completed with status "+rbr.getStatus());
  51.   }else{
  52.    System.out.println(" Organization Search resulted with Null info ");
  53.   }
  54.  }
  55.  public static RelationshipBulkResult remove(List<String> entity1List, List<String> entity2List) {
  56.   // for now ignoring timestamps
  57.   if (entity1List.size() != entity2List.size()) {
  58.    // they should have the same size
  59.    return new RelationshipBulkResult(
  60.      RelationshipBulkResult.Status.FAILURE);
  61.   }
  62.   Object[] orgKeys = new Object[entity1List.size()];
  63.   for (int i = 0; i < entity1List.size(); i++) {
  64.    orgKeys[i] = new Long(entity1List.get(i));
  65.   }
  66.   Object[] userKeys = new Object[entity2List.size()];
  67.   for (int i = 0; i < entity2List.size(); i++) {
  68.    userKeys[i] = entity2List.get(i);
  69.   }
  70.   RelationshipBulkResult rbr = new RelationshipBulkResult(RelationshipBulkResult.Status.FAILURE);
  71.   Connection conn = null;
  72.   CallableStatement cstmt = null;
  73.   try {   
  74.    conn = DataSource.getConnection();
  75.    ARRAY orgKeysArray = getARRAY(conn, "OIM_TYP_NUMBERARR", orgKeys);
  76.    ARRAY userKeysArray = getARRAY(conn, "OIM_TYP_CHARARR", userKeys);    
  77.    cstmt = conn.prepareCall("{call OIM_ORGANIZATION_MGMT.DeleteOrgMemberships(?,?,?,?,?,?)}");
  78.    int index = 0;
  79.    cstmt.setArray(++index, orgKeysArray);
  80.    cstmt.setArray(++index, userKeysArray);
  81.    cstmt.setString(++index, "1");
  82.    cstmt.setString(++index, "0");
  83.    // out params:
  84.    cstmt.registerOutParameter(++index, java.sql.Types.BIGINT); // param                  // 5
  85.    cstmt.registerOutParameter(++index, java.sql.Types.VARCHAR); // param
  86.                    // 6
  87.    try {
  88.     cstmt.execute();
  89.    } catch (SQLException ee) {
  90.     ee.printStackTrace();
  91.    }
  92.    if (cstmt != null) {
  93.     rbr = new RelationshipBulkResult(RelationshipBulkResult.Status.SUCCESS);
  94.     System.out.println(" Removing Home Org Status is  " +rbr.getStatus());    
  95.    }
  96.   } catch (Exception e) {
  97.    e.printStackTrace();
  98.    rbr.setError(e.getMessage());
  99.   } finally {   
  100.    try {
  101.     if (cstmt != null) {
  102.      cstmt.close();
  103.      cstmt = null;
  104.     }
  105.     if (conn != null) {
  106.       DataSource.closeConnection();
  107.     }
  108.    } catch (Exception e) {
  109.     e.printStackTrace();
  110.    }
  111.   }
  112.   return rbr;
  113.  }
  114.  protected static ARRAY getARRAY(Connection conn, String type,
  115.    Object[] arrayValues) throws Exception {
  116.   Connection metaDataConn = conn
  117.     .unwrap(oracle.jdbc.OracleConnection.class);
  118.   if (metaDataConn == null) {
  119.    throw new Exception("Could not get DB connection from repository");
  120.   }
  121.   ArrayDescriptor arrayDescriptor = ArrayDescriptor.createDescriptor(type
  122.     .toUpperCase(), metaDataConn);
  123.   ARRAY array = new ARRAY(arrayDescriptor, metaDataConn, arrayValues);
  124.   return array;
  125.  } 
  126.  public static List<String> getUserKeyFromOrgUsrMembershipTable(String orgKey) throws SQLException{ 
  127.   Connection con = DataSource.getConnection();
  128.   List<String> usrList = new ArrayList<String>();
  129.   Statement stmt = con.createStatement();
  130.   String query = " select usr_key from org_user_memberships where act_key="+orgKey;
  131.   ResultSet rs = stmt.executeQuery(query);
  132.   while (rs.next()) {
  133.     String usrKey = rs.getString("usr_key");
  134.    System.out.println(" User Key -> : "+ usrKey);
  135.    usrList.add(usrKey);   
  136.   } 
  137.   if(usrList.isEmpty()){
  138.    System.out.println("There is no data in org_user_memberships for the given AUD_JMS_KEY "+ orgKey);
  139.   }
  140.   return usrList;
  141.  } 
  142. }

No comments:

Post a Comment