It's all about the answers!

Ask a question

How to add a Comment using the RTC API.


SImon Starkie (135) | asked Jun 02 '15, 12:16 p.m.
edited Jun 05 '15, 2:24 a.m. by Geoffrey Clemm (30.1k33035)
I would like the java code for adding a comment to an RTC work item.

Comments
Tiago Moura commented Jun 02 '15, 12:29 p.m.

 Is it supposed to be a question?


SImon Starkie commented Jun 02 '15, 12:42 p.m.

 Is that supposed to be an answer ;)

If you don't have anything useful to say then don't say anything. 
The full code above may be useful to someone who is in a hurry, if not to you, bud.


Geoffrey Clemm commented Jun 05 '15, 2:17 a.m. | edited Jun 05 '15, 2:20 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

Taigo's comment is valid for several reasons:
- We discourage posting answers to questions that nobody has asked.   There are 45,000 questions in the forum.   If nobody has asked the question, probably nobody is interested in the answer (at least, not yet).
- In this case, this question has been asked ( https://jazz.net/forum/questions/25483/add-comment-to-work-item-using-api ), and this would be a good answer, but it is much more useful to post this as an answer to that question, instead of posting it as a duplicate question that users have to then browse through to find which one has a good answer.
- And if you decide to ignore the first two comments, at the very least you should post your answer as an "answer" to your question, and not embed the answer in the question field, so that the question shows up as being answered.   I'll go ahead and make that edit.

One answer



permanent link
Geoffrey Clemm (30.1k33035) | answered Jun 05 '15, 2:23 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
(Answer from Simon Starkie)
This is working for us:
package com.ibm.js.team.workitem.plainjava.example;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;

import com.ibm.team.filesystem.client.internal.rest.util.LoginUtil.LoginHandler;
import com.ibm.team.repository.client.ITeamRepository;
import com.ibm.team.repository.client.TeamPlatform;
import com.ibm.team.repository.common.IContributor;
import com.ibm.team.repository.common.ItemNotFoundException;
import com.ibm.team.repository.common.TeamRepositoryException;
import com.ibm.team.workitem.common.IWorkItemCommon;
import com.ibm.team.workitem.common.model.IWorkItem;

public class WorkItemAddCommentTest {

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 {
boolean result = false;
if (args.length  != 6) {
System.out
.println("Usage: AddComment      []");
return result;
}

String repositoryURI = args[0];
String userId = args[1];
String password = args[2];
String idString = args[3];
String commentText = args[4];
String commenterID = null;
if (args.length == 6) {
commenterID = args[5];
}

IProgressMonitor monitor = new NullProgressMonitor();
ITeamRepository teamRepository = TeamPlatform
.getTeamRepositoryService().getTeamRepository(repositoryURI);
teamRepository.registerLoginHandler(new LoginHandler(userId, password));
teamRepository.login(monitor);

IContributor commentUser = null;
if (null != commenterID) {
try {
commentUser = teamRepository.contributorManager().fetchContributorByUserId(
commenterID, monitor);
} catch (ItemNotFoundException e) {
}
}
if (commentUser == null) {
commentUser = teamRepository.loggedInContributor();
}

// Use IWorkItemClient or IWorkItemCommon
IWorkItemCommon workItemCommon = (IWorkItemCommon) teamRepository
.getClientLibrary(IWorkItemCommon.class);
int id = new Integer(idString).intValue();
IWorkItem workItem = workItemCommon.findWorkItemById(id,
IWorkItem.SMALL_PROFILE, monitor);

if(null!=workItem){
WorkItemAddComment operation = new WorkItemAddComment(commentUser,
commentText);
operation.run(workItem, monitor);
System.out.println("Modified item " + workItem.getId() + ".");
result=true;

} else{
System.out.println("Canfind work Item " + idString + ".");
}
teamRepository.logout();
return result;
}
}

Your answer


Register or to post 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.