Jazz Forum Welcome to the Jazz Community Forum Connect and collaborate with IBM Engineering experts and users

Programatically assign roles to RTC Users -- Using Plain Java

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

1

0 votes

Comments

 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
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

1 vote

Comments

 

 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);

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 vote


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);

1 vote

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.

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.

1 vote

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?

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

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)

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

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.

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!..

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

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.

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.

1 vote

showing 5 of 15 show 10 more comments

Your answer

Register or log in 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.

Search context
Follow this question

By Email: 

Once you sign in you will be able to subscribe for any updates here.

By RSS:

Answers
Answers and Comments
Question details
× 10,937

Question asked: Feb 05 '13, 7:54 a.m.

Question was seen: 7,551 times

Last updated: Dec 27 '13, 6:25 p.m.

Confirmation Cancel Confirm