How to set Attributes of an RTC Work Item programatically
Hello Ralf,
I found this code from your old articles for setting the attributes of an RTC Work Item:
IWorkItemClient workItemClient= (IWorkItemClient) teamRepository.getClientLibrary(IWorkItemClient.class);
IAttribute attribute = workItemClient.findAttribute(workItem.getProjectArea(),
fAttributeID, monitor);
if(null!=attribute){
if(workItem.hasAttribute(attribute))
workItem.setValue(attribute, fValue);
}
Here i get an Error that Work Item is Immutable. Can you please help me as to how to make the Work Item object Mutable so i can Set Attributes to it.
Thanks!
Vaibhav
Accepted answer
You have to use a work item working copy and a WorkitemOperation.
See https://rsjazz.wordpress.com/2013/03/20/understanding-and-using-the-rtc-java-client-api/ - you can search for WorkitemOperation. There are numerous examples on that blog, with downloadable code, that you could look at. https://jazz.net/wiki/bin/view/Main/ProgrammaticWorkItemCreation is the original source. WorkitemOperation is the way to do it.
Comments
Hi Ralf,
Thanks for the response.
I am able to edit String Type Attributes now.
For Enum type Attributes i referred your code from article: https://rsjazz.wordpress.com/2012/08/20/manipulationg-work-item-enumeration-values/
For some reason i get Error as: "ASSEERTION FAILED".
Can you please help me with how to solve this issue.
Thanks!
Vaibhav
No, you do not provide any actionable information.
Assertion Failed is typical for passing an invalid value. I would suggest you use the Eclipse debugging capabilities to find out where this happens and what the data is. The RTC Extensions Workshop describes how to set up an RTC/EWM development environment that allows debugging of the Plain Java Client Libraries. It is pretty much the same information that is up on my blog since 8 years, just a bit more organized. Good luck.
One other answer
Hello, i was able to use this successfully as below:
package **
import java.io.IOException;
import java.net.URI;
import java.util.Iterator;
import java.util.List;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import com.ibm.team.foundation.common.text.XMLString;
import com.ibm.team.process.client.IProcessClientService;
import com.ibm.team.process.common.IDevelopmentLine;
import com.ibm.team.process.common.IDevelopmentLineHandle;
import com.ibm.team.process.common.IIteration;
import com.ibm.team.process.common.IIterationHandle;
import com.ibm.team.process.common.IProjectArea;
import com.ibm.team.repository.client.IItemManager;
import com.ibm.team.repository.client.ITeamRepository;
import com.ibm.team.repository.client.ITeamRepository.ILoginHandler;
import com.ibm.team.repository.client.ITeamRepository.ILoginHandler.ILoginInfo;
import com.ibm.team.repository.client.TeamPlatform;
import com.ibm.team.repository.common.IContributor;
import com.ibm.team.repository.common.TeamRepositoryException;
import com.ibm.team.workitem.client.IAuditableClient;
import com.ibm.team.workitem.client.IDetailedStatus;
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.ICategory;
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;
import com.ibm.team.workitem.common.model.Identifier;
import com.ibm.team.workitem.common.model.ItemProfile;
public class CreateWorkItem {
private static ITeamRepository teamRepositoryTemp = null;
static public boolean DEBUG = false;
static String AUTHREQUIRED = "X-com-ibm-team-repository-web-auth-msg";
public CreateWorkItem() {
}
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;
}
}
@SuppressWarnings("unused")
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);
}
}
@SuppressWarnings("unused")
public static int run (String repositoryURI, String userId, String password, String projectAreaName, String summaryFromDng, String artifactUrl, String ecuNameFromSrr, int flag) throws TeamRepositoryException, IOException {
TeamPlatform.startup();
String workItemtype = "com.ibm.team.workitem.workItemType.SRR";
//"com.ibm.team.workitem.workItemType";
String plannedFor = "";
String requirementType0 = "* Level";
String requirementType1 = " Level";
IWorkItem workItem = null;
try {
ITeamRepository teamRepository = TeamPlatform.getTeamRepositoryService().getTeamRepository(repositoryURI);
teamRepository.registerLoginHandler(new LoginHandler(userId, password));
teamRepository.login(null);
teamRepositoryTemp = TeamPlatform.getTeamRepositoryService().getTeamRepository(repositoryURI);
teamRepositoryTemp.registerLoginHandler(new LoginHandler(userId, password));
teamRepositoryTemp.login(null);
IProcessClientService processClient = (IProcessClientService) teamRepository.getClientLibrary(IProcessClientService.class);
IAuditableClient auditableClient = (IAuditableClient) teamRepository.getClientLibrary(IAuditableClient.class);
IWorkItemClient workItemClient = (IWorkItemClient) teamRepository.getClientLibrary(IWorkItemClient.class);
IWorkItemClient workItemClientTemp = (IWorkItemClient) teamRepositoryTemp.getClientLibrary(IWorkItemClient.class);
URI uri = URI.create(projectAreaName.replaceAll(" ", "%20"));
IProjectArea projectArea = (IProjectArea) processClient.findProcessArea(uri, null, null);
if (projectAreaName == null)
{
//System.out.println("Project area not found.");
return 0;
}
IWorkItemType workItemType = workItemClient.findWorkItemType(projectArea, workItemtype, null);
if (workItemType == null) {
// System.out.println("Work item type not found.");
return 0;
} else {
//.out.println("Work item type found");
}
IWorkItemClient service = (IWorkItemClient) teamRepositoryTemp.getClientLibrary(IWorkItemClient.class);
IWorkItemHandle handle = service.getWorkItemWorkingCopyManager().connectNew(workItemType, new NullProgressMonitor());
WorkItemWorkingCopy wc = service.getWorkItemWorkingCopyManager().getWorkingCopy(handle);
workItem = wc.getWorkItem();
IDevelopmentLineHandle[] developmentLineHandles = projectArea.getDevelopmentLines();
IIteration programIteration = null;
boolean itrFoundFlag = false;
for (IDevelopmentLineHandle developmentLineHandle : developmentLineHandles) {
IDevelopmentLine developmentLine = auditableClient.resolveAuditable(developmentLineHandle, ItemProfile.DEVELOPMENT_LINE_DEFAULT, null);
IIterationHandle[] iterHandles = developmentLine.getIterations();
for (IIterationHandle itrHandle : iterHandles) {
IIteration iteration = (IIteration) teamRepository.itemManager().fetchCompleteItem((IIterationHandle) itrHandle, IItemManager.DEFAULT, null);
//System.out.println(iteration.getName());
if (plannedFor.equalsIgnoreCase(iteration.getName()))
{
programIteration = iteration;
//System.out.println(developmentLine.getName() + " : " + developmentLine.getLabel() + " : "+ iteration.getName());
itrFoundFlag = true;
break;
}
}
}
if (programIteration != null)
{
IAttribute programAttribute = workItemClientTemp.findAttribute(projectArea, "target", null);
workItem.setValue(programAttribute,programIteration);
}
IAttribute programAttributeVFUrl = workItemClientTemp.findAttribute(projectArea, "com.stellantis.team.attribute.vfurl", null);
workItem.setValue(programAttributeVFUrl, artifactUrl);
workItem.setHTMLSummary(XMLString.createFromPlainText(summaryFromDng));
IContributor contributor = null;
@SuppressWarnings("unchecked")
List<IContributor> listContributor= teamRepositoryTemp.contributorManager().fetchAllContributors(null);
for (IContributor str : listContributor) {
contributor = str;
// System.out.println(contributor.getName());
if((contributor.getName()).equalsIgnoreCase("****"))
{
workItem.setOwner(contributor);
break;
}
}
IAttribute attributrEcuName = service.findAttribute(projectArea, "com.stellantis.team.attribute.ecuname", null);
Identifier<?> identifierEcuName = getLiteralEqualsString(ecuNameFromSrr, attributrEcuName);
workItem.setValue(attributrEcuName, identifierEcuName);
List<ICategory> findCategories = workItemClient.findCategories(projectArea, ICategory.FULL_PROFILE, (org.eclipse.core.runtime.IProgressMonitor) null);
ICategory category = findCategories.get(1);
//System.out.println("Category: " + category);
if (category == null) {
//System.out.println("Category not found.");
return 0;
} else {
// System.out.println("Category found");
}
workItem.setCategory(category);
//com.stellantis.team.attribute.requirementtype //com.stellantis.team.attribute.requirementtype
IAttribute attributrRequirementType = service.findAttribute(projectArea, "com.stellantis.team.attribute.requirementtype", null);
if(flag == 0) {
Identifier<?> identifierRequirementType = getLiteralEqualsString(requirementType0,attributrRequirementType);
workItem.setValue(attributrRequirementType, identifierRequirementType);
}
else{
Identifier<?> identifierRequirementType = getLiteralEqualsString(requirementType1,attributrRequirementType);
workItem.setValue(attributrRequirementType, identifierRequirementType);
}
IDetailedStatus status = wc.save(new NullProgressMonitor());
//System.out.println(status.getCode() + "HTTP Code ");
if (status.getCode() != org.eclipse.core.runtime.IStatus.OK) {
//System.out.println("Error: " + status.getDetails());
}
System.out.println(workItem.getId());
teamRepository.logout();
return workItem.getId();
}
catch(Exception ex) {
ex.printStackTrace();
}
return workItem.getId();
}
private static Identifier<?> getLiteralEqualsString(String name, IAttributeHandle ia) throws TeamRepositoryException {
IWorkItemClient workItemClient = (IWorkItemClient) teamRepositoryTemp.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();
// System.out.println(iLiteral.getName());
if (iLiteral.getName().equals(name)) {
literalID = iLiteral.getIdentifier2();
break;
}
}
// System.out.println("returning literal ID : " + literalID.getType() + " "+ literalID.getClass() );
return literalID;
}
}