Search in sources :

Example 16 with License

use of org.eclipse.sw360.datahandler.thrift.licenses.License in project sw360portal by sw360.

the class ModerationDatabaseHandler method createRequest.

public RequestStatus createRequest(License license, User user) {
    License dblicense;
    try {
        dblicense = licenseDatabaseHandler.getLicenseForOrganisation(license.getId(), user.getDepartment());
    } catch (SW360Exception e) {
        log.error("Could not get original license from database. Could not generate moderation request.", e);
        return RequestStatus.FAILURE;
    }
    // Define moderators
    Set<String> moderators = getLicenseModerators(user.getDepartment());
    ModerationRequest request = createStubRequest(user, false, license.getId(), moderators);
    // Set meta-data
    request.setDocumentType(DocumentType.LICENSE);
    request.setDocumentName(SW360Utils.printName(license));
    // Fill the request
    ModerationRequestGenerator generator = new LicenseModerationRequestGenerator();
    request = generator.setAdditionsAndDeletions(request, license, dblicense);
    addOrUpdate(request, user);
    return RequestStatus.SENT_TO_MODERATOR;
}
Also used : ModerationRequest(org.eclipse.sw360.datahandler.thrift.moderation.ModerationRequest) License(org.eclipse.sw360.datahandler.thrift.licenses.License) SW360Exception(org.eclipse.sw360.datahandler.thrift.SW360Exception)

Example 17 with License

use of org.eclipse.sw360.datahandler.thrift.licenses.License in project sw360portal by sw360.

the class LicenseModerationRequestGenerator method setAdditionsAndDeletions.

@Override
public ModerationRequest setAdditionsAndDeletions(ModerationRequest request, License updateLicense, License actualLicense) {
    updateDocument = updateLicense;
    actualDocument = actualLicense;
    documentAdditions = new License();
    documentDeletions = new License();
    // required fields:
    documentAdditions.setFullname(updateLicense.getFullname());
    documentAdditions.setId(actualLicense.getId());
    documentDeletions.setFullname(actualLicense.getFullname());
    documentDeletions.setId(actualLicense.getId());
    Map<String, Todo> actualTodos = Maps.uniqueIndex(nullToEmptyList(actualLicense.getTodos()), Todo::getId);
    for (Todo updateTodo : updateLicense.getTodos()) {
        if (!actualTodos.containsKey(updateTodo.getId())) {
            if (!documentAdditions.isSetTodos()) {
                documentAdditions.setTodos(new ArrayList<>());
            }
            documentAdditions.getTodos().add(updateTodo);
        } else {
            Todo actualTodo = actualTodos.get(updateTodo.getId());
            Set<String> actualWhitelist = actualTodo.whitelist != null ? actualTodo.whitelist : new HashSet<String>();
            Set<String> updateWhitelist = updateTodo.whitelist != null ? updateTodo.whitelist : new HashSet<String>();
            String departement = request.getRequestingUserDepartment();
            if (updateWhitelist.contains(departement) && !actualWhitelist.contains(departement)) {
                if (!documentAdditions.isSetTodos()) {
                    documentAdditions.setTodos(new ArrayList<>());
                }
                documentAdditions.getTodos().add(updateTodo);
            } else if (!updateWhitelist.contains(departement) && actualWhitelist.contains(departement)) {
                if (!documentDeletions.isSetTodos()) {
                    documentDeletions.setTodos(new ArrayList<>());
                }
                documentDeletions.getTodos().add(actualTodo);
            }
        }
    }
    request.setLicenseAdditions(documentAdditions);
    request.setLicenseDeletions(documentDeletions);
    return request;
}
Also used : Todo(org.eclipse.sw360.datahandler.thrift.licenses.Todo) License(org.eclipse.sw360.datahandler.thrift.licenses.License)

Example 18 with License

use of org.eclipse.sw360.datahandler.thrift.licenses.License in project sw360portal by sw360.

the class LicenseInfoHandler method getLicenseInfoForAttachment.

@Override
public List<LicenseInfoParsingResult> getLicenseInfoForAttachment(Release release, String attachmentContentId, User user) throws TException {
    if (release == null) {
        return Collections.singletonList(noSourceParsingResult(MSG_NO_RELEASE_GIVEN));
    }
    List<LicenseInfoParsingResult> cachedResults = licenseInfoCache.getIfPresent(attachmentContentId);
    if (cachedResults != null) {
        return cachedResults;
    }
    Attachment attachment = nullToEmptySet(release.getAttachments()).stream().filter(a -> a.getAttachmentContentId().equals(attachmentContentId)).findFirst().orElseThrow(() -> {
        String message = String.format("Attachment selected for license info generation is not found in release's attachments. Release id: %s. Attachment content id: %s", release.getId(), attachmentContentId);
        return new IllegalStateException(message);
    });
    try {
        List<LicenseInfoParser> applicableParsers = parsers.stream().filter(parser -> wrapTException(() -> parser.isApplicableTo(attachment, user, release))).collect(Collectors.toList());
        if (applicableParsers.size() == 0) {
            LOGGER.warn("No applicable parser has been found for the attachment selected for license information");
            return assignReleaseToLicenseInfoParsingResult(assignFileNameToLicenseInfoParsingResult(noSourceParsingResult("No applicable parser has been found for the attachment"), attachment.getFilename()), release);
        } else if (applicableParsers.size() > 1) {
            LOGGER.info("More than one parser claims to be able to parse attachment with contend id " + attachmentContentId);
        }
        List<LicenseInfoParsingResult> results = applicableParsers.stream().map(parser -> wrapTException(() -> parser.getLicenseInfos(attachment, user, release))).flatMap(Collection::stream).collect(Collectors.toList());
        filterEmptyLicenses(results);
        results = assignReleaseToLicenseInfoParsingResults(results, release);
        licenseInfoCache.put(attachmentContentId, results);
        return results;
    } catch (WrappedTException exception) {
        throw exception.getCause();
    }
}
Also used : org.eclipse.sw360.licenseinfo.parsers(org.eclipse.sw360.licenseinfo.parsers) java.util(java.util) User(org.eclipse.sw360.datahandler.thrift.users.User) Release(org.eclipse.sw360.datahandler.thrift.components.Release) AttachmentDatabaseHandler(org.eclipse.sw360.attachments.db.AttachmentDatabaseHandler) LicenseNameWithTextUtils(org.eclipse.sw360.licenseinfo.util.LicenseNameWithTextUtils) Enums(com.google.common.base.Enums) Logger(org.apache.log4j.Logger) Lists(com.google.common.collect.Lists) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment) org.eclipse.sw360.licenseinfo.outputGenerators(org.eclipse.sw360.licenseinfo.outputGenerators) Project(org.eclipse.sw360.datahandler.thrift.projects.Project) WrappedException.wrapTException(org.eclipse.sw360.datahandler.common.WrappedException.wrapTException) MalformedURLException(java.net.MalformedURLException) TException(org.apache.thrift.TException) ComponentDatabaseHandler(org.eclipse.sw360.datahandler.db.ComponentDatabaseHandler) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) REPORT(org.eclipse.sw360.datahandler.thrift.licenseinfo.OutputFormatVariant.REPORT) DISCLOSURE(org.eclipse.sw360.datahandler.thrift.licenseinfo.OutputFormatVariant.DISCLOSURE) Sets(com.google.common.collect.Sets) DatabaseSettings(org.eclipse.sw360.datahandler.common.DatabaseSettings) TimeUnit(java.util.concurrent.TimeUnit) CommonUtils(org.eclipse.sw360.datahandler.common.CommonUtils) Entry(java.util.Map.Entry) org.eclipse.sw360.datahandler.thrift.licenseinfo(org.eclipse.sw360.datahandler.thrift.licenseinfo) VisibleForTesting(com.google.common.annotations.VisibleForTesting) CacheBuilder(com.google.common.cache.CacheBuilder) Cache(com.google.common.cache.Cache) WrappedTException(org.eclipse.sw360.datahandler.common.WrappedException.WrappedTException) SW360Assert.assertNotNull(org.eclipse.sw360.datahandler.common.SW360Assert.assertNotNull) CommonUtils.nullToEmptySet(org.eclipse.sw360.datahandler.common.CommonUtils.nullToEmptySet) WrappedTException(org.eclipse.sw360.datahandler.common.WrappedException.WrappedTException) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment)

Example 19 with License

use of org.eclipse.sw360.datahandler.thrift.licenses.License in project sw360portal by sw360.

the class LicenseDatabaseHandler method updateLicenseFromInputLicense.

private License updateLicenseFromInputLicense(License license, License inputLicense, String businessUnit, User user) {
    if (inputLicense.isSetTodos()) {
        for (Todo todo : inputLicense.getTodos()) {
            if (isTemporaryTodo(todo)) {
                todo.unsetId();
                try {
                    String todoDatabaseId = addTodo(todo, user);
                    license.addToTodoDatabaseIds(todoDatabaseId);
                } catch (SW360Exception e) {
                    log.error("Error adding todo to database.");
                }
            } else if (todo.isSetId()) {
                Todo dbTodo = todoRepository.get(todo.id);
                if (todo.whitelist.contains(businessUnit) && !dbTodo.whitelist.contains(businessUnit)) {
                    dbTodo.addToWhitelist(businessUnit);
                    todoRepository.update(dbTodo);
                }
                if (!todo.whitelist.contains(businessUnit) && dbTodo.whitelist.contains(businessUnit)) {
                    dbTodo.whitelist.remove(businessUnit);
                    todoRepository.update(dbTodo);
                }
            }
        }
    }
    license.setText(inputLicense.getText());
    license.setFullname(inputLicense.getFullname());
    // only a new license gets its id from the shortname. Id of an existing license isn't supposed to be changed anyway
    if (!license.isSetId())
        license.setId(inputLicense.getShortname());
    license.unsetShortname();
    license.setLicenseTypeDatabaseId(inputLicense.getLicenseTypeDatabaseId());
    license.unsetLicenseType();
    license.setGPLv2Compat(Optional.ofNullable(inputLicense.getGPLv2Compat()).orElse(Ternary.UNDEFINED));
    license.setGPLv3Compat(Optional.ofNullable(inputLicense.getGPLv3Compat()).orElse(Ternary.UNDEFINED));
    license.setExternalLicenseLink(inputLicense.getExternalLicenseLink());
    return license;
}
Also used : CommonUtils.isTemporaryTodo(org.eclipse.sw360.datahandler.common.CommonUtils.isTemporaryTodo)

Example 20 with License

use of org.eclipse.sw360.datahandler.thrift.licenses.License in project sw360portal by sw360.

the class LicenseDatabaseHandler method getLicenseForOrganisationWithOwnModerationRequests.

public License getLicenseForOrganisationWithOwnModerationRequests(String id, String organisation, User user) throws SW360Exception {
    List<ModerationRequest> moderationRequestsForDocumentId = moderator.getModerationRequestsForDocumentId(id);
    License license = getLicenseForOrganisation(id, organisation);
    DocumentState documentState;
    if (moderationRequestsForDocumentId.isEmpty()) {
        documentState = CommonUtils.getOriginalDocumentState();
    } else {
        final String email = user.getEmail();
        Optional<ModerationRequest> moderationRequestOptional = CommonUtils.getFirstModerationRequestOfUser(moderationRequestsForDocumentId, email);
        if (moderationRequestOptional.isPresent() && isInProgressOrPending(moderationRequestOptional.get())) {
            ModerationRequest moderationRequest = moderationRequestOptional.get();
            license = moderator.updateLicenseFromModerationRequest(license, moderationRequest.getLicenseAdditions(), moderationRequest.getLicenseDeletions(), organisation);
            for (Todo todo : license.getTodos()) {
                // remove other organisations from whitelist of todo
                todo.setWhitelist(SW360Utils.filterBUSet(organisation, todo.whitelist));
            }
            documentState = CommonUtils.getModeratedDocumentState(moderationRequest);
        } else {
            documentState = new DocumentState().setIsOriginalDocument(true).setModerationState(moderationRequestsForDocumentId.get(0).getModerationState());
        }
    }
    license.setPermissions(makePermission(license, user).getPermissionMap());
    license.setDocumentState(documentState);
    return license;
}
Also used : CommonUtils.isTemporaryTodo(org.eclipse.sw360.datahandler.common.CommonUtils.isTemporaryTodo) ModerationRequest(org.eclipse.sw360.datahandler.thrift.moderation.ModerationRequest)

Aggregations

User (org.eclipse.sw360.datahandler.thrift.users.User)20 TException (org.apache.thrift.TException)16 License (org.eclipse.sw360.datahandler.thrift.licenses.License)16 UsedAsLiferayAction (org.eclipse.sw360.portal.common.UsedAsLiferayAction)8 RequestStatus (org.eclipse.sw360.datahandler.thrift.RequestStatus)7 java.util (java.util)5 Collectors (java.util.stream.Collectors)5 Logger (org.apache.log4j.Logger)5 CommonUtils (org.eclipse.sw360.datahandler.common.CommonUtils)5 Project (org.eclipse.sw360.datahandler.thrift.projects.Project)5 CommonUtils.isTemporaryTodo (org.eclipse.sw360.datahandler.common.CommonUtils.isTemporaryTodo)4 SW360Exception (org.eclipse.sw360.datahandler.thrift.SW360Exception)4 Release (org.eclipse.sw360.datahandler.thrift.components.Release)4 Maps (com.google.common.collect.Maps)3 Sets (com.google.common.collect.Sets)3 XWPFRun (org.apache.poi.xwpf.usermodel.XWPFRun)3 CommonUtils.nullToEmptyString (org.eclipse.sw360.datahandler.common.CommonUtils.nullToEmptyString)3 Ternary (org.eclipse.sw360.datahandler.thrift.Ternary)3 LicenseService (org.eclipse.sw360.datahandler.thrift.licenses.LicenseService)3 ModerationRequest (org.eclipse.sw360.datahandler.thrift.moderation.ModerationRequest)3