From 202a77ad94a6cae4415ca338385df43ed167297d Mon Sep 17 00:00:00 2001 From: jamal-khey Date: Wed, 11 Oct 2023 13:01:21 +0200 Subject: [PATCH] add get all modification with meta data Signed-off-by: jamal-khey --- .../server/NetworkModificationController.java | 10 +++++++++ .../NetworkModificationRepository.java | 10 ++++----- .../service/NetworkModificationService.java | 22 ++++++++++++++++--- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/gridsuite/modification/server/NetworkModificationController.java b/src/main/java/org/gridsuite/modification/server/NetworkModificationController.java index b07338f02..46fe5b13e 100644 --- a/src/main/java/org/gridsuite/modification/server/NetworkModificationController.java +++ b/src/main/java/org/gridsuite/modification/server/NetworkModificationController.java @@ -57,6 +57,16 @@ public ResponseEntity> getNetworkModifications(@Paramete return ResponseEntity.ok().body(networkModificationService.getNetworkModifications(groupUuid, onlyMetadata, errorOnGroupNotFound, stashed)); } + @GetMapping(value = "/groups/{groupUuid}/network-modifications-all", produces = MediaType.APPLICATION_JSON_VALUE) + @Operation(summary = "Get modifications list of a group") + @ApiResponse(responseCode = "200", description = "List of modifications of the group") + public ResponseEntity> getNetworkModificationsAll(@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 = "onlyStashed", required = false, defaultValue = "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.getAllNetworkModificationsWithMetaData(groupUuid, errorOnGroupNotFound)); + } + @PostMapping(value = "/groups") @Operation(summary = "Create a modification group based on another group") @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The group and its modifications have been duplicated")}) 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 47885148d..2df46ab00 100644 --- a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java +++ b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java @@ -161,16 +161,14 @@ public List getAllModificationInfo(UUID groupUuid) { .collect(Collectors.toList()); } - //find all stashed modifications @Transactional(readOnly = true) - public List getAllStashedModificationsInfo(UUID groupUuid) { - ModificationGroupEntity groupEntity = getModificationGroup(groupUuid); - return groupEntity.getModifications().stream() - .filter(ModificationEntity::getStashed) + public List getAllModificationsMetaData(UUID groupUuid) { + return modificationRepository + .findAllBaseByGroupId(getModificationGroup(groupUuid).getId()) + .stream() .map(ModificationEntity::toModificationInfos) .collect(Collectors.toList()); } - @Transactional // To have the 2 delete in the same transaction (atomic) public void deleteModificationGroup(UUID groupUuid, boolean errorOnGroupNotFound) { try { 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 e930b28ee..ed2bbc793 100644 --- a/src/main/java/org/gridsuite/modification/server/service/NetworkModificationService.java +++ b/src/main/java/org/gridsuite/modification/server/service/NetworkModificationService.java @@ -84,12 +84,28 @@ public ModificationInfos getNetworkModification(UUID networkModificationUuid) { //get all network modifications @Transactional(readOnly = true) public List getAllNetworkModifications(UUID groupUuid) { - return networkModificationRepository.getAllModificationInfo(groupUuid); + List modifications = networkModificationRepository.getAllModificationInfo(groupUuid); + return modifications; } @Transactional(readOnly = true) - public List getAllStashedModifications(UUID groupUuid) { - return networkModificationRepository.getAllStashedModificationsInfo(groupUuid); + // Need a transaction for collections lazy loading + public List getAllNetworkModificationsWithMetaData(UUID groupUuid, boolean errorOnGroupNotFound) { + List modifications = networkModificationRepository.getAllModificationInfo(groupUuid); + 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; } public void deleteModificationGroup(UUID groupUuid, boolean errorOnGroupNotFound) {