class ProjectPortletUtils {

    public static Map<String, Set<LicenseNameWithText>> getExcludedLicensesPerAttachmentIdFromRequest(Set<String> attachmentContentIds,
                                                                                                      ResourceRequest request) {
        Map<String, Set<LicenseNameWithText>> excludedLicenses = Maps.newHashMap();

        for (String attachmentContentId : attachmentContentIds) {
            String[] checkboxes = request.getParameterValues(attachmentContentId);
            String[] keys = request.getParameterValues(attachmentContentId + "_key");

            if (checkboxes == null) {
                // no details present
                continue;
            }

            @SuppressWarnings("unchecked")
            Map<String, LicenseNameWithText> licenseStore = (Map<String, LicenseNameWithText>) request.getPortletSession()
                    .getAttribute(ProjectPortlet.LICENSE_STORE_KEY_PREFIX + attachmentContentId);
            if (licenseStore == null) {
                throw new IllegalStateException("No license store found for attachment content id " + attachmentContentId);
            }

            Set<Integer> includedIds = Arrays.stream(checkboxes).map(s -> Integer.valueOf(s)).collect(Collectors.toSet());
            Set<LicenseNameWithText> licenseNameWithTexts = Sets.newHashSet();
            for (int index = 0; index < keys.length; index++) {
                if (includedIds.contains(index)) {
                    // a request will only contain selected ids because unselected checkboxes are
                    // not transferred. Due to that we have to exclude everything that was NOT
                    // transferred
                    continue;
                }

                LicenseNameWithText licenseNameWithText = licenseStore.get(keys[index]);
                if (licenseNameWithText == null) {
                    throw new IllegalStateException("No license found for key " + keys[index]);
                }

                licenseNameWithTexts.add(licenseNameWithText);
            }

            excludedLicenses.put(attachmentContentId, licenseNameWithTexts);
        }

        return excludedLicenses;
    }

    private  ProjectPortletUtils();

}