It's all about the answers!

Ask a question

How to upload RTC attachments to a Work Item? Need WorkItemUploadAttachmentModification example please.


SImon Starkie (135) | asked May 30 '15, 1:22 p.m.
We are trying to develop an RTC API for uploading attachments to a Work Item.
We are following the example at https://rsjazz.wordpress.com/2013/07/12/work-item-command-line-client-to-add-a-comment/

But the sample code has compile errors for the "WorkItemUploadAttachmentModification" statements:
WorkItemUploadAttachmentModification operation = new WorkItemUploadAttachmentModification("TestAttachment.txt",IContent.CONTENT_TYPE_TEXT,IContent.ENCODING_UTF_8operation.run(workItem, null);

WorkItemUploadAttachmentModification operation1 = new WorkItemUploadAttachmentModification("Test.pdf",IContent.CONTENT_TYPE_UNKNOWN,IContent.ENCODING_UTF_8operation1.run(workItem, null);

The compile errors are:
Description Resource Path Location Type
ENCODING_UTF_8operation cannot be resolved or is not a field ModifyWorkItemUploadAttachmentOperation.java /EISRTCUtility/src/com/ibm/js/team/workitem/plainjava/example line 149 Java Problem

Description Resource Path Location Type
ENCODING_UTF_8operation1 cannot be resolved or is not a field ModifyWorkItemUploadAttachmentOperation.java /EISRTCUtility/src/com/ibm/js/team/workitem/plainjava/example line 150 Java Problem

Does anyone have a working example of "WorkItemUploadAttachmentModification"?

Thanks. 


 

2 answers



permanent link
sam detweiler (12.5k6195201) | answered May 30 '15, 3:33 p.m.
that code is in another blog post

https://rsjazz.wordpress.com/2012/08/01/uploading-attachments-to-work-items/

permanent link
SImon Starkie (135) | answered Jun 02 '15, 12:23 p.m.
This line of code in the sample was wrong: 
WorkItemUploadAttachmentModification operation = new WorkItemUploadAttachmentModification("TestAttachment.txt",IContent.CONTENT_TYPE_TEXT,IContent.ENCODING_UTF_8operation.run(workItem, null);

We replaced it with these two lines of code:
WorkItemUploadAttachmentModification operation = new WorkItemUploadAttachmentModification("TestAttachment.txt",IContent.CONTENT_TYPE_TEXT,IContent.ENCODING_UTF_8);
operation.run(workItem, null);

The following is the full code and is working for us:
package com.ibm.js.team.workitem.plainjava.example;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URI;

import org.eclipse.core.runtime.IProgressMonitor;

import com.ibm.team.links.common.IItemReference;
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.ITeamRepository.ILoginHandler;
import com.ibm.team.repository.client.ITeamRepository.ILoginHandler.ILoginInfo;
import com.ibm.team.repository.client.TeamPlatform;
import com.ibm.team.repository.common.IContent;
import com.ibm.team.repository.common.TeamRepositoryException;
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.IAttachment;
import com.ibm.team.workitem.common.model.IWorkItem;
import com.ibm.team.workitem.common.model.WorkItemEndPoints;
import com.ibm.team.workitem.common.model.WorkItemLinkTypes;

/**
* Example code, see
* https://jazz.net/wiki/bin/view/Main/ProgrammaticWorkItemCreation.
*/
public class ModifyWorkItemUploadAttachmentOperation {

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 WorkItemUploadAttachmentModification extends WorkItemOperation {

private String fFileName;
private String fContentType;
private String fEncoding;

public WorkItemUploadAttachmentModification(String fileName, String contentType, String encoding) {
super("Initializing Work Item",IWorkItem.FULL_PROFILE);
fFileName=fileName;
fContentType=contentType;
fEncoding=encoding;
}

@Override
protected void execute(WorkItemWorkingCopy workingCopy,
IProgressMonitor monitor) throws TeamRepositoryException {
try {
attachFile(workingCopy, fFileName, fContentType, fEncoding, monitor);
} catch (IOException e) {
e.printStackTrace();
}
}

private static void attachFile( WorkItemWorkingCopy workingCopy, String name, String contentType, String encoding, IProgressMonitor monitor)
throws TeamRepositoryException, IOException {
File attachmentFile = new File(name);
FileInputStream fis = new FileInputStream(attachmentFile);

IWorkItem workItem = workingCopy.getWorkItem();
IWorkItemClient workItemClient = (IWorkItemClient) ((ITeamRepository)workItem.getOrigin()).getClientLibrary(IWorkItemClient.class);
try {
IAttachment newAttachment = workItemClient.createAttachment(
workItem.getProjectArea(), attachmentFile.getName(), "", contentType,
encoding, fis, monitor);

newAttachment = (IAttachment) newAttachment.getWorkingCopy();
newAttachment = workItemClient.saveAttachment(newAttachment, monitor);
IItemReference reference = WorkItemLinkTypes.createAttachmentReference(newAttachment);
workingCopy.getReferences().add(WorkItemEndPoints.ATTACHMENT, reference);
} finally {
if (fis != null) {
fis.close();
}
}
}
}

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 != 5) {
System.out.println("UsageifyWorkItem ");
return false;
}

String repositoryURI = args[0];
String userId = args[1];
String password = args[2];
String projectAreaName = args[3];
String idString = args[4];

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

IProcessClientService processClient = (IProcessClientService) teamRepository.getClientLibrary(IProcessClientService.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 not found.");
return false;
}

int id = new Integer(idString).intValue();
IWorkItem workItem = workItemClient.findWorkItemById(id, IWorkItem.FULL_PROFILE, null);
// WorkItemUploadAttachmentModification operation = new WorkItemUploadAttachmentModification("TestAttachment.txt",IContent.CONTENT_TYPE_TEXT,IContent.ENCODING_UTF_8operation.run(workItem, null);
WorkItemUploadAttachmentModification operation = new WorkItemUploadAttachmentModification("TestAttachment.txt",IContent.CONTENT_TYPE_TEXT,IContent.ENCODING_UTF_8);
operation.run(workItem, null);
// WorkItemUploadAttachmentModification operation1 = new WorkItemUploadAttachmentModification("Test.pdf",IContent.CONTENT_TYPE_UNKNOWN,IContent.ENCODING_UTF_8operation1.run(workItem, null);
System.out.println("Modified item " + workItem.getId() + ".");
teamRepository.logout();

return true;
}
}
 

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.