How to programmatically add components to workspace
We would programmatically add existing components to new workspace:
IScmService scmService = ... IContributorHandle contributorH = ... IWorkspaceHandle stream = ... IWorkspace wks = scmService.createWorkspace(contributorH, wksName, wksDescription, stream, null, stream, null, null, IRepositoryProgressMonitor.ITEM_FACTORY.createItem(monitor)).getWorkspace();
IComponentHandle componentH = ...
IWorkspaceHandle targetH = ...
scmService.addComponentFromWorkspace(wks, componentH, targetH, false, IScmService.DELTA_PER_INVOCATION, null, IRepositoryProgressMonitor.ITEM_FACTORY.createItem(monitor));
While the createWorkspace operation creates the new repository workspace, the addComponentFromWorkspace operation does not add the component... We have to call any save/refresh operation?
The code above is called by a save work-item follow up action.
Thanks in advance.
4 answers
I worked from Snippet 2 from the plain Java Client Libraries. I am not sure where you have your code. On a client I was able to add components this way:
// Add new components Collectioncomponents = extComponents.values(); for (Object comp : components) { IComponentHandle cHandle = (IComponentHandle) comp; workspaceConnection.applyComponentOperations(Collections .singletonList(workspaceConnection.componentOpFactory() .addComponent(cHandle, false)), true, monitor); }
Comments
1 vote
We tried a different approach adding an existing flow target to the new workspace:
IWorkspace wks = ...
List<FlowEntry> flows = ((Workspace) wks).getFlows();
List<FlowEntry> acceptFlows = new ArrayList<FlowEntry>();
List<FlowEntry> deliverFlows = new ArrayList<FlowEntry>();
for (FlowEntry flow : flows) {
if ((flow.getFlags() & FlowFlags.ACCEPT ) != 0) {
acceptFlows.add((FlowEntry) EcoreUtil.copy((EObject) flow));
} else if ((flow.getFlags() & FlowFlags.DELIVER) != 0) {
deliverFlows.add((FlowEntry) EcoreUtil.copy((EObject) flow));
}
}
FlowEntry flow = ScmFactory.eINSTANCE.createFlowEntry();
flow.setTargetWorkspace(target);
flow.setFlags(FlowFlags.DELIVER);
deliverFlows.add(flow);
CurrentFlows currentFlows = (CurrentFlows) EcoreUtil.copy((EObject) ((Workspace) wks).getCurrentFlows());
IComponentHandle[] components = ...
CurrentFlows[] currentComponentFlows = ...
scmService.setWorkspaceFlows(wks, acceptFlows.toArray(new FlowEntry[0]), deliverFlows.toArray(new FlowEntry[0]), currentFlows, components, currentComponentFlows, null, IRepositoryProgressMonitor.ITEM_FACTORY.createItem(monitor));
We have to use EcoreUtil.copy() due to ImmutablePropertyException throwned.
We don't undertand how to set components and currentComponentFlows values... Looking at SCMHelper.addDeliverFlow(), we see empty arrays were passed, but in our case we need to add components, too... Any advice?
In fact, we would obtain this client behavior: adding a flow target to build workspace leads to add all components to workspace when a build is request.
Thanks in advance.
We tried to use IScmService#updateComponents (instead of IScmService#updateComponents2 beacuse we are still on RTC v2.0.0.2) but nothing changed:
IWorkspace wks = scmService.createWorkspace(contributorH, wksName, wksDescription, stream, null, stream, null, null, IRepositoryProgressMonitor.ITEM_FACTORY.createItem(monitor)).getWorkspace();
IComponentHandle componentH = ...
IWorkspaceHandle seedH = ...
IComponentOpDescriptor op = IComponentOpDescriptor.FACTORY.addComponent(componentH, seedH, false);
scmService.updateComponents(wksH, null, new IComponentOpDescriptor[]{ op }, false, IScmService.DELTA_PER_INVOCATION, null, IRepositoryProgressMonitor.ITEM_FACTORY.createItem(monitor));
Any advice? (To solve this problem is really important for us!)
Thanks in advance.
Comments
Ok, we found what was the problem! :D
The repository worskpace is created as well as components are added, but when that workspace is used as a build workspace (and this is our case!), components are dropped by the build request process if the flow targets table does not contain streams where components belong.
By the way, this behavior seems quite strange for us...
Comments
SEC Servizi
Sep 09 '13, 10:33 a.m.