How to programmatically add Related Change Request link?
6 answers
Hi,
Please look at https://jazz.net/wiki/bin/view/Main/ProgrammaticLinkCreation
Does that answer your question?
Rgs,
/N
No, all examples in the document references to code on client side. I need to be able create a link to the workitem in the save operation for the Operation Advisor on the server side.
No, all examples in the document references to code on client side. I need to be able create a link to the workitem in the save operation for the Operation Advisor on the server side.
OK -- might not be the best way, but code below is working (tried using RTC 3) .. and some lessons learned.
1. See https://jazz.net/wiki/bin/view/Main/RTCSDK20_ProcessPreConditionExample and the example https://jazz.net/wiki/pub/Main/RTCSDK20_ProcessPreConditionExample/advisor-example.zip
2. To get access to services for creating items etc - make your handler a subclass to the AbstractService. And to get access to services in the advisor you must ask for them as preconditions. See example below
<plugin>
<extension
point="com.ibm.team.process.service.operationAdvisors">
<operationAdvisor
class="com.ibm.ecat.exampleadvisor.LinkCreationTest"
id="com.ibm.ecat.exampleAdvisor.prohibitSave"
name="Creating Parent 2"
operationId="com.ibm.team.workitem.operation.workItemSave">
<extensionService
componentId="com.ibm.ecat.exampleAdvisor.linkCreationTest2"
implementationClass="com.ibm.ecat.exampleadvisor.LinkCreationTest">
<prerequisites>
<requiredService interface="com.ibm.team.workitem.service.IAuditableServer" />
<requiredService interface="com.ibm.team.workitem.service.IWorkItemServer" />
</prerequisites>
</extensionService>
</operationAdvisor>
</extension>
</plugin>
3. And a code snippet with an advisor that creates a Parent & and link to the new parent each time the item is saved (note: needs to turn off the FiledAgainst precondition for SaveItem to have this work)
package com.ibm.ecat.exampleadvisor;
import java.util.Collections;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import com.ibm.team.foundation.common.text.XMLString;
import com.ibm.team.process.common.IProcessArea;
import com.ibm.team.process.common.IProcessConfigurationElement;
import com.ibm.team.process.common.IProjectArea;
import com.ibm.team.process.common.IProjectAreaHandle;
import com.ibm.team.process.common.advice.AdvisableOperation;
import com.ibm.team.process.common.advice.IAdvisorInfoCollector;
import com.ibm.team.process.common.advice.runtime.IOperationAdvisor;
import com.ibm.team.repository.common.IAuditable;
import com.ibm.team.repository.common.TeamRepositoryException;
import com.ibm.team.repository.service.AbstractService;
import com.ibm.team.workitem.common.ISaveParameter;
import com.ibm.team.workitem.common.model.IWorkItem;
import com.ibm.team.workitem.common.model.IWorkItemReferences;
import com.ibm.team.workitem.common.model.IWorkItemType;
import com.ibm.team.workitem.common.model.ItemProfile;
import com.ibm.team.workitem.common.model.WorkItemEndPoints;
import com.ibm.team.workitem.common.model.WorkItemLinkTypes;
import com.ibm.team.workitem.common.model.WorkItemTypes;
import com.ibm.team.workitem.service.IAuditableServer;
import com.ibm.team.workitem.service.IWorkItemServer;
public class LinkCreationTest extends AbstractService implements IOperationAdvisor {
public static final String PROBLEM_TYPE = " HelloWorldAdvisor.prohibitSave";
public void run(AdvisableOperation operation,
IProcessConfigurationElement advisorConfiguration,
IAdvisorInfoCollector collector, IProgressMonitor monitor)
throws TeamRepositoryException {
Object data = operation.getOperationData();
if (data instanceof ISaveParameter) {
ISaveParameter param = (ISaveParameter) data;
IAuditable auditable = param.getNewState();
if (auditable instanceof IWorkItem) {
IWorkItem sourceworkItem = (IWorkItem) auditable;
if (sourceworkItem.isNewItem())
return;
IProcessArea processArea = operation.getProcessArea();
IProjectAreaHandle projectAreaHandle = processArea.getProjectArea();
IAuditableServer auditableServer= getService(IAuditableServer.class);
IProjectArea projectArea= auditableServer.resolveAuditable(
projectAreaHandle,
ItemProfile.<IProjectArea>createFullProfile(IProjectArea.ITEM_TYPE),
monitor);
createWorkItem(projectArea, "Parent to " + sourceworkItem.getId(), sourceworkItem);
}
}
}
private IWorkItem createWorkItem(IProjectArea projectArea, String summary, IWorkItem source) throws TeamRepositoryException {
IWorkItemServer workItemServer = getService(IWorkItemServer.class);
IWorkItemType defectType = workItemServer.findWorkItemType(projectArea, WorkItemTypes.DEFECT, null);
IWorkItem workItem= workItemServer.createWorkItem2(defectType);
workItem.setHTMLSummary(XMLString.createFromPlainText(summary));
IWorkItemReferences references= workItemServer.resolveWorkItemReferences(workItem, null);
references.add(WorkItemEndPoints.PARENT_WORK_ITEM, WorkItemLinkTypes.createWorkItemReference(source));
System.out.println("Create work item " + summary + ": " + workItem.getItemId().getUuidValue());
long saveStart= System.currentTimeMillis();
IStatus status= workItemServer.saveWorkItem3(workItem, references, null, Collections.<String>emptySet());
System.out.println("Save total: "+ (System.currentTimeMillis() - saveStart));
if (status.matches(IStatus.ERROR)) {
throw new TeamRepositoryException(status.getMessage());
}
return workItem;
}
}
Follow up question from celin:
===
Now I had time to test your example and got it to work, but unfortunately it does not address the problem I have, I want to add a Change Management link to the current workitem.
I have not found how to create a IEndPointDescriptor of the type that the reference to MHWeb becomes, to use the code below.
I have resolved the references and see that id = "relatedChangeManagement" for this type EndPointDecsriptor when we sent the link to the RTC from Mhweb by passing the tag
<oslc_cm>
references.add(WorkItemEndPoints.PARENT_WORK_ITEM, WorkItemLinkTypes.createWorkItemReference(source));
===
So -- how to find the way of programmatically create a link of Change Management linktype?
/N
===
Now I had time to test your example and got it to work, but unfortunately it does not address the problem I have, I want to add a Change Management link to the current workitem.
I have not found how to create a IEndPointDescriptor of the type that the reference to MHWeb becomes, to use the code below.
I have resolved the references and see that id = "relatedChangeManagement" for this type EndPointDecsriptor when we sent the link to the RTC from Mhweb by passing the tag
<oslc_cm>
references.add(WorkItemEndPoints.PARENT_WORK_ITEM, WorkItemLinkTypes.createWorkItemReference(source));
===
So -- how to find the way of programmatically create a link of Change Management linktype?
/N
Something like this may work, where you create a link of WorkItemLinkeTypes.RELATED_CHANGE_MANAGEMENT
ILinkManager linkManager= (ILinkManager) repo.getClientLibrary(ILinkManager.class);
IItemReference sourceRef= IReferenceFactory.INSTANCE.createReferenceToItem(source.getItemHandle());
IItemReference targetRef= IReferenceFactory.INSTANCE.createReferenceToItem(target.getItemHandle());
ILink link= ILinkFactory.INSTANCE.createLink(WorkItemLinkTypes.PARENT_WORK_ITEM, sourceRef, targetRef);
linkManager.saveLink(link, monitor);
For advisors, the solutions above don't work to add link to current work item.
What you have to do in this case is only that:
You don't need to save anything to edit the current work item because you are already in a save operation.
What you have to do in this case is only that:
Object data = operation.getOperationData();
ISaveParameter saveParameter = (ISaveParameter) data;
IWorkItemReferences references = saveParameter.getNewReferences();
Reference reference = (Reference)IReferenceFactory.INSTANCE.createReferenceToItem(wiParent);
references.add(WorkItemEndPoints.PARENT_WORK_ITEM, reference);
You don't need to save anything to edit the current work item because you are already in a save operation.