Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rest) : attachment usage type fix in response #2326

Merged
merged 1 commit into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ public Release convertToEmbeddedReleaseAttachments(Release release) {
return embeddedRelease;
}

public void addEmbeddedProjectAttachmentUsage(HalResource halResource, List<EntityModel<Release>> releases, List<Map<String, Object>> attachmentUsageMap) {
public void addEmbeddedProjectAttachmentUsage(HalResource halResource, List<Map<String, Object>> releases, List<Map<String, Object>> attachmentUsageMap) {
halResource.addEmbeddedResource("sw360:release", releases);
halResource.addEmbeddedResource("sw360:attachmentUsages", attachmentUsageMap);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
import org.eclipse.sw360.rest.resourceserver.licenseinfo.Sw360LicenseInfoService;
import org.eclipse.sw360.rest.resourceserver.packages.PackageController;
import org.eclipse.sw360.rest.resourceserver.packages.SW360PackageService;
import org.eclipse.sw360.rest.resourceserver.release.ReleaseController;
import org.eclipse.sw360.rest.resourceserver.release.Sw360ReleaseService;
import org.eclipse.sw360.rest.resourceserver.user.Sw360UserService;
import org.eclipse.sw360.rest.resourceserver.vulnerability.Sw360VulnerabilityService;
Expand Down Expand Up @@ -136,6 +137,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.InvalidPropertiesFormatException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -159,6 +161,9 @@
@RestController
@SecurityRequirement(name = "tokenAuth")
public class ProjectController implements RepresentationModelProcessor<RepositoryLinksResource> {
private static final String CREATED_BY = "createdBy";
private static final String ATTACHMENT_TYPE = "attachmentType";
private static final String ATTACHMENTS = "attachments";
public static final String PROJECTS_URL = "/projects";
public static final String SW360_ATTACHMENT_USAGES = "sw360:attachmentUsages";
private static final Logger log = LogManager.getLogger(ProjectController.class);
Expand Down Expand Up @@ -343,7 +348,7 @@ private ResponseEntity<CollectionModel<EntityModel<Project>>> getProjectResponse
public ResponseEntity<CollectionModel<EntityModel<Project>>> getProjectsFilteredForUser(
Pageable pageable,
@Parameter(description = "Projects with current user as creator.")
@RequestParam(value = "createdBy", required = false, defaultValue = "true") boolean createdBy,
@RequestParam(value = CREATED_BY, required = false, defaultValue = "true") boolean createdBy,
@Parameter(description = "Projects with current user as moderator.")
@RequestParam(value = "moderator", required = false, defaultValue = "true") boolean moderator,
@Parameter(description = "Projects with current user as contributor.")
Expand Down Expand Up @@ -1544,11 +1549,67 @@ public ResponseEntity attachmentUsages(@Parameter(description = "Project ID.") @
}
}

HalResource userHalResource = attachmentUsageReleases(sw360Project, sw360User, releaseList, listOfAttachmentUsages);
List<Map<String, Object>> releaseObjMap = getReleaseObjectMapper(releaseList);
HalResource userHalResource = attachmentUsageReleases(sw360Project, releaseObjMap, listOfAttachmentUsages);
return new ResponseEntity<>(userHalResource, HttpStatus.OK);
}

private HalResource attachmentUsageReleases(Project sw360Project, User sw360User, List<EntityModel<Release>> releases, List<Map<String, Object>> attachmentUsageMap)
private List<Map<String, Object>> getReleaseObjectMapper(List<EntityModel<Release>> releaseList) {
ObjectMapper oMapper = new ObjectMapper();
List<Map<String, Object>> modifiedList = new ArrayList<>();
for (EntityModel<Release> rel : releaseList) {
Map<String, Object> relMap = (Map<String, Object>) oMapper.convertValue(rel, Map.class);
if (relMap.get(ATTACHMENTS) != null) {
for (Object attach : (ArrayList<?>) relMap.get(ATTACHMENTS)) {
Map<String, Object> attachmentMap = (Map<String, Object>) attach;
AttachmentType type = AttachmentType.valueOf((String) attachmentMap.get(ATTACHMENT_TYPE));
attachmentMap.replace(ATTACHMENT_TYPE, attachmentMap.get(ATTACHMENT_TYPE),
ThriftEnumUtils.MAP_ATTACHMENT_TYPE_SHORT_STRING.get(type));
}
}
final ImmutableSet<String> fieldsToKeep = ImmutableSet.of("name", "version", ATTACHMENTS);
Map<String, Object> valueToKeep = new LinkedHashMap<>();
Link releaseLink = null;
if (relMap != null) {
for (Map.Entry<String, Object> entry : relMap.entrySet()) {
if (entry != null && entry.getKey().equals("id")) {
releaseLink = linkTo(ReleaseController.class).slash("api/releases/" + entry.getValue())
.withSelfRel();
} else if (entry != null && fieldsToKeep.contains(entry.getKey())) {
if (entry.getKey().equals(ATTACHMENTS) && null != entry.getValue()) {
List<Map<String, Object>> attList = new ArrayList<>();
for (LinkedHashMap att : ((List<LinkedHashMap>) entry.getValue())) {
Map<String, Object> map = new LinkedHashMap<>();
map.put("attachmentContentId", att.get("attachmentContentId"));
map.put("filename", att.get("filename"));
map.put("sha1", att.get("sha1"));
map.put(ATTACHMENT_TYPE, att.get(ATTACHMENT_TYPE));
map.put(CREATED_BY, att.get(CREATED_BY));
map.put("createdTeam", att.get("createdTeam"));
map.put("createdOn", att.get("createdOn"));
map.put("checkStatus", att.get("checkStatus"));
map.put("createdComment", att.get("createdComment"));
map.put("checkedBy", att.get("checkedBy"));
map.put("checkedTeam", att.get("checkedTeam"));
map.put("checkedComment", att.get("checkedComment"));
map.put("checkedOn", att.get("checkedOn"));
attList.add(map);
}
valueToKeep.put(entry.getKey(), attList);
} else {
valueToKeep.put(entry.getKey(), entry.getValue());
}

}
}
}
valueToKeep.put("_links", releaseLink);
modifiedList.add(valueToKeep);
}
return modifiedList;
}

private HalResource attachmentUsageReleases(Project sw360Project, List<Map<String, Object>> releases, List<Map<String, Object>> attachmentUsageMap)
throws TException {
ObjectMapper oMapper = new ObjectMapper();
Map<String, ProjectReleaseRelationship> releaseIdToUsages = sw360Project.getReleaseIdToUsage();
Expand Down Expand Up @@ -1846,7 +1907,7 @@ private HalResource<Project> createHalLicenseClearing(Project sw360Project, List
private HalResource<Project> createHalProject(Project sw360Project, User sw360User) throws TException {
HalResource<Project> halProject = new HalResource<>(sw360Project);
User projectCreator = restControllerHelper.getUserByEmail(sw360Project.getCreatedBy());
restControllerHelper.addEmbeddedUser(halProject, projectCreator, "createdBy");
restControllerHelper.addEmbeddedUser(halProject, projectCreator, CREATED_BY);

Map<String, ProjectReleaseRelationship> releaseIdToUsage = sw360Project.getReleaseIdToUsage();
if (releaseIdToUsage != null) {
Expand Down Expand Up @@ -1959,7 +2020,7 @@ private RequestStatus linkOrUnlinkPackages(String id, Set<String> packagesInRequ

private HalResource<Project> createHalProjectResourceWithAllDetails(Project sw360Project, User sw360User) {
HalResource<Project> halProject = new HalResource<>(sw360Project);
halProject.addEmbeddedResource("createdBy", sw360Project.getCreatedBy());
halProject.addEmbeddedResource(CREATED_BY, sw360Project.getCreatedBy());

Set<String> packageIds = sw360Project.getPackageIds();

Expand Down Expand Up @@ -2135,7 +2196,7 @@ private HalResource<ProjectDTO> createHalProjectDTO(Project sw360Project, User s
HalResource<ProjectDTO> halProject = new HalResource<>(projectDTO);

User projectCreator = restControllerHelper.getUserByEmail(projectDTO.getCreatedBy());
restControllerHelper.addEmbeddedUser(halProject, projectCreator, "createdBy");
restControllerHelper.addEmbeddedUser(halProject, projectCreator, CREATED_BY);

Map<String, ProjectProjectRelationship> linkedProjects = projectDTO.getLinkedProjects();
if (linkedProjects != null) {
Expand Down
Loading