Code in Component Controller:-
package sap.corp.comp.co_generateugrfile.com;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import com.sap.security.api.IGroup;
import com.sap.security.api.IGroupFactory;
import com.sap.security.api.IGroupSearchFilter;
import com.sap.security.api.IRole;
import com.sap.security.api.IRoleFactory;
import com.sap.security.api.IRoleSearchFilter;
import com.sap.security.api.ISearchAttribute;
import com.sap.security.api.ISearchResult;
import com.sap.security.api.IUser;
import com.sap.security.api.IUserFactory;
import com.sap.security.api.IUserSearchFilter;
import com.sap.security.api.UMFactory;
import com.sap.tc.webdynpro.progmodel.api.IWDMessageManager;
import sap.corp.comp.co_generateugrfile.com.wdp.IPrivateCO_GenerateUGRFile;
publicclass CO_GenerateUGRFile
/**
*Logginglocation.
*/
privatestaticfinal com.sap.tc.logging.Location logger =
com.sap.tc.logging.Location.getLocation(CO_GenerateUGRFile.class);
static
//@@begin id
String id = "$Id$";
//@@end
com.sap.tc.logging.Location.getLocation("ID.com.sap.tc.webdynpro").infoT(id);
/**
*PrivateaccesstothegeneratedWebDynprocounterpart
*forthiscontrollerclass. </p>
*
*Use<code>wdThis</code>togaintypedaccesstothecontext,
*totriggernavigationviaoutboundplugs,togetandenable/disable
*actions,firedeclaredevents,andaccessusedcontrollersand/or
*componentusages.
*
*@seesap.corp.comp.co_generateugrfile.com.wdp.IPrivateCO_GenerateUGRFileformoredetails
*/
privatefinal IPrivateCO_GenerateUGRFile wdThis;
/**
*Rootnodeofthiscontroller'scontext.</p>
*
*Providestypedaccessnotonlytotheelementsoftherootnode
*butalsotoallnodesinthecontext(methodsnode<i>XYZ</i>())
*andtheircurrentlyselectedelement(methodscurrent<i>XYZ</i>Element()).
*Italsofacilitatesthecreationofnewelementsforallnodes
*(methodscreate<i>XYZ</i>Element()).</p>
*
*@seesap.corp.comp.co_generateugrfile.com.wdp.IPrivateCO_GenerateUGRFile.IContextNodeformoredetails.
*/
privatefinal IPrivateCO_GenerateUGRFile.IContextNode wdContext;
/**
*Ashortcutfor<code>wdThis.wdGetAPI()</code>.</p>
*
*RepresentsthegenericAPIofthegenericWebDynprocounterpart
*forthiscontroller.</p>
*/
privatefinal com.sap.tc.webdynpro.progmodel.api.IWDComponent wdControllerAPI;
/**
*Ashortcutfor<code>wdThis.wdGetAPI().getComponent()</code>.</p>
*
*RepresentsthegenericAPIoftheWebDynprocomponentthiscontroller
*belongsto.Canbeusedtoaccessthemessagemanager,thewindowmanager,
*toadd/removeeventhandlersandsoon.</p>
*/
privatefinal com.sap.tc.webdynpro.progmodel.api.IWDComponent wdComponentAPI;
public CO_GenerateUGRFile(IPrivateCO_GenerateUGRFile wdThis)
this.wdThis = wdThis;
this.wdContext = wdThis.wdGetContext();
this.wdControllerAPI = wdThis.wdGetAPI();
this.wdComponentAPI = wdThis.wdGetAPI().getComponent();
//@@begin javadoc:wdDoInit()
/**Hookmethodcalledtoinitializecontroller.*/
//@@end
publicvoid wdDoInit()
//@@begin wdDoInit()
//@@end
//@@begin javadoc:wdDoExit()
/**Hookmethodcalledtocleanupcontroller.*/
//@@end
publicvoid wdDoExit()
//@@begin wdDoExit()
//@@end
//@@begin javadoc:wdDoPostProcessing()
/**
*Hookcalledtohandledataretrievalerrorsbeforerendering.
*
*AfterdoModifyView(),theWebDynproFrameworkgetsallcontextdataneeded
*forrenderingbyvalidatingthecontexts(whichinturncallsthesupply
*functionsandsupplyingrelationroles).Inthishook,theapplication
*shouldhandletheerrorswhichoccurredduringvalidationofthecontexts.
*
*Usingpreorderdepth-firsttraversal,thishookiscalledforallcomponent
*controllersstartingwiththecurrentrootcomponent.
*
*Permittedoperations:
*-Flushingmodelqueue
*-Creatingmessages
*-Readingcontextandmodeldata
*
*Forbiddenoperations:
*-Invalidatingmodeldata
*-Manipulatingthecontext
*-Firingoutboundplugs
*-Creatingcomponents
*-...
*
*@paramisCurrentRoottrueifthisistherootofthecurrentrequest
*/
//@@end
publicvoid wdDoPostProcessing(boolean isCurrentRoot)
//@@begin wdDoPostProcessing()
//@@end
//@@begin javadoc:wdDoBeforeNavigation()
/**
*Hookbeforethenavigationphasestarts.
*
*Thishookallowsyoutoflushthemodelqueueandhandleany
*errorsthatoccur.Firingoutboundplugsisallowedinthishook.
*
*Usingpreorderdepth-firsttraversal,thishookiscalledforallcomponent
*controllersstartingwiththecurrentrootcomponent.
*
*@paramisCurrentRoottrueifthisistherootofthecurrentrequest
*/
//@@end
publicvoid wdDoBeforeNavigation(boolean isCurrentRoot)
//@@begin wdDoBeforeNavigation()
//@@end
//@@begin javadoc:wdDoApplicationStateChange()
/**
*Hookthatinformstheapplicationaboutastatechange.
*<p>
*Thishookiscallede.g.totelltheapplicationthatwillbe
*<ul>
* <li>leftviaasuspendplugandthereforeshouldgointoasuspend/sleep
* modewithminimalneedofresources.errorsthatoccur.Firing
* outboundplugsisallowedinthishook.
* <li>leftduetoatimeoutandcouldwriteit'sstatetoadatabaseifthe
* usercomesbacklateron
*</ul>
*
*TheconcretereasonisavailableviaIWDApplicationStateChangeInfo
*<p>
*<b>Important</b>:Thishookiscalledforthetoplevelcomponentonly!
*
*@paramstateChangeInfocontainstheinformationaboutthenatureofthestatechange
*@paramstateChangeReturnallowstheapplicationtoaskforadifferentstatechange.
* Theframeworkisallowedtoignoreitconsideringi.e.thecurrentresourcessituation.
*/
//@@end
publicvoid wdDoApplicationStateChange(com.sap.tc.webdynpro.progmodel.api.IWDApplicationStateChangeInfo stateChangeInfo, com.sap.tc.webdynpro.progmodel.api.IWDApplicationStateChangeReturn stateChangeReturn)
//@@begin wdDoApplicationStateChange()
//@@end
//@@begin javadoc:generate_User_Group_Role_Scriptfile()
/**Declaredmethod.*/
//@@end
publicvoid generate_User_Group_Role_Scriptfile( java.io.BufferedWriter logFile )
//@@begin generate_User_Group_Role_Scriptfile()
String schCriteriaText ="*";
if (schCriteriaText == null || schCriteriaText.trim().length() == 0) {
schCriteriaText = "*";
try
groupFilt.setUniqueName(schCriteriaText.trim(), ISearchAttribute.LIKE_OPERATOR, false);
//String grpId = "";
//String grpDesc = "";
//String grpRole = "";
if (result.size() > 0)
if(result.getState() == ISearchResult.SEARCH_RESULT_OK)
File dir = new File("c:\\Users_Group_Roles");
File file = new File("c:\\Users_Group_Roles\\"+"Groups"+"_"+ new SimpleDateFormat("MM-dd-yyyy HHmm").format(new Date())+ ".log");
FileWriter fw = new FileWriter(file,true);
logFile = new BufferedWriter(fw);
while(result.hasNext())
StringBuffer getGroupID =new StringBuffer();
StringBuffer getGroupDesc =new StringBuffer();
StringBuffer getGroupUser =new StringBuffer();
StringBuffer getGroupRole=new StringBuffer();
//grpTable.setDisplayName(thisGroup.getDisplayName());
//getGroupDesc.append(thisGroup.getDescription());
if(grpDesc!=null)
}else{
getGroupDesc.append("");
//wdComponentAPI.getMessageManager().reportSuccess("---getGroupID----:"+getGroupID);
//wdComponentAPI.getMessageManager().reportSuccess("---getGroupDesc----:"+getGroupDesc);
//grpTable.setUniqueID(thisGroup.getUniqueID());
//Iterator itrUserName = thisGroup.getUserMembers(true);
Iterator itrUserName = thisGroup.getUserMembers(true);
Iterator itrRoleName = thisGroup.getRoles(true);
while(itrUserName.hasNext())
//grpTable.setUserInfo(itrUserName.next().toString());
int lindex=str.lastIndexOf(":");
getGroupUser.append(str+",");
//getGroupUser.append(itrUserName.next().toString()+",");
//wdComponentAPI.getMessageManager().reportSuccess("---getGroupUser----:"+getGroupUser);
int GroupUserindex=getGroupUser.lastIndexOf(",");
String grpUserBuffer="";
String grpUser = "";
if(GroupUserindex!=-1)
while(itrRoleName.hasNext())
//grpTable.setRoleInfo(irole.getUniqueName());
if(!irole.getUniqueName().equalsIgnoreCase(""))
int lindex=str.lastIndexOf("/rl");
//wdComponentAPI.getMessageManager().reportSuccess("---str----:"+str);
getGroupRole.append(str+",");
//getGroupRole.append(irole.getUniqueName()+",");
//StringBuffer buffer=getGroupRole;
int GroupRoleindex=getGroupRole.lastIndexOf(",");
String grpRoleBuffer="";
if(GroupRoleindex!=-1)
//wdComponentAPI.getMessageManager().reportSuccess("---role----:"+index);
//wdContext.nodeGroupTable().addElement(grpTable);
String sqlGroupStatement = "";
String newLine = System.getProperty("line.separator");//This will retrieve line separator dependent on OS.
sqlGroupStatement += "INSERT INTO TableName(GID, DESCRIPTION) VALUES ";
sqlGroupStatement += "('"+getGroupID+ "', '"+getGroupDesc+"');"+newLine+"";
String delimiter = ",";
if(grpUserBuffer.equalsIgnoreCase(""))
}else
for(int i=0; i<groupUserParts.length;i++)
//grpUser = parts[i];
//wdComponentAPI.getMessageManager().reportSuccess("---grpUser----:"+grpUser);
//wdComponentAPI.getMessageManager().reportSuccess("---parts----:"+parts[i].length());
sqlGroupStatement += "INSERT INTO TableName(GID, UID) VALUES";
sqlGroupStatement += "('"+getGroupID+"', '"+groupUserParts[i]+"');"+newLine+"";
if(grpRoleBuffer.equalsIgnoreCase(""))
else
for(int i=0; i<roleParts.length;i++)
sqlGroupStatement += "INSERT INTO TableName(GID, RID) VALUES ";
sqlGroupStatement += "('"+ getGroupID + "', '"+roleParts[i]+ "' );"+newLine+"";
long srtTime = System.currentTimeMillis();
if (!dir.exists())
try
//logFile.write("[group]"+newLine);
//wdThis.generate_User_Group_Role_Scriptfile(logFile);
catch(IOException ex)
wdComponentAPI.getMessageManager().reportException("Exception In Roles():"+ex.getMessage(), false);
}else
msgMgr.reportWarning("No element found.");
roleSrcFilter.setUniqueName(schCriteriaText.trim(),ISearchAttribute.LIKE_OPERATOR,false);
if (srhResults.size() > 0){
if(srhResults.getState() == ISearchResult.SEARCH_RESULT_OK) {
File dir = new File("c:\\Users_Group_Roles");
File file = new File("c:\\Users_Group_Roles\\"+"Roles"+"_"+ new SimpleDateFormat("MM-dd-yyyy HHmm").format(new Date())+ ".log");
FileWriter fw = new FileWriter(file,true);
logFile = new BufferedWriter(fw);
while(srhResults.hasNext()) {
StringBuffer getRoleID=new StringBuffer();
StringBuffer getRoleDesc=new StringBuffer();
StringBuffer getRoleGroup =new StringBuffer();
StringBuffer getRoleUser =new StringBuffer();
//getRoleDesc.append(thisRole.getDescription());
if(roleDesc!=null){
}else{
getRoleDesc.append("");
Iterator itrUserName = thisRole.getUserMembers(true);
Iterator itrGroupName = thisRole.getGroupMembers(true);
while(itrUserName.hasNext())
//rTable.setUserInfo(itrUserName.next().toString());
int lindex=str.lastIndexOf(":");
getRoleUser.append(str+",");
//getRoleUser.append(itrUserName.next().toString()+",");
//wdComponentAPI.getMessageManager().reportSuccess("---itrUserName.hasNext()----:"+itrUserName.next());
int roleUserindex=getRoleUser.lastIndexOf(",");
String roleUserBuffer="";
if(roleUserindex!=-1)
while(itrGroupName.hasNext())
//rTable.setGroupInfo(itrGroupName.next().toString());
//getRoleGroup.append(itrGroupName.next().toString()+",");
int lindex=str.lastIndexOf(":");
getRoleGroup.append(str+",");
//wdComponentAPI.getMessageManager().reportSuccess("---itrGroupName.hasNext()----:"+itrGroupName.next());
int roleGroupindex=getRoleGroup.lastIndexOf(",");
String roleGroupBuffer="";
if(roleGroupindex!=-1)
//wdContext.nodeRoleTable().addElement(rTable);
String sqlRoleStatement = "";
String newLine = System.getProperty("line.separator");//This will retrieve line separator dependent on OS.
sqlRoleStatement += "INSERT INTO TableName(RID, DESCRIPTION) VALUES";
sqlRoleStatement += "('"+ getRoleID +"', '"+ getRoleDesc +"');"+newLine+"";
String delimiter = ",";
if(roleGroupBuffer.equalsIgnoreCase(""))
}else
for(int i=0; i<roleGroupParts.length;i++)
sqlRoleStatement += "INSERT INTO TableName(RID, GID) VALUES";
sqlRoleStatement += "('"+getRoleID+ "', '"+roleGroupParts[i]+ "' );"+newLine+"";
if(roleUserBuffer.equalsIgnoreCase(""))
}else{
for(int i=0; i<roleUserParts.length;i++)
sqlRoleStatement += "INSERT INTO TableName(RID, UID) VALUES ";
sqlRoleStatement += "('"+ getRoleID + "', '"+roleUserParts[i]+ "' );"+newLine+"";
long srtTime = System.currentTimeMillis();
if (!dir.exists())
try
//logFile.write("[role]"+newLine);
//wdThis.generate_User_Group_Role_Scriptfile(logFile);
catch(IOException ex)
wdComponentAPI.getMessageManager().reportException("Exception In Roles():"+ex.getMessage(), false);
} else {
wdComponentAPI.getMessageManager().reportWarning("No element found.");
usrSrchFilter.setUniqueName(schCriteriaText.trim(),ISearchAttribute.LIKE_OPERATOR,false);
if (srchResluts.size() >0){
if(srchResluts.getState() == ISearchResult.SEARCH_RESULT_OK) {
File dir = new File("c:\\Users_Group_Roles");
File file = new File("c:\\Users_Group_Roles\\"+"User"+"_"+ new SimpleDateFormat("MM-dd-yyyy HHmm").format(new Date())+ ".log");
FileWriter fw = new FileWriter(file,true);
logFile = new BufferedWriter(fw);
while (srchResluts.hasNext()){
StringBuffer getuserID =new StringBuffer();
StringBuffer getlastName =new StringBuffer();
StringBuffer getfirstName =new StringBuffer();
StringBuffer getemailID =new StringBuffer();
StringBuffer getRoleInfo=new StringBuffer();
StringBuffer getGroupInfo =new StringBuffer();
if(lastName!=null){
}else{
getlastName.append("");
if(firstName!=null){
}else{
getfirstName.append("");
if(eMail!=null){
}else{
getemailID.append("");
//String getLogonID = sapUser.getUserAccounts()[0].getLogonUid();
//usrTable.setUserNames(sapUser.getLastName()+','+" "+sapUser.getFirstName());
//wdComponentAPI.getMessageManager().reportSuccess("---UserID----:"+getLogonID);
Iterator itrRoleName = sapUser.getRoles(true);
Iterator itrGroupName = sapUser.getParentGroups(true);
while(itrRoleName.hasNext())
//IRole roleId = itrRoleName.next();
//wdComponentAPI.getMessageManager().reportSuccess("---roleId----:"+irole.getUniqueName());
//wdComponentAPI.getMessageManager().reportSuccess("---itrRoleName----:"+irole.getDescription());
//String roletext = roleId.getUniqueName();
//usrTable.setRoleInfo(irole.getUniqueName());
//wdComponentAPI.getMessageManager().reportSuccess("---roleId----:"+irole.getUniqueID());
int lindex=str.lastIndexOf("/rl");
//wdComponentAPI.getMessageManager().reportSuccess("---str----:"+str);
getRoleInfo.append(str+",");
//getRoleInfo.append(irole.getUniqueName()+",");
// wdComponentAPI.getMessageManager().reportSuccess("---roleId----:"+getRoleInfo);
int userRoleindex=getRoleInfo.lastIndexOf(",");
String userRoleBuffer="";
if(userRoleindex!=-1)
while(itrGroupName.hasNext())
//usrTable.setGroupInfo(itrGroupName.next().toString());
//getGroupInfo.append(itrGroupName.next().toString()+",");
int lindex=str.lastIndexOf(":");
getGroupInfo.append(str+",");
//wdComponentAPI.getMessageManager().reportSuccess("---itrGroupName.hasNext()----:"+getGroupInfo);
int userGroupindex=getGroupInfo.lastIndexOf(",");
String userGroupBuffer="";
if(userGroupindex!=-1)
String sqlUserStatement = "";
String newLine = System.getProperty("line.separator");//This will retrieve line separator dependent on OS.
sqlUserStatement += "INSERT INTO TableName(UID, LASTNAME, FIRSTNAME, EMAIL) VALUES ";
sqlUserStatement += "('"+ getuserID +"', '"+ getlastName + "', '"+ getfirstName + "', '"+ getemailID + "');"+newLine+"";
String delimiter = ",";
if(userRoleBuffer.equalsIgnoreCase(""))
}else{
for(int i=0; i<userRoleParts.length;i++)
sqlUserStatement += "INSERT INTO TableName(UID, GID) VALUES";
sqlUserStatement += "('"+ getuserID + "', '"+ userRoleParts[i] + "' );"+newLine+"";
if(userGroupBuffer.equalsIgnoreCase(""))
}else{
for(int i=0; i<userGroupParts.length;i++)
sqlUserStatement += "INSERT INTO TableName(UID, RID) VALUES ";
sqlUserStatement += "('"+ getuserID + "', '"+ userGroupParts[i] + "' );"+newLine+"";
//wdComponentAPI.getMessageManager().reportSuccess("---sqlStatement----:"+sqlStatement);
long srtTime = System.currentTimeMillis();
if (!dir.exists())
try
// logFile.write("[User]"+newLine);
//wdThis.generate_User_Group_Role_Scriptfile(logFile);
catch(IOException ex)
wdComponentAPI.getMessageManager().reportException("Exception In Users:"+ex.getMessage(), false);
} else {
wdComponentAPI.getMessageManager().reportWarning("No element found.");
//wdComponentAPI.getMessageManager().reportSuccess("---Group Node Size----:"+wdContext.nodeGroupTable().size());
//wdComponentAPI.getMessageManager().reportSuccess("---Role Node Size----:"+wdContext.nodeRoleTable().size());
//wdComponentAPI.getMessageManager().reportSuccess("---User Node Size----:"+wdContext.nodeUserTable().size());
}catch(Exception ex){
//wdComponentAPI.getMessageManager().reportSuccess("---Exception----:"+ex.getLocalizedMessage());
//@@end
/*
//@@begin others
IWDMessageManager msgMgr = null;
Finally the OUTPUT will generate like this
USER Example
[User]
uid=AAAAAA
last_name=B
first_name=A
email_address=AAAAAA@sap.com
role=
group=New_Portal User;Guest;
GROUP Example
[group]
gid=10
gdesc=xyz
user=Vijay,Ram.
role=??
Roles Example
[role]
rid=Administrator
rdesc=Administrator
user=Vijay,ram
group=Administrator
After creation of these 3 files will receives a mail
For Example:-
![12.JPG]()
Thank you
Regards
Vijay Kalluri