use of javax.portlet.ResourceRequest in project sw360portal by sw360.
the class ProjectPortletUtils method getExcludedLicensesPerAttachmentIdFromRequest.
/**
* Returns a map of excluded licenses. They key is an attachment content id, the
* value is a list of excluded licenses.
* <p>
* For this method to work it is crucial that there is a so called
* "license-store-<attachmentContentId>" map in the session. This map must
* contain a mapping from a key to a {@link LicenseNameWithText} object.
*
* @param attachmentContentIds list of attachment content id to check for exclusions in the
* request
* @param request the request containing the excluded licenses as parameters
* @return a map containing the licenses to exclude
* @see ProjectPortletUtilsTest for a better understanding
*/
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)) {
// 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;
}
Aggregations