From 610652be2445a9452df7763c5662cab0ef712dae Mon Sep 17 00:00:00 2001 From: "REHILI Ghazwa (Externe)" Date: Thu, 26 Oct 2023 20:10:14 +0200 Subject: [PATCH] clean code --- .../powsybl/caseserver/CaseController.java | 3 --- .../com/powsybl/caseserver/CaseService.java | 27 +++++++++++++++++++ .../caseserver/FileSystemStorageService.java | 25 +++-------------- .../caseserver/ObjectStorageService.java | 23 +++------------- .../util/CaseDataSourceController.java | 2 +- .../util/CaseDataSourceService.java | 2 +- .../util/FsCaseDataSourceControllerTest.java | 6 +++++ .../util/S3CaseDataSourceControllerTest.java | 6 +++++ 8 files changed, 48 insertions(+), 46 deletions(-) diff --git a/src/main/java/com/powsybl/caseserver/CaseController.java b/src/main/java/com/powsybl/caseserver/CaseController.java index 0054b60..b300889 100644 --- a/src/main/java/com/powsybl/caseserver/CaseController.java +++ b/src/main/java/com/powsybl/caseserver/CaseController.java @@ -49,9 +49,6 @@ public class CaseController { @Qualifier("storageService") private CaseService caseService; - public CaseController() { - } - @GetMapping(value = "/cases") @Operation(summary = "Get all cases") //For maintenance purpose diff --git a/src/main/java/com/powsybl/caseserver/CaseService.java b/src/main/java/com/powsybl/caseserver/CaseService.java index c4961ae..6c212f0 100644 --- a/src/main/java/com/powsybl/caseserver/CaseService.java +++ b/src/main/java/com/powsybl/caseserver/CaseService.java @@ -10,13 +10,23 @@ import com.powsybl.caseserver.parsers.FileNameInfos; import com.powsybl.caseserver.parsers.FileNameParser; import com.powsybl.caseserver.parsers.FileNameParsers; +import com.powsybl.caseserver.repository.CaseMetadataEntity; +import com.powsybl.caseserver.repository.CaseMetadataRepository; +import com.powsybl.commons.datasource.DataSource; import com.powsybl.computation.ComputationManager; +import com.powsybl.iidm.network.Importer; import com.powsybl.iidm.network.Network; import org.apache.commons.lang3.Functions; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.server.ResponseStatusException; import java.nio.file.FileSystem; import java.nio.file.Path; +import java.time.LocalDateTime; +import java.time.ZoneOffset; import java.util.List; import java.util.Objects; import java.util.Optional; @@ -41,6 +51,23 @@ default CaseInfos createInfos(String fileBaseName, UUID caseUuid, String format) return CaseInfos.builder().name(fileBaseName).uuid(caseUuid).format(format).build(); } + default void createCaseMetadataEntity(UUID newCaseUuid, boolean withExpiration, CaseMetadataRepository caseMetadataRepository) { + LocalDateTime expirationTime = null; + if (withExpiration) { + expirationTime = LocalDateTime.now(ZoneOffset.UTC).plusHours(1); + } + caseMetadataRepository.save(new CaseMetadataEntity(newCaseUuid, expirationTime)); + } + + default Importer getImporterOrThrowsException(Path caseFile, ComputationManager computationManager) { + DataSource dataSource = DataSource.fromPath(caseFile); + Importer importer = Importer.find(dataSource, computationManager); + if (importer == null) { + throw CaseException.createFileNotImportable(caseFile); + } + return importer; + } + List getCases(); boolean caseExists(UUID caseUuid); diff --git a/src/main/java/com/powsybl/caseserver/FileSystemStorageService.java b/src/main/java/com/powsybl/caseserver/FileSystemStorageService.java index 57acdd5..4362cf3 100644 --- a/src/main/java/com/powsybl/caseserver/FileSystemStorageService.java +++ b/src/main/java/com/powsybl/caseserver/FileSystemStorageService.java @@ -77,17 +77,8 @@ public FileSystemStorageService(CaseMetadataRepository caseMetadataRepository) { this.caseMetadataRepository = caseMetadataRepository; } - Importer getImporterOrThrowsException(Path caseFile) { - DataSource dataSource = DataSource.fromPath(caseFile); - Importer importer = Importer.find(dataSource, computationManager); - if (importer == null) { - throw CaseException.createFileNotImportable(caseFile); - } - return importer; - } - String getFormat(Path caseFile) { - Importer importer = getImporterOrThrowsException(caseFile); + Importer importer = getImporterOrThrowsException(caseFile, computationManager); return importer.getFormat(); } @@ -207,7 +198,7 @@ public UUID importCase(MultipartFile mpf, boolean withExpiration) { Importer importer; try { - importer = getImporterOrThrowsException(caseFile); + importer = getImporterOrThrowsException(caseFile, computationManager); } catch (CaseException e) { try { Files.deleteIfExists(caseFile); @@ -218,7 +209,7 @@ public UUID importCase(MultipartFile mpf, boolean withExpiration) { throw e; } - createCaseMetadataEntity(caseUuid, withExpiration); + createCaseMetadataEntity(caseUuid, withExpiration, caseMetadataRepository); CaseInfos caseInfos = createInfos(caseFile.getFileName().toString(), caseUuid, importer.getFormat()); caseInfosService.addCaseInfos(caseInfos); sendImportMessage(caseInfos.createMessage()); @@ -242,7 +233,7 @@ public UUID duplicateCase(UUID sourceCaseUuid, boolean withExpiration) { CaseInfos existingCaseInfos = caseInfosService.getCaseInfosByUuid(sourceCaseUuid.toString()).orElseThrow(); CaseInfos caseInfos = createInfos(existingCaseInfos.getName(), newCaseUuid, existingCaseInfos.getFormat()); caseInfosService.addCaseInfos(caseInfos); - createCaseMetadataEntity(newCaseUuid, withExpiration); + createCaseMetadataEntity(newCaseUuid, withExpiration, caseMetadataRepository); sendImportMessage(caseInfos.createMessage()); return newCaseUuid; @@ -252,14 +243,6 @@ public UUID duplicateCase(UUID sourceCaseUuid, boolean withExpiration) { } } - private void createCaseMetadataEntity(UUID newCaseUuid, boolean withExpiration) { - LocalDateTime expirationTime = null; - if (withExpiration) { - expirationTime = LocalDateTime.now(ZoneOffset.UTC).plusHours(1); - } - caseMetadataRepository.save(new CaseMetadataEntity(newCaseUuid, expirationTime)); - } - @Override public R withS3DownloadedTempPath(UUID caseUuid, Functions.FailableFunction f) { return null; diff --git a/src/main/java/com/powsybl/caseserver/ObjectStorageService.java b/src/main/java/com/powsybl/caseserver/ObjectStorageService.java index 2a4c8ee..e5f0172 100644 --- a/src/main/java/com/powsybl/caseserver/ObjectStorageService.java +++ b/src/main/java/com/powsybl/caseserver/ObjectStorageService.java @@ -81,17 +81,8 @@ public ObjectStorageService(CaseMetadataRepository caseMetadataRepository) { this.caseMetadataRepository = caseMetadataRepository; } - Importer getImporterOrThrowsException(Path caseFile) { - DataSource dataSource = DataSource.fromPath(caseFile); - Importer importer = Importer.find(dataSource, computationManager); - if (importer == null) { - throw CaseException.createFileNotImportable(caseFile); - } - return importer; - } - String getFormat(Path caseFile) { - Importer importer = getImporterOrThrowsException(caseFile); + Importer importer = getImporterOrThrowsException(caseFile, computationManager); return importer.getFormat(); } @@ -320,7 +311,7 @@ public UUID importCase(MultipartFile mpf, boolean withExpiration) { throw new RuntimeException("Error uploading object to S3", e); } - createCaseMetadataEntity(caseUuid, withExpiration); + createCaseMetadataEntity(caseUuid, withExpiration, caseMetadataRepository); CaseInfos caseInfos = createInfos(caseName, caseUuid, format); caseInfosService.addCaseInfos(caseInfos); sendImportMessage(caseInfos.createMessage()); @@ -347,19 +338,11 @@ public UUID duplicateCase(UUID sourceCaseUuid, boolean withExpiration) { } CaseInfos caseInfos = createInfos(existingCaseInfos.getName(), newCaseUuid, existingCaseInfos.getFormat()); caseInfosService.addCaseInfos(caseInfos); - createCaseMetadataEntity(newCaseUuid, withExpiration); + createCaseMetadataEntity(newCaseUuid, withExpiration, caseMetadataRepository); sendImportMessage(caseInfos.createMessage()); return newCaseUuid; } - private void createCaseMetadataEntity(UUID newCaseUuid, boolean withExpiration) { - LocalDateTime expirationTime = null; - if (withExpiration) { - expirationTime = LocalDateTime.now(ZoneOffset.UTC).plusHours(1); - } - caseMetadataRepository.save(new CaseMetadataEntity(newCaseUuid, expirationTime)); - } - @Transactional public void disableCaseExpiration(UUID caseUuid) { CaseMetadataEntity caseMetadataEntity = caseMetadataRepository.findById(caseUuid).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "case " + caseUuid + " not found")); diff --git a/src/main/java/com/powsybl/caseserver/datasource/util/CaseDataSourceController.java b/src/main/java/com/powsybl/caseserver/datasource/util/CaseDataSourceController.java index 237b1e8..4db8ac4 100644 --- a/src/main/java/com/powsybl/caseserver/datasource/util/CaseDataSourceController.java +++ b/src/main/java/com/powsybl/caseserver/datasource/util/CaseDataSourceController.java @@ -26,7 +26,7 @@ @RestController @RequestMapping(value = "/" + CaseConstants.API_VERSION) @Tag(name = "Case datasource util") -@ComponentScan(basePackageClasses = {S3CaseDataSourceService.class}) +@ComponentScan(basePackageClasses = {CaseDataSourceService.class}) public class CaseDataSourceController { @Autowired diff --git a/src/main/java/com/powsybl/caseserver/datasource/util/CaseDataSourceService.java b/src/main/java/com/powsybl/caseserver/datasource/util/CaseDataSourceService.java index 38dd87d..285dc80 100644 --- a/src/main/java/com/powsybl/caseserver/datasource/util/CaseDataSourceService.java +++ b/src/main/java/com/powsybl/caseserver/datasource/util/CaseDataSourceService.java @@ -10,7 +10,7 @@ import java.util.UUID; /** - * @author Rehili Ghazwa + * @author Abdelsalem Hedhili */ public interface CaseDataSourceService { String getBaseName(UUID caseUuid); diff --git a/src/test/java/com/powsybl/caseserver/datasource/util/FsCaseDataSourceControllerTest.java b/src/test/java/com/powsybl/caseserver/datasource/util/FsCaseDataSourceControllerTest.java index 0ac8287..8adddce 100644 --- a/src/test/java/com/powsybl/caseserver/datasource/util/FsCaseDataSourceControllerTest.java +++ b/src/test/java/com/powsybl/caseserver/datasource/util/FsCaseDataSourceControllerTest.java @@ -1,3 +1,9 @@ +/** + * Copyright (c) 2023, 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 com.powsybl.caseserver.datasource.util; import com.powsybl.caseserver.ContextConfigurationWithTestChannel; diff --git a/src/test/java/com/powsybl/caseserver/datasource/util/S3CaseDataSourceControllerTest.java b/src/test/java/com/powsybl/caseserver/datasource/util/S3CaseDataSourceControllerTest.java index d64c0b7..fa5ea73 100644 --- a/src/test/java/com/powsybl/caseserver/datasource/util/S3CaseDataSourceControllerTest.java +++ b/src/test/java/com/powsybl/caseserver/datasource/util/S3CaseDataSourceControllerTest.java @@ -1,3 +1,9 @@ +/** + * Copyright (c) 2023, 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 com.powsybl.caseserver.datasource.util; import com.powsybl.caseserver.ContextConfigurationWithTestChannel;