FollowUp action
hi all,
it's possible programmatically modify the value for a custom attributes or bult-in attributes (like summary, description,...) in a follow up action on Save Work Item (server) operation?
I have tried so:
but I have this exception:
com.ibm.team.repository.common.StaleDataException: Stale Data
:((((
Any suggestion?
Thanks
Andrea
it's possible programmatically modify the value for a custom attributes or bult-in attributes (like summary, description,...) in a follow up action on Save Work Item (server) operation?
I have tried so:
IWorkItem wi = (IWorkItem) ((ISaveParameter) data).getNewState();
IWorkItem wiCopy = (IWorkItem) wi.getWorkingCopy();
wiCopy.setValue(MyCustomAttribute, null);
wis.saveWorkItem2(wiCopy, null, null);
but I have this exception:
com.ibm.team.repository.common.StaleDataException: Stale Data
:((((
Any suggestion?
Thanks
Andrea
8 answers
We got a Stale Data exception if we use:
IWorkItem wi = (IWorkItem) data.getNewState();IWorkItem wiFreshCopy = (IWorkItem) wi.getWorkingCopy();
to fetch the working copy of a work-item.
Instead, if we use:
IWorkItem wi = (IWorkItem) data.getNewState();IWorkItem wiFreshCopy = (IWorkItem) wiServer.findWorkItemById(wi.getId(), IWorkItem.FULL_PROFILE, monitor).getWorkingCopy();
the update of the work-item works properly.
Thanks to Patrick Streule for his tip: https://jazz.net/forum/questions/40205/followup-action/40208
Cheers.When you save an item more than once, you will need to make sure that you always have the latest state by:- either use the item returned by the save call- fetch the item from the repository again
Thanks for the answer.
Not sure if this can help, but I tend to not use the working copy manager, working copy and save anymore. Following https://jazz.net/wiki/bin/view/Main/ProgrammaticWorkItemCreation I use an WorkItemOperation to wrap the modification, similar to the creation in the examples presented in the link above.
My assumption is that it helps me sticking to the rules, even if they change better, since the framework parts is handled by the operation code.
This is the inner class implementing the operation. Please note the Load profile in the constructor.
private static class WorkItemModification extends WorkItemOperation {
private String fAttributeID;
private Object fValue;
public WorkItemModification(String attributeID, Object value) {
super("Modifying Work Item",IWorkItem.FULL_PROFILE);
fAttributeID=attributeID;
fValue=value;
}
@Override
protected void execute(WorkItemWorkingCopy workingCopy,
IProgressMonitor monitor) throws TeamRepositoryException {
IWorkItem workItem = workingCopy.getWorkItem();
ITeamRepository teamRepository = (ITeamRepository)workItem.getOrigin();
IWorkItemClient workItemClient= (IWorkItemClient) teamRepository.getClientLibrary(IWorkItemClient.class);
IAttribute attribute = workItemClient.findAttribute(workItem.getProjectArea(),
fAttributeID, monitor);
if(null!=attribute){
if(workItem.hasCustomAttribute(attribute))
workItem.setValue(attribute, fValue);
}
}
This is how it gets called:
IWorkItem workItem = workItemClient.findWorkItemById(id, IWorkItem.FULL_PROFILE, monitor);
WorkItemModification operation = new WorkItemModification("anAttributeID","someValue");
operation.run(workItem, monitor);
Not sure if this can help, but I tend to not use the working copy manager, working copy and save anymore. Following https://jazz.net/wiki/bin/view/Main/ProgrammaticWorkItemCreation I use an WorkItemOperation to wrap the modification, similar to the creation in the examples presented in the link above.
My assumption is that it helps me sticking to the rules, even if they change better, since the framework parts is handled by the operation code.
This is the inner class implementing the operation. Please note the Load profile in the constructor.
private static class WorkItemModification extends WorkItemOperation {
private String fAttributeID;
private Object fValue;
public WorkItemModification(String attributeID, Object value) {
super("Modifying Work Item",IWorkItem.FULL_PROFILE);
fAttributeID=attributeID;
fValue=value;
}
@Override
protected void execute(WorkItemWorkingCopy workingCopy,
IProgressMonitor monitor) throws TeamRepositoryException {
IWorkItem workItem = workingCopy.getWorkItem();
ITeamRepository teamRepository = (ITeamRepository)workItem.getOrigin();
IWorkItemClient workItemClient= (IWorkItemClient) teamRepository.getClientLibrary(IWorkItemClient.class);
IAttribute attribute = workItemClient.findAttribute(workItem.getProjectArea(),
fAttributeID, monitor);
if(null!=attribute){
if(workItem.hasCustomAttribute(attribute))
workItem.setValue(attribute, fValue);
}
}
This is how it gets called:
IWorkItem workItem = workItemClient.findWorkItemById(id, IWorkItem.FULL_PROFILE, monitor);
WorkItemModification operation = new WorkItemModification("anAttributeID","someValue");
operation.run(workItem, monitor);
I tried this simple code but still does not work:
At first time that I save my work item, the description changes.
The second time that I save my work item, the description don't changes and I found this exception "Stale Data".
package com.ibm.rational.rtc.docreview;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.eclipse.core.runtime.IProgressMonitor;
import com.ibm.team.foundation.common.text.XMLString;
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.runtime.IOperationParticipant;
import com.ibm.team.process.common.advice.runtime.IParticipantInfoCollector;
import com.ibm.team.repository.common.IExtensibleItem;
import com.ibm.team.repository.common.IItemHandle;
import com.ibm.team.repository.common.TeamRepositoryException;
import com.ibm.team.repository.migration.blackboard.IArbitraryProperties;
import com.ibm.team.repository.service.AbstractService;
import com.ibm.team.repository.service.IRepositoryItemService;
import com.ibm.team.workitem.common.ISaveParameter;
import com.ibm.team.workitem.common.internal.model.EnumerationAttributeType;
import com.ibm.team.workitem.common.model.AttributeType;
import com.ibm.team.workitem.common.model.AttributeTypes;
import com.ibm.team.workitem.common.model.IAttribute;
import com.ibm.team.workitem.common.model.IAttributeHandle;
import com.ibm.team.workitem.common.model.IEnumeration;
import com.ibm.team.workitem.common.model.ILiteral;
import com.ibm.team.workitem.common.model.IWorkItem;
import com.ibm.team.workitem.common.model.IWorkItemType;
import com.ibm.team.workitem.common.model.Identifier;
import com.ibm.team.workitem.common.model.WorkItemTypes;
import com.ibm.team.workitem.service.IWorkItemServer;
import com.ibm.team.workitem.service.IWorkItemWrapper;
import com.ibm.team.workitem.service.internal.WorkItemWrapper;
@SuppressWarnings("restriction")
public class OpenDocDefect extends AbstractService implements IOperationParticipant {
boolean done = false;
@SuppressWarnings({ "restriction", "deprecation" })
public void run(AdvisableOperation operation,
IProcessConfigurationElement participantConfig,
IParticipantInfoCollector collector, IProgressMonitor monitor)
throws TeamRepositoryException
{
Object data = operation.getOperationData();
IRepositoryItemService repositoryItemService = getService(IRepositoryItemService.class);
IWorkItem workingCopy;
if (data instanceof ISaveParameter) {
IWorkItem wi = (IWorkItem) ((ISaveParameter) data).getNewState();
if (wi != null)
{
workingCopy = (IWorkItem) wi.getWorkingCopy();
String wiType = workingCopy.getWorkItemType();
if (wiType.equalsIgnoreCase("task") || wiType.equalsIgnoreCase("defect") )
{
List<IAttributeHandle> customAttrs = workingCopy.getCustomAttributes();
try
{
IWorkItemServer wis = getService(IWorkItemServer.class);
workingCopy.setHTMLDescription(XMLString.createFromPlainText("BLABLABLA"));
wis.saveWorkItem2(workingCopy, null, null);
}
catch (Throwable e)
{
e.printStackTrace();
}
}
}
}
}
}
At first time that I save my work item, the description changes.
The second time that I save my work item, the description don't changes and I found this exception "Stale Data".
osgi>
osgi> com.ibm.team.repository.common.StaleDataException: Stale Data
at com.ibm.team.workitem.service.internal.WorkItemServer.saveWorkItems(W
orkItemServer.java:129)
at com.ibm.team.workitem.service.internal.WorkItemServer.saveWorkItem3(W
orkItemServer.java:104)
at com.ibm.team.workitem.service.internal.WorkItemServer.saveWorkItem2(W
orkItemServer.java:99)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:79)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:618)
at com.ibm.team.workitem.service.internal.ServiceAdapter.invoke(ServiceA
dapter.java:74)
at $Proxy179.saveWorkItem2(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:79)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:618)
at org.eclipse.soda.sat.core.internal.record.ExportProxyServiceRecord.in
voke(ExportProxyServiceRecord.java:370)
at org.eclipse.soda.sat.core.internal.record.ExportProxyServiceRecord.ac
cess$0(ExportProxyServiceRecord.java:356)
at org.eclipse.soda.sat.core.internal.record.ExportProxyServiceRecord$Ex
portedServiceInvocationHandler.invoke(ExportProxyServiceRecord.java:56)
at $Proxy180.saveWorkItem2(Unknown Source)
at com.ibm.rational.rtc.docreview.OpenDocDefect.run(OpenDocDefect.java:8
7)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:79)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:618)
at org.eclipse.soda.sat.core.internal.record.ExportProxyServiceRecord.in
voke(ExportProxyServiceRecord.java:370)
at org.eclipse.soda.sat.core.internal.record.ExportProxyServiceRecord.ac
cess$0(ExportProxyServiceRecord.java:356)
at org.eclipse.soda.sat.core.internal.record.ExportProxyServiceRecord$Ex
portedServiceInvocationHandler.invoke(ExportProxyServiceRecord.java:56)
at $Proxy258.run(Unknown Source)
at com.ibm.team.process.internal.common.advice.runtime.OperationAdviceMa
nager.runParticipant(OperationAdviceManager.java:1048)
at com.ibm.team.process.internal.common.advice.runtime.OperationAdviceMa
nager.runParticipants(OperationAdviceManager.java:907)
at com.ibm.team.process.internal.common.advice.runtime.OperationAdviceMa
nager.advise(OperationAdviceManager.java:290)
at com.ibm.team.process.internal.common.util.AbstractProcess.doAdvise(Ab
stractProcess.java:181)
at com.ibm.team.process.internal.service.ServerProcess.access$1(ServerPr
ocess.java:1)
at com.ibm.team.process.internal.service.ServerProcess$2.run(ServerProce
ss.java:132)
at com.ibm.team.repository.service.internal.TransactionService$1.run(Tra
nsactionService.java:104)
at com.ibm.team.repository.service.internal.rdb.RepositoryDatabase.runTr
ansaction(RepositoryDatabase.java:320)
at com.ibm.team.repository.service.internal.rdb.RepositoryDatabase.runIn
Transaction(RepositoryDatabase.java:244)
at com.ibm.team.repository.service.internal.TransactionService.runInTran
saction(TransactionService.java:79)
at com.ibm.team.repository.service.internal.TransactionService.runInTran
saction(TransactionService.java:68)
at sun.reflect.GeneratedMethodAccessor170.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:618)
at org.eclipse.soda.sat.core.internal.record.ExportProxyServiceRecord.in
voke(ExportProxyServiceRecord.java:370)
at org.eclipse.soda.sat.core.internal.record.ExportProxyServiceRecord.ac
cess$0(ExportProxyServiceRecord.java:356)
at org.eclipse.soda.sat.core.internal.record.ExportProxyServiceRecord$Ex
portedServiceInvocationHandler.invoke(ExportProxyServiceRecord.java:56)
at $Proxy44.runInTransaction(Unknown Source)
at com.ibm.team.process.internal.service.ProcessService.runInTransaction
(ProcessService.java:3361)
at com.ibm.team.process.internal.service.ProcessService.runInTransaction
(ProcessService.java:3368)
at com.ibm.team.process.internal.service.ServerProcess.adviseAndExecute(
ServerProcess.java:130)
at com.ibm.team.workitem.service.internal.AuditableServerProcess.adviseA
ndExecute(AuditableServerProcess.java:82)
at com.ibm.team.workitem.service.internal.WorkItemRepositoryService.proc
essSave(WorkItemRepositoryService.java:419)
at com.ibm.team.workitem.service.internal.WorkItemRepositoryService.save
(WorkItemRepositoryService.java:378)
at com.ibm.team.workitem.service.internal.WorkItemRepositoryService.save
(WorkItemRepositoryService.java:351)
at com.ibm.team.workitem.service.internal.WorkItemRepositoryService.acce
ss$3(WorkItemRepositoryService.java:347)
at com.ibm.team.workitem.service.internal.WorkItemRepositoryService$1.ru
n(WorkItemRepositoryService.java:230)
at com.ibm.team.workitem.service.internal.WorkItemRepositoryService$1.ru
n(WorkItemRepositoryService.java:1)
at com.ibm.team.repository.service.internal.TransactionService$1.run(Tra
nsactionService.java:104)
at com.ibm.team.repository.service.internal.rdb.RepositoryDatabase$Trans
action.run(RepositoryDatabase.java:466)
at com.ibm.team.repository.service.internal.rdb.RepositoryDatabase$1.run
(RepositoryDatabase.java:292)
at com.ibm.team.repository.service.internal.rdb.ConnectionPoolService.wi
thCurrentConnection(ConnectionPoolService.java:331)
at sun.reflect.GeneratedMethodAccessor85.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:618)
at org.eclipse.soda.sat.core.internal.record.ExportProxyServiceRecord.in
voke(ExportProxyServiceRecord.java:370)
at org.eclipse.soda.sat.core.internal.record.ExportProxyServiceRecord.ac
cess$0(ExportProxyServiceRecord.java:356)
at org.eclipse.soda.sat.core.internal.record.ExportProxyServiceRecord$Ex
portedServiceInvocationHandler.invoke(ExportProxyServiceRecord.java:56)
at $Proxy21.withCurrentConnection(Unknown Source)
at com.ibm.team.repository.service.internal.rdb.RepositoryDatabase.runTr
ansaction(RepositoryDatabase.java:288)
at com.ibm.team.repository.service.internal.rdb.RepositoryDatabase.runIn
Transaction(RepositoryDatabase.java:244)
at com.ibm.team.repository.service.internal.TransactionService.runInTran
saction(TransactionService.java:79)
at com.ibm.team.repository.service.internal.TransactionService.runInTran
saction(TransactionService.java:68)
at sun.reflect.GeneratedMethodAccessor170.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:618)
at org.eclipse.soda.sat.core.internal.record.ExportProxyServiceRecord.in
voke(ExportProxyServiceRecord.java:370)
at org.eclipse.soda.sat.core.internal.record.ExportProxyServiceRecord.ac
cess$0(ExportProxyServiceRecord.java:356)
at org.eclipse.soda.sat.core.internal.record.ExportProxyServiceRecord$Ex
portedServiceInvocationHandler.invoke(ExportProxyServiceRecord.java:56)
at $Proxy44.runInTransaction(Unknown Source)
at com.ibm.team.repository.service.AbstractService.runInTransaction(Abst
ractService.java:772)
at com.ibm.team.workitem.service.internal.WorkItemRepositoryService.save
Transaction(WorkItemRepositoryService.java:219)
at com.ibm.team.workitem.service.internal.WorkItemRepositoryService.save
(WorkItemRepositoryService.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:79)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:618)
at org.eclipse.soda.sat.core.internal.record.ExportProxyServiceRecord.in
voke(ExportProxyServiceRecord.java:370)
at org.eclipse.soda.sat.core.internal.record.ExportProxyServiceRecord.ac
cess$0(ExportProxyServiceRecord.java:356)
at org.eclipse.soda.sat.core.internal.record.ExportProxyServiceRecord$Ex
portedServiceInvocationHandler.invoke(ExportProxyServiceRecord.java:56)
at $Proxy192.save(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:79)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:618)
at com.ibm.team.repository.servlet.AbstractTeamServerServlet.handleMetho
d(AbstractTeamServerServlet.java:1170)
at com.ibm.team.repository.servlet.AbstractTeamServerServlet.executeMeth
od(AbstractTeamServerServlet.java:926)
at com.ibm.team.repository.servlet.AbstractTeamServerServlet.doPost(Abst
ractTeamServerServlet.java:728)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at com.ibm.team.repository.servlet.AbstractTeamServerServlet.handleReque
st2(AbstractTeamServerServlet.java:1754)
at com.ibm.team.repository.servlet.AbstractTeamServerServlet.handleReque
st(AbstractTeamServerServlet.java:1623)
at com.ibm.team.repository.servlet.AbstractTeamServerServlet.service(Abs
tractTeamServerServlet.java:1536)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.eclipse.equinox.http.registry.internal.ServletManager$ServletWrap
per.service(ServletManager.java:180)
at org.eclipse.equinox.http.servlet.internal.ServletRegistration.handleR
equest(ServletRegistration.java:90)
at org.eclipse.equinox.http.servlet.internal.ProxyServlet.processAlias(P
roxyServlet.java:111)
at org.eclipse.equinox.http.servlet.internal.ProxyServlet.service(ProxyS
ervlet.java:75)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.eclipse.equinox.servletbridge.BridgeServlet.service(BridgeServlet
.java:121)
at com.ibm.team.repository.server.servletbridge.JazzServlet.service(Jazz
Servlet.java:54)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:210)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:174)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(Authentica
torBase.java:525)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
a:151)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
:870)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.p
rocessConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpo
int.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFol
lowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadP
ool.java:685)
at java.lang.Thread.run(Thread.java:811)
Caused by: com.ibm.team.workitem.common.model.MultiStaleDataException: Stale Dat
a
at com.ibm.team.workitem.common.internal.util.Utils.checkSaveResult(Util
s.java:284)
at com.ibm.team.workitem.service.internal.WorkItemServer.saveWorkItems(W
orkItemServer.java:127)
... 121 more
Andrea,
in general I'd say yes. It's looks like your code is run before the item is written to the repository, i.e., the item was modified and should be a working copy already. Try to manipulate and save the data you get from the operation directly.
HTH,
Jan.
--
Jan Wloka
Jazz Tracking & Planning Team
in general I'd say yes. It's looks like your code is run before the item is written to the repository, i.e., the item was modified and should be a working copy already. Try to manipulate and save the data you get from the operation directly.
HTH,
Jan.
--
Jan Wloka
Jazz Tracking & Planning Team
hi all,
it's possible programmatically modify the value for a custom attributes or bult-in attributes (like summary, description,...) in a follow up action on Save Work Item (server) operation?
I have tried so:
IWorkItem wi = (IWorkItem) ((ISaveParameter) data).getNewState();
IWorkItem wiCopy = (IWorkItem) wi.getWorkingCopy();
wiCopy.setValue(MyCustomAttribute, null);
wis.saveWorkItem2(wiCopy, null, null);
but I have this exception:
com.ibm.team.repository.common.StaleDataException: Stale Data
:((((
Any suggestion?
Thanks
Andrea
Every time an item is saved, a new state is created. When you save an item, the newest state must be used, otherwise you get a StaleDataException.
When you save an item more than once, you will need to make sure that you always have the latest state by:
- either use the item returned by the save call
- fetch the item from the repository again
When you save an item more than once, you will need to make sure that you always have the latest state by:
- either use the item returned by the save call
- fetch the item from the repository again
Thanks to all!
I solved so:
I solved so:
package com.ibm.rational.rtc.docreview;
import org.eclipse.core.runtime.IProgressMonitor;
import com.ibm.team.foundation.common.text.XMLString;
import com.ibm.team.process.common.IProcessConfigurationElement;
import com.ibm.team.process.common.advice.AdvisableOperation;
import com.ibm.team.process.common.advice.runtime.IOperationParticipant;
import com.ibm.team.process.common.advice.runtime.IParticipantInfoCollector;
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.service.IWorkItemServer;
@SuppressWarnings("restriction")
public class OpenDocDefect2 extends AbstractService implements IOperationParticipant {
boolean done = false;
private IWorkItem getFreshCopy(IWorkItem workItem, IProgressMonitor monitor) throws TeamRepositoryException {
return getWorkItemService().findWorkItemById(workItem.getId(), IWorkItem.FULL_PROFILE, monitor);
}
private IWorkItemServer getWorkItemService() {
return getService(IWorkItemServer.class);
}
public void run(AdvisableOperation operation,
IProcessConfigurationElement participantConfig,
IParticipantInfoCollector collector, IProgressMonitor monitor)
throws TeamRepositoryException {
Object data = operation.getOperationData();
IWorkItem workingCopy;
if (!done) {
if (data instanceof ISaveParameter) {
IWorkItem wi = (IWorkItem) ((ISaveParameter) data).getNewState();
if (wi != null)
{
IWorkItem wiFreshCopy = getFreshCopy(wi, monitor);
workingCopy = (IWorkItem) wiFreshCopy.getWorkingCopy();
String wiType = workingCopy.getWorkItemType();
if (wiType.equalsIgnoreCase("task") || wiType.equalsIgnoreCase("defect") )
{
try
{
IWorkItemServer wis = getService(IWorkItemServer.class);
workingCopy.setHTMLDescription(XMLString.createFromPlainText("BLABLABLA"));
done = true;
wis.saveWorkItem2(workingCopy, null, null);
}
catch (Exception ex){
ex.printStackTrace();
}
finally {
done = false;
}
}
}
}
}
}
}
Hi Andrea, I'm facing few issues in creating a work item programmatically using operation participant.
My code:
package automaticcreation;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import com.ibm.team.process.common.IProcessConfigurationElement;
import com.ibm.team.process.common.IProjectAreaHandle;
import com.ibm.team.process.common.advice.AdvisableOperation;
import com.ibm.team.process.common.advice.IReportInfo;
import com.ibm.team.process.common.advice.runtime.IOperationParticipant;
import com.ibm.team.process.common.advice.runtime.IParticipantInfoCollector;
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.IWorkItemCommon;
import com.ibm.team.workitem.common.model.IAttribute;
import com.ibm.team.workitem.common.model.IWorkItem;
import com.ibm.team.workitem.common.model.IWorkItemType;
import com.ibm.team.workitem.service.IWorkItemServer;
public class AutomaticProhibitSave extends AbstractService implements IOperationParticipant {
public void run(AdvisableOperation operation,
IProcessConfigurationElement participantConfig,
IParticipantInfoCollector collector, IProgressMonitor monitor)
throws TeamRepositoryException {
// TODO Auto-generated method stub
Object data = operation.getOperationData();
if (data instanceof ISaveParameter) {
ISaveParameter param = (ISaveParameter) data;
IAuditable auditable = param.getNewState();
if (auditable instanceof IWorkItem) {
IWorkItem sourceworkItem = (IWorkItem) auditable;
IProjectAreaHandle projectArea = sourceworkItem.getProjectArea();
String parentWItype= sourceworkItem.getWorkItemType();
String parent = "com.ibm.team.apt.workItemType.epic";
if (parent.equalsIgnoreCase(parentWItype)) {
String targetWorkItemType = "com.ibm.team.apt.workItemType.story";
IWorkItemServer workItemServer = this.getService( IWorkItemServer.class );
IWorkItemCommon workItemCommon = this.getService( IWorkItemCommon.class );
IWorkItemType child= workItemCommon.findWorkItemType( projectArea,
targetWorkItemType, monitor );
IWorkItem targetWorkItem = workItemServer.createWorkItem2(child);
//Assigning attribute values
String attributeId = "summary" ;
String attributeValue = "Programmatic Story 1";
IAttribute attribute = workItemCommon.findAttribute( projectArea, attributeId, monitor );
targetWorkItem.setValue( attribute, attributeValue );
String attributeId2 = "test" ;
String attributeValue2 = "Test 1";
IAttribute attribute2 = workItemCommon.findAttribute( projectArea, attributeId2, monitor );
targetWorkItem.setValue( attribute2, attributeValue2 );
//Saving the child story WI
IWorkItem workItemForUpdate = (IWorkItem)targetWorkItem.getWorkingCopy();
IStatus status= workItemServer.saveWorkItem2( workItemForUpdate, null,null );
if (!status.isOK())
{
// TODO: Handle the exception
}
}
else {
IReportInfo problem = collector.createExceptionInfo(
"Work item is of type: "
+ sourceworkItem.getWorkItemType(),new Exception("Error"));
collector.addInfo(problem);
}
}
}
}
}
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import com.ibm.team.process.common.IProcessConfigurationElement;
import com.ibm.team.process.common.IProjectAreaHandle;
import com.ibm.team.process.common.advice.AdvisableOperation;
import com.ibm.team.process.common.advice.IReportInfo;
import com.ibm.team.process.common.advice.runtime.IOperationParticipant;
import com.ibm.team.process.common.advice.runtime.IParticipantInfoCollector;
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.IWorkItemCommon;
import com.ibm.team.workitem.common.model.IAttribute;
import com.ibm.team.workitem.common.model.IWorkItem;
import com.ibm.team.workitem.common.model.IWorkItemType;
import com.ibm.team.workitem.service.IWorkItemServer;
public class AutomaticProhibitSave extends AbstractService implements IOperationParticipant {
public void run(AdvisableOperation operation,
IProcessConfigurationElement participantConfig,
IParticipantInfoCollector collector, IProgressMonitor monitor)
throws TeamRepositoryException {
// TODO Auto-generated method stub
Object data = operation.getOperationData();
if (data instanceof ISaveParameter) {
ISaveParameter param = (ISaveParameter) data;
IAuditable auditable = param.getNewState();
if (auditable instanceof IWorkItem) {
IWorkItem sourceworkItem = (IWorkItem) auditable;
IProjectAreaHandle projectArea = sourceworkItem.getProjectArea();
String parentWItype= sourceworkItem.getWorkItemType();
String parent = "com.ibm.team.apt.workItemType.epic";
if (parent.equalsIgnoreCase(parentWItype)) {
String targetWorkItemType = "com.ibm.team.apt.workItemType.story";
IWorkItemServer workItemServer = this.getService( IWorkItemServer.class );
IWorkItemCommon workItemCommon = this.getService( IWorkItemCommon.class );
IWorkItemType child= workItemCommon.findWorkItemType( projectArea,
targetWorkItemType, monitor );
IWorkItem targetWorkItem = workItemServer.createWorkItem2(child);
//Assigning attribute values
String attributeId = "summary" ;
String attributeValue = "Programmatic Story 1";
IAttribute attribute = workItemCommon.findAttribute( projectArea, attributeId, monitor );
targetWorkItem.setValue( attribute, attributeValue );
String attributeId2 = "test" ;
String attributeValue2 = "Test 1";
IAttribute attribute2 = workItemCommon.findAttribute( projectArea, attributeId2, monitor );
targetWorkItem.setValue( attribute2, attributeValue2 );
//Saving the child story WI
IWorkItem workItemForUpdate = (IWorkItem)targetWorkItem.getWorkingCopy();
IStatus status= workItemServer.saveWorkItem2( workItemForUpdate, null,null );
if (!status.isOK())
{
// TODO: Handle the exception
}
}
else {
IReportInfo problem = collector.createExceptionInfo(
"Work item is of type: "
+ sourceworkItem.getWorkItemType(),new Exception("Error"));
collector.addInfo(problem);
}
}
}
}
}
Thanks to all!
I solved so:
package com.ibm.rational.rtc.docreview;
import org.eclipse.core.runtime.IProgressMonitor;
import com.ibm.team.foundation.common.text.XMLString;
import com.ibm.team.process.common.IProcessConfigurationElement;
import com.ibm.team.process.common.advice.AdvisableOperation;
import com.ibm.team.process.common.advice.runtime.IOperationParticipant;
import com.ibm.team.process.common.advice.runtime.IParticipantInfoCollector;
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.service.IWorkItemServer;
@SuppressWarnings("restriction")
public class OpenDocDefect2 extends AbstractService implements IOperationParticipant {
boolean done = false;
private IWorkItem getFreshCopy(IWorkItem workItem, IProgressMonitor monitor) throws TeamRepositoryException {
return getWorkItemService().findWorkItemById(workItem.getId(), IWorkItem.FULL_PROFILE, monitor);
}
private IWorkItemServer getWorkItemService() {
return getService(IWorkItemServer.class);
}
public void run(AdvisableOperation operation,
IProcessConfigurationElement participantConfig,
IParticipantInfoCollector collector, IProgressMonitor monitor)
throws TeamRepositoryException {
Object data = operation.getOperationData();
IWorkItem workingCopy;
if (!done) {
if (data instanceof ISaveParameter) {
IWorkItem wi = (IWorkItem) ((ISaveParameter) data).getNewState();
if (wi != null)
{
IWorkItem wiFreshCopy = getFreshCopy(wi, monitor);
workingCopy = (IWorkItem) wiFreshCopy.getWorkingCopy();
String wiType = workingCopy.getWorkItemType();
if (wiType.equalsIgnoreCase("task") || wiType.equalsIgnoreCase("defect") )
{
try
{
IWorkItemServer wis = getService(IWorkItemServer.class);
workingCopy.setHTMLDescription(XMLString.createFromPlainText("BLABLABLA"));
done = true;
wis.saveWorkItem2(workingCopy, null, null);
}
catch (Exception ex){
ex.printStackTrace();
}
finally {
done = false;
}
}
}
}
}
}
}