Jazz Forum Welcome to the Jazz Community Forum Connect and collaborate with IBM Engineering experts and users

Saving the attachments to disk programmatically

Hi All,
Am using RTC 2002; Have a requirement of saving attachments on a workitem to disk programmatically; I did refer couple of postings in this forum and could not get any pointers from them.

Am getting a run time cast exception on running the code I have written. The exception looks something like below:

Exception in thread "main" java.lang.ClassCastException: com.ibm.team.workitem.common.internal.model.impl.AttachmentHandleImpl incompatible with com.ibm.team.workitem.common.model.IAttachment
at com.ibm.team.workitem.ide.ui.example.SaveAttachments.run(SaveAttachments.java:178)
at com.ibm.team.workitem.ide.ui.example.SaveAttachments.main(SaveAttachments.java:102)


snippet of the code I have written is pated below:

 IWorkItemReferences fsrcReferences = srcWorkingCopy.getReferences();


List<IReference> srcAttachments = fsrcReferences.getReferences(WorkItemEndPoints.ATTACHMENT);
for (int i=0; i < srcAttachments.size(); i++) {
IReference srcReference = srcAttachments.get(i);

IItemReference attachmentReference = (IItemReference) srcReference;
IAttachmentHandle attachmentHandle = (IAttachmentHandle) attachmentReference.resolve();

IAttachment srcAttachment = (IAttachment) attachmentHandle;
String srcAttachmentName = srcAttachment.getName();

FileOutputStream fos = null;
try {
fos = new FileOutputStream("C:\\temp\\" + srcAttachmentName);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
IContentManager cm= srcTeamRepository.contentManager();
cm.retrieveContent(srcAttachment.getContent(), fos, null);

}


Any pointers/ guidance would be of great help. Many thanks in advance.

Regards,
Mohan.

0 votes



2 answers

Permanent link
IAttachment srcAttachment = (IAttachment) attachmentHandle;

You cannot cast a handle to an item, you need to resolve it:

IAttachment srcAttachment =
srcRepository.itemManager().fetchCompleteItem(attachmentHandle,
IItemManager.DEFAULT, monitor);

--
Regards,
Patrick
Jazz Work Item Team

0 votes


Permanent link
Thanks a lot Patrick; this resolved my prob.

Thanks & Regards,
Mohan.

0 votes

Your answer

Register or log in 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.

Search context
Follow this question

By Email: 

Once you sign in you will be able to subscribe for any updates here.

By RSS:

Answers
Answers and Comments
Question details
× 10,938

Question asked: Feb 11 '10, 7:08 a.m.

Question was seen: 6,369 times

Last updated: Feb 11 '10, 7:08 a.m.

Confirmation Cancel Confirm