Skip to content

Commit

Permalink
fix: getting tree is now linked to root network + fix apply modificat…
Browse files Browse the repository at this point in the history
…ions

Signed-off-by: LE SAULNIER Kevin <[email protected]>
  • Loading branch information
LE SAULNIER Kevin committed Dec 23, 2024
1 parent 231a472 commit 3c682ce
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 31 deletions.
10 changes: 6 additions & 4 deletions src/main/java/org/gridsuite/study/server/StudyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -1302,8 +1302,9 @@ public ResponseEntity<Void> restoreNodes(@Parameter(description = "study uuid")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "network modification tree"),
@ApiResponse(responseCode = "404", description = "The study or the node not found")})
public ResponseEntity<RootNode> getNetworkModificationTree(@Parameter(description = "study uuid") @PathVariable("studyUuid") UUID studyUuid) {
RootNode rootNode = networkModificationTreeService.getStudyTree(studyUuid);
public ResponseEntity<RootNode> getNetworkModificationTree(@Parameter(description = "study uuid") @PathVariable("studyUuid") UUID studyUuid,
@Parameter(description = "root network uuid") @RequestParam("rootNetworkUuid") UUID rootNetworkUuid) {
RootNode rootNode = networkModificationTreeService.getStudyTree(studyUuid, rootNetworkUuid);
return rootNode != null ?
ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(rootNode)
: ResponseEntity.notFound().build();
Expand All @@ -1315,8 +1316,9 @@ public ResponseEntity<RootNode> getNetworkModificationTree(@Parameter(descriptio
@ApiResponse(responseCode = "200", description = "network modification subtree"),
@ApiResponse(responseCode = "404", description = "The study or the parent node not found")})
public ResponseEntity<NetworkModificationNode> getNetworkModificationSubtree(@Parameter(description = "study uuid") @PathVariable("studyUuid") UUID studyUuid,
@Parameter(description = "parent node uuid") @RequestParam(value = "parentNodeUuid") UUID parentNodeUuid) {
NetworkModificationNode parentNode = (NetworkModificationNode) networkModificationTreeService.getStudySubtree(studyUuid, parentNodeUuid);
@Parameter(description = "parent node uuid") @RequestParam(value = "parentNodeUuid") UUID parentNodeUuid,
@Parameter(description = "root network uuid") @RequestParam(value = "rootNetworkUuid") UUID rootNetworkUuid) {
NetworkModificationNode parentNode = (NetworkModificationNode) networkModificationTreeService.getStudySubtree(studyUuid, parentNodeUuid, rootNetworkUuid);
return parentNode != null ?
ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(parentNode)
: ResponseEntity.notFound().build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* 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.study.server.dto.modification;

import java.util.List;
import java.util.UUID;

public record MultipleNetworkModificationsInfos(List<UUID> modificationsUuid, List<NetworkModificationContextInfos> networkModificationContextInfos) { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* 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.study.server.dto.modification;

import java.util.UUID;

public record NetworkModificationContextInfos(UUID networkUuid, String variantId, UUID reportUuid, UUID nodeUuid) { }
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.gridsuite.study.server.RemoteServicesProperties;
import org.gridsuite.study.server.dto.BuildInfos;
import org.gridsuite.study.server.dto.NodeReceiver;
import org.gridsuite.study.server.dto.modification.MultipleNetworkModificationsInfos;
import org.gridsuite.study.server.dto.modification.NetworkModificationResult;
import org.gridsuite.study.server.networkmodificationtree.entities.NetworkModificationNodeInfoEntity;
import org.gridsuite.study.server.networkmodificationtree.entities.RootNetworkNodeInfoEntity;
Expand Down Expand Up @@ -419,4 +420,66 @@ public void deleteStashedModifications(UUID groupUUid) {
throw handleHttpError(e, DELETE_NETWORK_MODIFICATION_FAILED);
}
}

public Optional<UUID> createModificationWithoutApplying(UUID studyUuid,
String createModificationAttributes,
UUID groupUuid) {
Optional<UUID> result;
Objects.requireNonNull(studyUuid);
Objects.requireNonNull(createModificationAttributes);

var uriComponentsBuilder = UriComponentsBuilder
.fromUriString(getNetworkModificationServerURI(false) + NETWORK_MODIFICATIONS_PATH)
.queryParam(GROUP_UUID, groupUuid);

var path = uriComponentsBuilder
.buildAndExpand()
.toUriString();

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

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

try {
result = restTemplate.exchange(path, HttpMethod.POST, httpEntity,
new ParameterizedTypeReference<Optional<UUID>>() {
}).getBody();
} catch (HttpStatusCodeException e) {
throw handleHttpError(e, CREATE_NETWORK_MODIFICATION_FAILED);
}

return result;
}

private HttpEntity<String> getMultipleNetworkModificationInfosBody(MultipleNetworkModificationsInfos multipleNetworkModificationInfos) {
HttpEntity<String> httpEntity;
try {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
httpEntity = new HttpEntity<>(objectMapper.writeValueAsString(multipleNetworkModificationInfos), headers);
} catch (JsonProcessingException e) {
throw new UncheckedIOException(e);
}
return httpEntity;
}

public List<Optional<NetworkModificationResult>> applyModifications(MultipleNetworkModificationsInfos multipleNetworkModificationInfos) {
List<Optional<NetworkModificationResult>> result;
var path = UriComponentsBuilder.fromPath(NETWORK_MODIFICATIONS_PATH + "/apply");

HttpEntity<String> httpEntity = getMultipleNetworkModificationInfosBody(multipleNetworkModificationInfos);

try {
result = restTemplate.exchange(
getNetworkModificationServerURI(false) + path.buildAndExpand().toUriString(),
HttpMethod.POST,
httpEntity,
new ParameterizedTypeReference<List<Optional<NetworkModificationResult>>>() {
}).getBody();
} catch (HttpStatusCodeException e) {
throw handleHttpError(e, CREATE_NETWORK_MODIFICATION_FAILED);
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -337,17 +337,18 @@ public NodeEntity createRoot(StudyEntity study) {
}

@Transactional
public RootNode getStudyTree(UUID studyId) {
public RootNode getStudyTree(UUID studyId, UUID rootNetworkUuid) {
NodeEntity rootNode = nodesRepository.findByStudyIdAndType(studyId, NodeType.ROOT).orElseThrow(() -> new StudyException(NODE_NOT_FOUND));
RootNode studyTree = (RootNode) getStudySubtree(studyId, rootNode.getIdNode());
RootNode studyTree = (RootNode) getStudySubtree(studyId, rootNode.getIdNode(), rootNetworkUuid);
if (studyTree != null) {
studyTree.setStudyId(studyId);
}
return studyTree;
}

private void completeNodeInfos(List<AbstractNode> nodes, UUID studyUuid) {
RootNetworkEntity rootNetworkEntity = rootNetworkRepository.findAllByStudyId(studyUuid).stream().findFirst().orElseThrow(() -> new StudyException(ROOT_NETWORK_NOT_FOUND));
private void completeNodeInfos(List<AbstractNode> nodes, UUID studyUuid, UUID rootNetworkUuid) {
// RootNetworkEntity rootNetworkEntity = rootNetworkRepository.findAllByStudyId(studyUuid).stream().findFirst().orElseThrow(() -> new StudyException(ROOT_NETWORK_NOT_FOUND));
RootNetworkEntity rootNetworkEntity = rootNetworkService.getRootNetwork(rootNetworkUuid).orElseThrow(() -> new StudyException(ROOT_NETWORK_NOT_FOUND));
nodes.forEach(nodeInfo -> {
if (nodeInfo instanceof RootNode rootNode) {
rootNode.setReportUuid(rootNetworkEntity.getReportUuid());
Expand All @@ -358,7 +359,7 @@ private void completeNodeInfos(List<AbstractNode> nodes, UUID studyUuid) {
}

@Transactional
public AbstractNode getStudySubtree(UUID studyId, UUID parentNodeUuid) {
public AbstractNode getStudySubtree(UUID studyId, UUID parentNodeUuid, UUID rootNetworkUuid) {
// TODO: not working because of proxy appearing in tests TOFIX later
// List<UUID> nodeUuids = nodesRepository.findAllDescendants(parentNodeUuid).stream().map(UUID::fromString).toList();
// List<NodeEntity> nodes = nodesRepository.findAllById(nodeUuids);
Expand All @@ -367,7 +368,7 @@ public AbstractNode getStudySubtree(UUID studyId, UUID parentNodeUuid) {
List<AbstractNode> allNodeInfos = new ArrayList<>();
repositories.forEach((key, repository) -> allNodeInfos.addAll(repository.getAll(
nodes.stream().filter(n -> n.getType().equals(key)).map(NodeEntity::getIdNode).collect(Collectors.toSet()))));
completeNodeInfos(allNodeInfos, studyId);
completeNodeInfos(allNodeInfos, studyId, rootNetworkUuid);
Map<UUID, AbstractNode> fullMap = allNodeInfos.stream().collect(Collectors.toMap(AbstractNode::getId, Function.identity()));

nodes.stream()
Expand All @@ -377,9 +378,9 @@ public AbstractNode getStudySubtree(UUID studyId, UUID parentNodeUuid) {
}

@Transactional
public void duplicateStudyNodes(StudyEntity studyEntity, UUID sourceStudyUuid) {
public void duplicateStudyNodes(StudyEntity studyEntity, UUID sourceStudyUuid, UUID rootNetworkUuid) {
createRoot(studyEntity);
AbstractNode rootNode = getStudyTree(sourceStudyUuid);
AbstractNode rootNode = getStudyTree(sourceStudyUuid, rootNetworkUuid);
cloneStudyTree(rootNode, null, studyEntity);
}

Expand Down Expand Up @@ -643,7 +644,8 @@ private void getBuildInfos(NodeEntity nodeEntity, UUID rootNetworkUuid, BuildInf
AbstractNode node = repositories.get(nodeEntity.getType()).getNode(nodeEntity.getIdNode());
if (node.getType() == NodeType.NETWORK_MODIFICATION) {
NetworkModificationNode modificationNode = (NetworkModificationNode) node;
if (!modificationNode.getNodeBuildStatus().isBuilt()) {
RootNetworkNodeInfoEntity rootNetworkNodeInfoEntity = rootNetworkNodeInfoService.getRootNetworkNodeInfo(nodeEntity.getIdNode(), rootNetworkUuid).orElseThrow(() -> new StudyException(ROOT_NETWORK_NOT_FOUND));
if (!rootNetworkNodeInfoEntity.getNodeBuildStatus().toDto().isBuilt()) {
UUID reportUuid = getModificationReportUuid(nodeEntity.getIdNode(), rootNetworkUuid, nodeToBuildUuid);
buildInfos.insertModificationInfos(modificationNode.getModificationGroupUuid(), new ReportInfos(reportUuid, modificationNode.getId()));
getBuildInfos(nodeEntity.getParentNode(), rootNetworkUuid, buildInfos, nodeToBuildUuid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.gridsuite.study.server.StudyException;
import org.gridsuite.study.server.dto.*;
import org.gridsuite.study.server.dto.dynamicsimulation.DynamicSimulationStatus;
import org.gridsuite.study.server.dto.modification.NetworkModificationContextInfos;
import org.gridsuite.study.server.dto.sensianalysis.SensitivityAnalysisCsvFileInfos;
import org.gridsuite.study.server.dto.timeseries.TimeSeriesMetadataInfos;
import org.gridsuite.study.server.dto.timeseries.TimelineEventInfos;
Expand Down Expand Up @@ -407,6 +408,13 @@ public List<CompletableFuture<Void>> getDeleteRootNetworkNodeInfosFutures(List<R
);
}

public NetworkModificationContextInfos getNetworkModificationContextInfos(UUID rootNetworkUuid, UUID nodeUuid, UUID networkUuid) {
RootNetworkNodeInfoEntity rootNetworkNodeInfoEntity = getRootNetworkNodeInfo(nodeUuid, rootNetworkUuid).orElseThrow(() -> new StudyException(ROOT_NETWORK_NOT_FOUND));
String variantId = rootNetworkNodeInfoEntity.getVariantId();
UUID reportUuid = rootNetworkNodeInfoEntity.getModificationReports().get(nodeUuid);
return new NetworkModificationContextInfos(networkUuid, variantId, reportUuid, nodeUuid);
}

private List<UUID> getReportUuids(RootNetworkNodeInfo rootNetworkNodeInfo) {
return Stream.of(
rootNetworkNodeInfo.getModificationReports().values().stream(),
Expand Down
Loading

0 comments on commit 3c682ce

Please sign in to comment.