Skip to content
This repository has been archived by the owner on Jul 25, 2018. It is now read-only.

Commit

Permalink
fix(licenseinfo): fix saving the selection of attachments for license…
Browse files Browse the repository at this point in the history
… info

saving empty selection now actually deletes all respective attachment usages from the db
replacing attachment usages with no usage type actually works now (but is not yet used)
add thrift method to delete attachment usages by a specific type
clean up .gitignore
  • Loading branch information
alexbrdn committed Feb 1, 2018
1 parent 9a36683 commit 59116d3
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 112 deletions.
17 changes: 0 additions & 17 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

backend/*/target/
frontend/*/target/
libraries/*/target/
target/

# Ignore private and public keys
Expand All @@ -37,18 +34,4 @@ target/
#ignore any log files
*.log

/libraries/target/
/libraries/lib-datahandler/target/
/libraries/commonIO/target/
/libraries/importers/component-importer/target/
/libraries/exporters/target/
/backend/src-common/target/
/backend/src/src-vendors/target/
/backend/src/src-components/target/
/backend/src/src-attachments/target/
/frontend/sw360-portlets/target/
/frontend/target/
/libraries/importers/license-importer/target/
/backend/src/src-projects/target/
/backend/svc/svc-licenses/target/
/backend/src-common/src/main/resources/sw360.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright Siemens AG, 2013-2017. Part of the SW360 Portal Project.
* Copyright Siemens AG, 2013-2018. Part of the SW360 Portal Project.
*
* SPDX-License-Identifier: EPL-1.0
*
Expand All @@ -13,6 +13,7 @@
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import org.apache.log4j.Logger;
import org.apache.thrift.TException;
Expand All @@ -35,10 +36,8 @@
import org.ektorp.DocumentOperationResult;

import java.net.MalformedURLException;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -203,7 +202,7 @@ public void deleteAttachmentUsage(AttachmentUsage attachmentUsage) throws TExcep
public void deleteAttachmentUsages(List<AttachmentUsage> attachmentUsages) throws TException {
assertNotNull(attachmentUsages);
assertNotEmpty(attachmentUsages);
assertIds(attachmentUsages, attachmentUsage -> attachmentUsage.isSetId());
assertIds(attachmentUsages, AttachmentUsage::isSetId);

List<DocumentOperationResult> results = attachmentUsageRepository.executeBulk(
attachmentUsages.stream().map(attachmentUsage -> BulkDeleteDocument.of(attachmentUsage)).collect(Collectors.toList()));
Expand All @@ -212,6 +211,29 @@ public void deleteAttachmentUsages(List<AttachmentUsage> attachmentUsages) throw
}
}

private void deleteAttachmentUsagesByUsageDataTypes(Source usedBy, Set<UsageData._Fields> typesToReplace, boolean deleteWithEmptyType) throws TException {
List<AttachmentUsage> existingUsages = attachmentUsageRepository.getUsedAttachments(usedBy.getFieldValue().toString());
List<AttachmentUsage> usagesToDelete = existingUsages.stream().filter(usage -> {
if (usage.isSetUsageData()) {
return typesToReplace.contains(usage.getUsageData().getSetField());
} else {
return deleteWithEmptyType;
}
}).collect(Collectors.toList());

if (!usagesToDelete.isEmpty()) {
deleteAttachmentUsages(usagesToDelete);
}
}

@Override
public void deleteAttachmentUsagesByUsageDataType(Source usedBy, UsageData usageData) throws TException {
assertNotNull(usedBy);
assertTrue(usedBy.isSet());
Set<UsageData._Fields> usageDataTypes = usageData == null ? Collections.emptySet() : ImmutableSet.of(usageData.getSetField());
deleteAttachmentUsagesByUsageDataTypes(usedBy, usageDataTypes, usageData == null);
}

@Override
public List<AttachmentUsage> getAttachmentUsages(Source owner, String attachmentContentId, UsageData filter) throws TException {
assertNotNull(owner);
Expand Down Expand Up @@ -244,28 +266,19 @@ public void replaceAttachmentUsages(Source usedBy, List<AttachmentUsage> attachm
assertNotNull(usedBy);
assertTrue(usedBy.isSet());
assertNotNull(attachmentUsages);
assertNotEmpty(attachmentUsages);

Set<UsageData._Fields> typesToReplace = attachmentUsages.stream().filter(usage -> usage.isSetUsageData())
.map(usage -> usage.getUsageData().getSetField()).collect(Collectors.toSet());
boolean hasEmptyType = typesToReplace.size() != attachmentUsages.size();
List<AttachmentUsage> usagesWithNonEmptyType = attachmentUsages.stream()
.filter(AttachmentUsage::isSetUsageData)
.collect(Collectors.toList());
boolean hasEmptyUsageDataType = usagesWithNonEmptyType.size() != attachmentUsages.size();
Set<UsageData._Fields> typesToReplace = usagesWithNonEmptyType.stream()
.map(usage -> usage.getUsageData().getSetField())
.collect(Collectors.toSet());

// these ones will be deleted
List<AttachmentUsage> existingUsages = attachmentUsageRepository.getUsedAttachments(usedBy.getFieldValue().toString());
List<AttachmentUsage> usagesToDelete = existingUsages.stream().filter(usage -> {
if (usage.isSetUsageData()) {
return typesToReplace.contains(usage.getUsageData().getSetField());
} else {
return hasEmptyType;
}
}).collect(Collectors.toList());
// for some reason it is not possible to delete and update documents in the same
// request
if (!usagesToDelete.isEmpty()) {
deleteAttachmentUsages(usagesToDelete);
}
// delete all the existing usages of the types given
deleteAttachmentUsagesByUsageDataTypes(usedBy, typesToReplace, hasEmptyUsageDataType);

// these ones will be added/updated
// then save the new ones
List<DocumentOperationResult> results = attachmentUsageRepository.executeBulk(attachmentUsages);
if (!results.isEmpty()) {
throw new SW360Exception("Some of the usage documents could not be updated: " + results);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright Siemens AG, 2013-2017. Part of the SW360 Portal Project.
* Copyright Siemens AG, 2013-2018. Part of the SW360 Portal Project.
*
* SPDX-License-Identifier: EPL-1.0
*
Expand Down Expand Up @@ -32,6 +32,7 @@
import org.junit.*;
import org.junit.rules.ExpectedException;

import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
Expand Down Expand Up @@ -227,7 +228,7 @@ public void testGetUsedAttachmentsWithFilter() throws Exception {
}

@Test
public void testReplacementOfUsageWithoutEmptyType() throws Exception {
public void testReplacementOfUsageWithoutEmptyUsageData() throws Exception {
AttachmentUsage usage1 = createUsage("p1", "r1", "a11");
usage1.setUsageData(UsageData.licenseInfo(new LicenseInfoUsage(Sets.newHashSet("l1", "l2"))));
AttachmentUsage usage2 = createUsage("p1", "r1", "a12");
Expand All @@ -248,7 +249,7 @@ public void testReplacementOfUsageWithoutEmptyType() throws Exception {
}

@Test
public void testReplacementOfUsageWithEmptyType() throws Exception {
public void testReplacementOfUsageWithEmptyUsageData() throws Exception {
AttachmentUsage usage1 = createUsage("p1", "r1", "a11");
usage1.setUsageData(UsageData.licenseInfo(new LicenseInfoUsage(Sets.newHashSet("l1", "l2"))));
AttachmentUsage usage2 = createUsage("p1", "r1", "a12");
Expand All @@ -261,11 +262,67 @@ public void testReplacementOfUsageWithEmptyType() throws Exception {

AttachmentUsage usage6 = createUsage("p1", "r8", "a81");
AttachmentUsage usage7 = createUsage("p1", "r9", "a91");
usage7.setUsageData(UsageData.licenseInfo(new LicenseInfoUsage(Sets.newHashSet("l9"))));

handler.replaceAttachmentUsages(Source.projectId("p1"), Lists.newArrayList(usage6, usage7));

Assert.assertThat(handler.getUsedAttachments(Source.projectId("p1"), null), Matchers.containsInAnyOrder(usage6, usage7));
Assert.assertThat(handler.getUsedAttachments(Source.projectId("p1"), null), Matchers.containsInAnyOrder(usage1, usage2, usage6, usage7));
Assert.assertThat(handler.getUsedAttachments(Source.projectId("p2"), null), Matchers.containsInAnyOrder(usage4));
Assert.assertThat(handler.getUsedAttachments(Source.projectId("p3"), null), Matchers.containsInAnyOrder(usage5));
}

@Test
public void testReplacingWithEmptyUsagesListDoesNothing() throws Exception {
AttachmentUsage usage1 = createUsage("p1", "r1", "a11");
usage1.setUsageData(UsageData.licenseInfo(new LicenseInfoUsage(Sets.newHashSet("l1", "l2"))));
AttachmentUsage usage2 = createUsage("p1", "r1", "a12");
usage2.setUsageData(UsageData.licenseInfo(new LicenseInfoUsage(Sets.newHashSet())));
AttachmentUsage usage3 = createUsage("p1", "r2", "a21");
AttachmentUsage usage4 = createUsage("p2", "r1", "a11");
AttachmentUsage usage5 = createUsage("p3", "r1", "a11");
usage5.setUsageData(UsageData.licenseInfo(new LicenseInfoUsage(Sets.newHashSet("l3"))));
handler.makeAttachmentUsages(Lists.newArrayList(usage1, usage2, usage3, usage4, usage5));

handler.replaceAttachmentUsages(Source.projectId("p1"), Lists.newArrayList());

Assert.assertThat(handler.getUsedAttachments(Source.projectId("p1"), null), Matchers.containsInAnyOrder(usage1, usage2, usage3));
Assert.assertThat(handler.getUsedAttachments(Source.projectId("p2"), null), Matchers.containsInAnyOrder(usage4));
Assert.assertThat(handler.getUsedAttachments(Source.projectId("p3"), null), Matchers.containsInAnyOrder(usage5));
}

@Test
public void testDeleteAttachmentUsagesByUsageDataTypeWithNonEmptyType() throws Exception {
AttachmentUsage usage1 = createUsage("p1", "r1", "a11");
usage1.setUsageData(UsageData.licenseInfo(new LicenseInfoUsage(Sets.newHashSet("l1", "l2"))));
AttachmentUsage usage2 = createUsage("p1", "r1", "a12");
usage2.setUsageData(UsageData.licenseInfo(new LicenseInfoUsage(Sets.newHashSet())));
AttachmentUsage usage3 = createUsage("p1", "r2", "a21");
AttachmentUsage usage4 = createUsage("p2", "r1", "a11");
AttachmentUsage usage5 = createUsage("p3", "r1", "a11");
usage5.setUsageData(UsageData.licenseInfo(new LicenseInfoUsage(Sets.newHashSet("l3"))));
handler.makeAttachmentUsages(Lists.newArrayList(usage1, usage2, usage3, usage4, usage5));

handler.deleteAttachmentUsagesByUsageDataType(Source.projectId("p1"), UsageData.licenseInfo(new LicenseInfoUsage(Collections.emptySet())));

Assert.assertThat(handler.getUsedAttachments(Source.projectId("p1"), null), Matchers.containsInAnyOrder(usage3));
Assert.assertThat(handler.getUsedAttachments(Source.projectId("p2"), null), Matchers.containsInAnyOrder(usage4));
Assert.assertThat(handler.getUsedAttachments(Source.projectId("p3"), null), Matchers.containsInAnyOrder(usage5));
}

@Test
public void testDeleteAttachmentUsagesByUsageDataTypeWithEmptyType() throws Exception {
AttachmentUsage usage1 = createUsage("p1", "r1", "a11");
usage1.setUsageData(UsageData.licenseInfo(new LicenseInfoUsage(Sets.newHashSet("l1", "l2"))));
AttachmentUsage usage2 = createUsage("p1", "r1", "a12");
usage2.setUsageData(UsageData.licenseInfo(new LicenseInfoUsage(Sets.newHashSet())));
AttachmentUsage usage3 = createUsage("p1", "r2", "a21");
AttachmentUsage usage4 = createUsage("p2", "r1", "a11");
AttachmentUsage usage5 = createUsage("p3", "r1", "a11");
usage5.setUsageData(UsageData.licenseInfo(new LicenseInfoUsage(Sets.newHashSet("l3"))));
handler.makeAttachmentUsages(Lists.newArrayList(usage1, usage2, usage3, usage4, usage5));

handler.deleteAttachmentUsagesByUsageDataType(Source.projectId("p1"), null);

Assert.assertThat(handler.getUsedAttachments(Source.projectId("p1"), null), Matchers.containsInAnyOrder(usage1, usage2));
Assert.assertThat(handler.getUsedAttachments(Source.projectId("p2"), null), Matchers.containsInAnyOrder(usage4));
Assert.assertThat(handler.getUsedAttachments(Source.projectId("p3"), null), Matchers.containsInAnyOrder(usage5));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class LicenseInfoHandler implements LicenseInfoService.Iface {
private static final int CACHE_MAX_ITEMS = 100;
private static final String DEFAULT_LICENSE_INFO_HEADER_FILE="/DefaultLicenseInfoHeader.txt";
private static final String DEFAULT_LICENSE_INFO_TEXT = loadDefaultLicenseInfoHeaderText();
public static final String MSG_NO_RELEASE_GIVEN = "No release given";

protected List<LicenseInfoParser> parsers;
protected List<OutputGenerator<?>> outputGenerators;
Expand Down Expand Up @@ -135,7 +136,7 @@ public OutputFormatInfo getOutputFormatInfoForGeneratorClass(String generatorCla
public List<LicenseInfoParsingResult> getLicenseInfoForAttachment(Release release, String attachmentContentId, User user)
throws TException {
if (release == null) {
return Collections.singletonList(noSourceParsingResult("No release given"));
return Collections.singletonList(noSourceParsingResult(MSG_NO_RELEASE_GIVEN));
}

List<LicenseInfoParsingResult> cachedResults = licenseInfoCache.getIfPresent(attachmentContentId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class DocxUtils {

private static final int FONT_SIZE = 12;
public static final String ALERT_COLOR = "e95850";
public static final String FONT_FAMILY = "Calibri";

private DocxUtils() {
//only static members
Expand Down Expand Up @@ -99,7 +100,7 @@ public static void fillReleasesTable(XWPFTable table, Collection<LicenseInfoPars
} else {
String filename = Optional.ofNullable(result.getLicenseInfo())
.map(LicenseInfo::getFilenames)
.map(l -> l.stream().findFirst().orElse(null))
.flatMap(l -> l.stream().findFirst())
.orElse("");
addReleaseTableErrorRow(table, releaseName, version, nullToEmptyString(result.getMessage()), filename);
}
Expand Down Expand Up @@ -241,11 +242,11 @@ private static void addFormattedText(XWPFRun run, String text, String fontFamily
}

private static void addFormattedText(XWPFRun run, String text, int fontSize, boolean bold, String rrggbbColor) {
addFormattedText(run, text, "Calibri", fontSize, bold, rrggbbColor);
addFormattedText(run, text, FONT_FAMILY, fontSize, bold, rrggbbColor);
}

private static void addFormattedText(XWPFRun run, String text, int fontSize, boolean bold) {
addFormattedText(run, text, "Calibri", fontSize, bold, null);
addFormattedText(run, text, FONT_FAMILY, fontSize, bold, null);
}

private static void addFormattedText(XWPFRun run, String text, int fontSize) {
Expand Down
Loading

0 comments on commit 59116d3

Please sign in to comment.