Oracle OIM - Search and Update Organization using java API

In this below example I would like to explain how to search and update organization.

To update any organization we need Org_key value. It is like primary key to identify Organization record in OIM.

To find organization matching with the information we have, first form the search criteria with available information and call oim search method. It will return list of matched organizations.

After that we can update this list of Organization objects and pass to oim org manager modify method for update. If we know org_key, then we no need to find for organization first. Instead set all attributes which want to update along with org_key to Organization object and pass it to modify method.

Please refer to below example for the same.

package view;

import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Set;

import javax.security.auth.login.LoginException;

import oracle.adf.share.logging.ADFLogger;

import oracle.iam.identity.exception.AccessDeniedException;
import oracle.iam.identity.exception.OrganizationManagerException;
import oracle.iam.identity.exception.RoleCategorySearchException;
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.OIMClient;
import oracle.iam.platform.entitymgr.vo.SearchCriteria;


public class UpdateOrganization {
    private static ADFLogger logger = ADFLogger.createADFLogger(CreateOrganization.class);
    //OIM User name
    public String oimUserName;
    // OIM Password
    public String oimPassword;
    public static final String INITIAL_CONTEXT_FACTORY = "weblogic.jndi.WLInitialContextFactory";
    public static final String WLS = "wls";
    public static final String OIM_APP_SERVER_TYPE = "OIM.AppServerType";
    public static final String APPSERVER_TYPE = "APPSERVER_TYPE";
    //OIM URL to connect throught browser. here we use t3 protocal to communicate with server.
    public static final String OIM_SERVER_URL = "t3://hostname.domain.com:14000";
    public static final String JAVA_SECURITY_AUTH_LOGIN_CONFIG = "java.security.auth.login.config";

    /**
     * Autherization configuration file location. In production environment we need
     * read this file from weblogic location. Use weblogic location relative path instead
     * of below value in that case
     */
    public static final String SERVER_AUTH_CONF_FILE =
        "D:\\MyPersonalDocuments\\Projects\\OIM_Objects_Creation\\propertyFile\\authwl.conf";
    //OIMClient class to connect to OIM server
    private OIMClient oimClient;
    // OrganizationManager class helpful to create/update/delete organizations in OIM
    public OrganizationManager orgManager;

    public UpdateOrganization() {
        super();
    }


    /**
     * I used this parameterized constructor to establish connection with OIM. Inside this constructor
     * establishConnection method takes userName and password as parameter and establish connection
     * with OIM.
     * initialize() method will initialze OrganizationManager class.
     * @param username
     * @param password
     */
    public UpdateOrganization(String username, String password) {
        super();
        this.oimUserName = username;
        this.oimPassword = password;
        long startTime = System.currentTimeMillis();
        logger.fine("UpdateOrganization constructor Starts");
        try {
            //establishing OIM connection
            establishConnection(oimUserName, oimPassword);
            //initializing OrganizationManager class
            initialize();
        } catch (RoleCategorySearchException e) {
            logger.severe("UpdateOrganization constructor Exception: " + e.toString());
        } catch (Throwable e) {
            logger.severe("UpdateOrganization constructor Exception: " + e.toString());
        }
        logger.fine("UpdateOrganization constructor  Ends");
        logger.fine("Time taken for OIMServiceFacade constructor : " + (System.currentTimeMillis() - startTime));
    }
    /**
     * This establish connection with weblogic server where OIM deployed.
     * @param username
     * @param password
     * @throws RoleCategorySearchException
     */
    private void establishConnection(String username, String password) throws RoleCategorySearchException {
        long startTime = System.currentTimeMillis();
        logger.fine("UpdateOrganization establishConnection Starts");
        logger.fine("UpdateOrganization establishConnection Starts11");
        Hashtable<Object, Object> env = new Hashtable<Object, Object>();
        env.put(OIMClient.JAVA_NAMING_FACTORY_INITIAL, INITIAL_CONTEXT_FACTORY);
        System.setProperty(OIM_APP_SERVER_TYPE, WLS);
        System.setProperty(APPSERVER_TYPE, WLS);

        env.put(OIMClient.JAVA_NAMING_PROVIDER_URL, OIM_SERVER_URL);
        System.setProperty(JAVA_SECURITY_AUTH_LOGIN_CONFIG,
                           SERVER_AUTH_CONF_FILE); //Path of authwl.conf file according to the environment

        oimClient = new oracle.iam.platform.OIMClient(env);
        try {
            logger.fine("UpdateOrganization establishConnection: with password " + username + ":" + password);
            logger.fine("Username : " + username);
            logger.fine("password : " + password);
            oimClient.login(oimUserName, oimPassword.toCharArray(), env);
            logger.fine("UpdateOrganization establishConnection: Connected to OIM");
        } catch (LoginException e) {
            logger.severe("UpdateOrganization establishConnection exception :" + e.toString());
            logger.severe("Time taken for establishConnection(exception): " +
                          (System.currentTimeMillis() - startTime));
            return;
        } catch (Throwable e) {
            logger.severe("UpdateOrganization establishConnection exception :" + e.toString());
            logger.severe("Time taken for establishConnection(exception): " +
                          (System.currentTimeMillis() - startTime));
            return;
        }
        logger.fine("UpdateOrganization establishConnection Ends");
        logger.fine("Time taken for establishConnection: " + (System.currentTimeMillis() - startTime));
    }
    /**
     * This method initializes orgManager.
     */
    private void initialize() {
        logger.fine("UpdateOrganization Initialize Starts");
        try {
            orgManager = oimClient.getService(OrganizationManager.class);
        } catch (Throwable e) {
            logger.severe("Unexpected exception occuredOIMServiceFacade constructor" + e.toString());
        }
        logger.fine("UpdateOrganization Initialize Ends");
    }

    /**
     * This method accepts orgKey and new OrganizationName as parameter and update it in OIM
     * We can set more attributes to Organization object
     * orgKey is used to identity Organization
     * @param entityId
     * @param OrganizationName
     * @return
     * @throws OrganizationManagerException
     * @throws AccessDeniedException
     */
    public String updateOrganization(String entityId, String OrganizationName) throws OrganizationManagerException,AccessDeniedException{
       String modifyOrg = null;
        try {
            Organization org = new Organization(entityId);
            //In this example I am updating only Name, we can set more attributes to update
            org.setAttribute("Organization Name", OrganizationName);
            modifyOrg = modifyOrganization(org);
        } catch (OrganizationManagerException e) {
            e.printStackTrace();
            throw e;
            }catch (AccessDeniedException e) {
            e.printStackTrace();
            throw e;
            }
        return modifyOrg;
   
    }
    /**
     * After populating the Organization attributes pass this organization object
     * to orgManager modify method to update Org in OIM
     * @param organization
     * @return
     * @throws OrganizationManagerException
     * @throws AccessDeniedException
     */
    public String modifyOrganization(Organization organization) throws OrganizationManagerException,AccessDeniedException{
       String modifyOrg = null;
        try {
            //Modifying Org in OIM
            modifyOrg = orgManager.modify(organization);
        } catch (OrganizationManagerException e) {
            e.printStackTrace();
            throw e;
            }catch (AccessDeniedException e) {
            e.printStackTrace();
            throw e;
            }
        return modifyOrg;
   
    }
    /**
     *Below method is used to find organization key. in this example I am finding Organization
     * based on only Organization name. We can pass more criteria values to find Org. It returns
     * list of Organizations matching the criteria
     *
     * @param organizationName
     * @return
     * @throws OrganizationManagerException
     * @throws AccessDeniedException
     */
    public List<Organization> findOrganization(String organizationName) throws OrganizationManagerException,AccessDeniedException{
    //The list of attributes which are to be returned for each Organization.
    Set<String> retAttrs = new HashSet<String>();
    retAttrs.add(OrganizationManagerConstants.AttributeName.ID_FIELD.getId());
    retAttrs.add(OrganizationManagerConstants.AttributeName.ORG_NAME.getId());
    //Buding search criteria to search or organization
        SearchCriteria vendorCriteria =
                    new SearchCriteria(OrganizationManagerConstants.AttributeName.ORG_NAME.getId(),
                                       organizationName,
                                       SearchCriteria.Operator.EQUAL);
        List<Organization> orgs = null;
        try {
            //Search for organization
         orgs = orgManager.search(vendorCriteria, retAttrs, null);
        } catch (OrganizationManagerException e) {
            e.printStackTrace();
            throw e;
            }catch (AccessDeniedException e) {
            e.printStackTrace();
            throw e;
            }
        return orgs;
   
    }
    /**
     * I am testing above update organization method using this main class.
     *
     * @param args
     */
    public static void main(String[] args) {
        UpdateOrganization updateOrganization = new UpdateOrganization("username", "password");

        try {
            List<Organization> orgList = null;
            String orgkey = null;
             orgList = updateOrganization.findOrganization("CreateOrgForBlogInDev");
             if(orgList != null && orgList.size() >0){
                 orgkey = updateOrganization.updateOrganization(orgList.get(0).getEntityId(), "UpdateOrgForBlogInDev");
             }
        } catch (OrganizationManagerException e) {
            e.printStackTrace();
        }
    }


}

Comments

Post a Comment

Popular posts from this blog

BEA-000362 Server failed. Reason: [Management:141268]Parsing Failure in config.xml

Trim Leading and Trailing spaces from InputText on tab out.