Skip to content

Commit e5efe48

Browse files
authored
Add reorder spreadsheet configs (#32)
Signed-off-by: achour94 <[email protected]>
1 parent 1476072 commit e5efe48

File tree

6 files changed

+94
-6
lines changed

6 files changed

+94
-6
lines changed

src/main/java/org/gridsuite/studyconfig/server/controller/SpreadsheetConfigCollectionController.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,16 @@ public ResponseEntity<Void> removeSpreadsheetConfigFromCollection(
137137
return ResponseEntity.noContent().build();
138138
}
139139

140+
@PutMapping("/{id}/reorder")
141+
@Operation(summary = "Reorder spreadsheet configs in a collection",
142+
description = "Updates the order of spreadsheet configs within a collection")
143+
@ApiResponse(responseCode = "204", description = "Order updated")
144+
@ApiResponse(responseCode = "404", description = "Collection not found")
145+
public ResponseEntity<Void> reorderSpreadsheetConfigs(
146+
@Parameter(description = "ID of the configuration collection") @PathVariable UUID id,
147+
@Valid @RequestBody List<UUID> newOrder) {
148+
spreadsheetConfigService.reorderSpreadsheetConfigs(id, newOrder);
149+
return ResponseEntity.noContent().build();
150+
}
151+
140152
}

src/main/java/org/gridsuite/studyconfig/server/entities/SpreadsheetConfigCollectionEntity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class SpreadsheetConfigCollectionEntity {
3232

3333
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
3434
@JoinColumn(name = "collection_id", foreignKey = @ForeignKey(name = "fk_spreadsheet_config_collection"))
35+
@OrderColumn(name = "config_order")
3536
@Builder.Default
3637
private List<SpreadsheetConfigEntity> spreadsheetConfigs = new ArrayList<>();
3738
}

src/main/java/org/gridsuite/studyconfig/server/service/SpreadsheetConfigService.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@
2727

2828
import java.io.IOException;
2929
import java.io.InputStream;
30-
import java.util.List;
31-
import java.util.Objects;
32-
import java.util.UUID;
30+
import java.util.*;
31+
import java.util.stream.Collectors;
3332

3433
/**
3534
* @author Achour BERRAHMA <achour.berrahma at rte-france.com>
@@ -303,4 +302,26 @@ public void removeSpreadsheetConfigFromCollection(UUID collectionId, UUID config
303302
spreadsheetConfigCollectionRepository.save(collection);
304303
}
305304

305+
@Transactional
306+
public void reorderSpreadsheetConfigs(UUID collectionId, List<UUID> newOrder) {
307+
SpreadsheetConfigCollectionEntity collection = spreadsheetConfigCollectionRepository.findById(collectionId)
308+
.orElseThrow(() -> new EntityNotFoundException(SPREADSHEET_CONFIG_COLLECTION_NOT_FOUND + collectionId));
309+
310+
// Validate inputs
311+
Set<UUID> existingIds = collection.getSpreadsheetConfigs().stream()
312+
.map(SpreadsheetConfigEntity::getId)
313+
.collect(Collectors.toSet());
314+
315+
if (existingIds.size() != newOrder.size() || !existingIds.containsAll(newOrder)) {
316+
throw new IllegalArgumentException("New order must contain exactly the same configs as the collection");
317+
}
318+
319+
List<SpreadsheetConfigEntity> configs = collection.getSpreadsheetConfigs();
320+
configs.sort((c1, c2) -> {
321+
int idx1 = newOrder.indexOf(c1.getId());
322+
int idx2 = newOrder.indexOf(c2.getId());
323+
return Integer.compare(idx1, idx2);
324+
});
325+
}
326+
306327
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
2+
<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">
3+
<changeSet author="berrahmaach (generated)" id="1741095166265-1">
4+
<addColumn tableName="spreadsheet_config">
5+
<column name="config_order" type="integer"/>
6+
</addColumn>
7+
</changeSet>
8+
<changeSet author="berrahmaach (generated)" id="1741095166265-2">
9+
<sql dbms="postgresql">
10+
WITH ordered_configs AS (
11+
SELECT id, collection_id, ROW_NUMBER() OVER (PARTITION BY collection_id ORDER BY id) - 1 as seq
12+
FROM spreadsheet_config
13+
WHERE collection_id IS NOT NULL
14+
)
15+
UPDATE spreadsheet_config sc
16+
SET config_order = oc.seq
17+
FROM ordered_configs oc
18+
WHERE sc.id = oc.id;
19+
</sql>
20+
</changeSet>
21+
</databaseChangeLog>

src/main/resources/db/changelog/db.changelog-master.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ databaseChangeLog:
2323
- include:
2424
file: changesets/changelog_20250220T172856Z.xml
2525
relativeToChangelogFile: true
26+
- include:
27+
file: changesets/changelog_20250304T133229Z.xml
28+
relativeToChangelogFile: true

src/test/java/org/gridsuite/studyconfig/server/SpreadsheetConfigCollectionIntegrationTest.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
import org.springframework.test.web.servlet.MockMvc;
2525
import org.springframework.test.web.servlet.MvcResult;
2626

27-
import java.util.Arrays;
28-
import java.util.List;
29-
import java.util.UUID;
27+
import java.util.*;
3028

3129
import static org.assertj.core.api.Assertions.assertThat;
3230
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
@@ -228,6 +226,38 @@ void testRemoveNonExistentSpreadsheetConfig() throws Exception {
228226
.andExpect(status().isNotFound());
229227
}
230228

229+
@Test
230+
void testReorderSpreadsheetConfigs() throws Exception {
231+
// Create a collection with multiple configs
232+
SpreadsheetConfigCollectionInfos collection = new SpreadsheetConfigCollectionInfos(null, createSpreadsheetConfigs());
233+
UUID collectionId = postSpreadsheetConfigCollection(collection);
234+
235+
// Get the created collection to get the config IDs
236+
SpreadsheetConfigCollectionInfos createdCollection = getSpreadsheetConfigCollection(collectionId);
237+
List<UUID> configIds = createdCollection.spreadsheetConfigs().stream()
238+
.map(SpreadsheetConfigInfos::id)
239+
.toList();
240+
241+
// Create a new order (reverse the existing order)
242+
List<UUID> newOrder = new ArrayList<>(configIds);
243+
Collections.reverse(newOrder);
244+
245+
// Send the reorder request
246+
String newOrderJson = mapper.writeValueAsString(newOrder);
247+
mockMvc.perform(put("/v1/spreadsheet-config-collections/" + collectionId + "/reorder")
248+
.content(newOrderJson)
249+
.contentType(MediaType.APPLICATION_JSON))
250+
.andExpect(status().isNoContent());
251+
252+
// Get the collection again and verify the order has changed
253+
SpreadsheetConfigCollectionInfos updatedCollection = getSpreadsheetConfigCollection(collectionId);
254+
List<UUID> updatedConfigIds = updatedCollection.spreadsheetConfigs().stream()
255+
.map(SpreadsheetConfigInfos::id)
256+
.toList();
257+
258+
assertThat(updatedConfigIds).isEqualTo(newOrder);
259+
}
260+
231261
private List<SpreadsheetConfigInfos> createSpreadsheetConfigs() {
232262
List<ColumnInfos> columnInfos = Arrays.asList(
233263
new ColumnInfos(null, "cust_a", ColumnType.NUMBER, 1, "cust_b + cust_c", "[\"cust_b\", \"cust_c\"]", "idA"),

0 commit comments

Comments
 (0)