It's all about the answers!

Ask a question

Saving the attachments to disk programmatically


Mohan K M (1111511) | asked Feb 11 '10, 7:08 a.m.
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.

2 answers



permanent link
Patrick Streule (4.9k21) | answered Feb 11 '10, 8:53 a.m.
JAZZ DEVELOPER
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

permanent link
Mohan K M (1111511) | answered Feb 15 '10, 3:32 a.m.
Thanks a lot Patrick; this resolved my prob.

Thanks & Regards,
Mohan.

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.