It's all about the answers!

Ask a question

RTC v4.0.1+ Java API: adding component selectively to a workspace

mordechi taitelman (46147) | asked Jul 17 '13, 4:02 a.m.
edited Jul 18 '13, 11:52 p.m.
as subsequent of another question (

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

permanent link
Surya Tripathi (65017) | answered Jul 18 '13, 3:47 p.m.
You could try something along these lines -

IWorkspaceManager wsMan = SCMPlatform.getWorkspaceManager(repo);
IWorkspaceConnection wsCon = wsMan.getWorkspaceConnection();
List<IComponentAdditionOp> ops = new ArrayList<IComponentAdditionOp>();
wsCon.applyComponentOperations(ops, null);

mordechi taitelman selected this answer as the correct answer

mordechi taitelman commented Jul 18 '13, 11:51 p.m.

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.

Surya Tripathi commented Jul 22 '13, 5:03 p.m. | edited Jul 22 '13, 5:09 p.m.

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.
public IComponentAdditionOp addComponent(IComponentHandle comp, boolean produceDetailedUpdates);

Tim Mok commented Jul 22 '13, 5:14 p.m.

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.

2 other answers

permanent link
mordechi taitelman (46147) | answered Jul 23 '13, 4:36 a.m.
edited Jul 23 '13, 4:44 a.m.
thanks for the answer.
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)
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);

     * 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 );
                        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;

        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();
                    oldWorkspace.applyComponentOperations(operationList, monitor);
                } catch (TeamRepositoryException e) {
                    // TODO Auto-generated catch block

permanent link
Tim Mok (6.6k38) | answered Jul 22 '13, 5:10 p.m.
You could also use the workspace connection to compare it against the stream's workspace connection (streams and workspaces are pretty much the same and implement IWorkspaceConnection). The comparison report will tell you if you have a component addition to the workspace. Then you can run an accept operation to get the component.

Your answer

Register or to post your answer.