Skip to content

Commit 73f4b17

Browse files
authored
Add append mode in spreadsheet collection import (#44)
Signed-off-by: David BRAQUART <[email protected]>
1 parent c4b9bbb commit 73f4b17

File tree

3 files changed

+74
-5
lines changed

3 files changed

+74
-5
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
@@ -105,6 +105,18 @@ public ResponseEntity<Void> updateSpreadsheetConfigCollectionWithConfigs(
105105
return ResponseEntity.noContent().build();
106106
}
107107

108+
@PutMapping("/{id}/append")
109+
@Operation(summary = "Append a collection to another collection",
110+
description = "")
111+
@ApiResponse(responseCode = "204", description = "Collection updated")
112+
@ApiResponse(responseCode = "404", description = "One of the collections not found")
113+
public ResponseEntity<Void> appendSpreadsheetConfigCollection(
114+
@Parameter(description = "ID of the configuration collection to update") @PathVariable UUID id,
115+
@Parameter(description = "ID of the configuration collection to be appended") @RequestParam(name = "sourceCollection") UUID sourceCollectionId) {
116+
spreadsheetConfigService.appendSpreadsheetConfigCollection(id, sourceCollectionId);
117+
return ResponseEntity.noContent().build();
118+
}
119+
108120
@PostMapping(params = { "duplicateFrom" })
109121
@Operation(summary = "Duplicate a spreadsheet configuration collection",
110122
description = "Creates a copy of an existing spreadsheet configuration collection")

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,40 @@ public void updateSpreadsheetConfigCollectionWithConfigs(UUID id, List<UUID> con
190190
.toList());
191191
}
192192

193+
@Transactional
194+
public void appendSpreadsheetConfigCollection(UUID id, UUID sourceCollectionId) {
195+
SpreadsheetConfigCollectionEntity targetEntity = spreadsheetConfigCollectionRepository.findById(id)
196+
.orElseThrow(() -> new EntityNotFoundException(SPREADSHEET_CONFIG_COLLECTION_NOT_FOUND + id));
197+
SpreadsheetConfigCollectionEntity sourceEntity = spreadsheetConfigCollectionRepository.findById(sourceCollectionId)
198+
.orElseThrow(() -> new EntityNotFoundException(SPREADSHEET_CONFIG_COLLECTION_NOT_FOUND + sourceCollectionId));
199+
// Make sure names are unique in the merged collection
200+
Set<String> targetNames = targetEntity.getSpreadsheetConfigs().stream().map(SpreadsheetConfigEntity::getName).collect(Collectors.toSet());
201+
Set<String> sourceNames = sourceEntity.getSpreadsheetConfigs().stream().map(SpreadsheetConfigEntity::getName).collect(Collectors.toSet());
202+
targetEntity.getSpreadsheetConfigs().addAll(sourceEntity.getSpreadsheetConfigs().stream().map(SpreadsheetConfigEntity::getId)
203+
.map(configId -> {
204+
SpreadsheetConfigEntity clone = duplicateSpreadsheetConfigEntity(configId);
205+
clone.setName(getUniqueName(clone.getName(), targetNames, sourceNames));
206+
return clone;
207+
})
208+
.toList());
209+
// keep only aliases of appended collection, they will be invalidated by the Front
210+
targetEntity.getNodeAliases().clear();
211+
targetEntity.getNodeAliases().addAll(sourceEntity.getNodeAliases());
212+
}
213+
214+
private String getUniqueName(String name, Set<String> targetNames, Set<String> sourceNames) {
215+
if (!targetNames.contains(name)) {
216+
return name;
217+
}
218+
final String newNameFormat = name + " (%d)";
219+
String nextName;
220+
int x = 0;
221+
do {
222+
nextName = String.format(newNameFormat, ++x);
223+
} while (targetNames.contains(nextName) || sourceNames.contains(nextName));
224+
return nextName;
225+
}
226+
193227
@Transactional
194228
public UUID duplicateSpreadsheetConfigCollection(UUID id) {
195229
SpreadsheetConfigCollectionEntity entity = spreadsheetConfigCollectionRepository.findById(id)

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

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,29 @@ void testUpdateCollection() throws Exception {
123123
.isEqualTo(updatedCollection);
124124
}
125125

126+
@Test
127+
void testAppendCollection() throws Exception {
128+
List<String> existingAliases = List.of("n1", "n2", "n3");
129+
SpreadsheetConfigCollectionInfos collectionToUpdate = new SpreadsheetConfigCollectionInfos(null, createSpreadsheetConfigs(), existingAliases);
130+
UUID collectionUuid = saveAndReturnId(collectionToUpdate);
131+
132+
List<String> appendedAliases = List.of("n1", "n6", "n3");
133+
SpreadsheetConfigCollectionInfos appendedCollection = new SpreadsheetConfigCollectionInfos(null, createUpdatedSpreadsheetConfigs(), appendedAliases);
134+
UUID appendedCollectionUuid = saveAndReturnId(appendedCollection);
135+
136+
mockMvc.perform(put(URI_SPREADSHEET_CONFIG_COLLECTION_BASE + "/" + collectionUuid + "/append?sourceCollection=" + appendedCollectionUuid)
137+
.contentType(MediaType.APPLICATION_JSON))
138+
.andExpect(status().isNoContent());
139+
140+
SpreadsheetConfigCollectionInfos mergedCollection = getSpreadsheetConfigCollection(collectionUuid);
141+
142+
// We have 3 new tabs coming from the appended collection, but 2 have been renamed to ensure name uniqueness.
143+
assertThat(mergedCollection.spreadsheetConfigs().stream().map(SpreadsheetConfigInfos::name).toList())
144+
.isEqualTo(List.of("TestSheet", "TestSheet1", "Generator", "TestSheet (2)", "TestSheet (1)"));
145+
// In the appended collection, we keep only the aliases from the appended collection
146+
assertThat(mergedCollection.nodeAliases()).isEqualTo(appendedAliases);
147+
}
148+
126149
@Test
127150
void testDeleteCollection() throws Exception {
128151
SpreadsheetConfigCollectionInfos collectionToDelete = new SpreadsheetConfigCollectionInfos(null, createSpreadsheetConfigs(), null);
@@ -185,8 +208,8 @@ void testAddSpreadsheetConfigToCollection() throws Exception {
185208
SpreadsheetConfigCollectionInfos initialCollection = new SpreadsheetConfigCollectionInfos(null, createSpreadsheetConfigs(), null);
186209
UUID collectionUuid = postSpreadsheetConfigCollection(initialCollection);
187210

188-
List<ColumnInfos> columnInfos = Arrays.asList(
189-
new ColumnInfos(null, "new_col", ColumnType.NUMBER, 1, "formula", "[\"dep\"]", "idNew")
211+
List<ColumnInfos> columnInfos = List.of(
212+
new ColumnInfos(null, "new_col", ColumnType.NUMBER, 1, "formula", "[\"dep\"]", "idNew")
190213
);
191214
SpreadsheetConfigInfos newConfig = new SpreadsheetConfigInfos(null, "NewSheet", SheetType.BATTERY, columnInfos);
192215

@@ -212,7 +235,7 @@ void testRemoveSpreadsheetConfigFromCollection() throws Exception {
212235
UUID collectionUuid = postSpreadsheetConfigCollection(initialCollection);
213236

214237
SpreadsheetConfigCollectionInfos createdCollection = getSpreadsheetConfigCollection(collectionUuid);
215-
UUID configIdToRemove = createdCollection.spreadsheetConfigs().get(0).id();
238+
UUID configIdToRemove = createdCollection.spreadsheetConfigs().getFirst().id();
216239

217240
mockMvc.perform(delete(URI_SPREADSHEET_CONFIG_COLLECTION_BASE + "/" + collectionUuid + "/spreadsheet-configs/" + configIdToRemove))
218241
.andExpect(status().isNoContent());
@@ -343,8 +366,8 @@ private List<SpreadsheetConfigInfos> createUpdatedSpreadsheetConfigs() {
343366

344367
return List.of(
345368
new SpreadsheetConfigInfos(null, "Generator", SheetType.GENERATOR, columnInfos),
346-
new SpreadsheetConfigInfos(null, "Generator1", SheetType.GENERATOR, columnInfos),
347-
new SpreadsheetConfigInfos(null, "Battery", SheetType.BATTERY, columnInfos)
369+
new SpreadsheetConfigInfos(null, "TestSheet", SheetType.GENERATOR, columnInfos),
370+
new SpreadsheetConfigInfos(null, "TestSheet (1)", SheetType.BATTERY, columnInfos)
348371
);
349372
}
350373

0 commit comments

Comments
 (0)