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) : modified attachment info in response to the moderation request rest api #2498

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -61,6 +61,11 @@
import org.springframework.web.client.HttpClientErrorException;

import jakarta.servlet.http.HttpServletRequest;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.net.URISyntaxException;
import java.util.*;
import java.util.function.Function;
Expand All @@ -75,6 +80,8 @@
@SecurityRequirement(name = "basic")
public class ModerationRequestController implements RepresentationModelProcessor<RepositoryLinksResource> {

private static final String REVIEWER = "reviewer";
private static final String REQUESTING_USER = "requestingUser";
public static final String MODERATION_REQUEST_URL = "/moderationrequest";

@Autowired
Expand Down Expand Up @@ -124,20 +131,33 @@ public ResponseEntity<CollectionModel<ModerationRequest>> getModerationRequests(
tags = {"Moderation Requests"}
)
@RequestMapping(value = MODERATION_REQUEST_URL + "/{id}", method = RequestMethod.GET)
public ResponseEntity<HalResource<ModerationRequest>> getModerationRequestById(
@Parameter(description = "The id of the moderation request to be retrieved.")
@PathVariable String id
) throws TException {
public ResponseEntity<HalResource<Map<String, Object>>> getModerationRequestById(
@Parameter(description = "The id of the moderation request to be retrieved.") @PathVariable String id)
throws TException {
User sw360User = restControllerHelper.getSw360UserFromAuthentication();

ModerationRequest moderationRequest = filterModerationRequestNoDuplicates(
sw360ModerationRequestService.getModerationRequestById(id));
HalResource<ModerationRequest> halModerationRequest = createHalModerationRequestWithAllDetails(moderationRequest,
sw360User);
Map<String, Object> modObjectMapper = getModObjectMapper(moderationRequest);
Link modLink = linkTo(ReleaseController.class).slash("api/moderationrequest/" + moderationRequest.getId())
.withSelfRel();
Map<String, Object> modLinkMap = new LinkedHashMap<>();
modLinkMap.put("self", modLink);
modObjectMapper.put("_links", modLinkMap);
HalResource<Map<String, Object>> halModerationRequest = createHalModerationRequestWithAllDetails(
modObjectMapper, sw360User);
HttpStatus status = halModerationRequest.getContent() == null ? HttpStatus.NO_CONTENT : HttpStatus.OK;
return new ResponseEntity<>(halModerationRequest, status);
}

private Map<String, Object> getModObjectMapper(ModerationRequest moderationRequest) {
ObjectMapper oMapper = new ObjectMapper();
oMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
oMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
oMapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE); // but only public getters
oMapper.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE); // and none of "is-setters"
return oMapper.convertValue(moderationRequest, Map.class);
}

@Operation(
summary = "Get moderation based on state.",
description = "List all the ModerationRequest visible to the user based on the state.",
Expand Down Expand Up @@ -169,38 +189,35 @@ public ResponseEntity<CollectionModel<ModerationRequest>> getModerationRequestsB
return getModerationResponseEntity(pageable, request, allDetails, modRequestsWithPageData);
}

private @NotNull HalResource<ModerationRequest> createHalModerationRequestWithAllDetails(
ModerationRequest moderationRequest, User sw360User) throws TException {
HalResource<ModerationRequest> halModerationRequest = new HalResource<>(moderationRequest);
User requestingUser = restControllerHelper.getUserByEmail(moderationRequest.getRequestingUser());
restControllerHelper.addEmbeddedUser(halModerationRequest, requestingUser, "requestingUser");

if (CommonUtils.isNotNullEmptyOrWhitespace(moderationRequest.getReviewer())) {
User reviewer = restControllerHelper.getUserByEmail(moderationRequest.getReviewer());
restControllerHelper.addEmbeddedUser(halModerationRequest, reviewer, "reviewer");
private @NotNull HalResource<Map<String, Object>> createHalModerationRequestWithAllDetails(
Map<String, Object> modObjectMapper, User sw360User) throws TException {
HalResource<Map<String, Object>> halModerationRequest = new HalResource<>(modObjectMapper);
User requestingUser = restControllerHelper.getUserByEmail(modObjectMapper.get(REQUESTING_USER).toString());
restControllerHelper.addEmbeddedUser(halModerationRequest, requestingUser, REQUESTING_USER);
if (modObjectMapper.get(REVIEWER) != null
&& CommonUtils.isNotNullEmptyOrWhitespace(modObjectMapper.get(REVIEWER).toString())) {
User reviewer = restControllerHelper.getUserByEmail(modObjectMapper.get(REVIEWER).toString());
restControllerHelper.addEmbeddedUser(halModerationRequest, reviewer, REVIEWER);
}

DocumentType documentType = moderationRequest.getDocumentType();
String documentId = moderationRequest.getDocumentId();
if (documentType.equals(DocumentType.PROJECT)) {
String documentType = modObjectMapper.get("documentType").toString();
String documentId = modObjectMapper.get("documentId").toString();
if (documentType.equals((DocumentType.PROJECT).toString())) {
Project project = projectService.getProjectForUserById(documentId, sw360User);
restControllerHelper.addEmbeddedProject(halModerationRequest, project, true);
} else if (documentType.equals(DocumentType.RELEASE)) {
} else if (documentType.equals((DocumentType.RELEASE).toString())) {
Release release = releaseService.getReleaseForUserById(documentId, sw360User);
restControllerHelper.addEmbeddedRelease(halModerationRequest, release);
} else if (documentType.equals(DocumentType.COMPONENT)) {
} else if (documentType.equals((DocumentType.COMPONENT).toString())) {
Component component = componentService.getComponentForUserById(documentId, sw360User);
restControllerHelper.addEmbeddedComponent(halModerationRequest, component);
}

return halModerationRequest;
}

private @NotNull HalResource<ModerationRequest> createHalModerationRequest(ModerationRequest moderationRequest) {
HalResource<ModerationRequest> halModerationRequest = new HalResource<>(moderationRequest);
User requestingUser = restControllerHelper.getUserByEmail(moderationRequest.getRequestingUser());
restControllerHelper.addEmbeddedUser(halModerationRequest, requestingUser, "requestingUser");

restControllerHelper.addEmbeddedUser(halModerationRequest, requestingUser, REQUESTING_USER);
return halModerationRequest;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ public void should_document_get_moderationrequest() throws Exception {
),
responseFields(
fieldWithPath("id").description("The id of the moderation request."),
fieldWithPath("type").description("The type of the moderation request."),
fieldWithPath("timestamp").description("Timestamp (in unix epoch) when the request was created."),
fieldWithPath("timestampOfDecision").description("Timestamp (in unix epoch) when the decision on the request was made."),
fieldWithPath("documentId").description("The ID of the document for which the moderation request was made."),
Expand All @@ -320,22 +321,22 @@ public void should_document_get_moderationrequest() throws Exception {
subsectionWithPath("moderators.[]").description("List of users who are marked as moderators for the request."),
fieldWithPath("documentName").description("Name of the document for which the request was created."),
fieldWithPath("moderationState").description("The state of the moderation request. Possible values are: " + Arrays.asList(ModerationState.values())),
fieldWithPath("reviewer").description("User who is currently assigned as the reviewer of the request."),
fieldWithPath("reviewer").description("User who is currently assigned as the reviewer of the request.").optional().type(JsonFieldType.OBJECT),
fieldWithPath("requestDocumentDelete").description(""),
fieldWithPath("requestingUserDepartment").description("The Business Unit / Group of the Project, for which clearing request is created."),
fieldWithPath("componentType").description("Type of the component for which the moderation request is created. Possible values are: " + Arrays.asList(ComponentType.values())),
fieldWithPath("commentRequestingUser").description("The comment from requesting user."),
fieldWithPath("commentDecisionModerator").description("The comment from decision making user."),
fieldWithPath("commentDecisionModerator").description("The comment from decision making user.").optional().type(JsonFieldType.OBJECT),
subsectionWithPath("componentAdditions").description("Information which user wants to add for the component (if `documentType` is `COMPONENT`).").optional().type(JsonFieldType.VARIES),
subsectionWithPath("releaseAdditions").description("Information which user wants to add for the release (if `documentType` is `RELEASE`).").optional().type(JsonFieldType.OBJECT),
subsectionWithPath("projectAdditions").description("Information which user wants to add for the project (if `documentType` is `PROJECT`).").optional().type(JsonFieldType.OBJECT),
subsectionWithPath("licenseAdditions").description("Information which user wants to add for the license (if `documentType` is `LICENSE`).").optional().type(JsonFieldType.OBJECT),
fieldWithPath("user").description(""),
fieldWithPath("user").description("").optional().type(JsonFieldType.OBJECT),
subsectionWithPath("componentDeletions").description("Information which user wants to remove for the component (if `documentType` is `COMPONENT`).").optional().type(JsonFieldType.OBJECT),
subsectionWithPath("releaseDeletions").description("Information which user wants to remove for the release (if `documentType` is `RELEASE`).").optional().type(JsonFieldType.OBJECT),
subsectionWithPath("projectDeletions").description("Information which user wants to remove for the project (if `documentType` is `PROJECT`).").optional().type(JsonFieldType.OBJECT),
subsectionWithPath("licenseDeletions").description("Information which user wants to remove for the license (if `documentType` is `LICENSE`).").optional().type(JsonFieldType.OBJECT),
fieldWithPath("moderatorsSize").description("Number of users in moderators list."),
fieldWithPath("moderatorsSize").description("Number of users in moderators list.").optional().type(JsonFieldType.OBJECT),
subsectionWithPath("_embedded.requestingUser").description("<<resources-users, User>> who created the ModerationRequest."),
subsectionWithPath("_embedded.sw360:project").description("<<resources-projects, Project>> for which the ModerationRequest was created (if `documentType` is `PROJECT`).").optional().type(JsonFieldType.OBJECT),
subsectionWithPath("_embedded.sw360:releases").description("<<resources-releases, Release>> for which the ModerationRequest was created (if `documentType` is `RELEASE`).").optional().type(JsonFieldType.ARRAY),
Expand Down