How to get Custom Enumerated Attribute value using JAVA server side API
I have written the following code in a server side abstract service to obtain the value from enumerated attribute, but I can only seem to get the enumeration literal ID and not the value. I looked at several examples, many of which seem to be client side, that use an interface, "IWorkItemCommon". When I attempt to use this sample code I get exceptions that the interface is not found. I then updated the Required Service on the plugin.xml tab in the participant project, which resulted in even more errors.
Any suggestions on how to get the value from this enumerated attribute?
import java.util.Iterator;
import java.util.List;
import org.eclipse.core.runtime.IProgressMonitor;
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.IMailerService;
import com.ibm.team.repository.service.IRepositoryItemService;
import com.ibm.team.workitem.common.ISaveParameter;
import com.ibm.team.workitem.common.IWorkItemCommon;
import com.ibm.team.workitem.common.model.IAttributeHandle;
import com.ibm.team.workitem.common.model.IState;
import com.ibm.team.workitem.common.model.IWorkItem;
import com.ibm.team.workitem.common.model.Identifier;
import com.ibm.team.process.common.IProjectArea;
import com.ibm.team.workitem.common.IAuditableCommon;
import com.ibm.team.workitem.common.model.*;
public class AbstractService extends com.ibm.team.repository.service.AbstractService implements IOperationParticipant {
public AbstractService() {
// TODO Auto-generated constructor stub
}
@Override
public void run(AdvisableOperation operation,
IProcessConfigurationElement participantConfig,
IParticipantInfoCollector collector,
IProgressMonitor monitor) throws TeamRepositoryException {
/*
* First check that the operation data is work item save data.
*/
Object data = operation.getOperationData();
ISaveParameter saveParameter = null;
if (data instanceof ISaveParameter)
{
saveParameter = (ISaveParameter) data;
/*
* If the state id has not changed, do not email.
*/
IWorkItem nState = (IWorkItem) saveParameter.getNewState();
Identifier<IState> newStateId = nState.getState2();
Identifier<IState> oldStateId = null;
IWorkItem oldState = (IWorkItem) saveParameter.getOldState();
if (oldState != null) // New work item check.
oldStateId = oldState.getState2();
if ((newStateId != null) && !(newStateId.equals(oldStateId)))
{
/*
* If the work item is not of the proper type, do not email.
* Note that the work item type and state are hard coded.
*/
String newType = nState.getWorkItemType();
if ((newType.equals("ir")) || (newType.equals("projectchangerequest")))
{
/*
* Finally, if the new state is the target state, email.
*/
String newStateV = nState.getState2().getStringIdentifier();
if (newStateV.equals("com.ibm.team.workitem.pCRW.state.s1"))
{
// Get email address of RespMan
List<IAttributeHandle> custAttHandles = newState.getCustomAttributes();
IRepositoryItemService itemService = getService(IRepositoryItemService.class);
String respManEmail_attID = "";
String emailAdd = "";
for (Iterator iterator = custAttHandles.iterator(); iterator.hasNext();)
{
IAttributeHandle iAttributeHandle = (IAttributeHandle) iterator.next();
IAttribute custAtt = (IAttribute) itemService.fetchItem(iAttributeHandle,IRepositoryItemService.COMPLETE);
String attName = custAtt.getDisplayName();
if (attName.equalsIgnoreCase("RespManEmailAdd"))
{
// Get the Attrribute Identifier for the enumeration value
respManEmail_attID = newState.getValue(custAtt).toString();
//// This code for getting the value from identifier results in service not found exception.
//// IEnumeration enumeration = fWorkItemCommon.resolveEnumeration(custAtt, monitor);
Object value = newState.getValue(custAtt);
Identifier literalID = (Identifier) value;
//// This code is dependent upon the enumeration type above
//// ILiteral literal = enumeration.findEnumerationLiteral(literalID);
emailAdd = literalID.getStringIdentifier();
//// This code is dependent upon the enumeration type above
//// emailAdd = literal.getName();
}
}
String workItemID = ((Integer) newState.getId()).toString();
String workItemSummary = (newState.getHTMLSummary()).toString();
if ((emailAdd != "") || (emailAdd != "Unconfigured"))
{
EmailRespMan(emailAdd, workItemID, workItemSummary);
}
}
}
}
}
}
private void EmailRespMan(String address, String wiID, String wiSummary) throws TeamRepositoryException
{
/*
* Need to gather name of Responsible Manager
* Need to identify service or methods to email proper Contributor
*/
try
{
IMailerService mailer = getService(IMailerService.class);
String toAddress = address;
String subject = "Work Item " + wiID + "needs your attention";
String msg = "Work Item "
+ wiID
+ "has identified you as the Responsible Manager for: \n\n\t Summary: "
+ wiSummary
+ "\n\n Please take appropriate action.";
String cc = "";
mailer.sendMail(mailer.getDefaultSender(), toAddress, subject, msg, cc);
}
catch(Exception ex)
{
// log(collector, ex.getMessage());
}
}
One answer
com.ibm.team.workitem.common.IWorkItemCommon has the name postfix Common and .common. in the namespace because it is a common API. Common API basically means it is common to the client and the server API. The code example below is from a server follow up action I wrote.
IEnumeration<? extends ILiteral> phaseEnumeration = IWorkItemCommon
.resolveEnumeration(phaseAttribute, monitor);
List<? extends ILiteral> literals = phaseEnumeration
.getEnumerationLiterals();
for (ILiteral phaseLiteral : literals) {
if (parsedConfig.fSkipLiterals.contains(phaseLiteral
.getIdentifier2().getStringIdentifier())) {
continue;
}
//
}
Since you fail to provide a good error message there is not a lot to say in addition. Please note, you might have to add IWorkItemCommon to the prerequisites like below:
Comments
Thanks for the tip about adding the interface to the prerequisites, this resolved one of the errors I am experiencing.
Thank you as well for posting some sample code. This code allows me to see all of the literal values for the enumerated custom attribute, but how do I get the literal value selected by the user?
As this is an advisor, the value will be in the new state of the attribute. Writing advisors and participants requires some understanding of what you do and in which context. I would suggest to carefully read https://rsjazz.wordpress.com/2015/09/30/learning-to-fly-getting-started-with-the-rtc-java-apis/ and especially carefully perform and read the Extensions Workshop as it provides answers to most of these questions.
Note, there is only a small difference between advisors and participants. The interface is a bit different and an advisor typically prevents operations whereas a participant participates in the operation and runs an additional operation.