It's all about the answers!

Ask a question

Programatically assign roles to RTC Users -- Using Plain Java


0
1
A B (881811) | asked Feb 05 '13, 7:54 a.m.

Hi,

We are trying to programatically assign roles to RTC users(we are on 4.0.0.1) in a Project Area. We got some information at https://jazz.net/forum/questions/33551/teams-role-api but are not able to do much with it.

Any code snippets, examples, pointers would be very usefull.

Regards,

A


Comments
A B commented Feb 06 '13, 12:52 p.m. | edited Feb 06 '13, 1:01 p.m.

 Created some dirty code to do POC on roles of updation. Created a code to Add roles of an administrative user to the user who last updated the PA. However, following line gives an error:

processArea.addRoleAssignments(handle, contributorRoles);

com.ibm.team.repository.common.internal.ImmutablePropertyException. It seems that i need to make a working copy of handle or we are doing some thing wrong over here?

Pasted below are the relevant code lines:

-------------------------------------------------------------------------------------------------------------------------------

//Update the Role of user who last modified the PA with Roles of an Administrator user.

IContributorHandle handle = processArea.getModifiedBy();
IContributor contributor = (IContributor) teamRepository.itemManager().fetchCompleteItem(handle, IItemManager.REFRESH, null);
IContributor contributorWorkingCopy = (IContributor) contributor.getWorkingCopy();
//Get roles of a Admin Contributor
IRole[] contributorRoles = process.getContributorRoles(Admin_contributor,processArea, null);
processArea.addRoleAssignments(handle, contributorRoles);
teamRepository.contributorManager().saveContributor(contributorWorkingCopy, null);

Accepted answer


permanent link
Ralph Schoon (63.1k33646) | answered Feb 05 '13, 8:47 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
This depends on how you want to do that. An example using the Plain Java Client Libraries to get the data can be found here: http://rsjazz.wordpress.com/2012/12/09/analyzing-a-aroject-areas-members-and-roles-using-the-plain-java-client-libraries/

Scanning the post https://jazz.net/forum/questions/33551/teams-role-api seems to indicate that some (the first part) is server code, whereas the last parts is mostly client API. although these are typically very similar, there are differences for example the names of client Libraries and Server Services are slightly different and the mechanisms to get them too. I have not tried to set a user role, but the blog post might give you some hints about the API. Another hint would be that modifying a lot of the artifacts requires to get a WorkingcopyManager and a working copy, then modify the data in the working copy and save the workingcopy using the manager. 
A B selected this answer as the correct answer

Comments
A B commented Feb 06 '13, 8:00 a.m. | edited Feb 06 '13, 8:25 a.m.

 


A B commented Feb 06 '13, 8:24 a.m. | edited Feb 06 '13, 8:25 a.m.

 Thanks a lot for sharing the information Ralph, it was helpful.I am now trying to use setRoleAssignments to set the roles at project level. However, I am not able to able to figure out how to give value for its second argument.  


setRoleAssignments(IContributorHandle arg0,IRole[] arg1)

Any more clues, hints or some reference documents…?(sorry if it is too naive).It seems to me that some kind of typecasting is required here but not able to figure it out till now (sigh.. that’s what happen when a DB guy works on java :P). :)

//Get Contributor Handle --- For POC trying to update roles of user who last modified the PA 
 IContributorHandle handle2 = processArea.getModifiedBy();
 IContributor contributor = (IContributor) teamRepository.itemManager().fetchCompleteItem(handle2, IItemManager.REFRESH, null);

//Get Working Copy 
 IContributor contributorWorkingCopy = (IContributor) contributor.getWorkingCopy(); 
   
//Print Contributor Name 
 System.out.print(": " + contributor.getUserId() + "\t"+ contributor.getName() + "\t" + contributor.getEmailAddress()+ "\t");
 

//Get Roles
 IRole[] roles2 = null;
 processArea.setRoleAssignments(handle2, roles2);
 teamRepository.contributorManager().saveContributor(contributorWorkingCopy, null);


1
Ralph Schoon commented Feb 06 '13, 8:37 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

I would suggest to set up the dev client according to https://jazz.net/library/article/1000 . Then make your project a Plugin Project. Then you have the PDE and can search for examples for the call in the RTC Eclipse Client (SDK).

If you closely look into my example, it shows the code below. So you have to iterate the roles, get an IRole type element by some criteria, have a new IRole[] variable and add the role. You can also use an ArrayList<IRole> and then get the array from that. You need to pass an IRole[] as far as I can tell.

    IRole[] contributorRoles = process.getContributorRoles(contributor,
                processArea, null);
        for (int j = 0; j < contributorRoles.length; j++) {
            IRole role = (IRole) contributorRoles[j];
            System.out.print(role.getId() + " ");
        }



1
Ralph Schoon commented Feb 06 '13, 8:58 a.m. | edited Feb 06 '13, 8:58 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER


I found this API call:

    /**
     * Appends the given roles to the collection of roles assigned to the given
     * contributor.
     * 

* If any of the given roles are already assigned to the given * contributor, those roles will be ignored. Callers wishing to reorder * role assignments should call setRoleAssignments(...) instead. *

* Roles passed into this method should be a subset of available roles * for this process area retrieved by calling either: * com.ibm.team.process.service.IServerProcess#getRoles(IProcessArea) or * com.ibm.team.process.client.IClientProcess#getRoles(IProcessArea, * IProgressMonitor) * * @param contributor the contributor whose roles to assign * @param roles the roles to append */ public void addRoleAssignments(IContributorHandle contributor, IRole[] roles);


A B commented Feb 06 '13, 9:50 a.m. | edited Feb 06 '13, 9:51 a.m.

Thanks a lot for sharing all these details Ralph..!! In our case we will have user role names in a CSV file. So seems like for us steps will be:

1. Read data from CSV into a variable.

2. Create a role type element and assing the value from step 1 to it.

3. create a IRole[] Variable and add the role.

I work to implement it. Thanks again for sharing the insight.


1
Ralph Schoon commented Feb 06 '13, 11:02 a.m. | edited Feb 06 '13, 11:02 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

The code above assumes you have the roles specified in the process specification.
If you have user/role mappings in the CSV file you would have to

  • Read the CSV file, user team, roles
  • Find the Project Area, team area, user(contributor), add the user if he is not already member, get the users roles, map the roes to the desired ones. If there are roles missing, read the available roles from the process area and add the role  with the desired ID or name to the process area for the specific user.


sam detweiler commented Dec 17 '13, 5:42 p.m.

Ralph, nice find.. (the addRoleAssignments).. I need that.. 


but its unclear on what class/service that api is available from. 

not from processitemservice or IClientProcess

any helpful pointers?


Ralph Schoon commented Dec 18 '13, 2:40 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

Sam in the post above it appears to be an interface of the IProcessArea class.

This should be common to client and server API See https://rsjazz.wordpress.com/2013/09/18/deploying-templates-and-creating-projects-using-the-plain-java-clients-library/ for the client libraries in method

addUsersAndRoles


sam detweiler commented Dec 18 '13, 8:40 a.m.

hm.. your code uses the IProjectArea

    area = (IProjectArea) service.getMutableCopy(area);
    System.out.println("Trying to add members with roles: "
            + "ScrumMaster" + " to Project Area" + area.getName());

// IProjectArea wArea = (IProjectArea)service.getMutableCopy(area);
IContributor member = teamRepository.loggedInContributor();

IRole role = getRole(area, "ScrumMaster", monitor);
area.addAdministrator(member);
area.addMember(member);&nbsp;

 area.addRoleAssignments(member, new IRole[] { role });
 
I don't see that method on projectarea. (3.0.1)


sam detweiler commented Dec 18 '13, 9:19 a.m. | edited Dec 18 '13, 9:58 a.m.

that method is in com.ibm.team.process.common.IProcessArea. 

but ONLY in 4.x and above.  not there in 3.x 

RolePersistence.getPersistedRoleData(teamdata,contributor)
and
RolePersistence.setPersistedRoleData(teamdata,contributor,(String roles); 

and should work on 4.x
work


Ralph Schoon commented Dec 18 '13, 9:57 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

Sam, I would really, really, really, really, really like to suggest to follow http://rsjazz.wordpress.com/2013/03/20/understanding-and-using-the-rtc-java-client-api/ and the related articles to set up the plain java client libraries with a compatible SDK. This makes searching for API so much easier. For example, you could search for where IRole or IRole2 is used in the SDK and easily find the service and code you want.

Just the Plain Java Javadoc, is really very limited.


sam detweiler commented Dec 18 '13, 11:02 a.m.

thanks.. I do the api search quite often, and can view the source all the time.

but you cannot find what isn't there.. addRoleAssignments() is not in 3.x

another example

set the project available roles,  I can get them, but in 3.x (at least) there is no set method. 

search will not find it either!..


sam detweiler commented Dec 18 '13, 12:07 p.m.

turns out I can shortcut setting the project roles by using the existing project as a project template for the new projects..  


sam detweiler commented Dec 18 '13, 9:02 p.m.

Ralph, thanks for pushing me..

I of course have looked at all the source for all the plugins I have done and system extensions and improvements..  nice to have it available for the plain java apps too.


1
Sterling Bates commented Dec 27 '13, 6:25 p.m.

As a heads up, I've found "addRoleAssignments" to be unreliable, and actually remove random roles while also not setting the new one.  For example, when calling it with a new "projectmanager" role for my own user, the first time it removed my "Administrator" role assignment, and the second time it removed "team-member".  Neither time added the new role.

setRoleAssignments works fine, however.

showing 5 of 15 show 10 more comments

Your answer


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.