RTC v4.0.1+ Java API: adding component selectively to a workspace
it is nice you can create via Java API a workspace from a parent stream object.
What if I have a stream
then create from it a workspace (using the API from the previous question)
later I update the stream and insert a new component.
How can I programatically , traverse the stream components and add to my old Workspace the missing component ?
Accepted answer
IWorkspaceManager wsMan = SCMPlatform.getWorkspaceManager(repo);
IWorkspaceConnection wsCon = wsMan.getWorkspaceConnection();
List<IComponentAdditionOp> ops = new ArrayList<IComponentAdditionOp>();
ops.add(wsCon.componentOpFactory().addComponent());
wsCon.applyComponentOperations(ops, null);
Comments
I don't understand here what addComponent() does. adds a new empty component ?
What I need is to traverse the component list of a specific Workspace.
from your sample it seems you find the Component factory and creates a new one.
I want to add an exisiting component from a a stream to an exisitng workspace.
You can useĀ IWorkspaceManager.findComponents() to get the list of components you are interested in. Once you have a list of components, you can use addComponent(), which takes IComponentHandle as argument, to add desired component to your workspace.
IComponentOpFactory:
public IComponentAdditionOp addComponent(IComponentHandle comp, boolean produceDetailedUpdates);
You should get the IWorkspaceConnection for the stream and workspace. Then you can call #getComponents() on both objects to compare the list of components.
When you know there's a component missing, you create the component operation and specify the stream as the seed. Run the operation on the workspace's IWorkspaceConnection to add the component. You will need one operation per component that you want to add and you can add it to the list to add multiple components at once.
Check the javadoc for how to use it. Come back if it isn't clear what a method does.
1 vote
2 other answers
As always, I like to post a code sniplet . so here is a sample code to perform the task:
(to find the content of getRepositoryWorkspace method, look at my older posts)
<code>
public void someMethod(ITeamRepository repo, IProgressMonitor monitor)
{
IWorkspaceConnection wrkspace = getRepositoryWorkspace(repo,rtcWorkspaceName,IWorkspaceSearchCriteria.WORKSPACES, monitor);
String masterStreamName = ....;
IWorkspaceConnection masterStream = getRepositoryWorkspace(repo,masterStreamName,IWorkspaceSearchCriteria.STREAMS,monitor);
...
compareAndAddMissingComponents(masterStream,wrkspace,monitor);
...
}
/**
* A method that takes an old workspace and verifies its in sync with all the new component in some stream.
* If the stream has been updated recently with new RTC components, the method
* will update the workspace to include the new components too.
* @param baseStream
* @param oldWorkspace
* @param monitor
*/
public void compareAndAddMissingComponents(IWorkspaceConnection baseStream, IWorkspaceConnection oldWorkspace,IProgressMonitor monitor)
{
Collection<? extends ICurrentComponentInfo> streamComponents = baseStream.getComponentsInfo();
Collection<? extends ICurrentComponentInfo> workspaceComponents = oldWorkspace.getComponentsInfo();
if (streamComponents != null && workspaceComponents != null) {
if (streamComponents.size() != workspaceComponents.size()) {
for (ICurrentComponentInfo newComponents : streamComponents) {
if (!workspaceComponents.contains(newComponents)) {
UUID uuid = newComponents.getComponent().getItemId();
WorkspaceMissingDialog dialog = new WorkspaceMissingDialog(newComponents,oldWorkspace );
Display.getDefault().syncExec(dialog);
System.out.println(oldWorkspace.getName() + " is missing a new component:"+
uuid+ ". should I add to "+oldWorkspace.getName());
}
}
}
}
}
class WorkspaceMissingDialog implements Runnable
{
ICurrentComponentInfo newComponent = null;
IWorkspaceConnection oldWorkspace = null;
public WorkspaceMissingDialog(ICurrentComponentInfo NewComponents,IWorkspaceConnection OldWorkspace)
{
newComponent = NewComponents;
oldWorkspace = OldWorkspace;
}
@Override
public void run() {
final String oldWorkspaceName = oldWorkspace.getName();
UUID uuid = newComponent.getComponent().getItemId();
String message= oldWorkspaceName + " workspace is mising a new component named:"+uuid+
". should I update the workspace definitions ?";
approvedToAppendToOldWorkspace = MessageDialog.openConfirm(shell, "New Workspace?", message);
if (approvedToAppendToOldWorkspace) {
try {
IProgressMonitor monitor = null;
IComponentOp addOperatrion = oldWorkspace.componentOpFactory().addComponent(newComponent.getComponent(), false);
List operationList = new ArrayList();
operationList.add(addOperatrion);
oldWorkspace.applyComponentOperations(operationList, monitor);
} catch (TeamRepositoryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
</code>