It's all about the answers!

Ask a question

[SOLVED] Programmatically adding attachment - weird issue


Joshua Giangrande (2611216) | asked Jan 16 '12, 2:54 p.m.
Hey Everyone,

So, I have been working on a program that will migrate some attachments from one system to another. I have successfully added attachments to work items in the new system, however I am noticing some strange behavior.

*When I go into work item view in Eclipse and view the work item I just attached files to, it displays no attached files. If I view the same work item from the web GUI, however, the files show up and can be viewed and/or downloaded as intended.

*I attempted to delete them from the web GUI (I was running the same test a number of times and attached the same file a number of times, and wished to remove them to run further tests). I click the X next to the attachment name and it tells me it is marked for deletion, however after saving the work item it still will not delete the attachment.

*Manually adding an attachment (my code uploaded a bunch of .png screenshots, so I added a text file) caused the other files I had uploaded to "disappear." Only the .txt file is visible in the web GUI. I checked the Eclipse view at this time, and the .txt file DID appear as an attachment. When I delete the .txt file in the web GUI, all of the other attachments become visible again and can still be viewed/downloaded.

My program functions like this:

1. Parse a file with a list of issues from the old system.
2. Locate corresponding work item and get the work item's ID.
3. Connect to database on the old system and look up all attachments belonging to the issue.
4. For every attachment found in the database, update the RTC work item by adding the database entry as an attachment.

Here is my code for modifying the work item (NOTE: as currently written, it will only modify the work item with id 1100 - I wanted to test with one work item before I started adding attachments wildly to every work item in my sample):


private static void modifyWorkItem(IProjectArea projectArea, String fileName, final ITeamRepository REPO, final IProgressMonitor MONITOR, int id){
final String ATTACH_PATH = "C:\\Downloads\\";
fileName = "Example.png";
File attachmentFile = null;
FileInputStream fis = null;
try{
IWorkItemClient workItemClient = (IWorkItemClient) REPO.getClientLibrary(IWorkItemClient.class);
IWorkItem workItem = workItemClient.findWorkItemById(id, IWorkItem.SMALL_PROFILE, null);
IWorkItemWorkingCopyManager wcm = workItemClient.getWorkItemWorkingCopyManager();
wcm.connect(workItem, IWorkItem.FULL_PROFILE, null);

try {
if(id==1100){
System.out.println(" Working on Work Item: " + id);
attachmentFile = new File(ATTACH_PATH+fileName);
fis = new FileInputStream(attachmentFile);

WorkItemWorkingCopy wc = wcm.getWorkingCopy(workItem);
IAttachment newAttachment = workItemClient.createAttachment(projectArea, attachmentFile.getName(), "", "text", "UTF-8", fis, null);
newAttachment = (IAttachment) newAttachment.getWorkingCopy();
newAttachment = workItemClient.saveAttachment(newAttachment, MONITOR);
linkAttachment(newAttachment, wc.getWorkItem(), REPO, MONITOR);

IDetailedStatus s = wc.save(null);
if (!s.isOK()) {
throw new TeamRepositoryException("Error saving work item",
s.getException());
}

System.out.println("INFO - Modified work item: " + workItem.getId() + ".\n");

}else{
System.out.println(" Skipping Work Item: " + id + "\n");
}
} finally {
wcm.disconnect(workItem);

if(fis != null){
fis.close();
}
}


After having done some research here (and thank you to everyone who participated in those posts), my linkAttachment method looks like this:


private static void linkAttachment(IAttachment attachment, IWorkItem workItem, final ITeamRepository REPO, IProgressMonitor MONITOR){
try{
ILinkManager linkManager = (ILinkManager) REPO.getClientLibrary(ILinkManager.class);
IItemReference attachmentRef= IReferenceFactory.INSTANCE.createReferenceToItem(attachment.getItemHandle());
IItemReference workItemRef= IReferenceFactory.INSTANCE.createReferenceToItem(workItem.getItemHandle());
ILink link = ILinkFactory.INSTANCE.createLink(WorkItemLinkTypes.ATTACHMENT, attachmentRef, workItemRef);
linkManager.saveLink(link, MONITOR);
}catch(Exception e){
System.out.println("ERROR - " + e.getMessage());
e.printStackTrace();
}
}


I am honestly quite confused by the behavior I'm noticing and am hoping someone here might see something in my code that is perhaps causing this, or can tell me what might be the underlying cause, because right now I am running out of ideas. Any other comments/suggestions are also welcome. =)

Accepted answer


permanent link
Steve Gates (9134) | answered Feb 10 '12, 10:38 a.m.
So, has anyone else seen this behavior when adding attachments? Anyone at all? I'm hoping I'm not alone in this.


Try reversing the order in which you pass in the references in this line of code.

ILink link = ILinkFactory.INSTANCE.createLink(WorkItemLinkTypes.ATTACHMENT, attachmentRef, workItemRef);


The parent reference (work item) should be first. With the way you're passing in the references right now, the references aren't created correctly in the links_auditable_link table in the database and I think that's probably why you're seeing the issues you're seeing.
Joshua Giangrande selected this answer as the correct answer

2 other answers



permanent link
Joshua Giangrande (2611216) | answered Jan 19 '12, 9:01 a.m.
So, has anyone else seen this behavior when adding attachments? Anyone at all? I'm hoping I'm not alone in this.

permanent link
Karthik Krishnan (859497139) | answered Feb 28 '13, 7:24 a.m.

I am still facing the same problem as the Original poster. Any idea how to overcome this?

I tried @Stevegates suggestion but still the same result, Attachment is seen in the attchments but not visible as link and not availabe for download


Comments
Joshua Giangrande commented Feb 28 '13, 9:46 a.m.

Hi Karthik,

The solution posted by Steve worked for me, however I have since modified my method which attaches files to work items. The code is as follows:

private static void linkFile(IAttachment attachment, WorkItemWorkingCopy wc, final ITeamRepository repo){
    try{
        IItemReference reference = WorkItemLinkTypes.createAttachmentReference(attachment);
        wc.getReferences().add(WorkItemEndPoints.ATTACHMENT, reference);
    }catch(Exception e){
        ...
    }  
}

This also solved a problem where, in Eclipse, files attached to work items had their file names all listed as "<unnamed>", even though mousing over or downloading produced the right file name. Hope this helps! If not, please let me know.


Karthik Krishnan commented Mar 01 '13, 4:36 a.m.

Thanks. I shall check this

Your answer


Register or to post your answer.