To set custom enum attribute value in WI
![]()
Hi All,
Here is my sample code. I have some enum attributes on my Work item. Could you please update me how to set the value for the userdefined enum attributes? package rtctestlogin; import java.net.URI; import java.util.Arrays; import java.util.List; import org.eclipse.core.runtime.IProgressMonitor; import com.ibm.team.foundation.common.text.XMLString; import com.ibm.team.process.client.IProcessClientService; import com.ibm.team.process.common.IProjectArea; import com.ibm.team.repository.client.ITeamRepository; import com.ibm.team.repository.client.TeamPlatform; import com.ibm.team.repository.client.ITeamRepository.ILoginHandler; import com.ibm.team.repository.client.ITeamRepository.ILoginHandler.ILoginInfo; import com.ibm.team.repository.common.TeamRepositoryException; import com.ibm.team.workitem.client.IAuditableClient; import com.ibm.team.workitem.client.IWorkItemClient; import com.ibm.team.workitem.client.WorkItemOperation; import com.ibm.team.workitem.client.WorkItemWorkingCopy; import com.ibm.team.workitem.common.model.IAttribute; import com.ibm.team.workitem.common.model.ICategoryHandle; import com.ibm.team.workitem.common.model.IWorkItem; import com.ibm.team.workitem.common.model.IWorkItemHandle; import com.ibm.team.workitem.common.model.IWorkItemType; /** * Example code, see https://jazz.net/wiki/bin/view/Main/ProgrammaticWorkItemCreation. */ public class CreateWI { private static class LoginHandler implements ILoginHandler, ILoginInfo { private String fUserId; private String fPassword; private LoginHandler(String userId, String password) { fUserId= userId; fPassword= password; } public String getUserId() { return fUserId; } public String getPassword() { return fPassword; } public ILoginInfo challenge(ITeamRepository repository) { return this; } } private static class WorkItemInitialization extends WorkItemOperation { private String fSummary; private ICategoryHandle fCategory; public WorkItemInitialization(String summary, ICategoryHandle category) { super("Initializing Work Item"); fSummary= summary; fCategory= category; } @Override protected void execute(WorkItemWorkingCopy workingCopy, IProgressMonitor monitor) throws TeamRepositoryException { IWorkItem workItem= workingCopy.getWorkItem(); workItem.setHTMLSummary(XMLString.createFromPlainText(fSummary)); workItem.setCategory(fCategory); } } public static void main(String[] args) { boolean result; TeamPlatform.startup(); try { result= run(args); } catch (TeamRepositoryException x) { x.printStackTrace(); result= false; } finally { TeamPlatform.shutdown(); } if (!result) System.exit(1); } private static boolean run(String[] args) throws TeamRepositoryException { if (args.length != 7) { System.out.println("Usage: CreateWI <repositoryURI> <userId> <password> <projectArea> <workItemType> <summary> <category>"); return false; } String repositoryURI= args; String userId= args; String password= args; String projectAreaName= args; String typeIdentifier= args; String summary= args; String categoryName= args; ITeamRepository teamRepository= TeamPlatform.getTeamRepositoryService().getTeamRepository(repositoryURI); teamRepository.registerLoginHandler(new LoginHandler(userId, password)); teamRepository.login(null); IProcessClientService processClient= (IProcessClientService) teamRepository.getClientLibrary(IProcessClientService.class); IAuditableClient auditableClient= (IAuditableClient) teamRepository.getClientLibrary(IAuditableClient.class); IWorkItemClient workItemClient= (IWorkItemClient) teamRepository.getClientLibrary(IWorkItemClient.class); URI uri= URI.create(projectAreaName.replaceAll(" ", "%20")); IProjectArea projectArea= (IProjectArea) processClient.findProcessArea(uri, null, null); if (projectArea == null) { System.out.println("Project area not found."); return false; } IWorkItemType workItemType= workItemClient.findWorkItemType(projectArea, typeIdentifier, null); if (workItemType == null) { System.out.println("Work item type not found."); return false; } List<String> path= Arrays.asList(categoryName.split("/")); ICategoryHandle category= workItemClient.findCategoryByNamePath(projectArea, path, null); if (category == null) { System.out.println("Category not found."); return false; } WorkItemInitialization operation= new WorkItemInitialization(summary, category); IWorkItemHandle handle= operation.run(workItemType, null); IWorkItem workItem= auditableClient.resolveAuditable(handle, IWorkItem.FULL_PROFILE, null); System.out.println("Created work item " + workItem.getId() + "."); teamRepository.logout(); return true; } } Cheers, Jose |
6 answers
![]()
The general approach is:
- Get a handle to the work item client IWorkItemClient workItemClient = - Get a handle to the attribute in question (the attribute id is the id in the process configuration) IAttribute myAttribute = - Resolve the attribute as an enumeration to get the literals IEnumeration<? extends ILiteral> myEnumeration = - Iterate over the literals to find the one you want to set ILiteral targetLiteral = null; - Find the work item you want to update IWorkItem workItem = workItemClient.findWorkItemById(id, IWorkItem.FULL_PROFILE, monitor); - Get a working copy of the work item IWorkItemWorkingCopyManager wcm = workItemClient.getWorkItemWorkingCopyManager(); - Update the working copy and save it workItemCopy.setValue(myAttribute, targetLiteral.getIdentifier2().getStringIdentifier()); |
![]()
Hi Jrussell,
Thanks for your detailed update. Could you please update me how these snippets will fit into my program? Thanks in advance. Cheers, Jose The general approach is: |
![]()
Hi,
I tied to plugin your snippets to my java program and getting some exceptions. CreateWI.java: package rtctestlogin; import java.net.URI; import java.util.Arrays; import java.util.List; import org.eclipse.core.runtime.IProgressMonitor; import com.ibm.team.foundation.common.text.XMLString; import com.ibm.team.process.client.IProcessClientService; import com.ibm.team.process.common.IProjectArea; import com.ibm.team.repository.client.ITeamRepository; import com.ibm.team.repository.client.TeamPlatform; import com.ibm.team.repository.client.ITeamRepository.ILoginHandler; import com.ibm.team.repository.client.ITeamRepository.ILoginHandler.ILoginInfo; import com.ibm.team.repository.common.TeamRepositoryException; import com.ibm.team.workitem.client.IAuditableClient; import com.ibm.team.workitem.client.IWorkItemClient; import com.ibm.team.workitem.client.WorkItemOperation; import com.ibm.team.workitem.client.WorkItemWorkingCopy; import com.ibm.team.workitem.common.model.IAttribute; import com.ibm.team.workitem.common.model.IAttributeHandle; import com.ibm.team.workitem.common.model.ICategoryHandle; import com.ibm.team.workitem.common.model.IEnumeration; import com.ibm.team.workitem.common.model.ILiteral; import com.ibm.team.workitem.common.model.IWorkItem; import com.ibm.team.workitem.common.model.IWorkItemHandle; import com.ibm.team.workitem.common.model.IWorkItemType; public class CreateWI { private static class LoginHandler implements ILoginHandler, ILoginInfo { private String fUserId; private String fPassword; private LoginHandler(String userId, String password) { fUserId= userId; fPassword= password; } public String getUserId() { return fUserId; } public String getPassword() { return fPassword; } public ILoginInfo challenge(ITeamRepository repository) { return this; } } private static class WorkItemInitialization extends WorkItemOperation { private String fSummary; private String fDesc; private ICategoryHandle fCategory; private IAttribute fRole; private ILiteral fRoleliteral; public WorkItemInitialization(String summary, ICategoryHandle category, String description, IAttribute roleAttribute, ILiteral targetLiteral) { super("Initializing Work Item"); fSummary= summary; fCategory= category; fDesc= description; fRole = roleAttribute; fRoleliteral = targetLiteral; } @Override protected void execute(WorkItemWorkingCopy workingCopy, IProgressMonitor monitor) throws TeamRepositoryException { IWorkItem workItem= workingCopy.getWorkItem(); workItem.setHTMLSummary(XMLString.createFromPlainText(fSummary)); workItem.setCategory(fCategory); workItem.setHTMLDescription(XMLString.createFromPlainText(fDesc)); workItem.setValue(fRole, fRoleliteral.getIdentifier2().getStringIdentifier()); } } public static void main(String[] args) { boolean result; TeamPlatform.startup(); try { result= run(args); } catch (TeamRepositoryException x) { x.printStackTrace(); result= false; } finally { TeamPlatform.shutdown(); } if (!result) System.exit(1); } private static boolean run(String[] args) throws TeamRepositoryException { if (args.length != 9) { System.out.println("Usage: CreateWI <repositoryURI> <userId> <password> <projectArea> <workItemType> <summary> <category> <description> <role>"); return false; } String repositoryURI= args; String userId= args; String password= args; String projectAreaName= args; String typeIdentifier= args; String summary= args; String categoryName= args; String desc= args; String sdsrole= args; ITeamRepository teamRepository= TeamPlatform.getTeamRepositoryService().getTeamRepository(repositoryURI); teamRepository.registerLoginHandler(new LoginHandler(userId, password)); teamRepository.login(null); IProcessClientService processClient= (IProcessClientService) teamRepository.getClientLibrary(IProcessClientService.class); IAuditableClient auditableClient= (IAuditableClient) teamRepository.getClientLibrary(IAuditableClient.class); IWorkItemClient workItemClient= (IWorkItemClient) teamRepository.getClientLibrary(IWorkItemClient.class); URI uri= URI.create(projectAreaName.replaceAll(" ", "%20")); IProjectArea projectArea= (IProjectArea) processClient.findProcessArea(uri, null, null); if (projectArea == null) { System.out.println("Project area not found."); return false; } IWorkItemType workItemType= workItemClient.findWorkItemType(projectArea, typeIdentifier, null); if (workItemType == null) { System.out.println("Work item type not found."); return false; } List<String> path= Arrays.asList(categoryName.split("/")); ICategoryHandle category= workItemClient.findCategoryByNamePath(projectArea, path, null); if (category == null) { System.out.println("Category not found."); return false; } IAttribute roleAttribute = workItemClient.findAttribute(projectArea, "rolelist_a", null); IEnumeration<extends> myEnumeration = workItemClient.resolveEnumeration(roleAttribute, null); ILiteral targetLiteral = null; for(ILiteral literal : myEnumeration.getEnumerationLiterals()) { if(literal.getName().equals(sdsrole)) { targetLiteral = literal; } } WorkItemInitialization operation= new WorkItemInitialization(summary, category, desc, roleAttribute, targetLiteral); IWorkItemHandle handle= operation.run(workItemType, null); IWorkItem workItem= auditableClient.resolveAuditable(handle, IWorkItem.FULL_PROFILE, null); System.out.println("Created work item " + workItem.getId() + "."); teamRepository.logout(); return true; } } Error: Exception in thread "main" java.lang.ClassCastException: java.lang.String incompatible with com.ibm.team.workitem.common.model.Identifier at com.ibm.team.workitem.common.internal.model.impl.WorkItemImpl.setValue(WorkItemImpl.java:2945) at rtctestlogin.CreateWI$WorkItemInitialization.execute(CreateWI.java:83) at com.ibm.team.workitem.client.WorkItemOperation.execute(WorkItemOperation.java:85) at com.ibm.team.workitem.client.WorkItemOperation.doRun(WorkItemOperation.java:272) at com.ibm.team.workitem.client.WorkItemOperation.run(WorkItemOperation.java:242) at com.ibm.team.workitem.client.WorkItemOperation.run(WorkItemOperation.java:189) at rtctestlogin.CreateWI.run(CreateWI.java:165) at rtctestlogin.CreateWI.main(CreateWI.java:95) Run Command: CreateWI xxxxxx joel xxxxx SDAgile Request TestingSummary SD Testingdescrioption Tester Have you got any update on this error? Cheers, Jose Hi Jrussell, The general approach is: |
![]()
Hi,
I am getting the exception while saving the enum literal. workItem.setValue(fRole, fRoleliteral.getIdentifier2().getStringIdentifier()); Error: workItem.setValue(fRole, fRoleliteral.getIdentifier2().getStringIdentifier()); Full source code: package rtctestlogin; import java.net.URI; import java.util.Arrays; import java.util.List; import org.eclipse.core.runtime.IProgressMonitor; import com.ibm.team.foundation.common.text.XMLString; import com.ibm.team.process.client.IProcessClientService; import com.ibm.team.process.common.IProjectArea; import com.ibm.team.repository.client.ITeamRepository; import com.ibm.team.repository.client.TeamPlatform; import com.ibm.team.repository.client.ITeamRepository.ILoginHandler; import com.ibm.team.repository.client.ITeamRepository.ILoginHandler.ILoginInfo; import com.ibm.team.repository.common.TeamRepositoryException; import com.ibm.team.workitem.client.IAuditableClient; import com.ibm.team.workitem.client.IWorkItemClient; import com.ibm.team.workitem.client.WorkItemOperation; import com.ibm.team.workitem.client.WorkItemWorkingCopy; import com.ibm.team.workitem.common.model.IAttribute; //import com.ibm.team.workitem.common.model.IAttributeHandle; import com.ibm.team.workitem.common.model.ICategoryHandle; import com.ibm.team.workitem.common.model.IEnumeration; import com.ibm.team.workitem.common.model.ILiteral; import com.ibm.team.workitem.common.model.IWorkItem; import com.ibm.team.workitem.common.model.IWorkItemHandle; import com.ibm.team.workitem.common.model.IWorkItemType; public class CreateWI { private static class LoginHandler implements ILoginHandler, ILoginInfo { private String fUserId; private String fPassword; private LoginHandler(String userId, String password) { fUserId= userId; fPassword= password; } public String getUserId() { return fUserId; } public String getPassword() { return fPassword; } public ILoginInfo challenge(ITeamRepository repository) { return this; } } private static class WorkItemInitialization extends WorkItemOperation { private String fSummary; private String fDesc; private ICategoryHandle fCategory; private IAttribute fRole; private ILiteral fRoleliteral; public WorkItemInitialization(String summary, ICategoryHandle category, String description, IAttribute roleAttribute, ILiteral targetLiteral) { super("Initializing Work Item"); fSummary= summary; fCategory= category; fDesc= description; fRole = roleAttribute; fRoleliteral = targetLiteral; } @Override protected void execute(WorkItemWorkingCopy workingCopy, IProgressMonitor monitor) throws TeamRepositoryException { IWorkItem workItem= workingCopy.getWorkItem(); workItem.setHTMLSummary(XMLString.createFromPlainText(fSummary)); workItem.setCategory(fCategory); workItem.setHTMLDescription(XMLString.createFromPlainText(fDesc)); System.out.println("Going to Save Literal"); //workItem.setValue(fRole, fRoleliteral.getIdentifier2().getStringIdentifier()); workItem.setValue(fRole, fRoleliteral.getIdentifier2().getStringIdentifier()); System.out.println("Saved Literal"); workingCopy.save(monitor); } } public static void main(String[] args) { boolean result; TeamPlatform.startup(); try { result= run(args); } catch (TeamRepositoryException x) { x.printStackTrace(); result= false; } finally { TeamPlatform.shutdown(); } if (!result) System.exit(1); } private static boolean run(String[] args) throws TeamRepositoryException { if (args.length != 9) { System.out.println("Usage: CreateWI <repositoryURI> <userId> <password> <projectArea> <workItemType> <summary> <category> <description> <role>"); return false; } String repositoryURI= args; String userId= args; String password= args; String projectAreaName= args; String typeIdentifier= args; String summary= args; String categoryName= args; String desc= args; String sdsrole= args; System.out.println("XCHECKING"); ITeamRepository teamRepository= TeamPlatform.getTeamRepositoryService().getTeamRepository(repositoryURI); teamRepository.registerLoginHandler(new LoginHandler(userId, password)); teamRepository.login(null); IProcessClientService processClient= (IProcessClientService) teamRepository.getClientLibrary(IProcessClientService.class); IAuditableClient auditableClient= (IAuditableClient) teamRepository.getClientLibrary(IAuditableClient.class); IWorkItemClient workItemClient= (IWorkItemClient) teamRepository.getClientLibrary(IWorkItemClient.class); URI uri= URI.create(projectAreaName.replaceAll(" ", "%20")); IProjectArea projectArea= (IProjectArea) processClient.findProcessArea(uri, null, null); if (projectArea == null) { System.out.println("Project area not found."); return false; } IWorkItemType workItemType= workItemClient.findWorkItemType(projectArea, typeIdentifier, null); if (workItemType == null) { System.out.println("Work item type not found."); return false; } List<String> path= Arrays.asList(categoryName.split("/")); ICategoryHandle category= workItemClient.findCategoryByNamePath(projectArea, path, null); if (category == null) { System.out.println("Category not found."); return false; } System.out.println("XCHECKING1"); IAttribute roleAttribute = workItemClient.findAttribute(projectArea, "rolelist_a", null); IEnumeration<extends> myEnumeration = workItemClient.resolveEnumeration(roleAttribute, null); ILiteral targetLiteral = null; for(ILiteral literal : myEnumeration.getEnumerationLiterals()) { if(literal.getName().equals(sdsrole)) { targetLiteral = literal; } } System.out.println("XCHECKING"); WorkItemInitialization operation= new WorkItemInitialization(summary, category, desc, roleAttribute, targetLiteral); IWorkItemHandle handle= operation.run(workItemType, null); IWorkItem workItem= auditableClient.resolveAuditable(handle, IWorkItem.FULL_PROFILE, null); System.out.println("Created work item " + workItem.getId() + "."); teamRepository.logout(); return true; } } Cheers, Jose Hi, |
![]()
Hi,
Thanks a lot. Its working now. Cheers, Simon Hi, |
![]() The only difference for above code that will resolve the issue(Exception in thread "main" java.lang.ClassCastException: java.lang.String incompatible with com.ibm.team.workitem.common.model.Identifier) is use 'fRoleliteral.getIdentifier2()' in place of 'fRoleliteral.getIdentifier2().getStringIdentifier()'. |
Your answer
Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.