RTC 4.0.3+ how to convert a URI to an ILink

I need to get an ILink from a String URI that points to a WI in a remote server, so the String URI is really an OSLC URI.
This is what I'm doing...after converting the String to an URI object, I then convert it to an IURIReference to run getLink(), but it returns empty, no ILink is associated with it.
The OSLC URI looks something like this:
https://localhost:9443/jazz/resource/itemName/com.ibm.team.workitem.WorkItem/71
I eventually want to get the full item from the remote server via the ILink. Does anyone know if this is possible? Or if there's a better and more correct way to do this?
This is what I'm doing...after converting the String to an URI object, I then convert it to an IURIReference to run getLink(), but it returns empty, no ILink is associated with it.
The OSLC URI looks something like this:
https://localhost:9443/jazz/resource/itemName/com.ibm.team.workitem.WorkItem/71
I eventually want to get the full item from the remote server via the ILink. Does anyone know if this is possible? Or if there's a better and more correct way to do this?
2 answers

Hi Jossie,
If you are writing client code, you need the ILinkManager.
If you are writing server code, you need the ILinkServiceLibrary.
Both of the interfaces above provide methods to create and save links in the repository. Also, if you are not doing so already, you should use the IReferenceFactory to create the IReference from the URI object.
Finally, I have written a method to convert a URI to a IWorkItem. This only works if you already have a client connection to the remote server via ITeamRepository.
public IWorkItemHandle getWorkItemFromURI(URI workItemURI, ITeamRepository repository) {
try{
Location workItemLocation = Location.location(workItemURI);
IItemHandle itemHandle = repository.itemManager().fetchPartialItem(workItemLocation, IItemManager.DEFAULT, Collections.emptyList(), null);
if(itemHandle != null && itemHandle instanceof IWorkItemHandle){
return (IWorkItemHandle) itemHandle;
}else{
return null;
}
}catch(ItemNotFoundException e){
throw e;
}catch(TeamRepositoryException e){
throw new TeamRepositoryException("Error fetching item by location uri: " + workItemURI.toString(), e);
}
}
Comments

Your example is for client code. How would a repository fetch the workitem object from another repository?

As below, you can look into
https://rsjazz.wordpress.com/2012/09/20/the-rtc-workitem-server-link-api-linking-to-work-items-and-other-elements/ how server API works. Server and client API is very similar, you only use different services and get them in a different way. The code above and the example in the link should provide you with enough example code to get it working.

I'm not real familiar with how two repositories communicate, but wouldn't one repository essentially act as the client of the other?

As far as I can tell, you have to be registered against the same JTS with two servers,or they have to have a friends relationship to be able to set up a CLM link.
Comments
sam detweiler
May 29 '13, 8:16 a.m.you said 'remote server'.. so my assumption is you have two rtc servers and are trying to build a link between workitems on different servers.
support for links only works in the SAME server. (unless it is one of the special change requests as part of the project sharing configuration)..
you can create a 'related artifact' which is really just a standard html link.
1 vote
Jossie McManus
May 30 '13, 12:26 a.m.If by "project sharing configuration" you mean that I have setup both servers to be "friends", then yes, they are setup that way in the configuration using the instructions from 2b on in this article: https://jazz.net/library/article/535
What is a related artifact? Can you point me to where I can get more info for this?
sam detweiler
May 30 '13, 8:40 a.m.in the article you referenced, this section talks about configuring the PROJECTs to share and consume 'Change Requests'. This is in addition to the friends config between servers.
c. Configure project areas
i. Add Change Management (Change Set) association
once you do this you will have an Create Change Request in the Add dropdown on the Links tab of a workitem. A Change Request creates 2 one way links between this workitem and and one in the other configured project. one to, and one back.
this is the only type of RTC link that is supported between two servers, no parent/child, blocks, ...
a Related Artifact is a link to some 'external to RTC' content. it is shown as a choice on the Add dropdown on the links tab. it takes a text string, and a URL.
typically we use them for linking to external to RTC web pages that contain relevant info for the workitem user.
Jossie McManus
May 30 '13, 1:54 p.m.All I have is the URI object for the change request and I just need a way to convert that URI object into a properly created ILink or IReference object. I don't actually need a link between the points, because I'm just trying to pass that ILink object to the client so they can query and do additional work with that change request. Not sure that the related artifact can be useful for my scenario...
sam detweiler
May 30 '13, 2:33 p.m.If you are on system A, with a Change Request to system B, there is not much you can do. you cannot query THRU the change request link, and you cannot get a direct link to the far side (system B) workitem. as link objects are server specific
Jossie McManus
May 30 '13, 3:04 p.m.That confirms my findings.
What about getting an IReference from a URI object? I created an IReference but it seems like an incomplete object, getLink() returns null. Maybe I'm creating it wrong?
IReferenceFactory factory= IReferenceFactory.INSTANCE;
for (URI uri : uriList) {
IURIReference iRef = factory.createReferenceFromURI(uri);
if (iRef != null) {
realLinks.add(iRef.getLink());
}
}
sam detweiler
May 30 '13, 3:44 p.m.ILinks need two IReferences
you need to use the ILinkManager service to manage (create/modify/delete) ILinks.