How to fetch the Workitem (built-in / Custom) Attribute Id and Value?
3 answers
The following snippet should get you started:
com.ibm.team.workitem.common.model.IAttributeHandle handle = workItem.getCustomAttributes().get(0);
com.ibm.team.workitem.common.model.IAttribute attribute = (IAttribute) teamRepository.itemManager().fetchCompleteItem(handle, IItemManager.DEFAULT, new NullProgressMonitor());
System.out.println("The attribute id: " + attribute.getIdentifier());
System.out.println("The value of the custom attribute: " + workItem.getValue(attribute));
Comments
here is my code from an advisor that loops thru the previous variable states (oldstate)
// 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());
}
}
}
}
// 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());
}
}
}
}
Lauren, I replicated the same code above , IT DOESN'T WORKED. Any Sugg ?
com.ibm.team.workitem.common.model.IAttributeHandle handle = sourceworkItem.getCustomAttributes().get(0);
IRepositoryItemService itemService = getService(IRepositoryItemService.
class);
IAttribute attribute = (IAttribute) itemService.fetchItem(handle, IRepositoryItemService.
COMPLETE);
System.out.println (" Get Cust Attribute Display Name :" + attribute.getDisplayName());
System.out.println (" Get Cust Attribute Type :" + attribute.getAttributeType());