How to get custom attribute value with java api
7 answers
the issue with work items and attributes is that the EMF implementation only provides access to very few attributes directly using methods. Otherwise you have to get the IAttribute using its ID or by iterating the built in and custom attributes of a work item and the using getValue(iAttribute).
Plase see https://rsjazz.wordpress.com/2012/07/31/rtc-update-parent-duration-estimation-and-effort-participant/ the method getDuration() uses this technique.
Comments
thanks you, first I want to know is it possible? and if it is ,could you give me the code that I can finish this extension, I really need them, by the way, I cannot visit https://rsjazz.wordpress.com/2012/07/31/rtc-update-parent-duration-estimation-and-effort-participant/
The advantage with blogs is that they all provide RSS feeds so you can easily read any blog using a web based feed readers even if the main WordPress site is blocked.
In case of WordPress blogs, you can append the string ".nyud.net" to the blog URL and it should open just fine. For instance, if the main blog is located at labnol.wordpress.com, you can access a mirror image of this site from labnol.wordpress.com.nyud.net.
Wordpress is blocked in China.
Here is the code:
get an attribute from the ID
IWorkItem parent = (IWorkItem)workItemServer.getAuditableCommon().resolveAuditable(parentHandle,IWorkItem.FULL_PROFILE,monitor).getWorkingCopy();
IAttribute timeSpentAttribute = wiCommon.findAttribute(parent.getProjectArea(), WORKITEM_ATTRIBUTE_TIMESPENT, monitor);
IAttribute correctedEstimateAttribute = wiCommon.findAttribute(parent.getProjectArea(), WORKITEM_ATTRIBUTE_CORRECTEDESTIMATE, monitor);
Read the value
private long getDuration(IWorkItem child, IAttribute attribute, IProgressMonitor monitor) throws TeamRepositoryException {
long duration = 0;
if(attribute!=null && child.hasAttribute(attribute)){
Long tempDuration = (Long)child.getValue(attribute);
if(tempDuration!=null&& tempDuration.longValue()>0)
return tempDuration.longValue();
}
return duration;
}
You can also search the forum and Jazz.net for getValue and setValue
ISaveParameter p = (ISaveParameter)data;
// get the affected object
IAuditable auditable = p.getNewState();
// if this is a workitem
if (auditable instanceof IWorkItem)
{
// reference the right object type (cast)
IWorkItem workItem = (IWorkItem) auditable;
// get the worker objects
IAuditableCommon iac = p.getSaveOperationParameter().getAuditableCommon();
WorkflowManager wfm = new WorkflowManager(iac);
if(false)
{
IWorkItemCommon workItemCommon = iac.getPeer(IWorkItemCommon.class);
// loop thru all the attributes in the project
// attributes are project wide, not workitem specific
for(IAttribute ia:workItemCommon.findAttributes(p.getOldProcessArea().getProjectArea(), monitor) )
{
// if this attribute is available on this workitem
if(workItem.hasAttribute(ia))
{
System.out.println("processing for variable="+ia.getDisplayName()+" attrib type="+ia.getAttributeType());
try
{
// attempt to get the enumeration literal for this attribute
// will throw exception if not an enum
IEnumeration<ILiteral> enumeration = (IEnumeration<ILiteral>) workItemCommon.resolveEnumeration(ia, monitor);
if(enumeration!=null)
{
// get the literal specifically
String[] iaval = ia.getValue(iac, workItem, monitor).toString().split(":");
// if present
if(iaval.length>1 && iaval[1]!=null)
{
// loop thru the literal to value mappings
for (ILiteral literal : enumeration.getEnumerationLiterals())
{
// if this literal matches the attribute value
if(literal.getIdentifier2().getStringIdentifier().equalsIgnoreCase(iaval[1]))
{
// display the usable name
System.out.println("attribute name="+ia.getIdentifier() +", type"+"="+ia.getAttributeType()+ " literal="+literal.getIdentifier2().getStringIdentifier()+" literal name="+literal.getName());
break;
}
}
}
}
}
catch (Exception e)
{
//System.out.println("Exception="+e.toString());
}
}
}
}
Comments
Thank you for your reply, I have got the custom attribute value, and next I'd like to copy that value to another attribute when i click the save button, but I am wondering if it is possible, this operation participant is for Workitem Saving operation, if I copy the value to another attribute, actually that equals I am editing the workitem, then I have to SAVE it, but if I save it, it will trigger the operation participant, it is the endless process, that will be impossible. is that right?
1st you should never modify anything in an advisor.. it is supposed to ADVISE
so you should do this in a PARTICIPANT, cause it PARTICIPATES in the transaction
you need to set a flag in the data that you can check when you are called a second time (recursion)
The saveWorkItem3() operation takes an additional parameter, a set of strings. This can be used to detect that a subsequent trigger of the participant was caused by this save operation. The following code inserted into the run() operation would allow to detect the recursion
to save the workitem and pass the parameter
additionalParams = new HashSet();
additionalParams.add(IExtensionsDefinitions.UPDATE_PARENT_DURATION_EXTENSION_ID);
workItemServer.saveWorkItem3(parent, null, null,additionalParams);
to check for the parameter
if (saveParameter.getAdditionalSaveParameters().contains( IExtensionsDefinitions.UPDATE_PARENT_DURATION_EXTENSION_ID))
return;
}