From d0c0e133036d4551242b4f51f63563fa874641e8 Mon Sep 17 00:00:00 2001 From: AAJELLAL <128486125+AAJELLAL@users.noreply.github.com> Date: Fri, 19 Jul 2024 16:10:46 +0200 Subject: [PATCH] Remove all references to a specific element type. (#94) Signed-off-by: AAJELLAL --- .../explore/server/ExploreController.java | 28 ++++++++++++ .../server/services/DirectoryService.java | 16 ++++++- .../server/services/ExploreService.java | 22 ++++++++++ .../explore/server/services/StudyService.java | 12 ++++++ .../gridsuite/explore/server/ExploreTest.java | 43 +++++++++++++++++++ 5 files changed, 120 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/gridsuite/explore/server/ExploreController.java b/src/main/java/org/gridsuite/explore/server/ExploreController.java index 4e14fde..f92f045 100644 --- a/src/main/java/org/gridsuite/explore/server/ExploreController.java +++ b/src/main/java/org/gridsuite/explore/server/ExploreController.java @@ -326,4 +326,32 @@ public ResponseEntity duplicateNetworkModifications(@RequestParam("duplica exploreService.duplicateNetworkModifications(networkModificationId, targetDirectoryId, userId); return ResponseEntity.ok().build(); } + + @PutMapping(value = "/explore/elements/{id}", consumes = MediaType.APPLICATION_JSON_VALUE) + @Operation(summary = "Modify an element") + @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The element has been modified successfully")}) + public ResponseEntity updateElement( + @PathVariable UUID id, + @RequestBody ElementAttributes elementAttributes, + @RequestHeader("userId") String userId) { + + exploreService.updateElement(id, elementAttributes, userId); + return ResponseEntity.ok().build(); + } + + @PutMapping(value = "/explore/elements", params = "targetDirectoryUuid") + @Operation(summary = "Move elements within directory tree") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "Elements was successfully updated"), + @ApiResponse(responseCode = "404", description = "The elements or the targeted directory was not found"), + @ApiResponse(responseCode = "403", description = "Not authorized execute this update") + }) + public ResponseEntity moveElementsDirectory( + @RequestParam UUID targetDirectoryUuid, + @RequestBody List elementsUuids, + @RequestHeader("userId") String userId) { + exploreService.moveElementsDirectory(elementsUuids, targetDirectoryUuid, userId); + return ResponseEntity.ok().build(); + } + } diff --git a/src/main/java/org/gridsuite/explore/server/services/DirectoryService.java b/src/main/java/org/gridsuite/explore/server/services/DirectoryService.java index 8dcbeb9..8f7d3ef 100644 --- a/src/main/java/org/gridsuite/explore/server/services/DirectoryService.java +++ b/src/main/java/org/gridsuite/explore/server/services/DirectoryService.java @@ -45,6 +45,7 @@ public class DirectoryService implements IDirectoryElementsService { private static final String PARAM_IDS = "ids"; private static final String PARAM_FOR_DELETION = "forDeletion"; + private static final String PARAM_TARGET_DIRECTORY_UUID = "targetDirectoryUuid"; private final Map genericServices; private final RestTemplate restTemplate; @@ -174,7 +175,7 @@ public ElementAttributes getElementInfos(UUID elementUuid) { .getBody(); } - private List getElementsInfos(List elementsUuids, List elementTypes) { + public List getElementsInfos(List elementsUuids, List elementTypes) { var ids = elementsUuids.stream().map(UUID::toString).collect(Collectors.joining(",")); String path = UriComponentsBuilder.fromPath(ELEMENTS_SERVER_ROOT_PATH).toUriString() + "?ids=" + ids; @@ -281,4 +282,17 @@ public void delete(UUID id, String userId) { List elementAttributesList = getDirectoryElements(id, userId); elementAttributesList.forEach(elementAttributes -> deleteElement(elementAttributes.getElementUuid(), userId)); } + + public void moveElementsDirectory(List elementsUuids, UUID targetDirectoryUuid, String userId) { + String path = UriComponentsBuilder + .fromPath(ELEMENTS_SERVER_ROOT_PATH) + .queryParam(PARAM_TARGET_DIRECTORY_UUID, targetDirectoryUuid) + .toUriString(); + HttpHeaders headers = new HttpHeaders(); + headers.set(HEADER_USER_ID, userId); + headers.setContentType(MediaType.APPLICATION_JSON); + + HttpEntity> httpEntity = new HttpEntity<>(elementsUuids, headers); + restTemplate.exchange(directoryServerBaseUri + path, HttpMethod.PUT, httpEntity, Void.class); + } } diff --git a/src/main/java/org/gridsuite/explore/server/services/ExploreService.java b/src/main/java/org/gridsuite/explore/server/services/ExploreService.java index e0e8bdf..21f4690 100644 --- a/src/main/java/org/gridsuite/explore/server/services/ExploreService.java +++ b/src/main/java/org/gridsuite/explore/server/services/ExploreService.java @@ -277,4 +277,26 @@ public void assertCanCreateCase(String userId) { } } } + + public void updateElement(UUID id, ElementAttributes elementAttributes, String userId) { + directoryService.updateElement(id, elementAttributes, userId); + ElementAttributes elementsInfos = directoryService.getElementInfos(id); + // send notification if the study name was updated + notifyStudyUpdate(elementsInfos, userId); + } + + public void moveElementsDirectory(List elementsUuids, UUID targetDirectoryUuid, String userId) { + directoryService.moveElementsDirectory(elementsUuids, targetDirectoryUuid, userId); + //send notification to all studies + List elementsAttributes = directoryService.getElementsInfos(elementsUuids, null); + elementsAttributes.forEach(elementAttributes -> notifyStudyUpdate(elementAttributes, userId)); + + } + + private void notifyStudyUpdate(ElementAttributes element, String userId) { + if (STUDY.equals(element.getType())) { + studyService.notifyStudyUpdate(element.getElementUuid(), userId); + } + } + } diff --git a/src/main/java/org/gridsuite/explore/server/services/StudyService.java b/src/main/java/org/gridsuite/explore/server/services/StudyService.java index 9816bc1..fea4947 100644 --- a/src/main/java/org/gridsuite/explore/server/services/StudyService.java +++ b/src/main/java/org/gridsuite/explore/server/services/StudyService.java @@ -24,6 +24,7 @@ public class StudyService implements IDirectoryElementsService { private static final String STUDY_SERVER_API_VERSION = "v1"; private static final String DELIMITER = "/"; + private static final String NOTIFICATION_TYPE_METADATA_UPDATED = "metadata_updated"; private final RestTemplate restTemplate; private String studyServerBaseUri; @@ -91,4 +92,15 @@ private HttpHeaders getHeaders(String userId) { headers.add(HEADER_USER_ID, userId); return headers; } + + public ResponseEntity notifyStudyUpdate(UUID studyUuid, String userId) { + String path = UriComponentsBuilder.fromPath(DELIMITER + STUDY_SERVER_API_VERSION + + "/studies/{studyUuid}/notification?type={metadata_updated}") + .buildAndExpand(studyUuid, NOTIFICATION_TYPE_METADATA_UPDATED) + .toUriString(); + + HttpHeaders headers = new HttpHeaders(); + headers.set(HEADER_USER_ID, userId); + return restTemplate.exchange(studyServerBaseUri + path, HttpMethod.POST, new HttpEntity<>(headers), Void.class); + } } diff --git a/src/test/java/org/gridsuite/explore/server/ExploreTest.java b/src/test/java/org/gridsuite/explore/server/ExploreTest.java index 1098de0..a6d29e9 100644 --- a/src/test/java/org/gridsuite/explore/server/ExploreTest.java +++ b/src/test/java/org/gridsuite/explore/server/ExploreTest.java @@ -95,6 +95,7 @@ public class ExploreTest { private final Map modificationSpecificMetadata = Map.of("id", MODIFICATION_UUID, "type", "LOAD_MODIFICATION"); private static final UUID SCRIPT_ID_BASE_FORM_CONTINGENCY_LIST_UUID = UUID.randomUUID(); + private static final UUID ELEMENT_UUID = UUID.randomUUID(); @Autowired private MockMvc mockMvc; @@ -159,6 +160,9 @@ public void setup() throws IOException { String newFilterUuidAsString = mapper.writeValueAsString(FILTER_COPY_UUID); String newParametersUuidAsString = mapper.writeValueAsString(PARAMETER_COPY_UUID); String newElementUuidAsString = mapper.writeValueAsString(ELEMENT_COPY_UUID); + String newElementAttributesAsString = mapper.writeValueAsString(new ElementAttributes(ELEMENT_UUID, STUDY1, "STUDY", USER1, 0, null)); + String listElementsAsString = "[" + newElementAttributesAsString + "," + publicStudyAttributesAsString + "]"; + final Dispatcher dispatcher = new Dispatcher() { @SneakyThrows @Override @@ -168,6 +172,8 @@ public MockResponse dispatch(RecordedRequest request) { if (path.matches("/v1/studies/cases/" + NON_EXISTING_CASE_UUID + ".*") && "POST".equals(request.getMethod())) { return new MockResponse().setResponseCode(404); + } else if (path.matches("/v1/studies/.*/notification?type=metadata_updated") && "POST".equals(request.getMethod())) { + return new MockResponse().setResponseCode(200); } else if (path.matches("/v1/studies\\?duplicateFrom=" + PUBLIC_STUDY_UUID + ".*") && "POST".equals(request.getMethod())) { return new MockResponse().setBody(newStudyUuidAsString).setResponseCode(200) .addHeader("Content-Type", "application/json; charset=utf-8"); @@ -241,6 +247,15 @@ public MockResponse dispatch(RecordedRequest request) { } else if (path.matches("/v1/elements\\?ids=" + FILTER_UUID + "," + PRIVATE_STUDY_UUID + "," + CONTINGENCY_LIST_UUID) && "GET".equals(request.getMethod())) { return new MockResponse().setBody(listElementsAttributesAsString).setResponseCode(200) .addHeader("Content-Type", "application/json; charset=utf-8"); + } else if (path.matches("/v1/elements\\?ids=" + ELEMENT_UUID + "," + PUBLIC_STUDY_UUID) && "GET".equals(request.getMethod())) { + return new MockResponse().setBody(listElementsAsString).setResponseCode(200) + .addHeader("Content-Type", "application/json; charset=utf-8"); + } else if (path.matches("/v1/elements/" + ELEMENT_UUID) && "PUT".equals(request.getMethod())) { + return new MockResponse().setResponseCode(200) + .addHeader("Content-Type", "application/json; charset=utf-8"); + } else if (path.matches("/v1/elements\\?targetDirectoryUuid=" + PARENT_DIRECTORY_UUID) && "PUT".equals(request.getMethod())) { + return new MockResponse().setResponseCode(200) + .addHeader("Content-Type", "application/json; charset=utf-8"); } else if (path.matches("/v1/elements/.*") && "PUT".equals(request.getMethod())) { return new MockResponse().setBody(newElementUuidAsString).setResponseCode(200) .addHeader("Content-Type", "application/json; charset=utf-8"); @@ -292,6 +307,8 @@ public MockResponse dispatch(RecordedRequest request) { } else if ("GET".equals(request.getMethod())) { if (path.matches("/v1/elements/" + INVALID_ELEMENT_UUID)) { return new MockResponse().setBody(invalidElementAsString).setResponseCode(200).addHeader("Content-Type", "application/json; charset=utf-8"); + } else if (path.matches("/v1/elements/" + ELEMENT_UUID)) { + return new MockResponse().setBody(newElementAttributesAsString).setResponseCode(200).addHeader("Content-Type", "application/json; charset=utf-8"); } else if (path.matches("/v1/directories/" + PARENT_DIRECTORY_UUID + "/elements")) { return new MockResponse().setResponseCode(200).addHeader("Content-Type", "application/json; charset=utf-8"); } else if (path.matches("/v1/elements/" + PARENT_DIRECTORY_UUID)) { @@ -329,6 +346,8 @@ public MockResponse dispatch(RecordedRequest request) { } else if (path.matches("/v1/users/.*/cases/count")) { return new MockResponse().setBody("0").setResponseCode(200) .addHeader("Content-Type", "application/json; charset=utf-8"); + } else if (path.matches("/v1/elements/" + ELEMENT_UUID)) { + return new MockResponse().setBody(invalidElementAsString).setResponseCode(200).addHeader("Content-Type", "application/json; charset=utf-8"); } } else if ("DELETE".equals(request.getMethod())) { if (path.matches("/v1/filters/" + FILTER_UUID)) { @@ -930,4 +949,28 @@ public void testMaxCaseCreationWithRemoteException() throws Exception { .andExpect(status().isBadRequest()); } } + + @Test + public void testUpdateElement() throws Exception { + ElementAttributes elementAttributes = new ElementAttributes(); + elementAttributes.setElementName(STUDY1); + mockMvc.perform(put("/v1/explore/elements/{id}", + ELEMENT_UUID) + .header("userId", USER1) + .contentType(MediaType.APPLICATION_JSON) + .content(mapper.writeValueAsString(elementAttributes)) + ).andExpect(status().isOk()); + } + + @Test + public void testMoveElementsDirectory() throws Exception { + ElementAttributes elementAttributes = new ElementAttributes(); + elementAttributes.setElementName(STUDY1); + mockMvc.perform(put("/v1/explore/elements?targetDirectoryUuid={parentDirectoryUuid}", + PARENT_DIRECTORY_UUID) + .header("userId", USER1) + .contentType(MediaType.APPLICATION_JSON) + .content(mapper.writeValueAsString(List.of(ELEMENT_UUID, PUBLIC_STUDY_UUID))) + ).andExpect(status().isOk()); + } }