diff --git a/docs/Create-And-Publish-Virtual-Study.md b/docs/Create-And-Publish-Virtual-Study.md index 1a8cc0e2169..14ebbf9c438 100644 --- a/docs/Create-And-Publish-Virtual-Study.md +++ b/docs/Create-And-Publish-Virtual-Study.md @@ -49,7 +49,8 @@ Specifying the `pmid` enables a link to the PubMed page of the study. ## Un-publish Virtual Study To un-publish a virtual study, you need to supply the publisher API key in the `X-PUBLISHER-API-KEY` header. -After un-publishing, the virtual study is still accessible by the link, but it will be unlisted from the list of public virtual studies and not shown on the landing page anymore. +After un-publishing, virtual study will no longer be displayed in the `Public Virtual Studies` section on the landing page. +However, it reappears in the `My Virtual Studies` section for the owner. Here is the command to un-publish a virtual study: ```shell diff --git a/src/main/java/org/cbioportal/web/PublicVirtualStudiesController.java b/src/main/java/org/cbioportal/web/PublicVirtualStudiesController.java index cc8fe356748..c48ac446041 100644 --- a/src/main/java/org/cbioportal/web/PublicVirtualStudiesController.java +++ b/src/main/java/org/cbioportal/web/PublicVirtualStudiesController.java @@ -24,6 +24,7 @@ import org.springframework.web.bind.annotation.RequestParam; import java.util.List; +import java.util.NoSuchElementException; import java.util.Set; @Controller @@ -72,15 +73,21 @@ public ResponseEntity publishVirtualStudy( @DeleteMapping("/{id}") @ApiResponse(responseCode = "200", description = "OK") - public ResponseEntity retractVirtualStudy( + public ResponseEntity unPublishVirtualStudy( @PathVariable String id, @RequestHeader(value = "X-PUBLISHER-API-KEY") String providedPublisherApiKey ) { ensureProvidedPublisherApiKeyCorrect(providedPublisherApiKey); - sessionServiceRequestHandler.softRemoveVirtualStudy(id); + unPublishVirtualStudy(id); return ResponseEntity.ok().build(); } + /** + * Publishes virtual study optionally updating metadata fields + * @param id - id of public virtual study to publish + * @param typeOfCancerId - if specified (not null) update type of cancer of published virtual study + * @param pmid - if specified (not null) update PubMed ID of published virtual study + */ private void publishVirtualStudy(String id, String typeOfCancerId, String pmid) { VirtualStudy virtualStudyDataToPublish = sessionServiceRequestHandler.getVirtualStudyById(id); VirtualStudyData virtualStudyData = virtualStudyDataToPublish.getData(); @@ -89,6 +96,24 @@ private void publishVirtualStudy(String id, String typeOfCancerId, String pmid) sessionServiceRequestHandler.updateVirtualStudy(virtualStudyDataToPublish); } + /** + * Un-publish virtual study + * @param id - id of public virtual study to un-publish + */ + private void unPublishVirtualStudy(String id) { + VirtualStudy virtualStudyToUnPublish = sessionServiceRequestHandler.getVirtualStudyById(id); + if (virtualStudyToUnPublish == null) { + throw new NoSuchElementException("The virtual study with id=" + id + " has not been found in the public list."); + } + VirtualStudyData virtualStudyData = virtualStudyToUnPublish.getData(); + Set users = virtualStudyData.getUsers(); + if (users == null || users.isEmpty() || !users.contains(ALL_USERS)) { + throw new NoSuchElementException("The virtual study with id=" + id + " has not been found in the public list."); + } + virtualStudyData.setUsers(Set.of(virtualStudyData.getOwner())); + sessionServiceRequestHandler.updateVirtualStudy(virtualStudyToUnPublish); + } + private void ensureProvidedPublisherApiKeyCorrect(String providedPublisherApiKey) { if (requiredPublisherApiKey.isBlank() || !requiredPublisherApiKey.equals(providedPublisherApiKey)) { diff --git a/src/main/java/org/cbioportal/web/error/GlobalExceptionHandler.java b/src/main/java/org/cbioportal/web/error/GlobalExceptionHandler.java index 9ddb93aaf70..1c24d60c431 100644 --- a/src/main/java/org/cbioportal/web/error/GlobalExceptionHandler.java +++ b/src/main/java/org/cbioportal/web/error/GlobalExceptionHandler.java @@ -18,6 +18,7 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import java.util.Iterator; +import java.util.NoSuchElementException; // TODO @@ -207,4 +208,9 @@ public ResponseEntity handleBadSqlGrammar(BadSqlGrammarException HttpStatus.INTERNAL_SERVER_ERROR ); } + + @ExceptionHandler(NoSuchElementException.class) + public ResponseEntity handleNoSuchElementException(NoSuchElementException ex) { + return new ResponseEntity<>(new ErrorResponse(ex.getMessage()), HttpStatus.NOT_FOUND); + } } diff --git a/src/test/java/org/cbioportal/test/integration/PublicVirtualStudiesIntegrationTest.java b/src/test/java/org/cbioportal/test/integration/PublicVirtualStudiesIntegrationTest.java index 01d54b41cee..e2c74996b75 100644 --- a/src/test/java/org/cbioportal/test/integration/PublicVirtualStudiesIntegrationTest.java +++ b/src/test/java/org/cbioportal/test/integration/PublicVirtualStudiesIntegrationTest.java @@ -109,6 +109,16 @@ public void test2CreateVirtualStudy() { virtualStudyId = savedVs.getId(); } + @Test + public void test2_1UnPublishVirtualStudyFails() { + ResponseEntity response = restTemplate.exchange( + CBIO_URL + "/api/public_virtual_studies/" + virtualStudyId, + HttpMethod.DELETE, + new HttpEntity<>(null, validKeyContainingHeaders), + Object.class); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); + } + @Test public void test3PublishVirtualStudy() { String url = CBIO_URL + "/api/public_virtual_studies/" + virtualStudyId + "?typeOfCancerId=acc&pmid=12345"; @@ -158,28 +168,28 @@ public void test4ListJustPublishedStudy() { @Test public void test5UnpublishVirtualStudy() { - ResponseEntity response5 = restTemplate.exchange( + ResponseEntity response5 = restTemplate.exchange( CBIO_URL + "/api/public_virtual_studies/" + virtualStudyId, HttpMethod.DELETE, null, - Object.class); + Void.class); assertThat(response5.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST); response5 = restTemplate.exchange( CBIO_URL + "/api/public_virtual_studies/" + virtualStudyId, HttpMethod.DELETE, new HttpEntity<>(null, invalidKeyContainingHeaders), - Object.class); + Void.class); assertThat(response5.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); response5 = restTemplate.exchange( CBIO_URL + "/api/public_virtual_studies/" + virtualStudyId, HttpMethod.DELETE, new HttpEntity<>(null, validKeyContainingHeaders), - Object.class); + Void.class); assertThat(response5.getStatusCode().is2xxSuccessful()).isTrue(); } - + @Test public void test6NoPublicVirtualStudiesAfterRemoval() { ResponseEntity> response6 = restTemplate.exchange( @@ -192,6 +202,18 @@ public void test6NoPublicVirtualStudiesAfterRemoval() { assertThat(response6.getBody()).isEmpty(); } + @Test + public void test7UnpublishedVirtualStudyExists() { + ResponseEntity response = restTemplate.exchange( + CBIO_URL + "/api/session/virtual_study/" + virtualStudyId, + HttpMethod.GET, + null, + VirtualStudy.class); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + VirtualStudy body = response.getBody(); + assertThat(body).isNotNull(); + } + static VirtualStudyData createTestVsData() { VirtualStudyData data = new VirtualStudyData(); data.setName("test virtual study name");