Skip to content

Commit

Permalink
Replace @entitygraph use with casting for springBoot migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Seddik Yengui committed Aug 27, 2024
1 parent 4d9293c commit 7511211
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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 com.powsybl.iidm.network.Generator;
import org.gridsuite.modification.server.entities.equipment.creation.GeneratorCreationEntity;
import org.gridsuite.modification.server.entities.equipment.modification.GeneratorModificationEntity;
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 <seddik.yengui_externe at rte-france.com>
*/

public interface GeneratorCreationRepository extends JpaRepository<GeneratorCreationEntity, UUID> {
@EntityGraph(attributePaths = {"reactiveCapabilityCurvePoints"}, type = EntityGraph.EntityGraphType.LOAD)
List<GeneratorCreationEntity> findAllReactiveCapabilityCurvePointsByIdIn(List<UUID> ids);

@EntityGraph(attributePaths = {"properties"}, type = EntityGraph.EntityGraphType.LOAD)
List<GeneratorCreationEntity> findAllPropertiesByIdIn(List<UUID> ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,5 @@ public interface ModificationRepository extends JpaRepository<ModificationEntity
@Query(value = "SELECT cast(modification_id AS VARCHAR) FROM composite_modification_sub_modifications WHERE id = :uuid ORDER BY modifications_order", nativeQuery = true)
List<UUID> findModificationIdsByCompositeModificationId(UUID uuid);

@EntityGraph(attributePaths = {"creations", "creations.reactiveCapabilityCurvePoints"}, type = EntityGraph.EntityGraphType.LOAD)
Optional<TabularCreationEntity> findTabularCreationWithReactiveCapabilityCurvePointsById(UUID id);

@EntityGraph(attributePaths = {"reactiveCapabilityCurvePoints"}, type = EntityGraph.EntityGraphType.LOAD)
Set<GeneratorCreationEntity> findAllCreationsWithReactiveCapabilityCurvePointsByIdIn(List<UUID> ids);

Integer countByGroupIdAndStashed(UUID groupId, boolean stashed);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -250,26 +254,40 @@ 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<UUID> subModificationsUuids = modificationRepository.findSubModificationIdsByTabularModificationIdOrderByModificationsOrder(modificationEntity.getId());
Map<UUID, GeneratorCreationEntity> generatorCreations = generatorCreationRepository.findAllReactiveCapabilityCurvePointsByIdIn(subModificationsUuids)
.stream()
.collect(Collectors.toMap(
ModificationEntity::getId,
Function.identity()
));
generatorCreationRepository.findAllPropertiesByIdIn(subModificationsUuids);
List<GeneratorCreationEntity> orderedGeneratorCreation = subModificationsUuids
.stream()
.map(generatorCreations::get)
.toList();
return TabularCreationInfos.builder()
.uuid(tabularCreationEntity.getId())
.date(tabularCreationEntity.getDate())
.stashed(tabularCreationEntity.getStashed())
.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();
}
Expand Down

0 comments on commit 7511211

Please sign in to comment.