Sunday, April 28, 2024

BusinessRoles Creation Utlity

   import java.io.BufferedReader;

  import java.io.File;

  import java.io.FileNotFoundException;

  import java.io.FileReader;

  import java.io.IOException;

  import java.util.ArrayList;

  import java.util.HashMap;

  import java.util.List;

  import java.util.Map;

  import org.apache.commons.logging.Log;

  import sailpoint.api.SailPointContext;

  import sailpoint.object.Application;

  import sailpoint.object.Bundle;

  import sailpoint.object.Filter;

  import sailpoint.object.Identity;

  import sailpoint.object.Profile;

  import sailpoint.tools.GeneralException;

  import sailpoint.tools.RFC4180LineParser;


  private void buildBusinessRole(HashMap roleHash) {


    String roleName = roleHash.get("Business Role Name").toString().trim();

    String roleDesc = roleHash.get("Business Role Description").toString().trim();

    String roleOwner = roleHash.get("Business Role Owner").toString().trim();

    String itRole = roleHash.get("IT Role Name").toString().trim();


    Map desc = new HashMap();

    desc.put("en_US", roleDesc);


    Bundle role = null;

    Identity identity = null;

    Identity workGroupOwner = null;


    try {

      role = context.getObjectByName(Bundle.class, roleName);

      if (role == null) {

        role = new Bundle();

      }

      identity = context.getObjectByName(Identity.class, roleOwner);


      if (identity == null) {

        // Create the Workgroup for this Business Role

        identity = new Identity();

        identity.setName(roleOwner);

        identity.setDisplayName(roleOwner);

        workGroupOwner = context.getObjectByName(Identity.class, "spadmin");

        identity.setOwner(workGroupOwner);

        identity.setWorkgroup(true);

        context.saveObject(identity);

        context.commitTransaction();

        context.decache(identity);

      }

      

      role.setType("business");

      role.setName(roleName);

      role.setDisplayName(roleName);

      role.setOwner(identity);

      role.setDescriptions(desc);


      RFC4180LineParser entParser = new RFC4180LineParser("|");

      ArrayList<String> itRoles = entParser.parseLine(itRole);


      Bundle bundleObj = context.getObjectByName(Bundle.class, "Mighty IT Roles");

      role.addInheritance(bundleObj);


      // Add the required IT Roles to this Business Role

      for (int e = 0; e < itRoles.size(); e++) {

        Bundle requiredRole = context.getObjectByName(Bundle.class, itRoles.get(e));


        if (requiredRole == null) {

          log.error("Reuired role not found...");

        } else {

          role.addRequirement(requiredRole);

        }

      }

      context.saveObject(role);

      context.commitTransaction();

      context.decache(role);

      return;


    } catch (GeneralException e) {

      log.error("GeneralException : " + e.getMessage());

    }

  }


  // Code Execution Starts here


  int lineCounter = 0;

  String dlm = ",";

  String thisLine = "";

  String headerString = "";

  String valueString = "";

  HashMap lineHash = null;

  List headerStrings = new ArrayList();

  String csvFileName = "Path";


  log.debug("Role Creation Started...");


  BufferedReader fileIn = null;

  File bundleFile = null;

  log.debug("Readin Bundle date from : " + csvFileName);


  try {

    bundleFile = new File(csvFileName);


    if ((!bundleFile.exists()) || bundleFile.isDirectory()) {

      log.error("Unable to find the bundle csv file : " + csvFileName);

      return;

    }


    fileIn = new BufferedReader(new FileReader(csvFileName));

    RFC4180LineParser parser = new RFC4180LineParser(dlm);


    while (null != (thisLine = fileIn.readLine())) {

      ArrayList tokens = parser.parseLine(thisLine);


      if (lineCounter == 0) {

        for (int i = 0; i < tokens.size(); i++) {

          headerStrings.add(tokens.get(i).toString().trim());

        }

      } else {

        lineHash = new HashMap();

        for (int i = 0; i < args.length; i++) {

          headerString = headerStrings.get(i).toString();

          valueString = tokens.get(i).toString();


          if (valueString != null) {

            valueString = valueString.trim();

          } else {

            valueString = "";

          }

          lineHash.put(headerString, valueString);

        }

        buildRole(lineHash);

      }

      lineCounter++;


      if ((lineCounter % 20) == 0) {

        context.decache();

      }

    }

  } catch (FileNotFoundException e) {

    log.error("Exception : " + e.getMessage());

  } catch (IOException e) {

    log.error("Exception : " + e.getMessage());

  } catch (GeneralException e) {

    log.error("Exception : " + e.getMessage());

  }

  return "Sucess";

No comments:

Post a Comment

Fetch Members from Workgroup

  import java.util.ArrayList;   import java.util.Iterator;   import java.util.List;   import sailpoint.api.ObjectUtil;   import sailpoint.ob...