Thursday, 5 April 2018

OIM API - How to Create Role in OIM?

public void createRole(){
    final String logp = CN + " :: createRole() - ";
    logger.info(logp + "START");
   
    String roleName = "TestRole";
    String roleDisplayName = "TestRole";
    String roleDescription = "TestRole";

    RoleManager rolemanager;
    RoleManagerResult result;

    try{
         //getting role manager service
         rolemanager = Platform.getService(RoleManager.class);
        
         //Adding parameters for role creation
         HashMap<String, Object> mapAttrs = new HashMap<String, Object>();
       
         mapAttrs.put(RoleManagerConstants.ROLE_NAME, roleName);
         mapAttrs.put(RoleManagerConstants.ROLE_DISPLAY_NAME, roleDisplayName);
         mapAttrs.put(RoleManagerConstants.ROLE_DESCRIPTION, roleDescription);
        
         Role role = new Role(mapAttrs);
         result = rolemanager.create(role);
        
         logger.info(logp + "Role creation ends, Result is - " + result.getStatus());
       
    }catch(Exception e){
        logger.error(logp + "Exception occured while creating role - " + e, e);
    }

    logger.info(logp + "END");


OR

public void createRole(){
    final String logp = CN + " :: createRole() - ";
    logger.info(logp + "START");
   
    String roleName = "TestRole";
    String roleDisplayName = "TestRole";
    String roleDescription = "TestRole";

    RoleManager rolemanager;
    RoleManagerResult result;

    try{
         //getting role manager service
         rolemanager = Platform.getService(RoleManager.class);
       
         Role role = new Role(roleName);
         role.setName(roleName);
         role.setDisplayName(roleDisplayName);
         role.setDescription(roleDescription);
       
         result = rolemanager.create(role);

         logger.info(logp + "Role creation ends, Result is - " + result.getStatus());

    }catch(Exception e){
        logger.error(logp + "Exception occured while creating role - " + e, e);
    }
   
    logger.info(logp + "END");
}

 
Happy Learning!!!

Sunday, 1 April 2018

OIM API - Code Snippet to Publish Entity to given Organization.

public void publishEntityToOrganization(String entityKey, String type, String targetOrgName, boolean includeSubOrgs) {
    final String logp = CN + " :: publishEntityToOrganization - ";
    LOGGER.debug(logp + "START");

    try {
        PolicyConstants.Resources resourceType = null;

        if("Entitlement".equalsIgnoreCase(type)){
            resourceType = PolicyConstants.Resources.IT_RESOURCE_ENTITLEMENT;
        }else if("Application Instance".equalsIgnoreCase(type)) {
            resourceType = PolicyConstants.Resources.APPLICATION_INSTANCE;
        }else{
            LOGGER.error(logp + "Invalid entity type - " + type);
            throw new Exception("Invalid entity type - " + type);
        }

        List<EntityPublication> newPubList = new ArrayList<EntityPublication>();

        Organization org = orgManager.getDetails("Organization Name", targetOrgName, null);

        EntityPublication entPub = new EntityPublication(entityKey, resourceType, Long.valueOf(org.getEntityId()), includeSubOrgs);
        entPub.setEntityType(type);

        newPubList.add(entPub);
       
        //publish entity to organization
        entityPubService.addEntityPublications(newPubList);
        LOGGER.debug(logp + "Successfully published...");
       
    }catch(Exception e){
            LOGGER.error(logp + "Exception while publishing entity to organization");
    }

    LOGGER.debug(logp + "END");
}


Happy Learning!!!

OIM API - Code Snippet to Revoke Entity from Current Publications(Organizations).

public void revokeEntityFromCurrentPublications(String entityKey, String type){
    final String logp = CN + " :: revokeEntityFromCurrentPublications - ";
    LOGGER.debug(logp + "START");
    EntityPublicationService entityPubService = null;
   
    try {
        entityPubService = Platform.getService(EntityPublicationService.class);
   
        PolicyConstants.Resources resourceType = null;

        if("Entitlement".equalsIgnoreCase(type)){
            resourceType = PolicyConstants.Resources.IT_RESOURCE_ENTITLEMENT;
        }else if("Application Instance".equalsIgnoreCase(type)) {
            resourceType = PolicyConstants.Resources.APPLICATION_INSTANCE;
        }else{
            LOGGER.error(logp + "Invalid entity type - " + type);
            throw new Exception("Invalid entity type - " + type);
        }
       
        //get current publications
        List<EntityPublication> currentPubList = entityPubService.listEntityPublications(resourceType, entityKey, null);
        LOGGER.info(logp + "Obtained current publications for entity " + entityName + " - " + currentPubList);

        //Revoke entity from current publications
        entityPubService.removeEntityPublications(currentPubList);
        LOGGER.debug(logp + "Successfully removed entity from current publications");

    }catch(Exception e){
        LOGGER.error(logp + "Exception while revoking entity from current publications");
    }

    LOGGER.debug(logp + "END");
}


Happy Learning!!!

OIM API - Code Snippet to get Current Publications(Organizations) for the given Entity.

public void getCurrentPublicationsForEntity(String entityKey, String type) {
    final String logp = CN + " :: getCurrentPublicationsForEntity - ";
    LOGGER.debug(logp + "START");
    EntityPublicationService entityPubService = null;
   
    try{
        entityPubService = Platform.getService(EntityPublicationService.class);
       
        PolicyConstants.Resources resourceType = null;

        if("Entitlement".equalsIgnoreCase(type)){
            resourceType = PolicyConstants.Resources.IT_RESOURCE_ENTITLEMENT;
        }else if("Application Instance".equalsIgnoreCase(type)) {
            resourceType = PolicyConstants.Resources.APPLICATION_INSTANCE;
        }else{
            LOGGER.error(logp + "Invalid entity type - " + type);
            throw new Exception("Invalid entity type - " + type);
        }

        //get current publications
        List<EntityPublication> currentPubList = entityPubService.listEntityPublications(resourceType, entityKey, null);
        LOGGER.info(logp + "Obtained current publications for entity " + entityName + " - " + currentPubList);
       
    }catch(Exception e){
        LOGGER.error(logp + "Exception while fetching current publications for entity - " + entityKey);
    }

    LOGGER.debug(logp + "END");
}


Happy Learning!!!

OIM API - Code Snippet to Update Scheduled Job Parameter.

public void updateScheduledJobParameter(String jobName, String paramName, String paramValue) {
    final String logp = CN + " :: updateScheduledJobParameter - ";
    LOGGER.debug(logp + "START");
    SchedulerService schedulerService = null;

    try{
        //get scheduler service
        schedulerService = Platform.getService(SchedulerService.class);
       
        JobDetails jbDetails = schedulerService.getJobDetail(jobName);

        HashMap jobAttributes = jbDetails.getAttributes();
        LOGGER.debug(logp + "Existing job attributes - " + jobAttributes);

        JobParameter jbParam = new JobParameter();
        jbParam.setName(paramName);
        jbParam.setValue(paramValue);

        jobAttributes.put(paramName, jbParam);

        jbDetails.setAttributes(jobAttributes);
        LOGGER.debug(logp + "Updated job attributes - " + jobAttributes);

        schedulerService.updateJob(jbDetails);
        LOGGER.debug(logp + "Job updated successfully");

    }catch(Exception e){
        LOGGER.error(logp + "Exception occured while updating scheduled job parameters - " + e, e);
    }
   
    LOGGER.debug(logp + "END");
}


Happy Learning!!!

OIM API - Code Snippet to Check Scheduled Job Status for the given Scheduled Job.

For checking if scheduled job is running?

public boolean isScheduledJobRunning(String jobName){
    final String logp = CN + " :: isScheduledJobRunning - ";
    LOGGER.debug(logp + "START");
    SchedulerService schedulerService = null;
    boolean result = false;
   
    if (null == jobName || jobName.trim().length() == 0){
        return false;
    }
    jobName = jobName.trim();
   
    try {
        //get scheduler service
        schedulerService = Platform.getService(SchedulerService.class);
       
        int status = schedulerService.getStatusOfJob(jobName);
        LOGGER.info(logp + "Status for job name " + jobName + " = " + status);

        if (status == 5) {
            result = true;
        }
    } catch (Exception e) {
        LOGGER.error(logp + "Exception while checking the schedule job status");
    }
    LOGGER.debug(logp + "END");
    return result;
}



For checking if scheduled job is started?

public boolean isScheduledJobStarted(String jobName) {
    final String logp = CN + "::isScheduledJobRunning - ";
    LOGGER.debug(logp + "START");
    SchedulerService schedulerService = null;
    boolean result = false;
   
    if (null == jobName || jobName.trim().length() == 0) {
        return false;
    }
   
    jobName = jobName.trim();

    try{
        //get scheduler service
        schedulerService = Platform.getService(SchedulerService.class);
       
        int status = schedulerService.getStatusOfJob(jobName);
        LOGGER.info(logp + "Status for job name " + jobName + " = " + status);

        if (status == 2) {
            result = true;
        }
    }catch(Exception e){
        LOGGER.error(logp + "Exception while checking the schedule job status");
    }
    LOGGER.debug(logp + "END");
    return result;
}


Happy Learning!!!

OIM API - Code Snippet to trigger OIM Scheduled Job.

public void triggerScheduledJob(String jobName){
    final String logp = CN + " :: triggerScheduledJob - ";
    LOGGER.debug(logp + "START");
    SchedulerService schedulerService = null;
   
    try {
        //get scheduler service
        schedulerService = Platform.getService(SchedulerService.class);
       
        schedulerService.triggerNow(jobName);
        LOGGER.info(logp + jobName + " invoked successfully");
    }catch(Exception e){
        LOGGER.error(logp + "Exception while invoking scheduled job - " + e, e);
    }

    LOGGER.debug(logp + "END");
}


Happy Learning!!!