Too big for a comment. The code would look like:
/*******************************************************************************
* Licensed Materials - Property of IBM
* (c) Copyright IBM Corporation 2013. All Rights Reserved.
*
* ServerSetup
*
* Note to U.S. Government Users Restricted Rights: Use, duplication or
* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
*******************************************************************************/
package com.ibm.js.team.admin.automation;
import java.net.URI;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import com.ibm.team.process.client.IClientProcess;
import com.ibm.team.process.client.IProcessItemService;
import com.ibm.team.process.common.IProcessArea;
import com.ibm.team.process.common.IProcessItem;
import com.ibm.team.process.common.IProjectArea;
import com.ibm.team.process.common.IRole;
import com.ibm.team.repository.client.ITeamRepository;
import com.ibm.team.repository.client.ITeamRepository.ILoginHandler;
import com.ibm.team.repository.client.ITeamRepository.ILoginHandler.ILoginInfo;
import com.ibm.team.repository.client.TeamPlatform;
import com.ibm.team.repository.common.IContributor;
import com.ibm.team.repository.common.TeamRepositoryException;
/**
* Automates setting up the RTC Extension workshop. Class to set up a project,
* users and the SCM component for a project Extracts given files into the SCM
* system.
*
*/
public class AddUserToProjectArea {
/**
* Basic constructor.
*
*/
public AddUserToProjectArea() {
super();
}
/**
* Main Entry Point run the whole server setup
*
* @throws Exception
*/
public static void main(String[] args) throws Exception {
boolean result;
System.out.println("Starting Team Platform");
if (!TeamPlatform.isStarted())
TeamPlatform.startup();
try {
System.out.println("Team Platform started");
AddUserToProjectArea setupParea = new AddUserToProjectArea();
result = setupParea.run(args);
} catch (TeamRepositoryException x) {
x.printStackTrace();
result = false;
} finally {
TeamPlatform.shutdown();
}
if (!result)
System.exit(1);
}
/**
* This actually does the work
*
* @return
* @throws Exception
*/
private boolean run(String[] args) throws Exception {
if (args.length != 5) {
System.out
.println("Usage: CreateProjectArea [repositoryURI] [adminUserId] [adminPassword] [projectAreaName] [addUserID] [addRoleID]");
return false;
}
// Get the parameters
String repositoryURI = args[0];
String adminUserId = args[1];
String adminPassword = args[2];
String projectAreaName = args[3];
String addUserID = args[4];
String addRoleID = args[5];
IProgressMonitor monitor = new NullProgressMonitor();
ITeamRepository teamRepository = logIntoTeamRepository(repositoryURI,
adminUserId, adminPassword, monitor);
IProcessItemService service = (IProcessItemService) teamRepository
.getClientLibrary(IProcessItemService.class);
URI uri = URI.create(projectAreaName.replaceAll(" ", "%20"));
IProjectArea area = (IProjectArea) service.findProcessArea(uri,
IProcessItemService.ALL_PROPERTIES, monitor);
if (area == null) {
System.out.println("Project Area not found: " + projectAreaName);
return false;
}
addUsersAndRoles(teamRepository, area, addUserID, addRoleID, monitor);
System.out.println("Success");
return true;
}
/**
* Adds users and roles to a project area.
*
* @return
* @throws TeamRepositoryException
*/
private IProjectArea addUsersAndRoles(ITeamRepository teamRepository,
IProjectArea area, String userID, String roleID, IProgressMonitor monitor)
throws TeamRepositoryException {
IProcessItemService service = (IProcessItemService) teamRepository
.getClientLibrary(IProcessItemService.class);
area = (IProjectArea) service.getMutableCopy(area);
System.out.println("Trying to add member with roles: "
+ roleID + " to Project Area" + area.getName());
IContributor user = teamRepository.contributorManager().fetchContributorByUserId(userID, monitor);
IRole role = getRole(area, roleID, monitor);
area.addAdministrator(user);
area.addMember(user);
area.addRoleAssignments(user, new IRole[] { role });
IProcessItem[] items = service.save(new IProcessItem[] { area },
monitor);
System.out.println("Users with Roles added to " + area.getName());
return area;
}
/**
* Gets a role by its ID.
*
* @return
* @throws TeamRepositoryException
*/
private IRole getRole(IProcessArea area, String roleID,
IProgressMonitor monitor) throws TeamRepositoryException {
ITeamRepository repo = (ITeamRepository) area.getOrigin();
IProcessItemService service = (IProcessItemService) repo
.getClientLibrary(IProcessItemService.class);
IClientProcess clientProcess = service.getClientProcess(area, monitor);
IRole[] availableRoles = clientProcess.getRoles(area, monitor);
for (int i = 0; i < availableRoles.length; i++) {
IRole role = availableRoles[i];
// IRole2 role2 = (IRole2)role;
// role2.getRoleName();
if (role.getId().equalsIgnoreCase(roleID))
return role;
}
throw new IllegalArgumentException("Couldn't find roles");
}
/**
* Manages the login process.
*
* @return
* @throws TeamRepositoryException
*/
private ITeamRepository logIntoTeamRepository(String repositoryURI,
String userId, String password, IProgressMonitor monitor)
throws TeamRepositoryException {
System.out.println("Trying to log into repository: " + repositoryURI);
ITeamRepository teamRepository = TeamPlatform
.getTeamRepositoryService().getTeamRepository(repositoryURI);
teamRepository.registerLoginHandler(new LoginHandler(userId, password));
teamRepository.login(monitor);
System.out.println("Login succeeded.");
return teamRepository;
}
/**
* Login Handler implementation
*
*/
private static class LoginHandler implements ILoginHandler, ILoginInfo {
private String fUserId;
private String fPassword;
private LoginHandler(String userId, String password) {
fUserId = userId;
fPassword = password;
}
public String getUserId() {
return fUserId;
}
public String getPassword() {
return fPassword;
}
public ILoginInfo challenge(ITeamRepository repository) {
return this;
}
}
}