Skip to content

Commit

Permalink
Add support for spreadsheet config collections (#111)
Browse files Browse the repository at this point in the history
Signed-off-by: Ayoub LABIDI <[email protected]>
  • Loading branch information
ayolab authored Dec 16, 2024
1 parent 56488ae commit 5a7d6af
Show file tree
Hide file tree
Showing 5 changed files with 365 additions and 1 deletion.
33 changes: 33 additions & 0 deletions src/main/java/org/gridsuite/explore/server/ExploreController.java
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,18 @@ public ResponseEntity<Void> createSpreadsheetConfig(@RequestBody String spreadsh
return ResponseEntity.status(HttpStatus.CREATED).build();
}

@PostMapping(value = "/explore/spreadsheet-config-collections", consumes = MediaType.APPLICATION_JSON_VALUE)
@Operation(summary = "Create a spreadsheet configuration collection")
@ApiResponses(value = {@ApiResponse(responseCode = "201", description = "Spreadsheet config collection created")})
public ResponseEntity<Void> createSpreadsheetConfigCollection(@RequestBody String spreadsheetConfigCollectionDto,
@RequestParam("name") String collectionName,
@RequestParam(QUERY_PARAM_DESCRIPTION) String description,
@RequestParam(QUERY_PARAM_PARENT_DIRECTORY_ID) UUID parentDirectoryUuid,
@RequestHeader(QUERY_PARAM_USER_ID) String userId) {
exploreService.createSpreadsheetConfigCollection(spreadsheetConfigCollectionDto, collectionName, description, parentDirectoryUuid, userId);
return ResponseEntity.status(HttpStatus.CREATED).build();
}

@PutMapping(value = "/explore/spreadsheet-configs/{id}", consumes = MediaType.APPLICATION_JSON_VALUE)
@Operation(summary = "Modify a spreadsheet configuration")
@ApiResponses(value = {@ApiResponse(responseCode = "204", description = "Spreadsheet config has been successfully modified")})
Expand All @@ -351,6 +363,17 @@ public ResponseEntity<Void> updateSpreadsheetConfig(@PathVariable UUID id,
return ResponseEntity.noContent().build();
}

@PutMapping(value = "/explore/spreadsheet-config-collections/{id}", consumes = MediaType.APPLICATION_JSON_VALUE)
@Operation(summary = "Modify a spreadsheet configuration collection")
@ApiResponses(value = {@ApiResponse(responseCode = "204", description = "Spreadsheet config collection has been successfully modified")})
public ResponseEntity<Void> updateSpreadsheetConfigCollection(@PathVariable UUID id,
@RequestBody String spreadsheetConfigCollectionDto,
@RequestHeader(QUERY_PARAM_USER_ID) String userId,
@RequestParam("name") String name) {
exploreService.updateSpreadsheetConfigCollection(id, spreadsheetConfigCollectionDto, userId, name);
return ResponseEntity.noContent().build();
}

@PostMapping(value = "/explore/spreadsheet-configs/duplicate", params = "duplicateFrom")
@Operation(summary = "Duplicate a spreadsheet configuration")
@ApiResponses(value = {@ApiResponse(responseCode = "201", description = "Spreadsheet config has been successfully duplicated")})
Expand All @@ -361,6 +384,16 @@ public ResponseEntity<Void> duplicateSpreadsheetConfig(@RequestParam("duplicateF
return ResponseEntity.status(HttpStatus.CREATED).build();
}

@PostMapping(value = "/explore/spreadsheet-config-collections/duplicate", params = "duplicateFrom")
@Operation(summary = "Duplicate a spreadsheet configuration collection")
@ApiResponses(value = {@ApiResponse(responseCode = "201", description = "Spreadsheet config collection has been successfully duplicated")})
public ResponseEntity<Void> duplicateSpreadsheetConfigCollection(@RequestParam("duplicateFrom") UUID sourceId,
@RequestParam(name = QUERY_PARAM_PARENT_DIRECTORY_ID, required = false) UUID targetDirectoryId,
@RequestHeader(QUERY_PARAM_USER_ID) String userId) {
exploreService.duplicateSpreadsheetConfigCollection(sourceId, targetDirectoryId, userId);
return ResponseEntity.status(HttpStatus.CREATED).build();
}

@PostMapping(value = "/explore/composite-modifications")
@Operation(summary = "create composite modification element from existing network modifications")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Modifications have been created and composite modification element created in the directory")})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class DirectoryService implements IDirectoryElementsService {

public DirectoryService(
FilterService filterService, ContingencyListService contingencyListService, StudyService studyService, NetworkModificationService networkModificationService,
CaseService caseService, SpreadsheetConfigService spreadsheetConfigService, ParametersService parametersService, RestTemplate restTemplate,
CaseService caseService, SpreadsheetConfigService spreadsheetConfigService, SpreadsheetConfigCollectionService spreadsheetConfigCollectionService, ParametersService parametersService, RestTemplate restTemplate,
RemoteServicesProperties remoteServicesProperties) {
this.directoryServerBaseUri = remoteServicesProperties.getServiceUri("directory-server");
this.restTemplate = restTemplate;
Expand All @@ -64,6 +64,7 @@ public DirectoryService(
Map.entry(MODIFICATION, networkModificationService),
Map.entry(CASE, caseService),
Map.entry(SPREADSHEET_CONFIG, spreadsheetConfigService),
Map.entry(SPREADSHEET_CONFIG_COLLECTION, spreadsheetConfigCollectionService),
Map.entry(ParametersType.VOLTAGE_INIT_PARAMETERS.name(), parametersService),
Map.entry(ParametersType.SECURITY_ANALYSIS_PARAMETERS.name(), parametersService),
Map.entry(ParametersType.LOADFLOW_PARAMETERS.name(), parametersService),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class ExploreService {
static final String MODIFICATION = "MODIFICATION";
static final String DIRECTORY = "DIRECTORY";
static final String SPREADSHEET_CONFIG = "SPREADSHEET_CONFIG";
static final String SPREADSHEET_CONFIG_COLLECTION = "SPREADSHEET_CONFIG_COLLECTION";

private final DirectoryService directoryService;
private final StudyService studyService;
Expand All @@ -48,6 +49,7 @@ public class ExploreService {
private final CaseService caseService;
private final ParametersService parametersService;
private final SpreadsheetConfigService spreadsheetConfigService;
private final SpreadsheetConfigCollectionService spreadsheetConfigCollectionService;
private final UserIdentityService userIdentityService;

private static final Logger LOGGER = LoggerFactory.getLogger(ExploreService.class);
Expand All @@ -62,6 +64,7 @@ public ExploreService(
CaseService caseService,
ParametersService parametersService, UserAdminService userAdminService,
SpreadsheetConfigService spreadsheetConfigService,
SpreadsheetConfigCollectionService spreadsheetConfigCollectionService,
UserIdentityService userIdentityService) {

this.directoryService = directoryService;
Expand All @@ -73,6 +76,7 @@ public ExploreService(
this.parametersService = parametersService;
this.userAdminService = userAdminService;
this.spreadsheetConfigService = spreadsheetConfigService;
this.spreadsheetConfigCollectionService = spreadsheetConfigCollectionService;
this.userIdentityService = userIdentityService;
}

Expand Down Expand Up @@ -268,16 +272,32 @@ public void createSpreadsheetConfig(String spreadsheetConfigDto, String configNa
directoryService.createElement(elementAttributes, parentDirectoryUuid, userId);
}

public void createSpreadsheetConfigCollection(String spreadsheetConfigCollectionDto, String collectionName, String description, UUID parentDirectoryUuid, String userId) {
UUID spreadsheetConfigUuid = spreadsheetConfigCollectionService.createSpreadsheetConfigCollection(spreadsheetConfigCollectionDto);
ElementAttributes elementAttributes = new ElementAttributes(spreadsheetConfigUuid, collectionName, SPREADSHEET_CONFIG_COLLECTION, userId, 0, description);
directoryService.createElement(elementAttributes, parentDirectoryUuid, userId);
}

public void updateSpreadsheetConfig(UUID id, String spreadsheetConfigDto, String userId, String name) {
spreadsheetConfigService.updateSpreadsheetConfig(id, spreadsheetConfigDto);
updateElementName(id, name, userId);
}

public void updateSpreadsheetConfigCollection(UUID id, String spreadsheetConfigCollectionDto, String userId, String name) {
spreadsheetConfigCollectionService.updateSpreadsheetConfigCollection(id, spreadsheetConfigCollectionDto);
updateElementName(id, name, userId);
}

public void duplicateSpreadsheetConfig(UUID sourceId, UUID targetDirectoryId, String userId) {
UUID newSpreadsheetConfigUuid = spreadsheetConfigService.duplicateSpreadsheetConfig(sourceId);
directoryService.duplicateElement(sourceId, newSpreadsheetConfigUuid, targetDirectoryId, userId);
}

public void duplicateSpreadsheetConfigCollection(UUID sourceId, UUID targetDirectoryId, String userId) {
UUID newSpreadsheetConfigUuid = spreadsheetConfigCollectionService.duplicateSpreadsheetConfigCollection(sourceId);
directoryService.duplicateElement(sourceId, newSpreadsheetConfigUuid, targetDirectoryId, userId);
}

public void createCompositeModifications(List<UUID> modificationUuids, String userId, String name,
String description, UUID parentDirectoryUuid) {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* Copyright (c) 2024, 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.explore.server.services;

import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.Objects;
import java.util.UUID;

/**
* @author Ayoub LABIDI <ayoub.labidi at rte-france.com>
*/
@Service
public class SpreadsheetConfigCollectionService implements IDirectoryElementsService {

private static final String SPREADSHEET_CONFIG_API_VERSION = "v1";
private static final String DELIMITER = "/";
private static final String SPREADSHEET_CONFIG_COLLECTIONS_PATH = DELIMITER + SPREADSHEET_CONFIG_API_VERSION + DELIMITER + "spreadsheet-config-collections";
private static final String DUPLICATE_FROM_PARAMETER = "duplicateFrom";

private final RestTemplate restTemplate;

@Setter
private String spreadsheetConfigServerBaseUri;

@Autowired
public SpreadsheetConfigCollectionService(RestTemplate restTemplate, RemoteServicesProperties remoteServicesProperties) {
this.spreadsheetConfigServerBaseUri = remoteServicesProperties.getServiceUri("spreadsheet-config-server");
this.restTemplate = restTemplate;
}

public UUID createSpreadsheetConfigCollection(String collection) {
Objects.requireNonNull(collection);

var path = UriComponentsBuilder
.fromPath(SPREADSHEET_CONFIG_COLLECTIONS_PATH)
.buildAndExpand()
.toUriString();

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

HttpEntity<String> httpEntity = new HttpEntity<>(collection, headers);

return restTemplate.exchange(spreadsheetConfigServerBaseUri + path, HttpMethod.POST, httpEntity, UUID.class).getBody();
}

public UUID duplicateSpreadsheetConfigCollection(UUID collectionId) {
Objects.requireNonNull(collectionId);

var path = UriComponentsBuilder
.fromPath(SPREADSHEET_CONFIG_COLLECTIONS_PATH + DELIMITER + "duplicate")
.queryParam(DUPLICATE_FROM_PARAMETER, collectionId)
.buildAndExpand()
.toUriString();

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

HttpEntity<String> httpEntity = new HttpEntity<>(headers);

return restTemplate.exchange(spreadsheetConfigServerBaseUri + path, HttpMethod.POST, httpEntity, UUID.class).getBody();
}

public void updateSpreadsheetConfigCollection(UUID collectionId, String collection) {
Objects.requireNonNull(collectionId);
Objects.requireNonNull(collection);

var path = UriComponentsBuilder
.fromPath(SPREADSHEET_CONFIG_COLLECTIONS_PATH + DELIMITER + collectionId)
.buildAndExpand()
.toUriString();

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

HttpEntity<String> httpEntity = new HttpEntity<>(collection, headers);

restTemplate.exchange(spreadsheetConfigServerBaseUri + path, HttpMethod.PUT, httpEntity, Void.class);
}

@Override
public void delete(UUID configUuid, String userId) {
Objects.requireNonNull(configUuid);

var path = UriComponentsBuilder
.fromPath(SPREADSHEET_CONFIG_COLLECTIONS_PATH + DELIMITER + configUuid)
.buildAndExpand()
.toUriString();

HttpHeaders headers = new HttpHeaders();
headers.add(HEADER_USER_ID, userId);

restTemplate.exchange(spreadsheetConfigServerBaseUri + path, HttpMethod.DELETE, new HttpEntity<>(headers), Void.class);
}
}
Loading

0 comments on commit 5a7d6af

Please sign in to comment.