Search in sources :

Example 31 with AttachmentContent

use of org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent in project sw360portal by sw360.

the class ComponentCSVRecord method getAttachmentContents.

public List<AttachmentContent> getAttachmentContents() {
    List<AttachmentContent> attachments = new ArrayList<>();
    if (CommonUtils.isValidUrl(releaseDownloadURL)) {
        String urlFileName = CommonUtils.getTargetNameOfUrl(releaseDownloadURL);
        String fileName = prefixFileNameIfNecessary(urlFileName);
        attachments.add(new AttachmentContent().setFilename(fileName).setRemoteUrl(releaseDownloadURL).setOnlyRemote(true));
    }
    return attachments;
}
Also used : ArrayList(java.util.ArrayList) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) CommonUtils.nullToEmptyString(org.eclipse.sw360.datahandler.common.CommonUtils.nullToEmptyString)

Example 32 with AttachmentContent

use of org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent in project sw360portal by sw360.

the class ComponentImportUtils method removeAutogeneratedAttachments.

private static HashSet<Attachment> removeAutogeneratedAttachments(AttachmentService.Iface attachmentClient, /*read value*/
Attachment attachment, /*read value*/
Release release) throws /*return value*/
TException {
    final HashSet<Attachment> attachmentsToRemove = new HashSet<>();
    if (release.isSetAttachments()) {
        final AttachmentContent attachmentContent = attachmentClient.getAttachmentContent(attachment.getAttachmentContentId());
        final Set<Attachment> attachments = release.getAttachments();
        if (attachmentContent.isSetRemoteUrl()) {
            for (Attachment existentAttachment : attachments) {
                final AttachmentContent existentAttachmentContent = attachmentClient.getAttachmentContent(existentAttachment.getAttachmentContentId());
                if (existentAttachmentContent.isSetRemoteUrl()) {
                    if (existentAttachmentContent.getRemoteUrl().equals(attachmentContent.getRemoteUrl())) {
                        attachmentsToRemove.add(existentAttachment);
                    }
                }
            }
            // This changes the release and this is actually used.
            attachments.removeAll(attachmentsToRemove);
        }
    }
    return attachmentsToRemove;
}
Also used : AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment)

Example 33 with AttachmentContent

use of org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent in project sw360portal by sw360.

the class ComponentImportUtils method writeToDatabase.

public static RequestSummary writeToDatabase(Iterable<ComponentCSVRecord> compCSVRecords, ComponentService.Iface componentClient, VendorService.Iface vendorClient, AttachmentService.Iface attachmentClient, User user) throws TException {
    Map<String, String> vendorNameToVendorId = getVendorNameToId(compCSVRecords, vendorClient);
    log.debug(format("Read vendors: (%d) %s ", vendorNameToVendorId.size(), vendorNameToVendorId));
    final RequestSummary componentRequestSummary = updateComponents(compCSVRecords, componentClient, user);
    Map<String, String> componentNameToId = new HashMap<>();
    final ArrayList<Release> releases = new ArrayList<>();
    for (Component component : componentClient.getComponentDetailedSummaryForExport()) {
        componentNameToId.put(component.getName(), component.getId());
        final List<Release> componentReleases = component.getReleases();
        if (componentReleases != null && componentReleases.size() > 0)
            releases.addAll(componentReleases);
    }
    Set<String> knownReleaseIdentifiers = Sets.newHashSet(getReleaseIdentifiers(releases));
    List<ComponentCSVRecord> relevantCSVRecords = new ArrayList<>();
    final HashMap<String, List<String>> releaseIdentifierToDownloadURL = new HashMap<>();
    List<AttachmentContent> attachmentContentsToUpdate = new ArrayList<>();
    filterRelevantCSVRecordsAndGetAttachmentContents(compCSVRecords, componentNameToId, knownReleaseIdentifiers, relevantCSVRecords, releaseIdentifierToDownloadURL, attachmentContentsToUpdate);
    attachmentContentsToUpdate = attachmentClient.makeAttachmentContents(attachmentContentsToUpdate);
    final ImmutableMap<String, AttachmentContent> URLtoAttachment = Maps.uniqueIndex(attachmentContentsToUpdate, new Function<AttachmentContent, String>() {

        @Override
        public String apply(AttachmentContent input) {
            return input.getRemoteUrl();
        }
    });
    Set<Release> releasesToUpdate = new HashSet<>();
    // I do not need so many checks here because I only iterate over the relevant CSV records
    for (ComponentCSVRecord componentCSVRecord : relevantCSVRecords) {
        String releaseIdentifier = componentCSVRecord.getReleaseIdentifier();
        String vendorName = componentCSVRecord.getVendorName();
        String vendorId = vendorNameToVendorId.get(vendorName);
        String componentId = componentNameToId.get(componentCSVRecord.getComponentName());
        List<AttachmentContent> attachmentContents = getAttachmentContents(releaseIdentifierToDownloadURL, URLtoAttachment, releaseIdentifier);
        Release releaseToAdd = componentCSVRecord.getRelease(vendorId, componentId, attachmentContents);
        knownReleaseIdentifiers.add(releaseIdentifier);
        if (releaseToAdd != null) {
            releasesToUpdate.add(releaseToAdd);
        }
    }
    final RequestSummary releaseRequestSummary = componentClient.updateReleases(releasesToUpdate, user);
    return CommonUtils.addRequestSummaries(componentRequestSummary, "component", releaseRequestSummary, "release");
}
Also used : AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) Component(org.eclipse.sw360.datahandler.thrift.components.Component) RequestSummary(org.eclipse.sw360.datahandler.thrift.RequestSummary) Release(org.eclipse.sw360.datahandler.thrift.components.Release)

Example 34 with AttachmentContent

use of org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent in project sw360portal by sw360.

the class ComponentImportUtilsTest method assertExpectedComponentsInDb.

private void assertExpectedComponentsInDb() throws TException {
    List<Component> importedComponents = componentClient.getComponentSummary(user);
    List<Release> importedReleases = componentClient.getReleaseSummary(user);
    // see the test file
    assertThat(importedComponents, hasSize(7));
    // see the test file
    assertThat(importedReleases, hasSize(8));
    sortByField(importedComponents, Component._Fields.NAME);
    sortByField(importedReleases, Release._Fields.VERSION);
    sortByField(importedReleases, Release._Fields.NAME);
    Component component = importedComponents.get(0);
    assertThat(component.getName(), is("7-Zip"));
    component = componentClient.getComponentById(component.getId(), user);
    assertThat(component.getName(), is("7-Zip"));
    assertThat(component.getHomepage(), is("http://commons.apache.org/proper/commons-exec"));
    assertThat(component.getVendorNames(), is(emptyOrNullCollectionOf(String.class)));
    assertThat(component.getAttachments(), is(emptyOrNullCollectionOf(Attachment.class)));
    assertThat(component.getCreatedBy(), equalTo(user.getEmail()));
    assertThat(component.getReleases(), is(not(nullValue())));
    assertThat(getReleaseIds(component.getReleases()), containsInAnyOrder(importedReleases.get(0).getId(), importedReleases.get(1).getId()));
    final Release release = importedReleases.get(4);
    assertThat(release.getVersion(), is("1.2.11"));
    // This release has an download url so the import creates an attachmen
    final Set<Attachment> attachments = release.getAttachments();
    assertThat(attachments.size(), is(1));
    final Attachment theAttachment = getFirst(attachments);
    final String attachmentContentId = theAttachment.getAttachmentContentId();
    final AttachmentContent attachmentContent = attachmentClient.getAttachmentContent(attachmentContentId);
    assertThat(attachmentContent.isOnlyRemote(), is(true));
    assertThat(attachmentContent.getRemoteUrl(), is(REMOTE_URL));
}
Also used : AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) Attachment(org.eclipse.sw360.datahandler.thrift.attachments.Attachment) Component(org.eclipse.sw360.datahandler.thrift.components.Component) Release(org.eclipse.sw360.datahandler.thrift.components.Release)

Example 35 with AttachmentContent

use of org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent in project sw360portal by sw360.

the class AttachmentContentWrapperTest method testUpdateNonMetadataTouchesAllFields.

public void testUpdateNonMetadataTouchesAllFields() throws Exception {
    AttachmentContent source;
    source = new AttachmentContent();
    source.setFilename("a");
    source.setType("b");
    source.setContentType("v");
    source.setPartsCount("1");
    // TODO this is not required !
    source.setRemoteUrl("uskt");
    AttachmentContentWrapper attachmentContentWrapper = new AttachmentContentWrapper();
    attachmentContentWrapper.updateNonMetadata(source);
    assertTFields(source, attachmentContentWrapper, AttachmentContentWrapper.class, AttachmentContent._Fields.class);
}
Also used : AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent)

Aggregations

AttachmentContent (org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent)52 InputStream (java.io.InputStream)24 Test (org.junit.Test)20 Attachment (org.eclipse.sw360.datahandler.thrift.attachments.Attachment)13 Project (org.eclipse.sw360.datahandler.thrift.projects.Project)11 IOException (java.io.IOException)10 SW360Exception (org.eclipse.sw360.datahandler.thrift.SW360Exception)10 User (org.eclipse.sw360.datahandler.thrift.users.User)10 Release (org.eclipse.sw360.datahandler.thrift.components.Release)8 TException (org.apache.thrift.TException)7 LicenseInfoParsingResult (org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult)6 AttachmentInputStream (org.ektorp.AttachmentInputStream)6 OutputStream (java.io.OutputStream)5 FilledAttachment (org.eclipse.sw360.datahandler.thrift.attachments.FilledAttachment)5 StringReader (java.io.StringReader)4 ReaderInputStream (org.apache.commons.io.input.ReaderInputStream)4 RequestSummary (org.eclipse.sw360.datahandler.thrift.RequestSummary)4 DatabaseConnector (org.eclipse.sw360.datahandler.couchdb.DatabaseConnector)3 LicenseInfo (org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfo)3 PipedInputStream (java.io.PipedInputStream)2