RTC Client 4.0.1+ API: can I add components to a workspace using a loadRule.xml ?
Hi
I am able to use the Java API to load existing workspace to my sandbox.
I wonder however, if its possible to create a new repository workspace and add it components using the same XML load rule file (or similar).
if not , what I the code to traverse a stream components and add it to a freshly crreated repository workspace.
it should be something of the form:
<code>
...
IWorkspaceConnection connection = null;
ITeamRepository repo = login(....)
IWorkspaceManager wm = SCMPlatform.getWorkspaceManager(repo);
IContributorService contributorService = (IContributorService) repo.getClientLibrary(IContributorService.class);
String userId = repo.getUserId();
IContributorHandle contributor_creator = contributorService.fetchContributorByUserId(userId);
connection = wm.createWorkspace(contributor_creator, searchName, "generated by me", monitor);
// is that the correct way to create a workspace ? what is the desired value of contributor_creator?
// how do I iteratea stream components and add them to this newly created workspace ?
</code>
I am able to use the Java API to load existing workspace to my sandbox.
I wonder however, if its possible to create a new repository workspace and add it components using the same XML load rule file (or similar).
if not , what I the code to traverse a stream components and add it to a freshly crreated repository workspace.
it should be something of the form:
<code>
...
IWorkspaceConnection connection = null;
ITeamRepository repo = login(....)
IWorkspaceManager wm = SCMPlatform.getWorkspaceManager(repo);
IContributorService contributorService = (IContributorService) repo.getClientLibrary(IContributorService.class);
String userId = repo.getUserId();
IContributorHandle contributor_creator = contributorService.fetchContributorByUserId(userId);
connection = wm.createWorkspace(contributor_creator, searchName, "generated by me", monitor);
// is that the correct way to create a workspace ? what is the desired value of contributor_creator?
// how do I iteratea stream components and add them to this newly created workspace ?
</code>
2 answers
ok. that worked very well:
<code>
private static IWorkspaceConnection createNewWorkspace(ITeamRepository repo ,IWorkspaceConnection parentStreamConn, String workspaceName , String description , IProgressMonitor progressMonitor) throws TeamRepositoryException
{
IWorkspaceConnection newConnection = null;
IWorkspaceManager wm = SCMPlatform.getWorkspaceManager(repo);
IContributorHandle scopeHandle = repo.loggedInContributor() ;
IWorkspaceConnection defaultFlow = parentStreamConn;
IWorkspaceConnection sourceWorkspace= parentStreamConn;
newConnection = wm.createWorkspace(scopeHandle, workspaceName, description, defaultFlow, sourceWorkspace, progressMonitor);
return newConnection;
}
/**
*
* @param repo - An RTC reporistory object
* @param searchName : a RTC workspace or stream name to search
* @param searchKind : if you search for RTC workspace send: IWorkspaceSearchCriteria.WORKSPACES
* if you search for stream use:IWorkspaceSearchCriteria.STREAMS
* @param monitor
* @return - a connection object to the found stream/workspace
* @throws TeamRepositoryException
* @throws ItemNotFoundException
*/
static IWorkspaceConnection getRepositoryWorkspace(ITeamRepository repo,String searchName , int searchKind, IProgressMonitor monitor)
throws TeamRepositoryException, ItemNotFoundException {
IWorkspaceConnection connection = null;
IWorkspaceManager wm = SCMPlatform.getWorkspaceManager(repo);
int maxSize = 4;
monitor.subTask("searching for desired workspace");
IWorkspaceSearchCriteria criteria = IWorkspaceSearchCriteria.FACTORY.newInstance();
criteria.setPartialName(searchName);
//criteria.setExactName(searchName);
criteria.setKind(searchKind);
java.util.List<IWorkspaceHandle> workspaceList = wm.findWorkspaces(criteria, maxSize, monitor);
System.out.println("found " + workspaceList.size() + " matching workspaces for search pattern:"+searchName);
if (workspaceList.size() == 1) {
IWorkspaceHandle handle = workspaceList.get(0);
connection = wm.getWorkspaceConnection(handle, monitor);
} else if (workspaceList.size() > 1){
System.out.println("search creteria was to common. serveral workspaces found:");
StringBuffer buffer = new StringBuffer();
for (IWorkspaceHandle handle :workspaceList ) {
connection = wm.getWorkspaceConnection(handle, monitor);
String desc = connection.getDescription();
System.out.println("desc=" + desc + " name="+connection.getName());
buffer.append(" name=").append(connection.getName());
if (desc != null) {
buffer.append(" description=").append(desc);
buffer.append('\n');
} else {
buffer.append('\n');
}
}
throw new TeamRepositoryException("workspace name is non-unique:"+buffer.toString());
}
return connection;
}
</code>
<code>
private static IWorkspaceConnection createNewWorkspace(ITeamRepository repo ,IWorkspaceConnection parentStreamConn, String workspaceName , String description , IProgressMonitor progressMonitor) throws TeamRepositoryException
{
IWorkspaceConnection newConnection = null;
IWorkspaceManager wm = SCMPlatform.getWorkspaceManager(repo);
IContributorHandle scopeHandle = repo.loggedInContributor() ;
IWorkspaceConnection defaultFlow = parentStreamConn;
IWorkspaceConnection sourceWorkspace= parentStreamConn;
newConnection = wm.createWorkspace(scopeHandle, workspaceName, description, defaultFlow, sourceWorkspace, progressMonitor);
return newConnection;
}
/**
*
* @param repo - An RTC reporistory object
* @param searchName : a RTC workspace or stream name to search
* @param searchKind : if you search for RTC workspace send: IWorkspaceSearchCriteria.WORKSPACES
* if you search for stream use:IWorkspaceSearchCriteria.STREAMS
* @param monitor
* @return - a connection object to the found stream/workspace
* @throws TeamRepositoryException
* @throws ItemNotFoundException
*/
static IWorkspaceConnection getRepositoryWorkspace(ITeamRepository repo,String searchName , int searchKind, IProgressMonitor monitor)
throws TeamRepositoryException, ItemNotFoundException {
IWorkspaceConnection connection = null;
IWorkspaceManager wm = SCMPlatform.getWorkspaceManager(repo);
int maxSize = 4;
monitor.subTask("searching for desired workspace");
IWorkspaceSearchCriteria criteria = IWorkspaceSearchCriteria.FACTORY.newInstance();
criteria.setPartialName(searchName);
//criteria.setExactName(searchName);
criteria.setKind(searchKind);
java.util.List<IWorkspaceHandle> workspaceList = wm.findWorkspaces(criteria, maxSize, monitor);
System.out.println("found " + workspaceList.size() + " matching workspaces for search pattern:"+searchName);
if (workspaceList.size() == 1) {
IWorkspaceHandle handle = workspaceList.get(0);
connection = wm.getWorkspaceConnection(handle, monitor);
} else if (workspaceList.size() > 1){
System.out.println("search creteria was to common. serveral workspaces found:");
StringBuffer buffer = new StringBuffer();
for (IWorkspaceHandle handle :workspaceList ) {
connection = wm.getWorkspaceConnection(handle, monitor);
String desc = connection.getDescription();
System.out.println("desc=" + desc + " name="+connection.getName());
buffer.append(" name=").append(connection.getName());
if (desc != null) {
buffer.append(" description=").append(desc);
buffer.append('\n');
} else {
buffer.append('\n');
}
}
throw new TeamRepositoryException("workspace name is non-unique:"+buffer.toString());
}
return connection;
}
</code>
Load rules only load content so you cannot specify adding a component to the workspace as part of the load rule.
You can use the other create workspace call to specify a source. The source will be used to specify what component configurations should be in the new workspace. The contributor is the owner for the workspace.
You can also get the currently logged-in user by calling repo.loggedInContributor() if you want to simplify that part.
You can use the other create workspace call to specify a source. The source will be used to specify what component configurations should be in the new workspace. The contributor is the owner for the workspace.
wm.createWorkspace(IContributorHandle scopeHandle, String workspaceName, String description, IWorkspaceConnection defaultFlow, IWorkspaceConnection sourceWorkspace, IProgressMonitor progressMonitor)If you haven't already, download the javadoc for the plain Java client API.
You can also get the currently logged-in user by calling repo.loggedInContributor() if you want to simplify that part.