Wednesday, August 29, 2012

User Management Scheduler - Scheduler for creating Users


User Management Scheduler

Step 1: Create a Project, through: File-> New-> Other


Step 2: Navigate to Portal Application-> Create a Portal Application Object.-> Next

Step 3: Give the project name : UserManagementScheduler and choose the location where you want to create the application. → Finish


Step 4: Now, create a template for your job through → Right click on src.comp-> New-> Other


Step 5: Select Repository Framework 7.1.5 → Scheduler Task Wizard-> Next

Step 6: Give your class name, package and Eclipse Project Name → Finish



Step 7: Right click on your project → Properties

Step 8: select Java Build Path → under Libraries tab → Click Add Variable



Step 9: Select ECLIPSE_HOME → Extend → Plugins → com.sap.km.rfwizard_7.1.5 → lib → bc.crt_api.jar → OK → OK → OK and also include bc.sf.service.scheduler_api.jar file the same way.

Step 10: Open create.java file and write following code under run method:
java.util.Date currentDt = new java.util.Date();
String TimeStamp=currentDt.getDate()+"_"+(currentDt.getMonth()+1)+"_"+(currentDt.getYear()+1900+"__"+currentDt.getHours()+"_"+currentDt.getMinutes()+"_"+currentDt.getSeconds());

String month="",datee="";

if((currentDt.getMonth()+1) <=9)
{ month="0"+(currentDt.getMonth()+1); }
else
{ month=""+(currentDt.getMonth()+1); }

if((currentDt.getDate()) <=9)
{ datee="0"+(currentDt.getDate()); }
else
{ datee=""+(currentDt.getDate()); }

String tstamp=(currentDt.getYear()+1900)+""+month+""+datee;

FileOutputStream outS,outE,outEX;
PrintStream PS,PE,PEX;

String UserId = "", lineVal="",FirstName = "",LastName = "",Salution = "",Group = "",Role = "";
try
{
StringBuffer contents= new StringBuffer();
StringBuffer error= new StringBuffer();
BufferedReader input = null;
try
{
outS = new FileOutputStream("/User_Enable/AutoCreLog_"+TimeStamp+".txt");
outE = new FileOutputStream("/User_Enable/CreErrorLog_"+TimeStamp+".txt");
PS= new PrintStream(outS);
PS.println("User Log: "+ currentDt);
PS.println("User Log: "+tstamp);
String filePath="/User_Enable/AUC_"+tstamp+".txt";
String uid="";
File testFile = new File(filePath);
input = new BufferedReader( new FileReader(testFile) );
String line = null; //not declared within while loop

while (( line = input.readLine()) != null)
{
ArrayList val = new ArrayList();
contents= new StringBuffer();
contents.append(line);
uid=contents.toString();
PS.println(uid+" from "+filePath);
StringTokenizer st = new StringTokenizer(line,"\t");
while (st.hasMoreTokens())
{
val.add(st.nextToken());
}

for(int i=0; i<val.size();i=i+3)
{
UserId = val.get(i).toString().trim();
FirstName = val.get(i+1).toString().trim();
LastName = val.get(i+2).toString().trim();
try
{
IUserFactory factory = UMFactory.getUserFactory();
IUserAccountFactory accountFactory = UMFactory.getUserAccountFactory();
IUserMaint newUser = factory.newUser(UserId);
newUser.setFirstName(FirstName);
newUser.setLastName(LastName);
newUser.save();
newUser.commit();
newUser.addToGroup("GRUP.PRIVATE_DATASOURCE.un:Client ESS Users");
IUserAccount account =accountFactory.newUserAccount(UserId,newUser.getUid());
account.save();
account.setPassword("client1234");
account.setPasswordChangeRequired(true);
account.commit();
PS.println(account.getLogonUid() + " Created Successfully");
}
catch (Exception e)
{
PE = new PrintStream(outE);
PE.println("Error Log " + currentDt);
PE.println(e.getMessage() + "\n");
}
}
}
input.close();
}
catch (Exception ex)
{
try
{
outE = new FileOutputStream("/User_Enable/FileNotFoundLog_"+TimeStamp+".txt");
PE= new PrintStream(outE);
PE.println("Error Log "+currentDt+"...."+ex+"...");
}
catch(Exception ext)
{
ext.printStackTrace();
input.close();
}
}
}
catch (Exception ex)
{
ex.printStackTrace();
}

Step 11: Build your component and deploy your par and then navigate to following location in portal.
System Administration → System Configuration → Knowledge Management → Configuration → Content Management → Global Services → Scheduler Tasks.
Step 12: You will see your task. Click on it and schedule it to run at a particular time. You can define your new periodic time as well.

Select the time you want to schedule and then click on Apply and OK. In my case am running it every day as 10 am.

No comments:

Post a Comment