use of org.eclipse.sw360.datahandler.thrift.components.Component 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");
}
use of org.eclipse.sw360.datahandler.thrift.components.Component in project sw360portal by sw360.
the class ComponentImportUtils method updateComponents.
private static RequestSummary updateComponents(Iterable<ComponentCSVRecord> compCSVRecords, ComponentService.Iface componentClient, User user) throws TException {
Set<String> componentNames = new HashSet<>();
for (Component component : componentClient.getComponentSummaryForExport()) {
componentNames.add(component.getName());
}
Set<Component> toBeUpdated = new HashSet<>();
for (ComponentCSVRecord componentCSVRecord : compCSVRecords) {
if (componentCSVRecord.isSetComponent()) {
String componentName = componentCSVRecord.getComponentName();
if (componentNames.add(componentName)) {
Component component = componentCSVRecord.getComponent();
toBeUpdated.add(component);
}
}
}
return componentClient.updateComponents(toBeUpdated, user);
}
use of org.eclipse.sw360.datahandler.thrift.components.Component 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));
}
use of org.eclipse.sw360.datahandler.thrift.components.Component in project sw360portal by sw360.
the class ReleaseHelper method addFieldValueToRow.
private void addFieldValueToRow(List<String> row, Release._Fields field, Release release) throws SW360Exception {
switch(field) {
case COMPONENT_ID:
// first, add data for given field
row.add(release.getComponentId());
// second, add joined data, remark that headers have already been added
// accordingly
// add component type in every case
Component component = this.preloadedComponents.get(release.componentId);
if (component == null) {
// maybe cache was not initialized properly, so try to load manually
try {
component = cClient.getComponentById(release.getComponentId(), user);
} catch (TException e) {
log.warn("No component found for id " + release.getComponentId() + " which is set in release with id " + release.getId(), e);
component = null;
}
}
// check again and add value
if (component == null) {
row.add("");
} else {
row.add(ThriftEnumUtils.enumToString(component.getComponentType()));
}
// and project origin only if wanted
if (addAdditionalData()) {
if (releaseClearingStatusDataByRelease.containsKey(release)) {
row.add(releaseClearingStatusDataByRelease.get(release).getProjectNames());
} else {
row.add("");
}
}
break;
case VENDOR:
addVendorToRow(release.getVendor(), row);
break;
case COTS_DETAILS:
addCotsDetailsToRow(release.getCotsDetails(), row);
break;
case CLEARING_INFORMATION:
addClearingInformationToRow(release.getClearingInformation(), row);
break;
case ECC_INFORMATION:
addEccInformationToRow(release.getEccInformation(), row);
break;
case RELEASE_ID_TO_RELATIONSHIP:
addReleaseIdToRelationShipToRow(release.getReleaseIdToRelationship(), row);
break;
case ATTACHMENTS:
String size = Integer.toString(release.isSetAttachments() ? release.getAttachments().size() : 0);
row.add(size);
break;
default:
Object fieldValue = release.getFieldValue(field);
row.add(fieldValueAsString(fieldValue));
}
}
use of org.eclipse.sw360.datahandler.thrift.components.Component in project sw360portal by sw360.
the class ReleaseHelper method batchloadComponents.
private void batchloadComponents(Set<String> cIds) throws SW360Exception {
try {
List<Component> componentsShort = cClient.getComponentsShort(cIds);
this.preloadedComponents.putAll(ThriftUtils.getIdMap(componentsShort));
} catch (TException e) {
throw new SW360Exception("Could not get Components for ids [" + cIds + "] because of:\n" + e.getMessage());
}
}
Aggregations