Skip to content

Commit 779dea1

Browse files
committed
feat(rest): Count of attachments used in different projects.
Signed-off-by: Rudra Chopra <prabhuchopra@gmail.com>
1 parent c1fbd1b commit 779dea1

File tree

2 files changed

+96
-16
lines changed

2 files changed

+96
-16
lines changed

rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/project/ProjectController.java

+48-16
Original file line numberDiff line numberDiff line change
@@ -136,20 +136,8 @@
136136
import java.net.URI;
137137
import java.net.URISyntaxException;
138138
import java.time.format.DateTimeFormatter;
139-
import java.util.ArrayList;
140-
import java.util.Arrays;
141-
import java.util.Collections;
142-
import java.util.HashMap;
143-
import java.util.HashSet;
144-
import java.util.InvalidPropertiesFormatException;
145-
import java.util.LinkedHashMap;
146-
import java.util.List;
147-
import java.util.Map;
139+
import java.util.*;
148140
import java.util.Map.Entry;
149-
import java.util.NoSuchElementException;
150-
import java.util.Objects;
151-
import java.util.Optional;
152-
import java.util.Set;
153141
import java.util.function.Consumer;
154142
import java.util.function.Function;
155143
import java.util.function.Predicate;
@@ -1895,6 +1883,24 @@ public ResponseEntity<?> saveAttachmentUsages(
18951883
}
18961884
}
18971885

1886+
public Map<String, Integer> countMap(Collection<AttachmentType> attachmentTypes, UsageData filter, Project project, User sw360User, String id) throws TException {
1887+
boolean projectWithSubProjects = !project.getLinkedProjects().isEmpty();
1888+
List<ProjectLink> mappedProjectLinks =
1889+
(!SW360Constants.ENABLE_FLEXIBLE_PROJECT_RELEASE_RELATIONSHIP)
1890+
? projectService.createLinkedProjects(project,
1891+
projectService.filterAndSortAttachments(attachmentTypes), true, sw360User)
1892+
: projectService.createLinkedProjectsWithAllReleases(project,
1893+
projectService.filterAndSortAttachments(attachmentTypes), true, sw360User);
1894+
1895+
if (!projectWithSubProjects) {
1896+
mappedProjectLinks = mappedProjectLinks.stream()
1897+
.filter(projectLink -> projectLink.getId().equals(id)).collect(Collectors.toList());
1898+
}
1899+
1900+
Map<String, Integer> countMap = projectService.storeAttachmentUsageCount(mappedProjectLinks, filter);
1901+
return countMap;
1902+
}
1903+
18981904
@Operation(
18991905
description = "Get all attachmentUsages of the projects.",
19001906
tags = {"Projects"}
@@ -1962,12 +1968,27 @@ public ResponseEntity attachmentUsages(
19621968
}
19631969
}
19641970

1965-
List<Map<String, Object>> releaseObjMap = getReleaseObjectMapper(releaseList);
1971+
Collection<AttachmentType> attachmentTypes;
1972+
UsageData type;
1973+
List<Map<String, Object>> releaseObjMap = new ArrayList<>();
1974+
if ("withCliAttachment".equalsIgnoreCase(filter)) {
1975+
attachmentTypes = SW360Constants.LICENSE_INFO_ATTACHMENT_TYPES;
1976+
type = UsageData.licenseInfo(new LicenseInfoUsage(Sets.newHashSet()));
1977+
Map<String, Integer> count = countMap(attachmentTypes, type, sw360Project, sw360User, id);
1978+
releaseObjMap = getReleaseObjectMapper(releaseList, count);
1979+
} else if ("withSourceAttachment".equalsIgnoreCase(filter)) {
1980+
attachmentTypes = SW360Constants.SOURCE_CODE_ATTACHMENT_TYPES;
1981+
type = UsageData.sourcePackage(new SourcePackageUsage());
1982+
Map<String, Integer> count = countMap(attachmentTypes, type, sw360Project, sw360User, id);
1983+
releaseObjMap = getReleaseObjectMapper(releaseList, count);
1984+
} else {
1985+
releaseObjMap = getReleaseObjectMapper(releaseList, null);
1986+
}
19661987
HalResource userHalResource = attachmentUsageReleases(sw360Project, releaseObjMap, listOfAttachmentUsages);
19671988
return new ResponseEntity<>(userHalResource, HttpStatus.OK);
19681989
}
19691990

1970-
private List<Map<String, Object>> getReleaseObjectMapper(List<EntityModel<Release>> releaseList) {
1991+
private List<Map<String, Object>> getReleaseObjectMapper(List<EntityModel<Release>> releaseList, Map<String, Integer> count) {
19711992
ObjectMapper oMapper = new ObjectMapper();
19721993
List<Map<String, Object>> modifiedList = new ArrayList<>();
19731994
for (EntityModel<Release> rel : releaseList) {
@@ -1983,6 +2004,7 @@ private List<Map<String, Object>> getReleaseObjectMapper(List<EntityModel<Releas
19832004
final ImmutableSet<String> fieldsToKeep = ImmutableSet.of("name", "version", "componentType", "clearingState", ATTACHMENTS);
19842005
Map<String, Object> valueToKeep = new LinkedHashMap<>();
19852006
Link releaseLink = null;
2007+
Integer attachmentCount = 0;
19862008
if (relMap != null) {
19872009
for (Map.Entry<String, Object> entry : relMap.entrySet()) {
19882010
if (entry != null && entry.getKey().equals("id")) {
@@ -2006,6 +2028,16 @@ private List<Map<String, Object>> getReleaseObjectMapper(List<EntityModel<Releas
20062028
map.put("checkedTeam", att.get("checkedTeam"));
20072029
map.put("checkedComment", att.get("checkedComment"));
20082030
map.put("checkedOn", att.get("checkedOn"));
2031+
if (count != null) {
2032+
for (Map.Entry<String, Integer> entryMap : count.entrySet()) {
2033+
String key = entryMap.getKey();
2034+
if (key.contains((CharSequence) att.get("attachmentContentId"))) {
2035+
attachmentCount = entryMap.getValue();
2036+
break;
2037+
}
2038+
}
2039+
map.put("attachmentUsageCount",attachmentCount);
2040+
}
20092041
attList.add(map);
20102042
}
20112043
valueToKeep.put(entry.getKey(), attList);
@@ -2078,7 +2110,7 @@ public List<Release> filterReleases(User sw360User, String filter, Set<String> r
20782110
final Release sw360Release = releaseService.getReleaseForUserById(relId, sw360User);
20792111
releaseService.setComponentDependentFieldsInRelease(sw360Release, sw360User);
20802112
List<Attachment> cliAttachments = sw360Release.getAttachments().stream()
2081-
.filter(attachment -> attachment.getAttachmentType() == AttachmentType.COMPONENT_LICENSE_INFO_XML)
2113+
.filter(attachment -> attachment.getAttachmentType() == AttachmentType.COMPONENT_LICENSE_INFO_XML || attachment.getAttachmentType() == AttachmentType.COMPONENT_LICENSE_INFO_COMBINED)
20822114
.collect(Collectors.toList());
20832115
Set<Attachment> cliAttachmentsSet = new HashSet<>(cliAttachments);
20842116
sw360Release.setAttachments(cliAttachmentsSet);

rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/project/Sw360ProjectService.java

+48
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,48 @@ public Function<ProjectLink, ProjectLink> filterAndSortAttachments(Collection<At
959959
.collect(Collectors.toList())));
960960
}
961961

962+
public Map<String, Integer> storeAttachmentUsageCount(List<ProjectLink> mappedProjectLinks, UsageData filter) throws TException {
963+
try {
964+
ThriftClients thriftClients = new ThriftClients();
965+
AttachmentService.Iface attachmentClient = thriftClients.makeAttachmentClient();
966+
Map<Source, Set<String>> containedAttachments = extractContainedAttachments(mappedProjectLinks);
967+
Map<Map<Source, String>, Integer> attachmentUsages = attachmentClient.getAttachmentUsageCount(containedAttachments,
968+
filter);
969+
Map<String, Integer> countMap = attachmentUsages.entrySet().stream().collect(Collectors.toMap(entry -> {
970+
Entry<Source, String> key = entry.getKey().entrySet().iterator().next();
971+
return key.getKey().getFieldValue() + "_" + key.getValue();
972+
}, Entry::getValue));
973+
return countMap;
974+
} catch (TException e) {
975+
log.error(e.getMessage());
976+
return Collections.emptyMap();
977+
}
978+
}
979+
980+
/**
981+
* Walks through a list of project links and extracts all release attachments
982+
* with their owner. The returned map is a mapping from a release to its
983+
* attachment content ids.
984+
*
985+
* @param projectLinks
986+
* list of project links to walk through
987+
*
988+
* @return map of releases and their attachment content ids
989+
*/
990+
public static Map<Source, Set<String>> extractContainedAttachments(Collection<ProjectLink> projectLinks) {
991+
Map<Source, Set<String>> attachments = Maps.newHashMap();
992+
993+
for (ProjectLink projectLink : projectLinks) {
994+
for (ReleaseLink releaseLink : projectLink.linkedReleases) {
995+
Set<String> attachmentIds = attachments.getOrDefault(Source.releaseId(releaseLink.getId()), Sets.newHashSet());
996+
attachmentIds.addAll(releaseLink.getAttachments().stream().map(a -> a.getAttachmentContentId()).collect(Collectors.toList()));
997+
attachments.put(Source.releaseId(releaseLink.getId()), attachmentIds);
998+
}
999+
}
1000+
1001+
return attachments;
1002+
}
1003+
9621004
public Function<ProjectLink, ProjectLink> filterAndSortAllAttachments(Collection<AttachmentType> attachmentTypes) {
9631005
Predicate<Attachment> filter = att -> attachmentTypes.contains(att.getAttachmentType());
9641006
return createProjectLinkMapper(rl -> {
@@ -1000,6 +1042,12 @@ public List<ProjectLink> createLinkedProjects(Project project, Function<ProjectL
10001042
return linkedProjects.stream().map(projectLinkMapper).collect(Collectors.toList());
10011043
}
10021044

1045+
protected List<ProjectLink> createLinkedProjectsWithAllReleases(Project project,
1046+
Function<ProjectLink, ProjectLink> projectLinkMapper, boolean deep, User user) {
1047+
final Collection<ProjectLink> linkedProjects = SW360Utils.getLinkedProjectsWithAllReleasesAsFlatList(project, deep, new ThriftClients(), log, user);
1048+
return linkedProjects.stream().map(projectLinkMapper).collect(Collectors.toList());
1049+
}
1050+
10031051
public Set<Release> getReleasesFromProjectIds(List<String> projectIds, boolean transitive, final User sw360User,
10041052
Sw360ReleaseService releaseService) {
10051053
final List<Callable<List<Release>>> callableTasksToGetReleases = new ArrayList<Callable<List<Release>>>();

0 commit comments

Comments
 (0)