Friday, 26 June 2015

JNDI Functionality Codes

package com.oim;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.ModificationItem;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;

public class LDAPConnection {

String LDAP ="";
String username ="";
String password = "";
LdapContext ctx = null;


public LDAPConnection(){
LDAP = "ldap://localhost:389/";
username = "CN=adminname,OU=accounts,DC=Eas";
password = "oim_user";
}

public LdapContext LDAPConn() {


Hashtable<String, String> env = new Hashtable<String, String>();

env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, username);
env.put(Context.SECURITY_CREDENTIALS, password);
env.put(Context.PROVIDER_URL, LDAP);


try {
ctx = new InitialLdapContext(env, null);
System.out.println("Connected to LDAP");
} catch (NamingException e) {
System.out.println("Problem connecting LDAP:"+e);
e.printStackTrace();
}
return ctx;
}

public void LDAPClose(){
if(ctx!=null){
try {
ctx.close();
System.out.println("LDAP Connection closed");
} catch (NamingException e) {
System.out.println("Problem closing LDAP:"+e);
e.printStackTrace();
}
}
}



public static String getString(Attributes attrs, String propertyName) {
String value = "";
if (null != attrs) {
Attribute attr = attrs.get(propertyName);
if (null != attr) {
value = String.valueOf(attr);
value = value.substring(value.indexOf(": ") + 2).trim();
}
}
return value;
}



/*public String getValues(LdapContext ctx, String username, String adBaseDN) {

String compareVal="";
NamingEnumeration<SearchResult> answer=null;
SearchControls searchCtls = new SearchControls();
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);

String returnedAtts[] = { "sAMAccountName","manager" };

searchCtls.setReturningAttributes(returnedAtts);

String filter = "(samaccountname=" + username + ")";

System.out.printlnfilter);

try {
answer = ctx.search(adBaseDN, filter,searchCtls);

if (answer.hasMoreElements()) {
SearchResult sr = answer.next();
Attributes attr = sr.getAttributes();

compareVal = getString(attr, returnedAtts[1]);
System.out.println"ComparedVal-->" + compareVal);
}



} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return compareVal;
}
*/

public String checkIfUidNumberExists(LdapContext ctx, String username, String adBaseDN)
{
System.out.println("MPCSBulkLoadScheduler -> getDN Started..");
String getVal = "";
SearchControls searchCtls = new SearchControls();
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
searchCtls.setCountLimit(1);
searchCtls.setTimeLimit(0);
String returnedAtts[] = { "uidNumber" };

searchCtls.setReturningAttributes(returnedAtts);

String filter = "(sAMAccountName=" + username + ")";
// String filter = "(manager=" + username + ")";
System.out.println(filter);
NamingEnumeration<SearchResult> answer;
try {
answer = ctx.search(adBaseDN, filter,searchCtls);
if (answer.hasMoreElements()) {
SearchResult sr = answer.next();
Attributes attr = sr.getAttributes();
getVal = getString(attr, returnedAtts[0]);

System.out.println("GetDN-->" + getVal);
}
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return getVal;



}

public String getDN(LdapContext ctx, String username, String adBaseDN)
throws NamingException, IOException {
System.out.println("MPCSBulkLoadScheduler -> getDN Started..");
String getDN = "";
SearchControls searchCtls = new SearchControls();
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);

String returnedAtts[] = {"distinguishedName"};

searchCtls.setReturningAttributes(returnedAtts);

String filter = "(samaccountname=" + username + ")";
// String filter = "(manager=" + username + ")";
System.out.println(filter);
NamingEnumeration<SearchResult> answer = ctx.search(adBaseDN, filter,searchCtls);

if (answer.hasMoreElements()) {
SearchResult sr = answer.next();
Attributes attr = sr.getAttributes();
getDN = getString(attr, returnedAtts[0]);
System.out.println(getDN);
}
if (getDN == null || getDN.equals(""))
getDN = "";
System.out.println("MPCSBulkLoadScheduler -> getDN End..");
return getDN;
}

public ArrayList<String> getDN1(LdapContext ctx, String username, String adBaseDN)
throws NamingException, IOException {
System.out.println("getDN Started..");
String getDN = "";
String getUID = "";
ArrayList<String> list =new ArrayList<String>();
SearchControls searchCtls = new SearchControls();
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);

String returnedAtts[] = {"distinguishedName","uidNumber"};

searchCtls.setReturningAttributes(returnedAtts);

String filter = "(samaccountname=" + username + ")";
NamingEnumeration<SearchResult> answer = ctx.search(adBaseDN, filter,searchCtls);

if (answer.hasMoreElements()) {
SearchResult sr = answer.next();
Attributes attr = sr.getAttributes();
getDN = getString(attr, returnedAtts[0]);
getUID = getString(attr, returnedAtts[1]);

list.add(getDN);
list.add(getUID);

System.out.println("DN and uidNumber Value in AD:"+list);
System.out.println(list.get(0));
System.out.println(list.get(1));
}

System.out.println("getDN End..");
return list;
}

public void updateAttr(LdapContext ctx){


   ModificationItem[] mods = new ModificationItem[2];

   Attribute mod0 = new BasicAttribute("displayName", "retert");
   Attribute mod1 = new BasicAttribute("mail", "Aerwerrwe105@tt.com");

   mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, mod0);
   mods[1] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, mod1);

   try {
ctx.modifyAttributes("CN=cfsdfsdfsdfsf", mods);
System.out.println("Attribute Upadated");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

  public void updateAttr(LdapContext ctx,String Userdn,String baseDN,String[] attributeValToUpdate){

int count=0;
Attribute mod =null;

String splitUpdateAttr="uid,gid,gec,unix,login";
String[] newVal = splitUpdateAttr.split(",");
for (String finale : newVal) {
count++;
}
System.out.println("No of attributes to Update:"+count);

ModificationItem[] mods = new ModificationItem[count];
int i=0;
for( i=0;i < count ;i++ ){

mod = new BasicAttribute(newVal[i],attributeValToUpdate[i]);
mods[i] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, mod);
System.out.println(""+mods[i]);
}
try {
System.out.println("Scheduler---"+"CN="+Userdn+",OU= Accounts,"+baseDN);
ctx.modifyAttributes("CN="+Userdn+",OU=Accounts,"+baseDN,mods);
System.out.println("Scheduler---Attribute Upadated");
} catch (NamingException e) {
System.out.println("Scheduler---NamingException:"+e);

e.printStackTrace();
}
}

public static void main(String[] args) {

LDAPConnection conn = new LDAPConnection();
LDAPConnection a = new LDAPConnection();


//conn.checkIfUidNumberExists(a.LDAPConn(), "AALBddfsdf", "DC=EfdfS,DC=Lm");
try {
// conn.getDN1(conn.LDAPConn(),"AMafdsfag","DC=EdasdS,DC=Lm");
conn.updateAttr(conn.LDAPConn(), "fsdfdsf", "OU= Accounts,DC=EdfdS,DC=LM", attributeValToUpdate)
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Clearing the OIM schedulers which is always running using DB triggers

Execute the below commands in OIm DB

1.update qrtz_triggers set next_fire_time=-1,TRIGGER_STATE='COMPLETE' where trigger_name='SCHEDULER_NAME';
2.delete qrtz_fired_triggers where trigger_name='SCHEDULER_NAME';
3.commit;

Friday, 26 April 2013

OES Policy import xml issue causes

Recently i was importing a OES application using xml import utility,there were couple of issues which i faced.The import functionality terminated saying "invalid attribute undefined"

Causes:

Issue 1:In Policy xml, in Authorization policy xml section i have added the role attributes first followed by group attributes.

Issue 2:Added constraints in policy xml and have the  role attributes first followed by group attributes.

Solution:

Added the group entries first and followed by role attributes.This helped me in rosolving the issue

Wednesday, 13 February 2013

Update OIM Form Details

public void updatehannel(String procInstKey, String Channel) {
  HashMap map = new HashMap();
  map.put("UD_SB_TYPE",Channel);
  try {
   moform.setProcessFormData(Long.parseLong(procInstKey), map);
  } catch (tcAPIException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (NumberFormatException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (tcInvalidValueException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (tcNotAtomicProcessException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (tcFormNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (tcRequiredDataMissingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (tcProcessNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

Getting OIM Form Details

public void getFormDetails(String userID, String resName) {
  HashMap map = new HashMap();
  System.out.println("Getting Proc Inst Key");
  long processInstKey = 0;
  ArrayList getlist = getProcessInstKeyOfResources(resName,
    getUserKey(userID));
  try {
   Iterator<Long> procinstKey = getlist.iterator();
   while (procinstKey.hasNext()) {
    long prockey = procinstKey.next();
    System.out.println("Keys--->" + prockey);
    tcResultSet resultset = moform.getProcessFormData(prockey);
    String[] values = resultset.getColumnNames();
    for (String name : values) {
     for (int i = 0; i < resultset.getRowCount(); i++) {
      resultset.goToRow(i);
      String value = resultset.getStringValue(name);
      map.put(name, value);
      map.remove("UD_OID_P_NOTE");
      map.remove("UD_ACC_PROV_NOTE");
      map.remove("UD_WAT_NOTE");
      map.remove("UD_STRM_NOTE");
      map.remove("UD_SBL_P_NOTE");
      map.remove("UD_ADUSER_NOTE");
      map.remove("UD_APR_NOTE");
     }
    }
    System.out.println(map.toString());
   }
  } catch (tcAPIException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (tcNotAtomicProcessException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (tcFormNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (tcProcessNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (tcColumnNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

private ArrayList getProcessInstKeyOfResources(String res_Name, long userkey) {
  long longProcessInstanceKey = 0;
  ArrayList list = new ArrayList();
  try {
   tcResultSet resultGetObjects = moUserUtility.getObjects(userkey);
   int countResultGetObjects = resultGetObjects.getRowCount();
   for (int i = 0; i < countResultGetObjects; i++) {
    resultGetObjects.goToRow(i);
    String objetcName = resultGetObjects
      .getStringValue("Objects.Name");
    String status = resultGetObjects
      .getStringValue("Objects.Object Status.Status");
    longProcessInstanceKey = resultGetObjects
      .getLongValue("Process Instance.Key");
    System.out.println("OBJ-->" + objetcName + "---->" + status
      + "---->" + longProcessInstanceKey);
    list.add(longProcessInstanceKey);
   }
   System.out.println(list);
  } catch (Exception e) {
  }
  return list;
 }

OIM Requests

public void createRequest(String Uid, String Object, String Admin) {
  HashMap map = new HashMap();
  map.put("Requests.Object Request Type", "Add");
  map.put("Requests.Target Type", "U");
  map.put("Requests.Type", "U");
  // map.put("Requests.Considated Data Value",
  // "<Data><Users><User><UserLogin>TestUser</UserLogin><FirstName>Test</FirstName><LastName>User</LastName></User></Users><Resources><Resource Key=\"109\"><ResourceName>iPlanet User</ResourceName></Resource></Resources></Data>"
  // );
  long userKey = getUserKey(Uid);
  System.out.println("UserKey--->" + userKey);
  try {
   long reqKey = moreq.createRequest(map);// THIS INITIATES THE rEQUEST
             // TO THE
             // rEQUESTER(xelsysadm)
   System.out.println("ReqKey=" + reqKey);
   System.out.println("Request INITIATED");
   moreq.addRequestUser(reqKey, userKey);
   System.out.println("Added the request user"); // Add the request to
               // the User
   moreq.addRequestObject(reqKey, getObjectKey(Object), false);
   System.out.println("Object Added the request user"); // Add the
                 // request
                 // to the
                 // Object
   moreq.completeRequestCreation(reqKey);
   System.out.println("Completed and submitted the request"); // Completes
                  // the
                  // request
                  // creation
                  // and
                  // waits
                  // for
                  // approval
   // Approve the request
   Thread.sleep(10000);
   approveRequest(reqKey, Admin);
   System.out.println("Admin has approved the request");
  } catch (tcAPIException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (tcAttributeMissingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (tcInvalidAttributeException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (tcRequestInvalidException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (tcRequestNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (tcRequestUserInvalidException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (tcRequestObjectInvalidException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 public void approveRequest(long reqKey, String Uid) {
  HashMap map = new HashMap();
  map.put("Requests.Object Request Type", "Add");
  map.put("Requests.Target Type", "U");
  map.put("Requests.Type", "U");
  long userKey = getUserKey(Uid);// Admin user has to be passed
          // (requester---xelsysadm)
  try {
   moreq.setRequestResponse(reqKey, userKey, "Approve");
   System.out.println("completed");
  } catch (tcAPIException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (tcInvalidResponseException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (tcAwaitingObjectDataCompletionException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (tcAwaitingApprovalDataCompletionException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  /*
   * try { tcResultSet set;
   * set=moreq.getApprovalTasksAssignedToUser(userKey, map); String[]
   * coumnName=set.getColumnNames();
   *
   * for(String cName : coumnName){
   *
   * System.out.println(cName); System.out.println(set.getRowCount());
   *
   * for (int i = 0; i < set.getRowCount(); i++) { set.goToRow(i); String
   * value = set.getStringValue(cName);
   * System.out.println(cName+"---->"+value);
   *
   * } }
   *
   * System.out.println("Request Approval Initiated...");
   *
   * } catch (tcAPIException e) { // TODO Auto-generated catch block
   * e.printStackTrace(); }catch (tcUserNotFoundException e) { // TODO
   * Auto-generated catch block e.printStackTrace(); } catch
   * (tcAttributeNotFoundException e) { // TODO Auto-generated catch block
   * e.printStackTrace(); } catch (tcColumnNotFoundException e) { // TODO
   * Auto-generated catch block e.printStackTrace(); }
   */
 }

Writing OIM Details to Excel

 public void WriteToExcel(String UserID) {
  XSSFWorkbook wb = new XSSFWorkbook();
 
  String Id = "";
  String Fn = "";
  String Ln = "";
  String Org = "";
  String Status = "";
  String Cdate="";
  try {
   System.out.println("Creating File...");
   FileOutputStream finalOutput = new FileOutputStream(
     "D:/Newfolder/AD.xls");
   int rowCount = 0;
   XSSFRow outputRow = null;
   XSSFCell cell;
   XSSFSheet reportSheet = wb.createSheet("User Details");
  
   System.out.println("Writing Records...");
  
   XSSFRow headerRow = reportSheet.createRow(rowCount++);
   cell = headerRow.createCell(0, Cell.CELL_TYPE_STRING);
   cell.setCellValue("UserId");
   cell = headerRow.createCell(1, Cell.CELL_TYPE_STRING);
   cell.setCellValue("First Name");
   cell = headerRow.createCell(2, Cell.CELL_TYPE_STRING);
   cell.setCellValue("Last Name");
   cell = headerRow.createCell(3, Cell.CELL_TYPE_STRING);
   cell.setCellValue("Organization");
   cell = headerRow.createCell(4, Cell.CELL_TYPE_STRING);
   cell.setCellValue("Status");
   cell = headerRow.createCell(5, Cell.CELL_TYPE_STRING);
   cell.setCellValue("Created Date");
  
  
   ArrayList getuserMap=getDetails(UserID);
  
  
   Iterator<HashMap> listval = getuserMap.iterator();
   while (listval.hasNext()) {
    HashMap values = listval.next();
    System.out.println("Keys--->" + values);
   
    Id=(String) values.get("Users.User ID");
    Fn=(String) values.get("Users.First Name");
    Ln=(String) values.get("Users.Last Name");
    Org=(String) values.get("Users.Xellerate Type");
    Status=(String) values.get("Users.Status");
    Cdate=(String) values.get("Users.Creation Date");   
  
  
  
  
   outputRow = reportSheet.createRow(rowCount++);
   cell = outputRow.createCell(0, Cell.CELL_TYPE_STRING);
   cell.setCellValue(Id);
   cell = outputRow.createCell(1, Cell.CELL_TYPE_STRING);
   cell.setCellValue(Fn);
   cell = outputRow.createCell(2, Cell.CELL_TYPE_STRING);
   cell.setCellValue(Ln);
   cell = outputRow.createCell(3, Cell.CELL_TYPE_STRING);
   cell.setCellValue(Org);
   cell = outputRow.createCell(4, Cell.CELL_TYPE_STRING);
   cell.setCellValue(Status);
   cell = outputRow.createCell(5, Cell.CELL_TYPE_STRING);
   cell.setCellValue(Cdate);
  
  
  
   }
  
   wb.write(finalOutput);
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 public ArrayList getDetails(String userID) {
 
  System.out.println("Getting the details of the user"+userID);
  ArrayList<HashMap> list=new ArrayList<HashMap>();
 
  String Fn = "";
  String Ln = "";
  String Org = "";
  String Status = "";
  String Cdate="";
  HashMap userMap = new HashMap();
  HashMap resultMap;
  try {
   userMap.put("Users.User ID", userID);
   tcResultSet moResultSet = moUserUtility.findUsers(userMap);
   for (int i = 0; i < moResultSet.getRowCount(); i++) {
    moResultSet.goToRow(i);
    userID = moResultSet.getStringValue("Users.User ID");
    Fn = moResultSet.getStringValue("Users.First Name");
    Ln = moResultSet.getStringValue("Users.Last Name");
    Org = moResultSet.getStringValue("Users.Xellerate Type");
    Status = moResultSet.getStringValue("Users.Status");
    Cdate = moResultSet.getStringValue("Users.Creation Date");
    resultMap = new HashMap();
    resultMap.put("Users.User ID", userID);
    resultMap.put("Users.First Name", Fn);
    resultMap.put("Users.Last Name", Ln);
    resultMap.put("Users.Xellerate Type", Org);
    resultMap.put("Users.Status", Status);
    resultMap.put("Users.Creation Date", Cdate);
    list.add(resultMap);
    //System.out.println(list);
   }
  
  
  
   System.out.println("Returning HashMap");
  
  } catch (Exception e) {
   e.printStackTrace();
  }
  return list;
 }