Search in sources :

Example 51 with Release

use of org.eclipse.sw360.datahandler.thrift.components.Release in project sw360portal by sw360.

the class OutputGenerator method renderTemplateWithDefaultValues.

/**
 * Creates a velocity context and fills it with the default, commonly used
 * values:
 * <ul>
 * <li>allLicenseNamesWithTexts: list of {@link LicenseNameWithText} objects,
 * sorted by license name</li>
 * <li>licenseNameWithTextToReferenceId: map from the license name to a unique
 * id inside the file. May be used to reference a license.</li>
 * <li>licenseInfoResults: map of {@link LicenseInfoParsingResult} objects,
 * where the key is the name of the release. The licenses within the objects are
 * sorted by name. Contains only the results with status {@link LicenseInfoRequestStatus#SUCCESS}</li>
 * <li>licenseInfoErrorResults: map {@link List}of {@link LicenseInfoParsingResult} objects,
 * where the key is the name of the release. Contains only the results with status other than
 * {@link LicenseInfoRequestStatus#SUCCESS}. These results are not merged, that's why the map values are lists.</li>
 * <li>acknowledgments: map of acknowledgments for a release where the key is
 * the release and the value is a set of strings (acknowledgments)</li>
 * </ul>
 * The given file will be used as velocity template and will be rendered with
 * the described data.
 *
 * @param projectLicenseInfoResults
 *            parsing results to be rendered
 * @param file
 *            name of template file
 * @return rendered template
 */
protected String renderTemplateWithDefaultValues(Collection<LicenseInfoParsingResult> projectLicenseInfoResults, String file, String projectTitle, String licenseInfoHeaderText) {
    VelocityContext vc = getConfiguredVelocityContext();
    // set header
    vc.put(LICENSE_INFO_PROJECT_TITLE, projectTitle);
    vc.put(LICENSE_INFO_HEADER_TEXT, licenseInfoHeaderText);
    // sorted lists of all license to be displayed at the end of the file at once
    List<LicenseNameWithText> licenseNamesWithTexts = getSortedLicenseNameWithTexts(projectLicenseInfoResults);
    vc.put(ALL_LICENSE_NAMES_WITH_TEXTS, licenseNamesWithTexts);
    // assign a reference id to each license in order to only display references for
    // each release. The references will point to
    // the list with all details at the and of the file (see above)
    int referenceId = 1;
    Map<LicenseNameWithText, Integer> licenseToReferenceId = Maps.newHashMap();
    for (LicenseNameWithText licenseNamesWithText : licenseNamesWithTexts) {
        licenseToReferenceId.put(licenseNamesWithText, referenceId++);
    }
    vc.put(LICENSE_REFERENCE_ID_MAP_CONTEXT_PROPERTY, licenseToReferenceId);
    Map<Boolean, List<LicenseInfoParsingResult>> partitionedResults = projectLicenseInfoResults.stream().collect(Collectors.partitioningBy(r -> r.getStatus() == LicenseInfoRequestStatus.SUCCESS));
    List<LicenseInfoParsingResult> goodResults = partitionedResults.get(true);
    Map<String, List<LicenseInfoParsingResult>> badResultsPerRelease = partitionedResults.get(false).stream().collect(Collectors.groupingBy(this::getComponentLongName));
    vc.put(LICENSE_INFO_ERROR_RESULTS_CONTEXT_PROPERTY, badResultsPerRelease);
    // be sure that the licenses inside a release are sorted by id. This looks nicer
    SortedMap<String, LicenseInfoParsingResult> sortedLicenseInfos = getSortedLicenseInfos(goodResults);
    // this will effectively change the objects in the collection and therefore the
    // objects in the sorted map above
    sortLicenseNamesWithinEachLicenseInfoById(sortedLicenseInfos.values(), licenseToReferenceId);
    vc.put(LICENSE_INFO_RESULTS_CONTEXT_PROPERTY, sortedLicenseInfos);
    // also display acknowledgments
    SortedMap<String, Set<String>> acknowledgements = getSortedAcknowledgements(sortedLicenseInfos);
    vc.put(ACKNOWLEDGEMENTS_CONTEXT_PROPERTY, acknowledgements);
    StringWriter sw = new StringWriter();
    Velocity.mergeTemplate(file, "utf-8", vc, sw);
    IOUtils.closeQuietly(sw);
    return sw.toString();
}
Also used : SW360Utils(org.eclipse.sw360.datahandler.common.SW360Utils) java.util(java.util) StringWriter(java.io.StringWriter) RuntimeConstants(org.apache.velocity.runtime.RuntimeConstants) ClasspathResourceLoader(org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader) LicenseNameWithTextUtils(org.eclipse.sw360.licenseinfo.util.LicenseNameWithTextUtils) SW360Exception(org.eclipse.sw360.datahandler.thrift.SW360Exception) Maps(com.google.common.collect.Maps) VelocityContext(org.apache.velocity.VelocityContext) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) ToolManager(org.apache.velocity.tools.ToolManager) IOUtils(org.apache.commons.io.IOUtils) CommonUtils(org.eclipse.sw360.datahandler.common.CommonUtils) org.eclipse.sw360.datahandler.thrift.licenseinfo(org.eclipse.sw360.datahandler.thrift.licenseinfo) Velocity(org.apache.velocity.app.Velocity) NotNull(org.jetbrains.annotations.NotNull) FileResourceLoader(org.apache.velocity.runtime.resource.loader.FileResourceLoader) VelocityContext(org.apache.velocity.VelocityContext) StringWriter(java.io.StringWriter)

Example 52 with Release

use of org.eclipse.sw360.datahandler.thrift.components.Release in project sw360portal by sw360.

the class CombinedCLIParser method getLicenseInfos.

@Override
public <T> List<LicenseInfoParsingResult> getLicenseInfos(Attachment attachment, User user, T context) throws TException {
    AttachmentContent attachmentContent = attachmentContentProvider.getAttachmentContent(attachment);
    InputStream attachmentStream = null;
    List<LicenseInfoParsingResult> parsingResults = new ArrayList<>();
    Map<String, Release> releasesByExternalId = prepareReleasesByExternalId(getCorrelationKey());
    try {
        attachmentStream = attachmentConnector.getAttachmentStream(attachmentContent, user, context);
        Document doc = getDocument(attachmentStream);
        Map<String, Set<String>> copyrightSetsByExternalId = getCopyrightSetsByExternalIdsMap(doc);
        Map<String, Set<LicenseNameWithText>> licenseNamesWithTextsByExternalId = getLicenseNamesWithTextsByExternalIdsMap(doc);
        Set<String> allExternalIds = Sets.union(copyrightSetsByExternalId.keySet(), licenseNamesWithTextsByExternalId.keySet());
        allExternalIds.forEach(extId -> {
            LicenseInfoParsingResult parsingResult = getLicenseInfoParsingResultForExternalId(attachmentContent, releasesByExternalId, copyrightSetsByExternalId, licenseNamesWithTextsByExternalId, extId);
            parsingResults.add(parsingResult);
        });
    } catch (ParserConfigurationException | IOException | XPathExpressionException | SAXException | SW360Exception e) {
        log.error(e);
        parsingResults.add(new LicenseInfoParsingResult().setStatus(LicenseInfoRequestStatus.FAILURE).setMessage("Error while parsing combined CLI file: " + e.toString()));
    } finally {
        closeQuietly(attachmentStream, log);
    }
    return parsingResults;
}
Also used : InputStream(java.io.InputStream) XPathExpressionException(javax.xml.xpath.XPathExpressionException) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) IOException(java.io.IOException) Document(org.w3c.dom.Document) LicenseInfoParsingResult(org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult) SAXException(org.xml.sax.SAXException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Release(org.eclipse.sw360.datahandler.thrift.components.Release) SW360Exception(org.eclipse.sw360.datahandler.thrift.SW360Exception)

Example 53 with Release

use of org.eclipse.sw360.datahandler.thrift.components.Release in project sw360portal by sw360.

the class CombinedCLIParser method getLicenseInfoParsingResultForExternalId.

@NotNull
private LicenseInfoParsingResult getLicenseInfoParsingResultForExternalId(AttachmentContent attachmentContent, Map<String, Release> releasesByExternalId, Map<String, Set<String>> copyrightSetsByExternalId, Map<String, Set<LicenseNameWithText>> licenseNamesWithTextsByExternalId, String extId) {
    LicenseInfo licenseInfo = new LicenseInfo().setFilenames(Arrays.asList(attachmentContent.getFilename()));
    licenseInfo.setCopyrights(copyrightSetsByExternalId.get(extId));
    licenseInfo.setLicenseNamesWithTexts(licenseNamesWithTextsByExternalId.get(extId));
    LicenseInfoParsingResult parsingResult = new LicenseInfoParsingResult().setLicenseInfo(licenseInfo);
    Release release = releasesByExternalId.get(extId);
    if (release != null) {
        parsingResult.setVendor(release.isSetVendor() ? release.getVendor().getShortname() : "");
        parsingResult.setName(release.getName());
        parsingResult.setVersion(release.getVersion());
    } else {
        parsingResult.setName("No info found for external component ID " + extId);
    }
    parsingResult.setStatus(LicenseInfoRequestStatus.SUCCESS);
    return parsingResult;
}
Also used : LicenseInfo(org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfo) LicenseInfoParsingResult(org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult) Release(org.eclipse.sw360.datahandler.thrift.components.Release) NotNull(org.jetbrains.annotations.NotNull)

Example 54 with Release

use of org.eclipse.sw360.datahandler.thrift.components.Release in project sw360portal by sw360.

the class FossologyFileHandlerTest method testAFailedSendToFossology.

@Test
public void testAFailedSendToFossology() throws Exception {
    final String id = "41";
    final FilledAttachment filledAttachment = getMockFilledAttachment(id);
    final AttachmentContent attachmentContent = filledAttachment.getAttachmentContent();
    final Release release = mock(Release.class);
    when(componentService.getReleaseById(releaseId, user)).thenReturn(release);
    spyGetFilledSourceAttachment(filledAttachment);
    final InputStream inputStream = mock(InputStream.class);
    when(release.isSetFossologyId()).thenReturn(false);
    when(attachmentConnector.getAttachmentStream(attachmentContent, user, release)).thenReturn(inputStream);
    when(fossologyUploader.uploadToFossology(inputStream, attachmentContent, clearingTeam)).thenReturn(-1);
    assertThat(fossologyFileHandler.sendToFossology(releaseId, user, clearingTeam), is(RequestStatus.FAILURE));
    verify(inputStream).close();
    // unimportant verifies
    verify(componentService, atLeastOnce()).getReleaseById(releaseId, user);
    verify(attachmentConnector).getAttachmentStream(attachmentContent, user, release);
    verify(fossologyUploader).uploadToFossology(inputStream, attachmentContent, clearingTeam);
}
Also used : InputStream(java.io.InputStream) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) FilledAttachment(org.eclipse.sw360.datahandler.thrift.attachments.FilledAttachment) Release(org.eclipse.sw360.datahandler.thrift.components.Release) Test(org.junit.Test)

Example 55 with Release

use of org.eclipse.sw360.datahandler.thrift.components.Release in project sw360portal by sw360.

the class FossologyFileHandlerTest method testSendToFossologyDuplicateDoesNotUpdateTheStatusOnErrors.

@Test
public void testSendToFossologyDuplicateDoesNotUpdateTheStatusOnErrors() throws Exception {
    final FilledAttachment filledAttachment = getMockFilledAttachment("17");
    final Release release = mock(Release.class);
    when(componentService.getReleaseById(releaseId, user)).thenReturn(release);
    when(release.getFossologyId()).thenReturn("41");
    when(release.isSetFossologyId()).thenReturn(true);
    spyGetFilledSourceAttachment(filledAttachment);
    when(fossologyUploader.duplicateInFossology(41, clearingTeam)).thenReturn(false);
    doNothing().when(fossologyFileHandler).setFossologyStatus(eq(release), anyString(), any(FossologyStatus.class));
    doReturn(true).when(fossologyFileHandler).checkSourceAttachment(release, filledAttachment);
    assertThat(fossologyFileHandler.sendToFossology(releaseId, user, clearingTeam), is(RequestStatus.FAILURE));
    verify(fossologyUploader).duplicateInFossology(41, clearingTeam);
    verify(fossologyFileHandler, never()).setFossologyStatus(eq(release), eq(clearingTeam), any(FossologyStatus.class));
    verify(fossologyUploader).getStatusInFossology(eq(41), eq(clearingTeam));
    verify(componentService, never()).updateReleaseFossology(release, user);
    verify(componentService).getReleaseById(releaseId, user);
    verifyNoMoreInteractions(attachmentConnector);
}
Also used : FilledAttachment(org.eclipse.sw360.datahandler.thrift.attachments.FilledAttachment) FossologyStatus(org.eclipse.sw360.datahandler.thrift.components.FossologyStatus) Release(org.eclipse.sw360.datahandler.thrift.components.Release) Test(org.junit.Test)

Aggregations

Release (org.eclipse.sw360.datahandler.thrift.components.Release)93 User (org.eclipse.sw360.datahandler.thrift.users.User)39 TException (org.apache.thrift.TException)34 Test (org.junit.Test)22 ComponentService (org.eclipse.sw360.datahandler.thrift.components.ComponentService)20 Component (org.eclipse.sw360.datahandler.thrift.components.Component)18 Attachment (org.eclipse.sw360.datahandler.thrift.attachments.Attachment)15 RequestStatus (org.eclipse.sw360.datahandler.thrift.RequestStatus)13 Vendor (org.eclipse.sw360.datahandler.thrift.vendors.Vendor)13 FossologyStatus (org.eclipse.sw360.datahandler.thrift.components.FossologyStatus)11 TestUtils.assertTestString (org.eclipse.sw360.datahandler.TestUtils.assertTestString)10 AttachmentContent (org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent)9 Project (org.eclipse.sw360.datahandler.thrift.projects.Project)9 HalResource (org.eclipse.sw360.rest.resourceserver.core.HalResource)7 Before (org.junit.Before)7 Collectors (java.util.stream.Collectors)6 WrappedTException (org.eclipse.sw360.datahandler.common.WrappedException.WrappedTException)6 FilledAttachment (org.eclipse.sw360.datahandler.thrift.attachments.FilledAttachment)6 IOException (java.io.IOException)5 java.util (java.util)5