Skip to content

Commit

Permalink
clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
REHILI Ghazwa (Externe) committed Oct 26, 2023
1 parent 03aeab5 commit 610652b
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 46 deletions.
3 changes: 0 additions & 3 deletions src/main/java/com/powsybl/caseserver/CaseController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/com/powsybl/caseserver/CaseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<CaseInfos> getCases();

boolean caseExists(UUID caseUuid);
Expand Down
25 changes: 4 additions & 21 deletions src/main/java/com/powsybl/caseserver/FileSystemStorageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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);
Expand All @@ -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());
Expand All @@ -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;
Expand All @@ -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, T extends Throwable> R withS3DownloadedTempPath(UUID caseUuid, Functions.FailableFunction<Path, R, T> f) {
return null;
Expand Down
23 changes: 3 additions & 20 deletions src/main/java/com/powsybl/caseserver/ObjectStorageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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());
Expand All @@ -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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.UUID;

/**
* @author Rehili Ghazwa <ghazwa.rehili at rte-france.com>
* @author Abdelsalem Hedhili <abdelsalem.hedhili at rte-france.com>
*/
public interface CaseDataSourceService {
String getBaseName(UUID caseUuid);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 610652b

Please sign in to comment.