-
Notifications
You must be signed in to change notification settings - Fork 2
import study #1057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ghazwarhili
wants to merge
41
commits into
main
Choose a base branch
from
razwa/import-study
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
import study #1057
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
f5553f3
export study
ghazwarhili c22592d
add TU
ghazwarhili 84ff55c
enhance coverage code
ghazwarhili aa595a3
rework exportStudyArchive()
etiennehomer db1e926
code review remarks
ghazwarhili e04ee14
remove BuildStatus
ghazwarhili 34e39f6
update TU
ghazwarhili 7b241aa
add index
ghazwarhili 6b0c1c8
Merge branch 'main' into razwa/export-study
ghazwarhili 2ca0b42
remove CaseExportInfos
ghazwarhili 115841b
Merge branch 'main' into razwa/export-study
ghazwarhili 836cbfd
fix sonar issues
ghazwarhili 83bb25b
code review Etienne L
ghazwarhili 107d377
enhance TU
ghazwarhili ca94ad3
import study
ghazwarhili a217703
simplify the studyentity creation
ghazwarhili 4c79e99
add TU
ghazwarhili 43b9d7d
resolve conflicts
ghazwarhili ae80bc2
revert useless changes
ghazwarhili 53a1e22
revert useless changes
ghazwarhili cf2cda1
revert useless changes
ghazwarhili 6223d03
add StudyImportService
ghazwarhili b1fb871
resolve conflicts
ghazwarhili 67b9307
code review rabbit
ghazwarhili f504693
enhance comments
ghazwarhili 7742935
revert
ghazwarhili f613fb4
renaming fix
ghazwarhili 05f683e
resolve conflicts
ghazwarhili 9dfe44c
Fix merge
etiennehomer 51776b7
resolve conflicts
ghazwarhili d3c6cb8
code review remarks
ghazwarhili df48eb0
revert unused changes
ghazwarhili 52c67f8
Merge branch 'main' into razwa/import-study
ghazwarhili 15797be
code review remarks
ghazwarhili d8e6c85
clean code
ghazwarhili 4d85977
add emitStudyCreationFinished
ghazwarhili 61f2dd2
fix test
ghazwarhili 91b55b3
Merge branch 'main' into razwa/import-study
ghazwarhili 14d9d7b
clean code
ghazwarhili fae45b0
Merge branch 'main' into razwa/import-study
ghazwarhili a4caf4d
fix merge
ghazwarhili File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
src/main/java/org/gridsuite/study/server/service/StudyImportService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| /** | ||
| * Copyright (c) 2026, 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.study.server.service; | ||
|
|
||
| import org.apache.commons.collections4.CollectionUtils; | ||
| import org.gridsuite.study.server.dto.CaseInfos; | ||
| import org.gridsuite.study.server.dto.NetworkInfos; | ||
| import org.gridsuite.study.server.dto.RootNetworkInfos; | ||
| import org.gridsuite.study.server.dto.RootNetworkLoadStatus; | ||
| import org.gridsuite.study.server.dto.studyexport.NodeTreeExportInfos; | ||
| import org.gridsuite.study.server.dto.studyexport.RootNetworkExportInfos; | ||
| import org.gridsuite.study.server.dto.studyexport.TreeExportInfos; | ||
| import org.gridsuite.study.server.error.StudyException; | ||
| import org.gridsuite.study.server.notification.NotificationService; | ||
| import org.gridsuite.study.server.repository.StudyEntity; | ||
| import org.gridsuite.study.server.repository.StudyRepository; | ||
| import org.gridsuite.study.server.repository.rootnetwork.RootNetworkEntity; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Comparator; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.UUID; | ||
|
|
||
| import static org.gridsuite.study.server.error.StudyBusinessErrorCode.NOT_FOUND; | ||
|
|
||
| /** | ||
| * @author Ghazwa Rehili <ghazwa.rehili at rte-france.com> | ||
| */ | ||
| @Service | ||
| public class StudyImportService { | ||
| private static final Logger LOGGER = LoggerFactory.getLogger(StudyImportService.class); | ||
|
|
||
| private final StudyService studyService; | ||
| private final StudyRepository studyRepository; | ||
| private final RootNetworkService rootNetworkService; | ||
| private final NetworkModificationService networkModificationService; | ||
| private final CaseService caseService; | ||
| private final NotificationService notificationService; | ||
|
|
||
| public StudyImportService(StudyService studyService, StudyRepository studyRepository, RootNetworkService rootNetworkService, | ||
| NetworkModificationService networkModificationService, CaseService caseService, NotificationService notificationService) { | ||
| this.studyService = studyService; | ||
| this.studyRepository = studyRepository; | ||
| this.rootNetworkService = rootNetworkService; | ||
| this.networkModificationService = networkModificationService; | ||
| this.caseService = caseService; | ||
| this.notificationService = notificationService; | ||
| } | ||
|
|
||
| @Transactional | ||
| public void importStudy(TreeExportInfos treeExportInfos, String userId) { | ||
| if (treeExportInfos.rootNetworks().isEmpty()) { | ||
| throw new StudyException(NOT_FOUND, "No root network found in import archive"); | ||
| } | ||
| Map<UUID, UUID> modificationGroupUuidMapping = duplicateModificationGroups(treeExportInfos.nodeTree()); | ||
| StudyEntity studyEntity = studyService.createStudyEntityWithTree(treeExportInfos.studyUuid(), userId, treeExportInfos.nodeTree(), modificationGroupUuidMapping); | ||
| studyRepository.save(studyEntity); | ||
| List<RootNetworkExportInfos> orderedRootNetworks = treeExportInfos.rootNetworks().stream() | ||
| .sorted(Comparator.comparing(RootNetworkExportInfos::index)) | ||
| .toList(); | ||
| List<UUID> duplicatedCaseUuids = new ArrayList<>(); | ||
| try { | ||
| for (RootNetworkExportInfos rootNetworkInfos : orderedRootNetworks) { | ||
| UUID newCaseUuid = caseService.duplicateCase(rootNetworkInfos.caseInfos().getCaseUuid(), true); | ||
| duplicatedCaseUuids.add(newCaseUuid); | ||
| RootNetworkEntity rootNetworkEntity = rootNetworkService.createRootNetwork(studyEntity, RootNetworkInfos.builder() | ||
| .id(UUID.randomUUID()) | ||
| .name(rootNetworkInfos.name()) | ||
| .tag(rootNetworkInfos.tag()) | ||
| .caseInfos(new CaseInfos(newCaseUuid, rootNetworkInfos.caseInfos().getOriginalCaseUuid(), | ||
| rootNetworkInfos.caseInfos().getCaseName(), rootNetworkInfos.caseInfos().getCaseFormat())) | ||
| .importParameters(rootNetworkInfos.importParameters()) | ||
| .networkInfos(new NetworkInfos(UUID.randomUUID(), "")) | ||
| .build()); | ||
| rootNetworkService.updateNetworkLoadStatus(rootNetworkEntity.getId(), RootNetworkLoadStatus.UNLOADED); | ||
| } | ||
| } catch (Exception e) { | ||
| modificationGroupUuidMapping.values().forEach(newGroupUuid -> { | ||
| try { | ||
| networkModificationService.deleteModifications(newGroupUuid); | ||
| } catch (Exception exception) { | ||
| LOGGER.error(String.format("Could not clean up orphaned modification group '%s' after import failure", newGroupUuid), exception); | ||
| } | ||
| }); | ||
| duplicatedCaseUuids.forEach(caseUuid -> { | ||
| try { | ||
| caseService.deleteCase(caseUuid); | ||
| } catch (Exception exception) { | ||
| LOGGER.error(String.format("Could not clean up orphaned case '%s' after import failure", caseUuid), exception); | ||
| } | ||
| }); | ||
| throw e; | ||
| } | ||
| notificationService.emitStudyCreationFinished(studyEntity.getId(), userId); | ||
| } | ||
|
|
||
| private Map<UUID, UUID> duplicateModificationGroups(NodeTreeExportInfos nodeTree) { | ||
| Map<UUID, UUID> modificationGroupUuidMapping = new HashMap<>(); | ||
| if (nodeTree == null) { | ||
| return modificationGroupUuidMapping; | ||
| } | ||
| try { | ||
| CollectionUtils.emptyIfNull(nodeTree.children()).forEach(child -> duplicateModificationGroupsRecursively(child, modificationGroupUuidMapping)); | ||
| } catch (Exception e) { | ||
| modificationGroupUuidMapping.values().forEach(newGroupUuid -> { | ||
| try { | ||
| networkModificationService.deleteModifications(newGroupUuid); | ||
| } catch (Exception cleanupException) { | ||
| LOGGER.error(String.format("Could not clean up orphaned modification group '%s' after import failure", newGroupUuid), cleanupException); | ||
| } | ||
| }); | ||
| throw e; | ||
| } | ||
| return modificationGroupUuidMapping; | ||
| } | ||
|
|
||
| private void duplicateModificationGroupsRecursively(NodeTreeExportInfos exportNode, Map<UUID, UUID> modificationGroupUuidMapping) { | ||
| studyService.toNetworkModificationNodeType(exportNode.nodeType()); | ||
| if (exportNode.modificationGroupUuid() != null) { | ||
| UUID newGroupUuid = UUID.randomUUID(); | ||
| networkModificationService.duplicateModificationsGroup(exportNode.modificationGroupUuid(), newGroupUuid); | ||
| modificationGroupUuidMapping.put(exportNode.modificationGroupUuid(), newGroupUuid); | ||
| } | ||
| CollectionUtils.emptyIfNull(exportNode.children()).forEach(child -> duplicateModificationGroupsRecursively(child, modificationGroupUuidMapping)); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.