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 ? |
Accepted answer
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
Comments
mordechi taitelman
commented Jul 18 '13, 11:51 p.m.
I don't understand here what addComponent() does. adds a new empty component ?
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.
1
You should get the IWorkspaceConnection for the stream and workspace. Then you can call #getComponents() on both objects to compare the list of components.
|
2 other answers
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> |
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
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.