Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import org.gridsuite.study.server.networkmodificationtree.entities.AbstractNodeInfoEntity;
import org.gridsuite.study.server.networkmodificationtree.entities.NetworkModificationNodeInfoEntity;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.UUID;
Expand All @@ -20,4 +21,7 @@ public interface NetworkModificationNodeInfoRepository extends NodeInfoRepositor
List<AbstractNodeInfoEntity> findAllByNodeStudyIdAndName(UUID studyUuid, String name);

List<NetworkModificationNodeInfoEntity> findByModificationGroupUuidIn(List<UUID> modificationGroupUuid);

@Query("select n.columnPosition from NetworkModificationNodeInfoEntity n where n.idNode in :uuids")
List<Integer> findColumnPositionsByUuidIn(List<UUID> uuids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public interface NodeRepository extends JpaRepository<NodeEntity, UUID> {
@NativeQuery("UPDATE node_export SET status = :status WHERE export_uuid = :exportUuid")
void updateExportNetworkStatus(UUID exportUuid, String status);

@NativeQuery("select cast(n.id_node AS VARCHAR) from NODE n where n.parent_node = :nodeUuid")
List<UUID> findChildrenUuids(UUID nodeUuid);

@NativeQuery("WITH RECURSIVE NodeHierarchy (id_node) AS ( " +
" SELECT n0.id_node" +
" FROM NODE n0 " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ public NetworkModificationTreeService(NodeRepository nodesRepository,
this.reportService = reportService;
}

private NodeEntity createNetworkModificationNode(StudyEntity study, NodeEntity parentNode, NetworkModificationNode networkModificationNode) {
private NetworkModificationNodeInfoEntity createNetworkModificationNode(StudyEntity study, NodeEntity parentNode, NetworkModificationNode networkModificationNode) {
NodeEntity newNode = nodesRepository.save(new NodeEntity(null, parentNode, NodeType.NETWORK_MODIFICATION, study, false, null, new ArrayList<>()));
if (networkModificationNode.getModificationGroupUuid() == null) {
networkModificationNode.setModificationGroupUuid(UUID.randomUUID());
}
if (networkModificationNode.getNodeType() == null) {
networkModificationNode.setNodeType(NetworkModificationNodeType.CONSTRUCTION);
}
networkModificationNodeInfoRepository.save(
NetworkModificationNodeInfoEntity newNodeInfo = networkModificationNodeInfoRepository.save(
NetworkModificationNodeInfoEntity.builder()
.modificationGroupUuid(networkModificationNode.getModificationGroupUuid())
.idNode(newNode.getIdNode())
Expand All @@ -96,7 +96,8 @@ private NodeEntity createNetworkModificationNode(StudyEntity study, NodeEntity p
.nodeType(networkModificationNode.getNodeType())
.build()
);
return newNode;
newNodeInfo.setNode(newNode);
return newNodeInfo;
}

public List<ModificationsSearchResultByNode> getNetworkModificationsByNodeInfos(
Expand All @@ -114,33 +115,52 @@ public List<ModificationsSearchResultByNode> getNetworkModificationsByNodeInfos(
.toList();
}

// TODO test if studyUuid exist and have a node <nodeId>
private NetworkModificationNode createAndInsertNode(StudyEntity study, UUID nodeId, NetworkModificationNode nodeInfo, InsertMode insertMode, String userId) {
private NetworkModificationNode createAndInsertNode(StudyEntity study, UUID nodeId, NetworkModificationNode nodeInfo, InsertMode insertMode) {
NodeEntity reference = getNodeEntity(nodeId);

assertNodeNameNotExist(study.getId(), nodeInfo.getName());

if (insertMode.equals(InsertMode.BEFORE) && reference.getType().equals(NodeType.ROOT)) {
throw new StudyException(NOT_ALLOWED);
}
NodeEntity parent = insertMode.equals(InsertMode.BEFORE) ? reference.getParentNode() : reference;
NodeEntity node = createNetworkModificationNode(study, parent, nodeInfo);
nodeInfo.setId(node.getIdNode());

if (insertMode.equals(InsertMode.BEFORE)) {
reference.setParentNode(node);
} else if (insertMode.equals(InsertMode.AFTER)) {
getChildren(nodeId).stream()
.filter(n -> !n.getIdNode().equals(node.getIdNode()))
.forEach(child -> child.setParentNode(node));
// Create node
NetworkModificationNodeInfoEntity nodeInfoEntity = createNetworkModificationNode(study, insertMode.equals(InsertMode.BEFORE) ? reference.getParentNode() : reference, nodeInfo);
NodeEntity nodeEntity = nodeInfoEntity.getNode();
nodeInfo.setId(nodeInfoEntity.getIdNode());

// Set parent and position
switch (insertMode) {
case CHILD -> nodeInfoEntity.setColumnPosition(getNextColumnPosition(reference.getIdNode()));
case BEFORE -> {
AbstractNodeInfoEntity referenceNodeInfoEntity = getNodeInfoEntity(reference.getIdNode());
nodeInfoEntity.setColumnPosition(referenceNodeInfoEntity.getColumnPosition());
reference.setParentNode(nodeEntity);
referenceNodeInfoEntity.setColumnPosition(0);
}
case AFTER -> {
nodeInfoEntity.setColumnPosition(0);
getChildren(nodeId).stream()
.filter(n -> !n.getIdNode().equals(nodeEntity.getIdNode()))
.forEach(child -> child.setParentNode(nodeEntity));
}
}
return nodeInfo;
}

private int getNextColumnPosition(UUID parentNodeId) {
return networkModificationNodeInfoRepository.findColumnPositionsByUuidIn(nodesRepository.findChildrenUuids(parentNodeId))
.stream()
.filter(Objects::nonNull)
.mapToInt(i -> i)
.max()
.orElse(-1) + 1;
}
Comment thread
SlimaneAmar marked this conversation as resolved.

@Transactional
public NetworkModificationNode createNode(@NonNull StudyEntity study, @NonNull UUID nodeId, @NonNull NetworkModificationNode nodeInfo, @NonNull InsertMode insertMode, String userId) {
// create new node
NetworkModificationNode newNode = createAndInsertNode(study, nodeId, nodeInfo, insertMode, userId);
NetworkModificationNode newNode = createAndInsertNode(study, nodeId, nodeInfo, insertMode);

NetworkModificationNodeInfoEntity newNodeInfoEntity = networkModificationNodeInfoRepository.getReferenceById(newNode.getId());
rootNetworkNodeInfoService.createNodeLinks(study, newNodeInfoEntity);
Expand All @@ -150,7 +170,7 @@ public NetworkModificationNode createNode(@NonNull StudyEntity study, @NonNull U

private NetworkModificationNode duplicateNode(@NonNull StudyEntity targetStudy, @NonNull UUID referenceNodeId, @NonNull NetworkModificationNode newNodeInfo, @NonNull UUID originNodeUuid, @NonNull InsertMode insertMode, Map<UUID, UUID> mappingModificationUuids) {
// create new node
NetworkModificationNode newNode = createAndInsertNode(targetStudy, referenceNodeId, newNodeInfo, insertMode, null);
NetworkModificationNode newNode = createAndInsertNode(targetStudy, referenceNodeId, newNodeInfo, insertMode);

NetworkModificationNodeInfoEntity newNodeInfoEntity = networkModificationNodeInfoRepository.getReferenceById(newNode.getId());
NetworkModificationNodeInfoEntity originNodeInfoEntity = networkModificationNodeInfoRepository.getReferenceById(originNodeUuid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1031,30 +1031,32 @@ void testNodeInsertion() throws Exception {
createNode(root.getStudyId(), root, networkModification1, userId);
createNode(root.getStudyId(), root, networkModification2, userId);
root = getRootNode(root.getStudyId());
assertEquals(Set.of(0, 1), root.getChildren().stream().map(AbstractNode::getColumnPosition).collect(Collectors.toCollection(TreeSet::new)));
/* root
/ \
n1 n2
*/
AbstractNode unchangedNode = root.getChildren().get(0);
AbstractNode willBeMoved = root.getChildren().get(1);
insertNode(root.getStudyId(), willBeMoved, networkModification3, InsertMode.BEFORE, root, userId);
root = getRootNode(root.getStudyId());
assertEquals(Set.of(0, 1), root.getChildren().stream().map(AbstractNode::getColumnPosition).collect(Collectors.toCollection(TreeSet::new)));
/* root
/ \
n3 n2
/
n1
*/
root = getRootNode(root.getStudyId());
assertEquals(1, root.getChildren().stream().filter(child -> child.getId().equals(unchangedNode.getId())).count());
AbstractNode newNode = root.getChildren().get(0).getId().equals(unchangedNode.getId()) ? root.getChildren().get(1) : root.getChildren().get(0);
assertEquals(willBeMoved.getId(), newNode.getChildren().get(0).getId());
assertEquals(Set.of(0), newNode.getChildren().stream().map(AbstractNode::getColumnPosition).collect(Collectors.toCollection(TreeSet::new)));

mockMvc.perform(post("/v1/studies/{studyUuid}/tree/nodes/{id}", root.getStudyId(), UUID.randomUUID())
.contentType(MediaType.APPLICATION_JSON)
.content(objectWriter.writeValueAsString(networkModification1))
.header(USER_ID_HEADER, "userId"))
.andExpect(status().isNotFound());

}

@Test
Expand All @@ -1070,12 +1072,18 @@ void testInsertAfter() throws Exception {
createNode(root.getStudyId(), root, node2, userId);
createNode(root.getStudyId(), root, node3, userId);
root = getRootNode(root.getStudyId());
var originalChildren = root.getChildren().stream().map(AbstractNode::getId).collect(Collectors.toSet());
Set<UUID> originalChildrenUuids = root.getChildren().stream().map(AbstractNode::getId).collect(Collectors.toSet());
assertEquals(Set.of(0, 1), root.getChildren().stream().map(AbstractNode::getColumnPosition).collect(Collectors.toCollection(TreeSet::new)));

insertNode(root.getStudyId(), root, node1, InsertMode.AFTER, root, userId);
root = getRootNode(root.getStudyId());
assertEquals(1, root.getChildren().size());
var grandChildren = getRootNode(root.getStudyId()).getChildren().get(0).getChildren().stream().map(AbstractNode::getId).collect(Collectors.toSet());
assertEquals(originalChildren, grandChildren);
assertEquals(Set.of(0), root.getChildren().stream().map(AbstractNode::getColumnPosition).collect(Collectors.toCollection(TreeSet::new)));

List<AbstractNode> grandChildren = getRootNode(root.getStudyId()).getChildren().get(0).getChildren();
Set<UUID> grandChildrenUuids = grandChildren.stream().map(AbstractNode::getId).collect(Collectors.toSet());
assertEquals(originalChildrenUuids, grandChildrenUuids);
assertEquals(Set.of(0, 1), grandChildren.stream().map(AbstractNode::getColumnPosition).collect(Collectors.toCollection(TreeSet::new)));

UUID rootNetworkUuid = studyTestUtils.getOneRootNetworkUuid(root.getStudyId());
assertEquals(VARIANT_ID, networkModificationTreeService.getVariantId(node1.getId(), rootNetworkUuid));
Expand Down
Loading
Loading