Retrieving enumeration values from a multi-select field
I have a custom attribute defined as shortString with a multi-select checkbox presentation set up with a custom property field with key 'enumeration' and the value being the name of the enumeration I want to display for selection.
What I want to do is programatically retrieve the enumeration values selected. IWorkItem.getValue() returns a comma-separated String of enumeration identifiers matching the selection, and from this I want to retrieve the actual ILiteral objects to call ILiteral.getName() on them to get their displayable names or 'values'.
The problem I'm having is actually resolving the IEnumeration object used in the multi-select checkbox presentation. Normally the attribute type itself is tied to an enumeration, with the enumeration being defined as the attribute type.
In that case it's easy: I just use
However for my mult-select checkbox field, the attribute is defined as type 'smallString' and so the call above won't work as theirs no enumeration directly associated with it, only associated with the presentation of it in an editor.
Therefore how can I resolve the enumeration in this situation?
From hunting around I've resolved it in the following manner, but it's not a clean implementation and so I would really like to know if there's a proper way of doing this.
You can see that I've had to use an internal method of WorkItemCommon and not available in the IWorkItemCommon interface to resolve it. Is it possible to resolve it through the standard API rather than this internal method?
For scope I've included my whole method for reference:
Many thanks in advance.
What I want to do is programatically retrieve the enumeration values selected. IWorkItem.getValue() returns a comma-separated String of enumeration identifiers matching the selection, and from this I want to retrieve the actual ILiteral objects to call ILiteral.getName() on them to get their displayable names or 'values'.
The problem I'm having is actually resolving the IEnumeration object used in the multi-select checkbox presentation. Normally the attribute type itself is tied to an enumeration, with the enumeration being defined as the attribute type.
In that case it's easy: I just use
IWorkItemCommon.resolveEnumeration(IAttribute, IProgressMonitor)
However for my mult-select checkbox field, the attribute is defined as type 'smallString' and so the call above won't work as theirs no enumeration directly associated with it, only associated with the presentation of it in an editor.
Therefore how can I resolve the enumeration in this situation?
From hunting around I've resolved it in the following manner, but it's not a clean implementation and so I would really like to know if there's a proper way of doing this.
IEnumeration enumeration = ((WorkItemCommon) workItemCommon).internalResolveEnumeration(projectArea, enumerationName, monitor);
You can see that I've had to use an internal method of WorkItemCommon and not available in the IWorkItemCommon interface to resolve it. Is it possible to resolve it through the standard API rather than this internal method?
For scope I've included my whole method for reference:
public String[] getMultiSelectEnumerationValues(final IWorkItem workItem, final String attributeName, final enumerationName, final IProjectAreaHandle projectArea, IWorkItemCommon workItemCommon, final IProgressMonitor monitor)
{
String[] result = null;
final String str = (String) workItem.getValue(attribute);
if(str != null)
{
final IAttribute attribute = workItemCommon.findAttribute(projectArea, fieldIdentifier, monitor);
// TODO Dodgy internal call to resolve the IEnumeration, need to fix
final IEnumeration enumeration = ((WorkItemCommon) workItemCommon).internalResolveEnumeration(projectArea, enumerationName, monitor);
result = str.split(",");
ILiteral currentLiteral;
for (int i = 0; i < result.length; i++)
{
currentLiteral = getEnumerationLiteralByID(enumeration, result[i]);
if(currentLiteral != null)
{
result[i] = currentLiteral.getName();
}
else
{
throw new IllegalArgumentException("Cannot find enumeration literal '" + result[i] + "' for enumeration: " + enumeration);
}
}
}
return result;
}
public ILiteral getEnumerationLiteralByID(final IEnumeration<? extends ILiteral> enumeration, final String identifierName)
{
final Identifier<? extends ILiteral> identifier = Identifier.create(ILiteral.class, identifierName);
return enumeration.findEnumerationLiteral(identifier);
}
Many thanks in advance.
Comments
Meera Varma
Jan 24 '13, 12:38 p.m.sam detweiler
Jan 24 '13, 2:00 p.m.sam detweiler
Jan 25 '13, 12:29 a.m.