How can I modify roles of an already existing user using Java API
One answer
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:
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);