Uploading Attachment to the WI in RTC
Hi,
I am trying to upload attachment to the WI from specified directory using plain java client library API. I am trying to implement that using example available in this below blog: https://rsjazz.wordpress.com/2012/08/01/uploading-attachments-to-work-items/ I have written code to take file from specified path but it will not take the attachment from the specified directory. Please,any help is much appreciated. Thanks in advance. |
Accepted answer
Ralph Schoon (63.6k●3●36●46)
| answered May 13 '15, 10:10 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER edited May 13 '15, 10:13 a.m.
I have this code - basically your example and it works for me on drive C and D.
Call parameters: https://clm.example.com:9443/ccm/ ralph ralph 21 "D:\temp\test.txt" https://clm.example.com:9443/ccm/ ralph ralph 21 "D:\aatemp\test.txt" Note the filetype, encoding etc is still hard coded. /******************************************************************************* * Licensed Materials - Property of IBM * (c) Copyright IBM Corporation 2008. All Rights Reserved. * * Note to U.S. Government Users Restricted Rights: Use, duplication or * disclosure restricted by GSA ADP Schedule Contract with IBM Corp. *******************************************************************************/ package com.ibm.js.team.workitem.automation.examples; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.eclipse.core.runtime.IProgressMonitor; import com.ibm.team.links.common.IItemReference; 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(), "Some Description", 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("Usage: ModifyWorkItem repositoryURI userId password workItemID FileName"); return false; } String repositoryURI = args[0]; String userId = args[1]; String password = args[2]; String idString = args[3]; String fileName = args[4]; ITeamRepository teamRepository = TeamPlatform .getTeamRepositoryService().getTeamRepository(repositoryURI); teamRepository.registerLoginHandler(new LoginHandler(userId, password)); teamRepository.login(null); IWorkItemClient workItemClient = (IWorkItemClient) teamRepository .getClientLibrary(IWorkItemClient.class); int id = new Integer(idString).intValue(); IWorkItem workItem = workItemClient.findWorkItemById(id, IWorkItem.FULL_PROFILE, null); WorkItemUploadAttachmentModification operation = new WorkItemUploadAttachmentModification( fileName, IContent.CONTENT_TYPE_TEXT, IContent.ENCODING_UTF_8); operation.run(workItem, null); // WorkItemUploadAttachmentModification operation1 = new WorkItemUploadAttachmentModification( // fileName, IContent.CONTENT_TYPE_UNKNOWN, // IContent.ENCODING_UTF_8); // operation1.run(workItem, null); System.out.println("Modified work item " + workItem.getId() + "."); teamRepository.logout(); return true; } } Parveen Patel selected this answer as the correct answer
|
One other answer
Ralph Schoon (63.6k●3●36●46)
| answered May 13 '15, 6:51 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
There is no way to answer your question with this few information provided. I have reused the very same code numerous times and it always worked. Unless you can come up with an exception or better description what does not work, we can't help.
Comments
Parveen Patel
commented May 13 '15, 8:10 a.m.
Hi Ralph,
Ralph Schoon
commented May 13 '15, 10:08 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
I can't follow it works for me with different drives. I share an answer with code and call.
|
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.