我正在尝试编写一个Java工具来为爵士乐工作项设置自定义属性 自定义属性具有以下定义:
Name: Phase Found
Type: HowFound (Enumeration)
ID: howfound`
HowFound
的文字:
Name: Automation
Name: Testing
这是尝试设置自定义属性值的Java代码
注意:打印log.info("Phase found: workitem has attribute");
,因此我知道它找到了属性,只是无法设置值。
public class WorkItemInitialization extends WorkItemOperation {
private static Logger log = Logger.getLogger(WorkItemInitialization.class.getName());
public WorkItemInitialization(ICategoryHandle filedAgainst) {
super("Initializing Work Item");
}
protected void execute(WorkItemWorkingCopy workingCopy, IProgressMonitor monitor) throws TeamRepositoryException {
log.info("Executing work item changes.");
IWorkItem workItem = workingCopy.getWorkItem();
IWorkItemClient workItemClient = (IWorkItemClient) Main.repo.getClientLibrary(IWorkItemClient.class);
IAttribute phaseFound = workItemClient.findAttribute(Main.projectAreaRTC, "howfound", monitor);
if (workItem.hasCustomAttribute(phaseFound)){
log.info("Phase found: workitem has attribute");
IEnumeration<? extends ILiteral> myEnumeration = workItemClient.resolveEnumeration(phaseFound, monitor);
ILiteral targetLiteral = null;
for(ILiteral literal : myEnumeration.getEnumerationLiterals()) {
if(literal.getName().equals("Automation")) {
targetLiteral = literal;
}
}
workItem.setValue(phaseFound, targetLiteral);
}
}
}
这是我在运行时遇到的错误:
Exception in thread "main" java.lang.ClassCastException: com.ibm.team.workitem.common.internal.model.ConfigurationItem incompatible with com.ibm.team.workitem.common.model.Identifier
at com.ibm.team.workitem.common.internal.model.impl.WorkItemImpl.setValue(WorkItemImpl.java:2945)
答案 0 :(得分:1)
这非常有帮助 http://rsjazz.wordpress.com/2012/08/20/manipulationg-work-item-enumeration-values/
private static Identifier getLiteralEqualsString(String name, IAttributeHandle ia) throws TeamRepositoryException {
IWorkItemClient workItemClient = (IWorkItemClient) teamrepository.getClientLibrary(IWorkItemClient.class);
Identifier literalID = null;
IEnumeration enumeration = workItemClient.resolveEnumeration(ia, null); // or IWorkitemCommon
List literals = enumeration.getEnumerationLiterals();
for (Iterator iterator = literals.iterator(); iterator.hasNext();) {
ILiteral iLiteral = (ILiteral) iterator.next();
if (iLiteral.getName().equals(name)) {
literalID = iLiteral.getIdentifier2();
break;
}
}
return literalID;
}