It's all about the answers!

Ask a question

How can I modify roles of an already existing user using Java API


Kaushambi Singh (371310379) | asked Jul 11 '13, 8:24 a.m.
 Is it possible to modify roles of user who is already present in team area using Java API. How? I am successful in adding roles to users but in case user already has roles , how can I modify (refresh) them using API ?

One answer



permanent link
Kevin Ramer (4.5k8178197) | answered Jul 11 '13, 8:32 a.m.
Oh oh,  I know this.   What you have to do is get the user's current role(s), filter out the 'default' and append the new role[s].

The below method came from Ralph's 'dumpContributor' example which I shamelessly have made use of:
	private static IRole[] getProcessAreaRoles(ITeamRepository teamRepository,
	            IProcessArea processArea, IContributorHandle handle)
throws TeamRepositoryException {
IContributor contributor = (IContributor) teamRepository.itemManager()
.fetchCompleteItem(handle, IItemManager.DEFAULT, null);
// System.out.print(": " + contributor.getUserId() + "\t"
// + contributor.getName() + "\t" + contributor.getEmailAddress()+ "\t");
IProcessItemService processService = (IProcessItemService) teamRepository
.getClientLibrary(IProcessItemService.class);
IClientProcess process = processService.getClientProcess(processArea,
null);
IRole[] contributorRoles = process.getContributorRoles(contributor,
processArea, null);
return contributorRoles;
// for (int j = 0; j < contributorRoles.length; j++) {
// IRole role = (IRole) contributorRoles[j];
// System.out.print(role.getId() + " ");
// }
// System.out.println();
}

private IRole[] appendRoles(IRole[] newRoles,IRole[] oldRoles) {
if (oldRoles == null)
return newRoles;

IRole[] roleList=new IRole[oldRoles.length];
int i=0,n=1;
roleList[0]=newRoles[0]; // this one should be a singular list
while (i < oldRoles.length) {
if (!"default".equals(oldRoles[i].getId()) ) {
roleList[n]=oldRoles[i];
n++;
}
i++;
}
return roleList;
}

In the part of the Class that uses the above that actually adds/updates the Contributor:

contributorRole[0] = findRoleByName(teamRepository, (IProcessArea)project, projectRole);
if (contributorRole[0] != null) {
if (userInRepos) {
allRoles=getProcessAreaRoles(teamRepository,project,newContributor);
}
if (allRoles != null) {
contributorRole=appendRoles(contributorRole,allRoles);
}
projectAreaWorkingCopy.setRoleCast(newContributor, contributorRole);
 

Comments
Kaushambi Singh commented Jul 12 '13, 6:10 a.m.

Thanks Kevin. 

Your answer


Register or to post your answer.