diff --git a/src/main/java/org/gridsuite/modification/server/NetworkModificationController.java b/src/main/java/org/gridsuite/modification/server/NetworkModificationController.java index 20d42cecc..5211456c8 100644 --- a/src/main/java/org/gridsuite/modification/server/NetworkModificationController.java +++ b/src/main/java/org/gridsuite/modification/server/NetworkModificationController.java @@ -52,9 +52,14 @@ public NetworkModificationController(NetworkModificationService networkModificat @ApiResponse(responseCode = "200", description = "List of modifications of the group") public ResponseEntity> getNetworkModifications(@Parameter(description = "Group UUID") @PathVariable("groupUuid") UUID groupUuid, @Parameter(description = "Only metadata") @RequestParam(name = "onlyMetadata", required = false, defaultValue = "false") Boolean onlyMetadata, - @Parameter(description = "Stashed modifications") @RequestParam(name = "stashed", required = false, defaultValue = "false") Boolean stashed, + @Parameter(description = "Stashed modifications") @RequestParam(name = "stashed", required = false) Boolean stashed, @Parameter(description = "Return 404 if group is not found or an empty list") @RequestParam(name = "errorOnGroupNotFound", required = false, defaultValue = "true") Boolean errorOnGroupNotFound) { - return ResponseEntity.ok().body(networkModificationService.getNetworkModifications(groupUuid, onlyMetadata, errorOnGroupNotFound, stashed)); + if(stashed == null) { + return ResponseEntity.ok().body(networkModificationService.getAllNetworkModificationsWithMetaData(groupUuid, errorOnGroupNotFound)); + } + else { + return ResponseEntity.ok().body(networkModificationService.getNetworkModifications(groupUuid, onlyMetadata, errorOnGroupNotFound, stashed)); + } } @PostMapping(value = "/groups") 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 bfb3533c3..214dfb048 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java @@ -128,6 +128,27 @@ public List getModifications(UUID groupUuid, boolean onlyMeta } } + @Transactional(readOnly = true) + public List getAllModifications(UUID groupUuid, boolean errorOnGroupNotFound) { + try { + return getAllModificationsInfos(List.of(groupUuid)); + } catch (NetworkModificationException e) { + if (e.getType() == MODIFICATION_GROUP_NOT_FOUND && !errorOnGroupNotFound) { + return List.of(); + } + throw e; + } + } + + @Transactional(readOnly = true) + public List getAllModificationsMetaData(UUID groupUuid) { + return modificationRepository + .findAllBaseByGroupId(getModificationGroup(groupUuid).getId()) + .stream() + .map(ModificationEntity::toModificationInfos) + .collect(Collectors.toList()); + } + public List getModificationsMetadata(UUID groupUuid, boolean stashedModifications) { return modificationRepository .findAllBaseByGroupId(getModificationGroup(groupUuid).getId()) @@ -144,6 +165,12 @@ public List getModificationsInfos(List groupUuids, bool .collect(Collectors.toList()); } + public List getAllModificationsInfos(List groupUuids) { + return groupUuids.stream().flatMap(this::getModificationEntityStream) + .map(ModificationEntity::toModificationInfos) + .collect(Collectors.toList()); + } + @Transactional(readOnly = true) public ModificationInfos getModificationInfo(UUID modificationUuid) { return modificationRepository @@ -255,4 +282,5 @@ public void deleteStashedModificationInGroup(UUID groupUuid, boolean errorOnGrou throw e; } } + } diff --git a/src/main/java/org/gridsuite/modification/server/service/NetworkModificationService.java b/src/main/java/org/gridsuite/modification/server/service/NetworkModificationService.java index 9216a670c..49a4d9074 100644 --- a/src/main/java/org/gridsuite/modification/server/service/NetworkModificationService.java +++ b/src/main/java/org/gridsuite/modification/server/service/NetworkModificationService.java @@ -70,6 +70,26 @@ public List getNetworkModifications(UUID groupUuid, boolean o return networkModificationRepository.getModifications(groupUuid, onlyMetadata, errorOnGroupNotFound, stashedModifications); } + @Transactional(readOnly = true) + // Need a transaction for collections lazy loading + public List getAllNetworkModificationsWithMetaData(UUID groupUuid, boolean errorOnGroupNotFound) { + List modifications = networkModificationRepository.getAllModifications(groupUuid, errorOnGroupNotFound); + List metaData = networkModificationRepository.getAllModificationsMetaData(groupUuid); + //put modification in a hashmap + Map modificationInfosMap = modifications.stream().collect(Collectors.toMap(ModificationInfos::getUuid, m -> m)); + //put metadata in a hashmap + Map metaDataMap = metaData.stream().collect(Collectors.toMap(ModificationInfos::getUuid, m -> m)); + + modificationInfosMap.forEach((uuid,modificationInfos) -> { + ModificationInfos metadata = metaDataMap.get(uuid); + if (modificationInfos != null) { + modificationInfos.setStashed(metadata.getStashed()); + modificationInfos.setDate(metadata.getDate()); + } + }); + return modifications; + } + @Transactional(readOnly = true) // Need a transaction for collections lazy loading public List getNetworkModifications(UUID groupUuid, boolean onlyMetadata, boolean errorOnGroupNotFound) {