It's all about the answers!

Ask a question

Changing state of stored "related" work items - pr


Rasto Gregor (2621) | asked Jan 25 '10, 12:25 p.m.
Hi everybody!

I'm creating work item save operation participant.
During that, I would like to update state of all work items "related" to (parent) item being saved.

I expected that following should work:

workItemService.saveWorkItem2(relatedItem.getWorkingCopy(), null, "com.ibm.team.workitem.eclipseWayWorkflow.action.close");


But it is not. Saving finishes successfully but related items stay unchanged.

Could someone please point me in the right direction..
Thanks in advice!

Rasto

3 answers



permanent link
Rasto Gregor (2621) | answered Feb 18 '10, 5:54 a.m.
I attach my code according to Massimo Caprinali's request.
I welcome any code improvements. :)


/** All related parent items are closed during saving child work item */
public class StateChanger extends AbstractService implements IOperationParticipant {

// Using type id's form com.ibm.team.workitem.common plugin.xml (default for simple process)
private final static String CHILD_WORK_ITEM_TYPE_ID = "defect";
private final static String PARENT_WORK_ITEM_TYPE_ID = "task";

// Using workflow constants from eclipseWayWorkflow.xml (default for simple process)
private final static String PARENT_RESOLVING_WORKFLOW_ACTION_ID = "bugzillaWorkflow.action.resolve";
private final static String PARENT_CLOSING_WORKFLOW_ACTION_ID = "bugzillaWorkflow.action.close";

public void run(AdvisableOperation operation, IProcessConfigurationElement participantConfig, IParticipantInfoCollector collector, IProgressMonitor monitor) throws TeamRepositoryException {
// Preparation I.
ISaveParameter saveParam = (ISaveParameter) operation.getOperationData();
IWorkItem child = (IWorkItem) saveParam.getNewState();

// Types other than CHILD_WORK_ITEM_TYPE_ID are not interesting
if (!CHILD_WORK_ITEM_TYPE_ID.equals(child.getWorkItemType())) {
return;
}

// Preparation II.
ILinkService linkService = getService(ILinkService.class);
ILinkServiceLibrary linkLibrary = (ILinkServiceLibrary) linkService.getServiceLibrary(ILinkServiceLibrary.class);

// Listing and resolving links to 'related' work items
IItemReference workItemRef = IReferenceFactory.INSTANCE.createReferenceToItem(child);
ILinkQueryPage linkPage = linkLibrary.findLinks(WorkItemLinkTypes.RELATED_WORK_ITEM, workItemRef);
if (linkPage.getResultSize() == 0) {
addErrorInfo("Defect must have related work item!", "Add related work item before save!", collector);
return;
}

// Preparation III.
IRepositoryItemService repositoryItemService = getService(IRepositoryItemService.class);
IWorkItemServer workItemService = getService(IWorkItemServer.class);
IWorkItemCommon workItemCommon = getService(IWorkItemCommon.class);

// Processing links
ILinkCollection links = linkPage.getAllLinksFromHereOn();
for (ILink link : links) {
IReference otherRef = link.getOtherRef(workItemRef);
IWorkItemHandle parentHandle = (IWorkItemHandle) otherRef.resolve();
IWorkItem parent = (IWorkItem) repositoryItemService.fetchItem(parentHandle, null);
if(PARENT_WORK_ITEM_TYPE_ID.equals(parent.getWorkItemType()) && !isClosed(workItemCommon, parent)) { // process only items with type = PARENT_WORK_ITEM_TYPE_ID and open ones
// Changing to "resolved"
listWorkflowActions(workItemCommon, monitor, parent);
IWorkItem parentWorkingCopy = (IWorkItem) parent.getWorkingCopy();
workItemService.saveWorkItem2(parentWorkingCopy, null, PARENT_RESOLVING_WORKFLOW_ACTION_ID);

// Changing to "closed"
parent = (IWorkItem) repositoryItemService.fetchItem(parentHandle, null);
listWorkflowActions(workItemCommon, monitor, parent);
parentWorkingCopy = (IWorkItem) parent.getWorkingCopy();
workItemService.saveWorkItem2(parentWorkingCopy, null, PARENT_CLOSING_WORKFLOW_ACTION_ID);
// ! Two-phase saving should not be needed for well customized workflow
}
}
}

private static void addErrorInfo(String summary, String description, IParticipantInfoCollector collector) {
if (collector != null) {
IReportInfo info = collector.createInfo(summary, description);
info.setSeverity(IProcessReport.ERROR);
collector.addInfo(info);
}
}

private boolean isClosed(IWorkItemCommon workItemCommon, IWorkItem workItem) {
if (workItem != null) {
Identifier<IState> state= workItem.getState2();
if (state != null) {
try {
IWorkflowInfo workflowInfo= workItemCommon.findWorkflowInfo(workItem, null);
if (workflowInfo.stateGroupContains(IWorkflowInfo.CLOSED_STATES, state))
return true;
} catch (TeamRepositoryException e) {
return false;
}
}
}
return false;
}

}

permanent link
Rasto Gregor (2621) | answered Jan 26 '10, 9:29 a.m.
Yes, there was the problem. The related work items used different workflow.
The action id should have been:
bugzillaWorkflow.action.close


Works well now. Thanks!

Rasto

permanent link
Patrick Streule (4.9k21) | answered Jan 25 '10, 2:38 p.m.
JAZZ DEVELOPER
I'm creating work item save operation participant.
During that, I would like to update state of all work items
"related" to (parent) item being saved.

I expected that following should work:

workItemService.saveWorkItem2(relatedItem.getWorkingCopy(), null,
"com.ibm.team.workitem.eclipseWayWorkflow.action.close");


But it is not. Saving finishes successfully but related items stay
unchanged.

Could someone please point me in the right direction..
Thanks in advice!

The snippet looks basically ok to me. Are the related work items also
using the Eclipse Way Workflow? IIRC, the action IDs are validated
against the workitem's workflow.

--
Regards,
Patrick
Jazz Work Item Team

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.