Set a new value to an attribute of a WorkItem with Java Plain API
Hello, I am brand new with Java Plain API and I am trying to set a new Value to an attibute of a workItem.
I am doing it in Groovy that is similar to Java.
Part of the code I have is (I already have the workItem and the iAttribute varibales):
def copyManager = workItemClient.getWorkItemWorkingCopyManager();
copyManager.connect(workItem, IWorkItem.FULL_PROFILE, new SysoutProgressMonitor());
def workItemCopy = copyManager.getWorkingCopy(workItem);
def workItem_to_edit = (IWorkItem) workItemCopy.getWorkItem();
def inter = new Integer("4");
workItem_to_edit.setValue(iAttribute, inter)
but I get the following error:
Caught: org.eclipse.core.runtime.AssertionFailedException: assertion failed:
org.eclipse.core.runtime.AssertionFailedException: assertion failed:
at org.eclipse.core.runtime.Assert.isTrue(Assert.java:110)
at org.eclipse.core.runtime.Assert.isTrue(Assert.java:96)
at com.ibm.team.workitem.common.internal.model.impl.WorkItemImpl.setValue(WorkItemImpl.java:2909)
at com.ibm.team.workitem.common.model.IWorkItem$setValue.call(Unknown Source)
at script.run(script.groovy:124)
What am I doing wrong?
Thanks for the help.
4 answers
List<IAttributeHandle> customAttributes = workItem.getCustomAttributes();If this list of attributes contains your attribute, you can call
workItem.setValue(attribute, value);Otherwise, first add attribute and then set value
workitem.addCustomAttribute(attribute); workItem.setValue(attribute, value);
More details of how my code is:
workItem = workItemClient.findWorkItemById(workItemNumber, IWorkItem.FULL_PROFILE, new SysoutProgressMonitor())
println("Listing all attributes....")
def allAttributeHandles = workItemClient.findAttributes(workItem.getProjectArea(), new SysoutProgressMonitor());
for (iterator = allAttributeHandles.iterator(); iterator.hasNext();){
def iAttributeHandle = (IAttributeHandle) iterator.next()
def iAttribute = (IAttribute) repo.itemManager().fetchCompleteItem(iAttributeHandle, IItemManager.DEFAULT, new SysoutProgressMonitor())
println("Attribute Name: " + iAttribute.getDisplayName() + ", Type: " + iAttribute.getAttributeType())
if(iAttribute.getDisplayName() == "Actual Spent Hours"){
println("Setting a value to the attribute Actual Spent Hours....")
def copyManager = workItemClient.getWorkItemWorkingCopyManager();
//def workItemHandle = workItemClient.findWorkItemById(workItemNumber, IWorkItem.FULL_PROFILE, new SysoutProgressMonitor());
copyManager.connect(workItem, IWorkItem.FULL_PROFILE, new SysoutProgressMonitor());
def workItemCopy = copyManager.getWorkingCopy(workItem);
def workItem_to_edit = (IWorkItem) workItemCopy.getWorkItem();
def inter = new Integer("4");
workItem_to_edit.setValue(iAttribute, inter)
}
}
I want to set an Integer value to a custom field of a WorkItem "Actual Spent Hours", and the iAttribute is obtained by doing a for of the result of workItemClient.findAttributes.
Since I am parsing all attributes in this case, is it necessary to do an hasAttribute()?
Trying to set a value to a custom attribute, change the scenario is some way? or it is the same as the built-in attributes?
What could I be doing wrong to get that AssertionFailedException error?
As always, Thanks for the help :)
Comments
If you don't check if the attribute is there with hasAttribute you can get an error. If all work item types you work with have the attribute, you don't have an issue. The attributes available to the type are not necessarily all there, especially on old items created before the attribute has been created. You would have to synchronize the attributes to make sure.
If you read the attribute you are trying to write. What type is the returned variable?
I am not sure in which line you get the Assertion Failed. I would suggest to debug this. See: https://rsjazz.wordpress.com/2013/03/20/understanding-and-using-the-rtc-java-client-api/ for hints on debugging Plain Java API.
if (!workItem_to_edit.hasAttribute(iAttribute)) { workItem_to_edit.addCustomAttribute(iAttribute) }
Comments
I did a println of the workItem_to_edit.hasAttribute(iAttribute) and it returned false.
The workingCopy seems it did get all attributes from the original workItem, I suspect that this line could be the key:
copyManager.connect(workItem, IWorkItem.FULL_PROFILE, new SysoutProgressMonitor())
Maybe the FULL_PROFILE is not retrieving all attributes, could it be?
Should I try with other value of that? What profile could return all attributes of the original workItem?
Thanks.
PS: I am sure that the original workItem has the attribute I am trying to setValue, but the workCopy doesn't have it :(
This has gone well beyond what I know. The best I can do right now is speculate.
Hello again,
I could figure out what the problem is:
the for I did to list all the workItem attributes, is listing twice the attibute I want to set the new value, in the code example above the "Actual Spent Hours".
I fix it adding a counter just to try to set the new value the first time and not the second.
What can be happening that it lists twice some attibutes?
I am printing all attributes, that is not the only one that is listed twice.
Thanks.
Comments
Philip Smith
JAZZ DEVELOPER Aug 14 '13, 4:38 p.m.A failure with that stack trace means there is no attribute by checking hasAttribute() (yes, the error could be much better).
Where did the value of iAttribute come from? Maybe it is not applicable to that work item's type?
Tiago Fernandez
Aug 14 '13, 4:53 p.m.I will show more details of my code, but as an answer since it doesn't fit as a comment....