on 07-18-2005 10:44 AM
Hello,
I have a standard portal application.
I would like to know under which Role the application is running. How can I retreive this info?
Roy
Hi Roy,
navHelperService = (NavigationEventsHelperService)
PortalRuntime.getRuntimeResources().getService(NavigationEventsHelperService.KEY);
String launchNode =
navHelperService.getCurrentLaunchNavNode(request).getName();should get you further. You'll see that this returns the structure of the PCD. If you want to check which role this is, access the PCD via JNDI and go through this pcd tree path, checking which portion of the string is a role.
Hope it helps
Detlev
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Roy,
the general way to know this is as follows:
- Add the code to your implementation
- If compiling fails, search with WinRAR or something
similar for the classes needed.
- Add the corresponding JAR to the build path.
- Add the portal application where this JAR belongs to
to the SharingReference.
Hope it helps
Detlev
PS: Try it yourself, helps learning If you fail, ask again, I'll be at the office in about 4 hours again...
Hey Detlev,
In order to find the relevant jar no WinRar is needed use the Class Locator plugin, it is a great tool that adds right click to any class you are writing. Once clicking it, it locates and adds the jar for you automaticlly. If you want, I can send it to you.
Regarding the last step: 10X for the tip, I didn't know it usually the related jar name which represents the Service.
Now back to my issue: After running your code I receive this String: "pdkfully://d0/d0e2/d0e3#". I'll try use it for JNDI access and see what I g
Roy
Correction: I'll use this String for JNDI Access, I'll see what I get and let you know.
Message was edited by: Roy Cohen
Message was edited by: Roy Cohen
Hi Roy,
> the Class Locator plugin
... or JarFinder by IBM ... or ... there are many possibilities. WinRAR is my friend, for it does not only help at this level, but also helps looking up classes where some class is reference... But as said, everybody should use the tool one likes (still searching for a tool, which adds the used SharingReference automatically... ).
Does ClassLocator add the classpath also by using a variable? (Without this, it's useless in a colloborative environment.
> I didn't know it usually the related jar name
> which represents the Service.
The SharingReference can always be derivated from the portal application the JAR is in. If you have
<classpathentry kind="var" path="PORTALAPPS/com.sap.portal.navigation.api_service/lib/com.sap.portal.navigation.api_service_api.jar"/>
with PORTALAPPS the variable pointing to .../WEB-INF/portal/portalapps, the SharingReference can be supplemented by "com.sap.portal.navigation.api_service".
You can retrieve all (needed) SharingReferences by looking at the .classpath file of your project.
Sometimes it might be shorter just to add some "central" alias (like "knowledgemanagement"), but for this you need to know the aliases as well as the SharingReferences among the portal apps.
> I receive this String: "pdkfully://d0/d0e2/d0e3#".
OK, this is something very special... The PDK has it's own navigation hook (this is my own term; like the CollaborationRooms), that's also why it works not that good sometimes...
At the moment, I've got no idea how to conclude from pdkfully:// to pcd:portal_content/com.sap.pct/platform_add_ons/com.sap.pct.pdk/Roles/com.sap.pct.pdk.JavaDeveloper - but there will be some connection, that's for sure...
Hope it helps nevertheless
Detlev
Hi Roy,
> > Does ClassLocator add the classpath
> > also by using a variable?
>
> Can you please explain me what you asked here?
You can use variables for building the build path, as shown in this example:
<classpathentry kind="var" path="PORTALAPPS/com.sap.portal.navigation.api_service/lib/com.sap.portal.navigation.api_service_api.jar"/>
kind="var" means the path uses a variable (here: PORTALAPPS) as the initial path segment. Without this it would read for example
<classpathentry kind="var" path="C:/usr/sap/btex/j2ee/j2ee_00/cluster/server/services/servlet_jsp/work/jspTemp/irj/root/WEB-INF/portal/portalapps/com.sap.portal.navigation.api_service/lib/com.sap.portal.navigation.api_service_api.jar"/>
Now imagine 2 or more developers, sharing this project. One possibility might be that the libraries used are shared via a network connection, that the latter version would do it. But this has two major disadvantages: (a) Speed. (b) No developer is able to work on the sources without network connection.
Having this said, it is a golden rule to publish the head note: "Each build path uses variables. Period."
By doing so, you only have to have a common rule how you name your variables. Then each developer can have his stuff on different places on his harddisk, but no problems arise sharing the projects (and also including the .classpath file into CVS just makes your life easier instead of harder).
Hope it helps
Detlev
Hi Roy,
just installed it, yeah, I like it (much more than JarFinder). Thanks for that hint!
It's a pity that it is not developed any more. This will be an issue when NWDS switches to Eclipse 3.x (hopefully soon).
Maybe that could get quite interesting to work on that project again...?!?!
Thanks again for the hint, I will refer to that tool in the future quite often, I think,
best regards
Detlev
Hi Roy,
Why not get the current navigation node that was selected, and then iterate back to the selected node.
For example. the following prints out the titles of the navigation nodes along the selected path:
NavigationEventsHelperService navHelperService = (NavigationEventsHelperService)PortalRuntime.getRuntimeResources().getService(NavigationEventsHelperService.KEY);
//Get selected node
INavigationNode myNode = navHelperService.getCurrentNavNode(request);
boolean moreNodes = true;
while (moreNodes) {
response.write(myNode.getTitle(request.getLocale()));
response.write ("<BR>");
if (myNode.getTitle(request.getLocale()).equals("DanRole")) {
moreNodes=false;
}
else {
myNode = navHelperService.getParentNode(myNode,request);
}
}
-
In the above, the iteration stops simply when I reach a node with DanRole as the title. In reality, you would check if you are currently at a role node.
You can take the PCD address of the navigation node (from getName()) and do a lookup in the PCD for that object, and check the com.sap.portal.pcd.gl.ObjectClass property to see if it is equal to com.sapportals.portal.role.
I would have liked to have an easier way to check the node type, but so far I don't see how.
Hope this helps.
Daniel
The way to check if a navigation node is a role -- without going into the PCD -- is something like this:
String objectClass;
try {
objectClass =(myNode.getAttributeValue(IPcdAttribute.OBJECT_CLASS).toString());
} catch (NoSuchAttributeException e) {
e.printStackTrace();
}
if (objectClass.equals("com.sap.portal.role")) {
... do something
}
-
myNode is the navigation node:
There is probably a constant for this string, but I don;t recall what it is.
Hope this helps.
Daniel
Hi Roy,
I faced a similar problem and came across this post so I thought I'd update. What i did was take the iview's context url (which contains the role name) and start working my way back the url checking the com.sap.portal.pcd.gl.ObjectClass property. If it was of type com.sapportals.portal.role, I knew i had a role and I then looked up the title.
If you are using nested roles, this code would stop on the first role (innermost role). You would need to modify a bit to handle this scenario (we use worksets here inside roles).
Hope this helps,
Marty
ps CommonTools and PcdTools are just two custom classes I use for generic things like setting up the environment and trimming pcd: off of the url.
public class CurrentRoleAssigned extends AbstractPortalComponent {
private final String PCDROLE =
"com.sapportals.portal.role";
public void doContent(
IPortalComponentRequest request,
IPortalComponentResponse response) {
response.write(
"<BR>This iview is on a page that is in the following role:<BR>");
String url = request.getComponentContext().getContextName();
response.write("<BR>context url of this iview is: " + url);
int slashCount = countSlash(url)-1;
String objectType = "";
String roleTitle = "";
for(int i=0; i<slashCount; i++)
{
objectType = lookupObjectType(request, response, url);
response.write("<BR>object type ="+objectType);
if(objectType.equals(PCDROLE))
{
roleTitle = lookupRoleTitle(request, response, url);
response.write("<BR><B><Font size=2>Role title = "+roleTitle+"</FONT></B>");
break;
}
url = url.substring(0, url.lastIndexOf("/"));
response.write("<BR>not a role, new url to lookup = "+url);
}
}
private String lookupObjectType(IPortalComponentRequest request, IPortalComponentResponse response, String url)
{
CommonTools ctools = new CommonTools();
PcdObjectTools pcdTools = new PcdObjectTools();
NamingEnumeration names;
//get pcdObjectFactory Service
IPcdObjectFactory pcdObjFactory =
((IPcdGlService) PortalRuntime
.getRuntimeResources()
.getService(IPcdGlService.KEY))
.getPcdObjectFactory();
InitialContext initialContext = ctools.getEnvironment(request);
String objectType = "";
try {
IPcdContext initPcdCtx = (IPcdContext) initialContext.lookup("");
IPcdContext pcdCtx =
(IPcdContext) initPcdCtx.lookup(ctools.cutPcdPrefix(url));
Object obj;
obj = pcdCtx.getAttributes("").get("com.sap.portal.pcd.gl.ObjectClass");
//found the object so get the title
if (obj instanceof IPcdAttribute) {
IPcdAttribute att = (IPcdAttribute) obj;
Attributes atts = pcdCtx.getAttributes("");
objectType = atts.get("com.sap.portal.pcd.gl.ObjectClass").get(0).toString();
}
} catch (Exception e) {
response.write("exception =" + e.toString());
}
//response.write("<BR>object type = " + objectType);
return objectType;
}
private String lookupRoleTitle(IPortalComponentRequest request, IPortalComponentResponse response, String url)
{
CommonTools ctools = new CommonTools();
PcdObjectTools pcdTools = new PcdObjectTools();
NamingEnumeration names;
//get pcdObjectFactory Service
IPcdObjectFactory pcdObjFactory =
((IPcdGlService) PortalRuntime
.getRuntimeResources()
.getService(IPcdGlService.KEY))
.getPcdObjectFactory();
InitialContext initialContext = ctools.getEnvironment(request);
String roleTitle = "";
try {
IPcdContext initPcdCtx = (IPcdContext) initialContext.lookup("");
IPcdContext pcdCtx =
(IPcdContext) initPcdCtx.lookup(ctools.cutPcdPrefix(url));
Object obj;
obj = pcdCtx.getAttributes("").get("com.sap.portal.pcm.Title");
//found the object so get the title
if (obj instanceof IPcdAttribute) {
IPcdAttribute att = (IPcdAttribute) obj;
Attributes atts = pcdCtx.getAttributes("");
roleTitle = ((ILocaleTextPair) atts.get("com.sap.portal.pcm.Title").get(0)).getText();
}
} catch (Exception e) {
response.write("exception =" + e.toString());
}
return roleTitle;
}
private int countSlash(String url)
{
int length = url.length();
int count = 0;
int index = 0;
while (index < length) {
if (url.charAt(index) == '/') {
count = count + 1;
}
index = index + 1;
}
return count;
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Marty,
If the role's title is all you're looking for,
then Detlev's post to which I pointed earlier in this thread
has a much simpler solution:
NavigationEventsHelperService navHelperService =
(NavigationEventsHelperService)PortalRuntime.getRuntimeResources().getService(NavigationEventsHelperService.KEY);
INavigationNode navNode = navHelperService.getNavNodesListForPath(request, "NavUnderEntryPoint").get(0);
String title = navNode.getTitle(request.getLocale());Yoav.
Thanks Yoav,
However I dont think you can use the Navigation Helper Service if the isolation method is set to "URL", correct? I think it only works for "EMBEDDED" type iViews. When I try it, the navigation service always returns the first node of my TLN--not the current node. Is there a way to use the code for URL isolation?
Thanks!
Marty
Hi All,
A few points about the above solution. It returns the top-level node under which the current iView is running -- NOT the role. If the entry point is a workset, it returns the workset.
To return the role, you have to check the object type, something like this:
-
NavigationEventsHelperService navHelperService = (NavigationEventsHelperService)PortalRuntime.getRuntimeResources().getService(NavigationEventsHelperService.KEY);
INavigationNode myNode = navHelperService.getCurrentNavNode(request);
boolean moreNodes = true;
while (moreNodes) {
response.write(myNode.getTitle(request.getLocale()) + "<BR>");
try {
objectType = myNode.getAttributeValue(IPcdAttribute.OBJECT_CLASS).toString();
if (objectType.equals("com.sapportals.portal.role")) {
moreNodes=false;
}
else {
myNode = navHelperService.getParentNode(myNode,request);
}
} catch (NoSuchAttributeException e) {
e.printStackTrace();
}
}
-
Also, note that the navigation helper service is not public, though it is used extensively and is not likely to change.
Hope this helps.
Daniel
P.S.: For a thread with 25 posts and plenty of code examples, I would have thought that this would be worth more than 4 points.
Hi Roy,
I am not sure abt the current role underwhich the application runs.
To get the role of a portal user you can use the following code .
IUser user = request.getUser();
Iterator roles = user.getRoles(true);
while (roles.hasNext()) {
try {
String uniqueID = roles.next().toString();
IRole userRole = UMFactory.getRoleFactory().getRole(uniqueID);
response.write("Role is :"+userRole.getDisplayName());
} catch (Exception e) {
response.write(""+e);
}
Hope this Helps
gEorgE
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Dear George and Bobu,
10X but I already know how to use user.getRoles(true);
What I want is very simple: getting the current Role the application is running at. I have a certain application which is revealed under few Roles and for some reasons I need to know under which Role it is currently running.
I think it's clear enough...
Roy
Hi Roy
U can get the role of the user by searching in User Administration with userid. It will reveal all the roles the user has been given.
For this, the curent user should have the user administration role assigned 2 him.
Let me ask, r u having a scenario in which the user is not having such a previllege and then also u want to know the roles?
Can u please clarify the doubt?
Regards
Bobu
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 3 | |
| 3 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.