diff --git a/pom.xml b/pom.xml index 845b7005a..1fe15bdac 100644 --- a/pom.xml +++ b/pom.xml @@ -15,7 +15,7 @@ com.powsybl powsybl-parent-ws - 19 + 20 @@ -44,7 +44,7 @@ - 31 + 33 1.0.5 0.0.2 1.16.2 @@ -287,7 +287,7 @@ org.wiremock - wiremock + wiremock-jetty12 test diff --git a/src/main/java/org/gridsuite/modification/server/repositories/GeneratorCreationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/GeneratorCreationRepository.java new file mode 100644 index 000000000..4f71b6c46 --- /dev/null +++ b/src/main/java/org/gridsuite/modification/server/repositories/GeneratorCreationRepository.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2024, RTE (http://www.rte-france.com) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +package org.gridsuite.modification.server.repositories; + +import org.gridsuite.modification.server.entities.equipment.creation.GeneratorCreationEntity; +import org.springframework.data.jpa.repository.EntityGraph; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; +import java.util.UUID; + +/** + * @author Seddik Yengui + */ + +public interface GeneratorCreationRepository extends JpaRepository { + @EntityGraph(attributePaths = {"reactiveCapabilityCurvePoints"}, type = EntityGraph.EntityGraphType.LOAD) + List findAllReactiveCapabilityCurvePointsByIdIn(List ids); + + @EntityGraph(attributePaths = {"properties"}, type = EntityGraph.EntityGraphType.LOAD) + List findAllPropertiesByIdIn(List ids); +} diff --git a/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java index 0fcc0a061..2ccbe0a55 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java @@ -7,16 +7,11 @@ package org.gridsuite.modification.server.repositories; import org.gridsuite.modification.server.entities.ModificationEntity; -import org.gridsuite.modification.server.entities.TabularCreationEntity; -import org.gridsuite.modification.server.entities.equipment.creation.GeneratorCreationEntity; -import org.springframework.data.jpa.repository.EntityGraph; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; import java.util.List; -import java.util.Optional; -import java.util.Set; import java.util.UUID; /** @@ -41,17 +36,14 @@ public interface ModificationRepository extends JpaRepository findSubModificationIdsByTabularModificationIdOrderByModificationsOrder(UUID uuid); + @Query(value = "SELECT cast(creations_id AS VARCHAR) FROM tabular_creation_creations WHERE tabular_creation_entity_id = :uuid ORDER BY creations_order", nativeQuery = true) + List findSubModificationIdsByTabularCreationIdOrderByModificationsOrder(UUID uuid); + @Query(value = "SELECT cast(modifications_id AS VARCHAR) FROM tabular_modification_modifications WHERE tabular_modification_entity_id = :uuid", nativeQuery = true) List findSubModificationIdsByTabularModificationId(UUID uuid); @Query(value = "SELECT cast(modification_id AS VARCHAR) FROM composite_modification_sub_modifications WHERE id = :uuid ORDER BY modifications_order", nativeQuery = true) List findModificationIdsByCompositeModificationId(UUID uuid); - @EntityGraph(attributePaths = {"creations", "creations.reactiveCapabilityCurvePoints"}, type = EntityGraph.EntityGraphType.LOAD) - Optional findTabularCreationWithReactiveCapabilityCurvePointsById(UUID id); - - @EntityGraph(attributePaths = {"reactiveCapabilityCurvePoints"}, type = EntityGraph.EntityGraphType.LOAD) - Set findAllCreationsWithReactiveCapabilityCurvePointsByIdIn(List ids); - Integer countByGroupIdAndStashed(UUID groupId, boolean stashed); } diff --git a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java index b7a02ed30..8c156b2cb 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java @@ -9,11 +9,9 @@ import lombok.NonNull; import org.gridsuite.modification.server.ModificationType; import org.gridsuite.modification.server.NetworkModificationException; -import org.gridsuite.modification.server.dto.CompositeModificationInfos; -import org.gridsuite.modification.server.dto.ModificationInfos; -import org.gridsuite.modification.server.dto.ModificationMetadata; -import org.gridsuite.modification.server.dto.TabularModificationInfos; +import org.gridsuite.modification.server.dto.*; import org.gridsuite.modification.server.entities.*; +import org.gridsuite.modification.server.entities.equipment.creation.GeneratorCreationEntity; import org.gridsuite.modification.server.entities.equipment.modification.GeneratorModificationEntity; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; @@ -38,12 +36,18 @@ public class NetworkModificationRepository { private final GeneratorModificationRepository generatorModificationRepository; + private final GeneratorCreationRepository generatorCreationRepository; + private static final String MODIFICATION_NOT_FOUND_MESSAGE = "Modification (%s) not found"; - public NetworkModificationRepository(ModificationGroupRepository modificationGroupRepository, ModificationRepository modificationRepository, GeneratorModificationRepository generatorModificationRepository) { + public NetworkModificationRepository(ModificationGroupRepository modificationGroupRepository, + ModificationRepository modificationRepository, + GeneratorModificationRepository generatorModificationRepository, + GeneratorCreationRepository generatorCreationRepository) { this.modificationGroupRepository = modificationGroupRepository; this.modificationRepository = modificationRepository; this.generatorModificationRepository = generatorModificationRepository; + this.generatorCreationRepository = generatorCreationRepository; } @Transactional // To have the 2 delete in the same transaction (atomic) @@ -251,26 +255,41 @@ public TabularModificationInfos loadTabularModificationSubEntities(ModificationE return tabularModificationEntity.toModificationInfos(); } - public TabularCreationEntity loadTabularCreationSubEntities(ModificationEntity modificationEntity) { + public TabularCreationInfos loadTabularCreationSubEntities(ModificationEntity modificationEntity) { TabularCreationEntity tabularCreationEntity = (TabularCreationEntity) modificationEntity; switch (tabularCreationEntity.getCreationType()) { case GENERATOR_CREATION: - tabularCreationEntity = modificationRepository.findTabularCreationWithReactiveCapabilityCurvePointsById(modificationEntity.getId()).orElseThrow(() -> - new NetworkModificationException(MODIFICATION_NOT_FOUND, String.format(MODIFICATION_NOT_FOUND_MESSAGE, modificationEntity.getId())) - ); - modificationRepository.findAllCreationsWithReactiveCapabilityCurvePointsByIdIn(tabularCreationEntity.getCreations().stream().map(ModificationEntity::getId).toList()); - break; + List subModificationsUuids = modificationRepository.findSubModificationIdsByTabularCreationIdOrderByModificationsOrder(modificationEntity.getId()); + Map generatorCreations = generatorCreationRepository.findAllReactiveCapabilityCurvePointsByIdIn(subModificationsUuids) + .stream() + .collect(Collectors.toMap( + ModificationEntity::getId, + Function.identity() + )); + generatorCreationRepository.findAllPropertiesByIdIn(subModificationsUuids); + List orderedGeneratorCreation = subModificationsUuids + .stream() + .map(generatorCreations::get) + .toList(); + return TabularCreationInfos.builder() + .uuid(tabularCreationEntity.getId()) + .date(tabularCreationEntity.getDate()) + .stashed(tabularCreationEntity.getStashed()) + .activated(tabularCreationEntity.getActivated()) + .creationType(tabularCreationEntity.getCreationType()) + .creations(orderedGeneratorCreation.stream().map(GeneratorCreationEntity::toModificationInfos).map(m -> (ModificationInfos) m).toList()) + .build(); default: break; } - return tabularCreationEntity; + return tabularCreationEntity.toModificationInfos(); } public ModificationInfos getModificationInfos(ModificationEntity modificationEntity) { if (modificationEntity instanceof TabularModificationEntity) { return loadTabularModificationSubEntities(modificationEntity); } else if (modificationEntity instanceof TabularCreationEntity) { - return loadTabularCreationSubEntities(modificationEntity).toModificationInfos(); + return loadTabularCreationSubEntities(modificationEntity); } return modificationEntity.toModificationInfos(); } diff --git a/src/test/java/org/gridsuite/modification/server/modifications/tabularcreations/TabularGeneratorCreationsTest.java b/src/test/java/org/gridsuite/modification/server/modifications/tabularcreations/TabularGeneratorCreationsTest.java index a2b408e08..3eb6548b8 100644 --- a/src/test/java/org/gridsuite/modification/server/modifications/tabularcreations/TabularGeneratorCreationsTest.java +++ b/src/test/java/org/gridsuite/modification/server/modifications/tabularcreations/TabularGeneratorCreationsTest.java @@ -183,7 +183,7 @@ public void testCheckSqlRequestsCount() throws Exception { status().isOk(), content().contentType(MediaType.APPLICATION_JSON)) .andReturn(); // We check that the request count is not dependent on the number of sub creations of the tabular creation (the JPA N+1 problem is correctly solved) - assertSelectCount(9); + assertSelectCount(4); List creations = List.of( GeneratorCreationInfos.builder() @@ -216,7 +216,7 @@ public void testCheckSqlRequestsCount() throws Exception { mockMvc.perform(get("/v1/groups/{groupUuid}/network-modifications", getGroupId())) .andExpect(status().isOk()); // We check that the request count is not dependent on the number of sub creations of the tabular creation (the JPA N+1 problem is correctly solved) - assertSelectCount(13); + assertSelectCount(8); } @Test