Jazz Forum Welcome to the Jazz Community Forum Connect and collaborate with IBM Engineering experts and users

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

as subsequent of another question ( https://jazz.net/forum/questions/119556/rtc-client-401-api-can-i-add-components-to-a-workspace-using-a-loadrulexml)

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 ?

1 vote


Accepted answer

Permanent link
You could try something along these lines -

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

mordechi taitelman selected this answer as the correct answer

1 vote

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

Permanent link
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)
<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>

2 votes


Permanent link
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.

1 vote

Your answer

Register or log in to post your answer.

Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.

Search context
Follow this question

By Email: 

Once you sign in you will be able to subscribe for any updates here.

By RSS:

Answers
Answers and Comments
Question details
× 10,936
× 411
× 24

Question asked: Jul 17 '13, 4:02 a.m.

Question was seen: 7,174 times

Last updated: Jul 23 '13, 4:44 a.m.

Confirmation Cancel Confirm