I am using this example http://rsjazz.wordpress.com/2012/09/19/the-rtc-workitem-link-api-linking-workitems-to-other-elements/ to create links between two work items.
How manyever times I run this sample applicaiotn that many times the same link is created. Normally this is not possible in GUI but via the script it is creating same link many times.
Here is my code snippet
public class LinkingWI {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TeamPlatform.startup();
System.out.println("Platform started successfully");
try {
IProgressMonitor monitor = new SysoutProgressMonitor();
ITeamRepository repo = Login.login(monitor);
System.out.println("Logged in successfully");
Integer iSource = 22;
Integer iTarget = 23;
IWorkItemClient workItemClient = (IWorkItemClient) repo.getClientLibrary(IWorkItemClient.class);
IWorkItem workItem = workItemClient.findWorkItemById(iSource, IWorkItem.FULL_PROFILE, null);
IWorkItemWorkingCopyManager wcm = workItemClient.getWorkItemWorkingCopyManager();
wcm.connect(workItem, IWorkItem.FULL_PROFILE, null);
try {
WorkItemWorkingCopy wc = wcm.getWorkingCopy(workItem);
System.out.println( "inside");
//System.out.println( wc.getWorkItem().getCreator().toString());
System.out.println( wc.getWorkItem().getHTMLSummary().toString());
System.out.println( "Adding contentstarts");
System.out.println( "Summary - " + wc.getWorkItem().getHTMLSummary().toString());
System.out.println( "Linking starts");
//IWorkItemClient oppositeEorkItemClient = (IWorkItemClient) repo.getClientLibrary(IWorkItemClient.class);
IWorkItem oppositeWorkItem = workItemClient.findWorkItemById(iTarget, IWorkItem.FULL_PROFILE, null);
IWorkItemWorkingCopyManager owcm = workItemClient.getWorkItemWorkingCopyManager();
owcm.connect(workItem, IWorkItem.FULL_PROFILE, null);
// WorkItemWorkingCopy owc = owcm.getWorkingCopy(oppositeWorkItem);
WorkItemReferencesModification oWIRFM = new WorkItemReferencesModification(oppositeWorkItem);
oWIRFM.execute(wc, monitor);
System.out.println( "Linking ends");
System.out.println( "save starts");
IDetailedStatus s = wc.save(monitor);
if (!s.isOK()) {
//throw new TeamRepositoryException("Error saving work item",s.getException());
System.out.println(" Skipping Work Item: " + workItem.getId() + "\n");
}else{
System.out.println("INFO - Modified work item: " + workItem.getId() + ".\n");
}
System.out.println( "save ends");
}
catch(Exception e) {
System.out.println( "Workitem not found");
}
} catch (TeamRepositoryException e) {
System.out.println( "Error found");
e.printStackTrace();
}finally
{
TeamPlatform.shutdown();
System.out.println("Platform stopped successfully");
}
}
private static class WorkItemReferencesModification extends WorkItemOperation {
private IWorkItemHandle fOpposite;
public WorkItemReferencesModification(IWorkItemHandle opposite) {
super("Modifying Work Item References",IWorkItem.FULL_PROFILE);
fOpposite = opposite;
}
@Override
protected void execute(WorkItemWorkingCopy workingCopy, IProgressMonitor monitor) throws TeamRepositoryException {
// Create a new reference to the opposite item
IItemReference reference = IReferenceFactory.INSTANCE.createReferenceToItem(fOpposite);
// Add the new reference using a specific work item end point
//workingCopy.getReferences().add(WorkItemEndPoints.BLOCKS_WORK_ITEM, reference);
workingCopy.getReferences().add(WorkItemEndPoints.RESOLVES_WORK_ITEM, reference);
System.out.println( "Setting REsolved by link ends");
}
}
Kindly note the login is done in different class.
Any tips on how to avoid this?