OIM API to Create SelfRegistartion Request


  1. package com.oimacademy.users;
  2. import java.util.Calendar;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import oracle.iam.identity.usermgmt.api.UserManagerConstants.AttributeName;
  6. import oracle.iam.platform.context.ContextManager;
  7. import oracle.iam.selfservice.uself.uselfmgmt.api.UnauthenticatedSelfService;
  8. import com.oimacademy.connection.Platform;
  9. public class CreateSelfRegistartionRequest {
  10.  public static void main(String[] args) {
  11.   CreateSelfRegistartionRequest selfRequest = new CreateSelfRegistartionRequest();
  12.   System.out.println("Calling registerUser Method..!");
  13.   selfRequest.registerUser();
  14.   System.out.println("End Of registerUser Method..!");
  15.  }
  16.  public String registerUser() {
  17.   System.out.println("Inside registerUser Method..!");  
  18.   System.out.println("Connection Established Without Credentials ..!");
  19.   String requestId = null;
  20.         if (ContextManager.getOIMUser() != null && 
  21.                 !ContextManager.getOIMUser().toString().equalsIgnoreCase("anonymous")) {
  22.             System.out.println ("Before pop context testSubmitRegistrationPasswordAutoGeneratedRequest loggedInUser : " + ContextManager.getOIMUser());
  23.             ContextManager.popContext();
  24.             ContextManager.clearContext();
  25.         }
  26.   UnauthenticatedSelfService unauth = Platform
  27.     .getService(UnauthenticatedSelfService.class);
  28.   Map<String, String> mapAttrRef = new HashMap<String, String>();
  29.   mapAttrRef.put("Last Name", "Last Name");
  30.   mapAttrRef.put("First Name", "First Name");
  31.   mapAttrRef.put("Email", "Email");
  32.   mapAttrRef.put("User Login", "User Login");
  33.   mapAttrRef.put("Password", "Password");
  34.   mapAttrRef.put("act_key", "Organization");
  35.   mapAttrRef.put("Start Date", "Start Date");
  36.   Calendar cal = Calendar.getInstance();
  37.   cal.add(Calendar.MONTH, 1);
  38.   Map<String, Object> mapRequest = new HashMap<String, Object>();
  39.   mapRequest.put("act_key", new Long("1"));
  40.   mapRequest.put("Last Name", "USER");
  41.   mapRequest.put("First Name", "TEST");
  42.   mapRequest.put("Email", "TEST.USER@acme.com");
  43.   mapRequest.put("User Login", "TUSER");
  44.   mapRequest.put("Password", "Welcome1");
  45.   mapRequest.put("Start Date", cal.getTime());
  46.   Map<String, Object> mapQnA = new HashMap<String, Object>();
  47.   mapQnA.put("What is your favorite color?", "1");
  48.   mapQnA.put("What is your mother's maiden name?", "2");
  49.   mapQnA.put("What is the city of your birth?", "3");
  50.   mapRequest=addSuffixToAttribute(mapRequest);
  51.   try {
  52.    requestId = unauth.submitRegistrationRequest(mapRequest, mapQnA,
  53.      "Self-Register User", mapAttrRef);
  54.    System.out.println("requestId ->" + requestId);
  55.   } catch (Exception e) {
  56.    e.printStackTrace();
  57.   }
  58.   return requestId;
  59.  }
  60.  public static Map<String, Object> addSuffixToAttribute(Map<String, Object> attrMap) {
  61.   StringBuffer strBuf = new StringBuffer();
  62.   int suffix = getRandomNumber();
  63.   if (attrMap.get(AttributeName.USER_LOGIN.getId()) != null) {
  64.    String login = attrMap.get(AttributeName.USER_LOGIN.getId())
  65.      .toString();
  66.    System.out.println("Before User Login :" + login);
  67.    strBuf.append(login);
  68.    strBuf.append(suffix);
  69.    attrMap.put("User Login", strBuf.toString());
  70.    System.out.println("After User Login :" + strBuf.toString());
  71.    strBuf.setLength(0);
  72.   }
  73.   if (attrMap.get(AttributeName.FIRSTNAME.getId()) != null) {
  74.    String firstName = attrMap.get(AttributeName.FIRSTNAME.getId())
  75.      .toString();
  76.    System.out.println("Before firstName :" + firstName);
  77.    strBuf.append(firstName);
  78.    strBuf.append(suffix);
  79.    attrMap.put(AttributeName.FIRSTNAME.getId(), strBuf.toString());
  80.    System.out.println("After firstName :" + strBuf.toString());
  81.    strBuf.setLength(0);
  82.   }
  83.   if (attrMap.get(AttributeName.LASTNAME.getId()) != null) {
  84.    String lastName = attrMap.get(AttributeName.LASTNAME.getId())
  85.      .toString();
  86.    System.out.println("Before Last Name :" + lastName);
  87.    strBuf.append(lastName);
  88.    strBuf.append(suffix);
  89.    attrMap.put(AttributeName.LASTNAME.getId(), strBuf.toString());
  90.    System.out.println("After Last Name :" + strBuf.toString());
  91.    strBuf.setLength(0);
  92.   }
  93.   if (attrMap.get(AttributeName.EMAIL.getId()) != null) {
  94.    String email = attrMap.get(AttributeName.EMAIL.getId()).toString();
  95.    System.out.println(" EMAIL Before Modify :" + email);
  96.    strBuf.append(email.replace(".com", suffix + ".com"));
  97.    attrMap.put(AttributeName.EMAIL.getId(), strBuf.toString());
  98.    System.out.println("EMAIL After Modify:" + strBuf.toString());
  99.    strBuf.setLength(0);
  100.   }
  101.   return attrMap;
  102.  }
  103.   public static int getRandomNumber() {
  104.          java.util.Random r = new java.util.Random(Calendar.getInstance()
  105.                  .getTimeInMillis());
  106.          int randint = Math.abs(r.nextInt()) % 1000;     
  107.          return randint;
  108.      }
  109. }

No comments:

Post a Comment