taskadapter / redmine-java-api

Redmine Java API
Apache License 2.0
270 stars 163 forks source link

How to add multiple attachments ? Only first attachment uploaded #194

Closed JeanBeaurepaire closed 8 years ago

JeanBeaurepaire commented 9 years ago

I need to add some Attachments with the function addAttachments :

Collection<Attachment> collectionAttachment = new ArrayList<>();
collectionAttachment.add(Attachment1);
collectionAttachement.add(Attachment2);

issue.addAttachments(collectionAttachment);

But juste the first attachment created is uploaded. Because all of this attachment created have an id equals to 0, i'm asking me if the problem is because we look on the hashCode in the file Attachment.java :

@Override
    public int hashCode() {
        return id != null ? id.hashCode() : 0;

    }

It seems to be that the hashcode depends of the id and the id is alltime equals to 0 when u create an Attachment.

jenghung commented 9 years ago

+1, the same problem. If I assign a sequence number to the id property, it will upload all attachments.

JeanBeaurepaire commented 9 years ago

Do you have some code that correct this issue @jenghung ?

chrisderham commented 9 years ago

I found multiple issues. I will try to share a solution we coded. The issues were that if you upload the attachment, token is set. However as stated above the equals method works on id alone. Second if you upload a file with the same name as an existing defect, by default the system will update the file.

We use this code to send up the attachments, and then add them to the issue attachments array. Then we save the issue with either issueManager.createIssue(issue); or issueManager.update(issue); if an update.

        String attachmentUniqueName = UUID.randomUUID().toString();
        Attachment attachment = attachmentManager.uploadAttachment(attachmentUniqueName, mimeType, contents);
        attachment.setFileName(file.getName());
        attachment.setAuthor(author);
        attachment.setCreatedOn(createdOn);

        issue.addAttachment(attachment);

Then update com.taskadapter.redmineapi.bean.Attachment as follows

@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }
    if (o == null || getClass() != o.getClass()) {
        return false;
    }

    Attachment that = (Attachment) o;

    if (id != null ? !id.equals(that.id) : that.id != null) {
        return false;
    }
    if (token != null ? !token.equals(that.token) : that.token != null) {
        return false;
    }

    return true;
}

@Override
public int hashCode() {
    return id != null ? id.hashCode() : (token == null ? 0 : token.hashCode());
}
adrianobr commented 8 years ago

1+

dulpneto commented 8 years ago

+1

alexeyOnGitHub commented 8 years ago

is anyone ready to submit a PR for this?

albfan commented 8 years ago

Let's see if these can be fixed. Any failing test already avaliable ( I mean TDD)?

alexeyOnGitHub commented 8 years ago

all existing tests pass. our dev Jenkins is currently down due to the OS upgrade which revealed this Jenkins bug: https://issues.jenkins-ci.org/browse/JENKINS-25333 . looks like this Jenkins issue is affecting multiple jenkins installs world-wide.

alexeyOnGitHub commented 8 years ago

fixed.