class PortletUtils {

    public static Set<Attachment> updateAttachmentsFromRequest(PortletRequest request, Set<Attachment> documentAttachments) {
        Set<Attachment> attachments = Sets.newHashSet();

        User user = UserCacheHolder.getUserFromRequest(request);
        String[] ids = request
                .getParameterValues(Release._Fields.ATTACHMENTS.toString() + Attachment._Fields.ATTACHMENT_CONTENT_ID.toString());
        String[] fileNames = request.getParameterValues(Release._Fields.ATTACHMENTS.toString() + Attachment._Fields.FILENAME.toString());
        String[] types = request
                .getParameterValues(Release._Fields.ATTACHMENTS.toString() + Attachment._Fields.ATTACHMENT_TYPE.toString());
        String[] createdComments = request.getParameterValues(Release._Fields.ATTACHMENTS.toString() + Attachment._Fields.CREATED_COMMENT.toString());
        String[] checkStatuses = request
                .getParameterValues(Release._Fields.ATTACHMENTS.toString() + Attachment._Fields.CHECK_STATUS.toString());
        String[] checkedComments = request.getParameterValues(Release._Fields.ATTACHMENTS.toString() + Attachment._Fields.CHECKED_COMMENT.toString());

        if (ids == null || ids.length == 0) {
            LOGGER.info("No ids transmitted. All attachments will be deleted.");
            return attachments;
        } else if (CommonUtils.oneIsNull(fileNames, types, createdComments, checkStatuses, checkedComments)) {
            LOGGER.error("Invalid request content. One of the attachment parameters is null. No attachments will be saved or deleted.");
            return documentAttachments;
        } else if (!CommonUtils.allHaveSameLength(ids, fileNames, types, createdComments, checkStatuses, checkedComments)) {
            LOGGER.error("Not all of the attachment parameter arrays have the same length! No attachments will be saved or deleted.");
            return documentAttachments;
        }

        Map<String, Attachment> documentAttachmentMap = CommonUtils.nullToEmptySet(documentAttachments).stream()
                .collect(Collectors.toMap(Attachment::getAttachmentContentId, Function.identity()));
        for (int i = 0; i < ids.length; ++i) {
            String id = ids[i];
            Attachment attachment = documentAttachmentMap.get(id);

            if (attachment == null) {
                // the sha1 checksum is not computed here, but in the backend, when updating the
                // component in the database
                attachment = CommonUtils.getNewAttachment(user, id, fileNames[i]);
                documentAttachmentMap.put(attachment.getAttachmentContentId(), attachment);
            }

            // Filename is not overwritten. Unknown reason.
            attachment.setAttachmentType(getAttachmentTypefromString(types[i]));
            attachment.setCreatedComment(createdComments[i]);

            if (getCheckStatusfromString(checkStatuses[i]) != CheckStatus.NOTCHECKED) {
                if (attachment.checkStatus != getCheckStatusfromString(checkStatuses[i])
                        || !checkedComments[i].equals(attachment.checkedComment)) {
                    attachment.setCheckedOn(SW360Utils.getCreatedOn());
                    attachment.setCheckedBy(UserCacheHolder.getUserFromRequest(request).getEmail());
                    attachment.setCheckedTeam(UserCacheHolder.getUserFromRequest(request).getDepartment());
                    attachment.setCheckedComment(checkedComments[i]);
                }
            } else {
                attachment.setCheckedOn(null);
                attachment.setCheckedBy(null);
                attachment.setCheckedTeam(null);
                attachment.setCheckedComment("");
            }
            attachment.setCheckStatus(getCheckStatusfromString(checkStatuses[i]));

            // add attachments to list of added/modified attachments. This way deleted
            // attachments are automatically not in the set
            attachments.add(attachment);
        }

        return attachments;
    }

    private  PortletUtils();

}