From 0314078ed22f00177201072fd363e3e8c3d544e1 Mon Sep 17 00:00:00 2001 From: David BRAQUART Date: Fri, 9 Feb 2024 11:27:48 +0100 Subject: [PATCH] use a map when we got duplicated modifications --- .../server/services/ExploreService.java | 27 +++++++------------ .../services/NetworkModificationService.java | 4 +-- .../gridsuite/explore/server/ExploreTest.java | 9 ++++++- 3 files changed, 20 insertions(+), 20 deletions(-) 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 ae14a838..ba008105 100644 --- a/src/main/java/org/gridsuite/explore/server/services/ExploreService.java +++ b/src/main/java/org/gridsuite/explore/server/services/ExploreService.java @@ -16,8 +16,6 @@ import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; -import java.util.Comparator; -import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.UUID; @@ -258,24 +256,19 @@ public void duplicateParameters(UUID parentParameterId, ParametersType parameter } public void createNetworkModifications(List modificationAttributesList, String userId, UUID parentDirectoryUuid) { - // This is important to sort on uuid, to make sure the input modification list will match the new/cloned modification list - List sortedInputList = modificationAttributesList.stream() - .sorted(Comparator.comparing(ElementAttributes::getElementUuid)) + List existingModificationsUuids = modificationAttributesList.stream() + .map(ElementAttributes::getElementUuid) .toList(); - List existingSortedModificationsUuids = sortedInputList.stream().map(ElementAttributes::getElementUuid).toList(); // create all duplicated modifications - List newSortedModificationsUuids = networkModificationService.createModifications(existingSortedModificationsUuids); - - // Iterate through both collection simultaneously (they have the same order) - Iterator newUuidIterator = newSortedModificationsUuids.iterator(); - Iterator modificationAttributeIterator = sortedInputList.iterator(); - while (newUuidIterator.hasNext() && modificationAttributeIterator.hasNext()) { - final UUID newid = newUuidIterator.next(); - final ElementAttributes attributes = modificationAttributeIterator.next(); - ElementAttributes elementAttributes = new ElementAttributes(newid, attributes.getElementName(), MODIFICATION, - null, userId, 0L, attributes.getDescription()); + Map newModificationsUuids = networkModificationService.createModifications(existingModificationsUuids); + + // create all corresponding directory elements + modificationAttributesList.forEach(m -> { + final UUID newId = newModificationsUuids.get(m.getElementUuid()); + ElementAttributes elementAttributes = new ElementAttributes(newId, m.getElementName(), MODIFICATION, + null, userId, 0L, m.getDescription()); directoryService.createElementWithNewName(elementAttributes, parentDirectoryUuid, userId, true); - } + }); } } diff --git a/src/main/java/org/gridsuite/explore/server/services/NetworkModificationService.java b/src/main/java/org/gridsuite/explore/server/services/NetworkModificationService.java index 5fc555e5..209615d5 100644 --- a/src/main/java/org/gridsuite/explore/server/services/NetworkModificationService.java +++ b/src/main/java/org/gridsuite/explore/server/services/NetworkModificationService.java @@ -49,7 +49,7 @@ public void setNetworkModificationServerBaseUri(String networkModificationServer this.networkModificationServerBaseUri = networkModificationServerBaseUri; } - public List createModifications(List modificationUuids) { + public Map createModifications(List modificationUuids) { String path = UriComponentsBuilder.fromPath(DELIMITER + NETWORK_MODIFICATION_API_VERSION + DELIMITER + NETWORK_MODIFICATIONS_PATH + "/duplicate") .buildAndExpand() .toUriString(); @@ -61,7 +61,7 @@ public List createModifications(List modificationUuids) { } catch (JsonProcessingException e) { throw new UncheckedIOException(e); } - return restTemplate.exchange(networkModificationServerBaseUri + path, HttpMethod.POST, httpEntity, new ParameterizedTypeReference>() { }) + return restTemplate.exchange(networkModificationServerBaseUri + path, HttpMethod.POST, httpEntity, new ParameterizedTypeReference>() { }) .getBody(); } diff --git a/src/test/java/org/gridsuite/explore/server/ExploreTest.java b/src/test/java/org/gridsuite/explore/server/ExploreTest.java index 4284cbcb..345508cf 100644 --- a/src/test/java/org/gridsuite/explore/server/ExploreTest.java +++ b/src/test/java/org/gridsuite/explore/server/ExploreTest.java @@ -144,7 +144,7 @@ public void setup() throws IOException { String caseInfosAttributesAsString = mapper.writeValueAsString(List.of(caseSpecificMetadata)); String modificationElementAttributesAsString = mapper.writeValueAsString(new ElementAttributes(MODIFICATION_UUID, "one modif", "MODIFICATION", new AccessRightsAttributes(true), USER1, 0L, null)); String modificationInfosAttributesAsString = mapper.writeValueAsString(List.of(modificationSpecificMetadata)); - String modificationIdsAsString = mapper.writeValueAsString(List.of(MODIFICATION_UUID)); + String modificationIdsAsString = mapper.writeValueAsString(Map.of(MODIFICATION_UUID, MODIFICATION_UUID)); final Dispatcher dispatcher = new Dispatcher() { @SneakyThrows @@ -471,6 +471,13 @@ public void deleteElementInvalidType(UUID elementUUid) throws Exception { @Test public void testDeleteElement() throws Exception { + deleteElement(FILTER_UUID); + deleteElement(PRIVATE_STUDY_UUID); + deleteElement(CONTINGENCY_LIST_UUID); + deleteElementInvalidType(INVALID_ELEMENT_UUID); + deleteElement(PARENT_DIRECTORY_UUID); + deleteElement(CASE_UUID); + deleteElement(PARAMETERS_UUID); deleteElement(MODIFICATION_UUID); }