It's all about the answers!

Ask a question

List and Create Links of a WorkItem in Java Plain API RTC


Tiago Fernandez (5351619) | asked Aug 13 '13, 2:59 p.m.
edited Aug 13 '13, 3:29 p.m.

Hello, please can provide a very simple example on:

1) List Links of a given WorkItem

2) Create a link of a given WorkItem of "Related Artifacs" type.

In my code I only have the workItem:

workItem = workItemClient.findWorkItemById(workItemNumber, IWorkItem.FULL_PROFILE, new SysoutProgressMonitor())

How should I continue ?

Thanks for the help.

Accepted answer


permanent link
Philip Smith (1862) | answered Aug 13 '13, 7:01 p.m.
JAZZ DEVELOPER
It will look something like the following:
import com.ibm.team.links.client.ILinkManager;
import com.ibm.team.links.common.IReference;
import com.ibm.team.links.common.factory.IReferenceFactory;
import com.ibm.team.workitem.common.model.WorkItemLinkTypes
...
final ILinkManager linkManager =
          (ILinkManager) repo.getClientLibrary(ILinkManager.class);
final IReference workItemRef =
          linkManager.referenceFactory().createReferenceToItem(workItemHandle);
final IReference relatedRef = IReferenceFactory.INSTANCE.createReferenceFromURI(
          relatedUri, comment);
final ILink newLink = linkManager.createLink(
WorkItemLinkTypes.RELATED_ARTIFACT, workItemRef, relatedRef); linkManager.saveLink(newLink, monitor);

Tiago Fernandez selected this answer as the correct answer

Comments
Tiago Fernandez commented Aug 13 '13, 8:59 p.m.

Thank you very much for the response,

I will do a try and let you know how it goes.

This seems to be how to create a link to a workItem,

Could you kindly give an example on how to list existing links of a given workItem as well?

Thank you very much.


Philip Smith commented Aug 13 '13, 9:48 p.m.
JAZZ DEVELOPER
Something like this:
final ILinkQueryPage queryPage =
            linkManager.findLinksBySource(linkType, workItemRef, monitor);
final Collection<ILink> links = queryPage.getAllLinksFromHereOn();
for (final ILink l : links)
{
      final IItemHandle linkHandle = (IItemHandle) l.getTargetRef().resolve();
      ...
}

 


Tiago Fernandez commented Aug 14 '13, 10:57 a.m.

Thanks again for the response.

I like the simplicity of your responses :) , that is exactly what I need.
In the code above, how should I do to println the link that has the linkHandle ?

Also I take advanyage and I'd like to do another question not related to the title, how to set a value (for example an integer or string value) to an attribute of a workItem? (I already have the workItem and the Attribute in my code).

Thanks a lot.


Philip Smith commented Aug 14 '13, 1:08 p.m.
JAZZ DEVELOPER

Depends on what you want to print. IReference, the result of l.getTargetRef(), has a createURI method which is probably the best way to get the value of the target of the link in this situation (I think my last example might get a ClassCastException since the target came from a URI). The URI should be easy to print.



I've not tried modifying workitems, but I think it requires creating a working copy of the workitem, modifying the working copy, then saving it.


Tiago Fernandez commented Aug 14 '13, 3:11 p.m.

I am trying to show the url that has link of type related artifacts of the workItem. The code I have is:

def linkManager = (ILinkManager) repo.getClientLibrary(ILinkManager.class)
  def workItemRef = linkManager.referenceFactory().createReferenceToItem(workItem)
  
  
  def queryPage = linkManager.findLinksBySource(WorkItemLinkTypes.RELATED_ARTIFACT, workItemRef, new SysoutProgressMonitor());
  def links = queryPage.getAllLinksFromHereOn();
  for(ILink l : links){ 
      println("Link is: " + l.getLinkTypeId() + ", url: " + l.getThisEndpointDescriptor(l.getTargetRef()).getIcon())

  }

But the print of the last for is showing another information, and I don't know what to put there. How should I use that println to show the URL and the comment of those related artifacts links?

Thanks for the help.


Philip Smith commented Aug 14 '13, 3:47 p.m.
JAZZ DEVELOPER

I was thinking: l.getTargetRef().createURI() 


Philip Smith commented Aug 14 '13, 3:48 p.m.
JAZZ DEVELOPER

For the comment: .getTargetRef().getComment() 


Tiago Fernandez commented Aug 14 '13, 4:39 p.m.

You rock!!!! :)

All your responses lead me to the solution. Now my code works.

I will mark this topic as Accepted Answer.

Thank you very very much.


LUIZ DE ALMEIDA commented Jan 29 '15, 8:15 a.m.

Tiago,
Could you share your code that lists the related workitens of a given workitem?
If you could, I will thank you so much.

showing 5 of 9 show 4 more comments

One other answer



permanent link
Benjamin Maier (3339) | answered Mar 28 '17, 5:54 a.m.

Hello Tiago,

Can you share the code please? I have the same problem.
Thanks!

Your answer


Register or 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.