Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New API point to update the nodes' column position #654

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
13 changes: 13 additions & 0 deletions src/main/java/org/gridsuite/study/server/StudyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,19 @@ public ResponseEntity<Void> updateNode(@RequestBody NetworkModificationNode node
return ResponseEntity.ok().build();
}

@PutMapping(value = "/studies/{studyUuid}/tree/nodes/{parentUuid}/children-column-positions")
@Operation(summary = "update children column positions")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "the node column positions have been updated"),
@ApiResponse(responseCode = "404", description = "The study or a node was not found")})
public ResponseEntity<Void> updateNodesColumnPositions(@RequestBody List<NetworkModificationNode> children,
@Parameter(description = "study uuid") @PathVariable("studyUuid") UUID studyUuid,
@Parameter(description = "parent node uuid") @PathVariable("parentUuid") UUID parentUuid,
@RequestHeader(HEADER_USER_ID) String userId) {
networkModificationTreeService.updateNodesColumnPositions(studyUuid, parentUuid, children, userId);
return ResponseEntity.ok().build();
}

@GetMapping(value = "/studies/{studyUuid}/tree/nodes/{id}")
@Operation(summary = "get simplified node")
@ApiResponses(value = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ protected U completeNodeInfo(AbstractNodeInfoEntity nodeInfoEntity, U node) {
node.setId(nodeInfoEntity.getId());
node.setName(nodeInfoEntity.getName());
node.setDescription(nodeInfoEntity.getDescription());
node.setColumnPosition(nodeInfoEntity.getColumnPosition());
node.setReadOnly(nodeInfoEntity.getReadOnly());
return node;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public abstract class AbstractNode {

String description;

Integer columnPosition;

Boolean readOnly;

NodeType type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public UUID getId() {
@Column
String description;

@Column
Integer columnPosition;

@Column
Boolean readOnly;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.time.Instant;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.UUID;

Expand Down Expand Up @@ -112,6 +113,7 @@ public class NotificationService {
public static final String NODE_RENAMED = "nodeRenamed";
public static final String NODE_BUILD_STATUS_UPDATED = "nodeBuildStatusUpdated";
public static final String SUBTREE_MOVED = "subtreeMoved";
public static final String COLUMNS_CHANGED = "columnsChanged";
public static final String SUBTREE_CREATED = "subtreeCreated";
public static final String MESSAGE_LOG = "Sending message : {}";
public static final String DEFAULT_ERROR_MESSAGE = "Unknown error";
Expand Down Expand Up @@ -300,6 +302,20 @@ public void emitSubtreeMoved(UUID studyUuid, UUID parentNodeSubtreeMoved, UUID r
);
}

@PostCompletion
public void emitColumnsChanged(UUID studyUuid, UUID parentNodeUuid, List<UUID> orderedUuids) {
try {
sendUpdateMessage(MessageBuilder.withPayload(objectMapper.writeValueAsString(orderedUuids))
.setHeader(HEADER_STUDY_UUID, studyUuid)
.setHeader(HEADER_UPDATE_TYPE, COLUMNS_CHANGED)
.setHeader(HEADER_PARENT_NODE, parentNodeUuid)
.build()
);
} catch (JsonProcessingException e) {
LOGGER.error("Unable to notify on column positions update", e);
}
}

@PostCompletion
public void emitSubtreeInserted(UUID studyUuid, UUID parentNodeSubtreeInserted, UUID referenceNodeUuid) {
sendUpdateMessage(MessageBuilder.withPayload("")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,27 @@ public void updateNode(UUID studyUuid, NetworkModificationNode node, String user
notificationService.emitElementUpdated(studyUuid, userId);
}

@Transactional
public void updateNodesColumnPositions(UUID studyUuid, UUID parentUuid, List<NetworkModificationNode> childrenNodes, String userId) {
// Convert to a map for quick lookup
Map<UUID, Integer> nodeIdToColumnPosition = childrenNodes.stream()
.collect(Collectors.toMap(NetworkModificationNode::getId, NetworkModificationNode::getColumnPosition));

List<UUID> childrenIds = childrenNodes.stream().map(NetworkModificationNode::getId).toList();
networkModificationNodeInfoRepository.findAllById(childrenIds).forEach(entity -> {
Integer newColumnPosition = nodeIdToColumnPosition.get(entity.getId());
entity.setColumnPosition(Objects.requireNonNull(newColumnPosition));
});

List<UUID> orderedUuids = childrenNodes.stream()
.sorted(Comparator.comparingInt(AbstractNode::getColumnPosition))
.map(NetworkModificationNode::getId)
.toList();

notificationService.emitColumnsChanged(studyUuid, parentUuid, orderedUuids);
notificationService.emitElementUpdated(studyUuid, userId);
}

private boolean isRenameNode(AbstractNode node) {
NetworkModificationNode renameNode = NetworkModificationNode.builder()
.id(node.getId())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-latest.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">
<changeSet author="boutiercha (generated)" id="1734094779057-10">
<addColumn tableName="network_modification_node_info">
<column name="column_position" type="integer"/>
</addColumn>
</changeSet>
<changeSet author="boutiercha (generated)" id="1734094779057-11">
<addColumn tableName="root_node_info">
<column name="column_position" type="integer"/>
</addColumn>
</changeSet>
</databaseChangeLog>
3 changes: 3 additions & 0 deletions src/main/resources/db/changelog/db.changelog-master.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,7 @@ databaseChangeLog:
relativeToChangelogFile: true
- include:
file: changesets/changelog_20241211T123019Z.xml
relativeToChangelogFile: true
- include:
file: changesets/changelog_20241213T125757Z.xml
relativeToChangelogFile: true
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,35 @@ void testNodeUpdate() throws Exception {
.andExpect(status().isNotFound());
}

@Test
void testUpdateNodesColumnPositions() throws Exception {
String userId = "userId";
RootNode root = createRoot();
final NetworkModificationNode node1 = buildNetworkModificationNode("nod", "silently", UUID.randomUUID(), VARIANT_ID, UUID.randomUUID(), UUID.randomUUID(), UUID.randomUUID(), UUID.randomUUID(), UUID.randomUUID(), UUID.randomUUID(), UUID.randomUUID(), BuildStatus.NOT_BUILT);
final NetworkModificationNode node2 = buildNetworkModificationNode("nodding", "politely", UUID.randomUUID(), VARIANT_ID, UUID.randomUUID(), UUID.randomUUID(), UUID.randomUUID(), UUID.randomUUID(), UUID.randomUUID(), UUID.randomUUID(), UUID.randomUUID(), BuildStatus.NOT_BUILT);
createNode(root.getStudyId(), root, node1, userId);
createNode(root.getStudyId(), root, node2, userId);
assertNull(node1.getColumnPosition());
node1.setColumnPosition(1);
node2.setColumnPosition(0);
ArrayList<NetworkModificationNode> nodes = new ArrayList<>();
nodes.add(node1);
nodes.add(node2);

mockMvc.perform(put("/v1/studies/{studyUuid}/tree/nodes/{parentUuid}/children-column-positions", root.getStudyId(), root.getId())
.contentType(MediaType.APPLICATION_JSON)
.content(objectWriter.writeValueAsString(nodes))
.header(USER_ID_HEADER, "userId"))
.andExpect(status().isOk());

SlimaneAmar marked this conversation as resolved.
Show resolved Hide resolved
UUID[] expectedInOrder = new UUID[]{
node2.getId(),
node1.getId()
};
checkColumnsChangedMessageSent(root.getStudyId(), root.getId(), expectedInOrder);
checkElementUpdatedMessageSent(root.getStudyId(), userId);
}

// This test is for a part of the code that is not used yet
// We update a node description (this is not used in the front) and we assume that it will emit a nodeUpdated notif
// If it's not the case or if this test causes problems feel free to update it / remove it as needed
Expand Down Expand Up @@ -1336,6 +1365,14 @@ private void checkElementUpdatedMessageSent(UUID elementUuid, String userId) {
assertEquals(userId, message.getHeaders().get(NotificationService.HEADER_MODIFIED_BY));
}

private void checkColumnsChangedMessageSent(UUID studyUuid, UUID parentNodeUuid, UUID[] orderedUuids) throws Exception {
Message<byte[]> message = output.receive(TIMEOUT, STUDY_UPDATE_DESTINATION);
assertEquals(NotificationService.COLUMNS_CHANGED, message.getHeaders().get(NotificationService.HEADER_UPDATE_TYPE));
assertEquals(studyUuid, message.getHeaders().get(NotificationService.HEADER_STUDY_UUID));
assertEquals(parentNodeUuid, message.getHeaders().get(NotificationService.HEADER_PARENT_NODE));
assertEquals(objectMapper.writeValueAsString(orderedUuids), new String(message.getPayload()));
}

private void checkUpdateNodesMessageReceived(UUID studyUuid, List<UUID> nodesUuids) {
Message<byte[]> messageStatus = output.receive(TIMEOUT, STUDY_UPDATE_DESTINATION);
assertEquals("", new String(messageStatus.getPayload()));
Expand Down
Loading