Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
f5553f3
export study
ghazwarhili Aug 7, 2026
c22592d
add TU
ghazwarhili Aug 7, 2026
84ff55c
enhance coverage code
ghazwarhili Aug 7, 2026
aa595a3
rework exportStudyArchive()
etiennehomer Aug 7, 2026
db1e926
code review remarks
ghazwarhili Aug 7, 2026
e04ee14
remove BuildStatus
ghazwarhili Aug 7, 2026
34e39f6
update TU
ghazwarhili Aug 7, 2026
7b241aa
add index
ghazwarhili Aug 10, 2026
6b0c1c8
Merge branch 'main' into razwa/export-study
ghazwarhili Aug 10, 2026
2ca0b42
remove CaseExportInfos
ghazwarhili Aug 10, 2026
115841b
Merge branch 'main' into razwa/export-study
ghazwarhili Aug 11, 2026
836cbfd
fix sonar issues
ghazwarhili Aug 11, 2026
83bb25b
code review Etienne L
ghazwarhili Aug 11, 2026
107d377
enhance TU
ghazwarhili Aug 11, 2026
ca94ad3
import study
ghazwarhili Aug 11, 2026
a217703
simplify the studyentity creation
ghazwarhili Aug 12, 2026
4c79e99
add TU
ghazwarhili Aug 12, 2026
43b9d7d
resolve conflicts
ghazwarhili Aug 13, 2026
ae80bc2
revert useless changes
ghazwarhili Aug 13, 2026
53a1e22
revert useless changes
ghazwarhili Aug 13, 2026
cf2cda1
revert useless changes
ghazwarhili Aug 13, 2026
6223d03
add StudyImportService
ghazwarhili Aug 14, 2026
b1fb871
resolve conflicts
ghazwarhili Aug 14, 2026
67b9307
code review rabbit
ghazwarhili Aug 14, 2026
f504693
enhance comments
ghazwarhili Aug 17, 2026
7742935
revert
ghazwarhili Aug 17, 2026
f613fb4
renaming fix
ghazwarhili Aug 17, 2026
05f683e
resolve conflicts
ghazwarhili Aug 28, 2026
9dfe44c
Fix merge
etiennehomer Aug 31, 2026
51776b7
resolve conflicts
ghazwarhili Sep 1, 2026
d3c6cb8
code review remarks
ghazwarhili Sep 4, 2026
df48eb0
revert unused changes
ghazwarhili Sep 4, 2026
52c67f8
Merge branch 'main' into razwa/import-study
ghazwarhili Sep 4, 2026
15797be
code review remarks
ghazwarhili Sep 4, 2026
d8e6c85
clean code
ghazwarhili Sep 4, 2026
4d85977
add emitStudyCreationFinished
ghazwarhili Sep 4, 2026
61f2dd2
fix test
ghazwarhili Sep 4, 2026
91b55b3
Merge branch 'main' into razwa/import-study
ghazwarhili Sep 7, 2026
14d9d7b
clean code
ghazwarhili Sep 7, 2026
fae45b0
Merge branch 'main' into razwa/import-study
ghazwarhili Sep 7, 2026
a4caf4d
fix merge
ghazwarhili Sep 7, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.gridsuite.study.server.dto.networkexport.ExportNetworkStatus;
import org.gridsuite.study.server.dto.networkexport.NodeExportInfos;
import org.gridsuite.study.server.dto.sequence.NodeSequenceType;
import org.gridsuite.study.server.dto.studyexport.TreeExportInfos;
import org.gridsuite.study.server.elasticsearch.EquipmentInfosService;
import org.gridsuite.study.server.error.StudyException;
import org.gridsuite.study.server.exception.PartialResultException;
Expand Down Expand Up @@ -70,6 +71,7 @@ public class StudyController {
private final NodeActivityRunnerService nodeActivityRunnerService;
private final NodeActivityService nodeActivityService;
private final StudyExportService studyExportService;
private final StudyImportService studyImportService;

public StudyController(StudyService studyService,
NetworkService networkStoreService,
Expand All @@ -82,7 +84,8 @@ public StudyController(StudyService studyService,
RebuildNodeService rebuildNodeService,
NodeActivityRunnerService nodeActivityRunnerService,
NodeActivityService nodeActivityService,
StudyExportService studyExportService) {
StudyExportService studyExportService,
StudyImportService studyImportService) {
this.nodeActivityService = nodeActivityService;
this.studyService = studyService;
this.networkModificationTreeService = networkModificationTreeService;
Expand All @@ -95,6 +98,7 @@ public StudyController(StudyService studyService,
this.rebuildNodeService = rebuildNodeService;
this.nodeActivityRunnerService = nodeActivityRunnerService;
this.studyExportService = studyExportService;
this.studyImportService = studyImportService;
}

@InitBinder
Expand Down Expand Up @@ -1642,4 +1646,13 @@ public ResponseEntity<Resource> exportStudy(@PathVariable("studyUuid") UUID stud
headers.setContentType(MediaType.parseMediaType("application/zip"));
return ResponseEntity.ok().headers(headers).body(studyExportService.exportStudy(studyUuid, userId));
}

@PostMapping(value = "/studies/import")
@Operation(summary = "Create a study and its root networks from a previously exported study archive")
@ApiResponse(responseCode = "200", description = "Study import initiated successfully")
public ResponseEntity<Void> importStudy(@RequestBody TreeExportInfos treeExportInfos,
@RequestHeader(HEADER_USER_ID) String userId) {
studyImportService.importStudy(treeExportInfos, userId);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public class ConsumerService {
private final CaseService caseService;
private final LoadFlowRestService loadFlowRestService;
private final NetworkModificationTreeService networkModificationTreeService;
private final StudyConfigService studyConfigService;
private final RootNetworkNodeInfoService rootNetworkNodeInfoService;
private final RootNetworkService rootNetworkService;
private final DirectoryService directoryService;
Expand All @@ -86,7 +85,6 @@ public ConsumerService(ObjectMapper objectMapper,
CaseService caseService,
LoadFlowRestService loadFlowRestService,
NetworkModificationTreeService networkModificationTreeService,
StudyConfigService studyConfigService,
RootNetworkNodeInfoService rootNetworkNodeInfoService,
RootNetworkService rootNetworkService,
DirectoryService directoryService,
Expand All @@ -101,7 +99,6 @@ public ConsumerService(ObjectMapper objectMapper,
this.caseService = caseService;
this.loadFlowRestService = loadFlowRestService;
this.networkModificationTreeService = networkModificationTreeService;
this.studyConfigService = studyConfigService;
this.rootNetworkNodeInfoService = rootNetworkNodeInfoService;
this.rootNetworkService = rootNetworkService;
this.directoryService = directoryService;
Expand Down Expand Up @@ -292,73 +289,15 @@ private void insertStudy(UUID studyUuid, String userId, NetworkInfos networkInfo
UserProfileInfos userProfileInfos = studyService.getUserProfile(userId);

ComputationParameterUUIDs computationParameterUUIDs = computationParametersService.createDefaultComputationParameters(userId, userProfileInfos);
UUID networkVisualizationParametersUuid = createDefaultNetworkVisualizationParameters(userId, userProfileInfos);
UUID spreadsheetConfigCollectionUuid = createDefaultSpreadsheetConfigCollection(userId, userProfileInfos);
UUID workspacesConfigUuid = createWorkspacesConfig(userProfileInfos);
UUID networkVisualizationParametersUuid = studyService.createDefaultNetworkVisualizationParameters(userId, userProfileInfos);
UUID spreadsheetConfigCollectionUuid = studyService.createDefaultSpreadsheetConfigCollection(userId, userProfileInfos);
UUID workspacesConfigUuid = studyService.createWorkspacesConfig(userProfileInfos);

studyService.insertStudy(studyUuid, userId, networkInfos, caseInfos, computationParameterUUIDs,
networkVisualizationParametersUuid, spreadsheetConfigCollectionUuid, workspacesConfigUuid,
importParameters, importReportUuid);
}

private UUID createDefaultNetworkVisualizationParameters(String userId, UserProfileInfos userProfileInfos) {
if (userProfileInfos != null && userProfileInfos.getNetworkVisualizationParameterId() != null) {
// try to access/duplicate the user profile network visualization parameters
try {
return studyConfigService.duplicateNetworkVisualizationParameters(userProfileInfos.getNetworkVisualizationParameterId());
} catch (Exception e) {
// TODO try to report a log in Root subreporter ?
LOGGER.error(String.format("Could not duplicate network visualization parameters with id '%s' from user/profile '%s/%s'. Using default parameters",
userProfileInfos.getNetworkVisualizationParameterId(), userId, userProfileInfos.getName()), e);
}
}
// no profile, or no/bad network visualization parameters in profile => use default values
try {
return studyConfigService.createDefaultNetworkVisualizationParameters();
} catch (final Exception e) {
LOGGER.error("Error while creating network visualization default parameters", e);
return null;
}
}

private UUID createDefaultSpreadsheetConfigCollection(String userId, UserProfileInfos userProfileInfos) {
if (userProfileInfos != null && userProfileInfos.getSpreadsheetConfigCollectionId() != null) {
// try to access/duplicate the user profile spreadsheet config collection
try {
return studyConfigService.duplicateSpreadsheetConfigCollection(userProfileInfos.getSpreadsheetConfigCollectionId());
} catch (Exception e) {
// TODO try to report a log in Root subreporter ?
LOGGER.error(String.format("Could not duplicate spreadsheet config collection with id '%s' from user/profile '%s/%s'. Using default spreadsheet config collection",
userProfileInfos.getSpreadsheetConfigCollectionId(), userId, userProfileInfos.getName()), e);
}
}
// no profile, or no/bad spreadsheet config collection in profile => use default values
try {
return studyConfigService.createDefaultSpreadsheetConfigCollection();
} catch (final Exception e) {
LOGGER.error("Error while creating default spreadsheet config collection", e);
return null;
}
}

@SuppressWarnings("checkstyle:LambdaBodyLength")
private UUID createWorkspacesConfig(UserProfileInfos userProfileInfos) {
try {
List<UUID> workspaceIds = new ArrayList<>();
if (userProfileInfos != null && userProfileInfos.getWorkspaceId() != null) {
// Create config with profile workspace as first, and two empty workspaces
workspaceIds.add(userProfileInfos.getWorkspaceId());
workspaceIds.add(null);
workspaceIds.add(null);
}
// Empty list will create default config
return studyConfigService.createWorkspacesConfigFromWorkspaces(workspaceIds);
} catch (final Exception e) {
LOGGER.error("Error while creating workspace collection", e);
return null;
}
}

@Bean
@SuppressWarnings("checkstyle:LambdaBodyLength")
public Consumer<Message<String>> consumeCaseImportFailed() {
Expand Down
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);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
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));
}
}
Loading
Loading