Search in sources :

Example 1 with CommonUtils.nullToEmptySet

use of org.eclipse.sw360.datahandler.common.CommonUtils.nullToEmptySet in project sw360portal by sw360.

the class ProjectPortletUtils method makeAttachmentUsages.

public static List<AttachmentUsage> makeAttachmentUsages(Project project, Map<String, Set<String>> selectedReleaseAndAttachmentIds, Map<String, Set<LicenseNameWithText>> excludedLicensesPerAttachmentId) {
    List<AttachmentUsage> attachmentUsages = Lists.newArrayList();
    for (String releaseId : selectedReleaseAndAttachmentIds.keySet()) {
        for (String attachmentContentId : selectedReleaseAndAttachmentIds.get(releaseId)) {
            AttachmentUsage usage = new AttachmentUsage();
            usage.setUsedBy(Source.projectId(project.getId()));
            usage.setOwner(Source.releaseId(releaseId));
            usage.setAttachmentContentId(attachmentContentId);
            Set<String> licenseIds = CommonUtils.nullToEmptySet(excludedLicensesPerAttachmentId.get(attachmentContentId)).stream().filter(LicenseNameWithText::isSetLicenseName).map(LicenseNameWithText::getLicenseName).collect(Collectors.toSet());
            usage.setUsageData(UsageData.licenseInfo(new LicenseInfoUsage(licenseIds)));
            attachmentUsages.add(usage);
        }
    }
    return attachmentUsages;
}
Also used : AttachmentUsage(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentUsage) LicenseNameWithText(org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseNameWithText) LicenseInfoUsage(org.eclipse.sw360.datahandler.thrift.attachments.LicenseInfoUsage)

Example 2 with CommonUtils.nullToEmptySet

use of org.eclipse.sw360.datahandler.common.CommonUtils.nullToEmptySet in project sw360portal by sw360.

the class PortletUtils method updateAttachmentsFromRequest.

/**
 * Returns a set of updated attachments from the given request.
 *
 * This function will also take a set of existing attachments that will be
 * updated according to the request. A new set with the updated attachments will
 * be returned.
 *
 * If some of the attachments in the given set are not present in the request,
 * they will not be part of the returned set.
 *
 * Note: the given set will not be changed. However, the containing attachments
 * are changed during update.
 *
 * @param request
 *            request to parse for attachments
 * @param documentAttachments
 *            existing attachments
 *
 * @return set of updated attachments present in the request
 */
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;
}
Also used : User(org.eclipse.sw360.datahandler.thrift.users.User) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment)

Example 3 with CommonUtils.nullToEmptySet

use of org.eclipse.sw360.datahandler.common.CommonUtils.nullToEmptySet in project sw360portal by sw360.

the class LicenseDatabaseHandler method fillTodos.

private void fillTodos(List<Todo> todos) {
    Set<String> obligationIdsToFetch = new HashSet<>();
    for (Todo todo : todos) {
        obligationIdsToFetch.addAll(CommonUtils.nullToEmptySet(todo.getObligationDatabaseIds()));
    }
    Map<String, Obligation> obligationIdMap = null;
    if (!obligationIdsToFetch.isEmpty()) {
        obligationIdMap = ThriftUtils.getIdMap(getObligationsByIds(obligationIdsToFetch));
    }
    if (obligationIdMap == null) {
        obligationIdMap = Collections.emptyMap();
    }
    for (Todo todo : todos) {
        if (todo.isSetObligationDatabaseIds()) {
            for (String id : todo.getObligationDatabaseIds()) {
                final Obligation obligation = obligationIdMap.get(id);
                if (obligation != null) {
                    todo.addToObligations(obligation);
                }
            }
        }
        todo.setDevelopmentString(todo.isDevelopment() ? "True" : "False");
        todo.setDistributionString(todo.isDistribution() ? "True" : "False");
        todo.unsetObligationDatabaseIds();
    }
    for (Todo todo : todos) {
        if (!todo.isSetWhitelist()) {
            todo.setWhitelist(Collections.emptySet());
        }
    }
}
Also used : CommonUtils.isTemporaryTodo(org.eclipse.sw360.datahandler.common.CommonUtils.isTemporaryTodo)

Aggregations

CommonUtils.isTemporaryTodo (org.eclipse.sw360.datahandler.common.CommonUtils.isTemporaryTodo)1 Attachment (org.eclipse.sw360.datahandler.thrift.attachments.Attachment)1 AttachmentUsage (org.eclipse.sw360.datahandler.thrift.attachments.AttachmentUsage)1 LicenseInfoUsage (org.eclipse.sw360.datahandler.thrift.attachments.LicenseInfoUsage)1 LicenseNameWithText (org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseNameWithText)1 User (org.eclipse.sw360.datahandler.thrift.users.User)1