Skip to content

Commit 8737870

Browse files
authored
Add spreadsheet config name and default spreadsheet collection (#30)
Signed-off-by: Ayoub LABIDI <[email protected]>
1 parent 9209f08 commit 8737870

12 files changed

+2134
-27
lines changed

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import lombok.RequiredArgsConstructor;
1717
import org.gridsuite.studyconfig.server.StudyConfigApi;
1818
import org.gridsuite.studyconfig.server.dto.SpreadsheetConfigCollectionInfos;
19+
import org.gridsuite.studyconfig.server.dto.SpreadsheetConfigInfos;
1920
import org.gridsuite.studyconfig.server.service.SpreadsheetConfigService;
2021
import org.springframework.http.HttpStatus;
2122
import org.springframework.http.ResponseEntity;
@@ -91,7 +92,7 @@ public ResponseEntity<Void> updateSpreadsheetConfigCollection(
9192
return ResponseEntity.noContent().build();
9293
}
9394

94-
@PostMapping(value = "/duplicate", params = { "duplicateFrom" })
95+
@PostMapping(params = { "duplicateFrom" })
9596
@Operation(summary = "Duplicate a spreadsheet configuration collection",
9697
description = "Creates a copy of an existing spreadsheet configuration collection")
9798
@ApiResponse(responseCode = "201", description = "Configuration collection duplicated",
@@ -101,4 +102,39 @@ public ResponseEntity<UUID> duplicateSpreadsheetConfigCollection(@Parameter(desc
101102
UUID newId = spreadsheetConfigService.duplicateSpreadsheetConfigCollection(id);
102103
return ResponseEntity.status(HttpStatus.CREATED).body(newId);
103104
}
105+
106+
@PostMapping(value = "/default")
107+
@Operation(summary = "Create a default spreadsheet configuration collection",
108+
description = "Creates a default spreadsheet configuration collection")
109+
@ApiResponse(responseCode = "201", description = "Default configuration collection created",
110+
content = @Content(schema = @Schema(implementation = UUID.class)))
111+
public ResponseEntity<UUID> createDefaultSpreadsheetConfigCollection() {
112+
UUID id = spreadsheetConfigService.createDefaultSpreadsheetConfigCollection();
113+
return ResponseEntity.status(HttpStatus.CREATED).body(id);
114+
}
115+
116+
// spreadsheet-configs endpoints
117+
@PostMapping("/{id}/spreadsheet-configs")
118+
@Operation(summary = "Add a spreadsheet configuration to a collection",
119+
description = "Adds a new spreadsheet configuration to a collection")
120+
@ApiResponse(responseCode = "204", description = "Configuration added")
121+
@ApiResponse(responseCode = "404", description = "Configuration collection not found")
122+
public ResponseEntity<UUID> addSpreadsheetConfigToCollection(
123+
@Parameter(description = "ID of the configuration collection") @PathVariable UUID id,
124+
@Parameter(description = "Configuration to add") @Valid @RequestBody SpreadsheetConfigInfos dto) {
125+
return ResponseEntity.status(HttpStatus.CREATED).body(spreadsheetConfigService.addSpreadsheetConfigToCollection(id, dto));
126+
}
127+
128+
@DeleteMapping("/{id}/spreadsheet-configs/{configId}")
129+
@Operation(summary = "Remove a spreadsheet configuration from a collection",
130+
description = "Removes an existing spreadsheet configuration from a collection")
131+
@ApiResponse(responseCode = "204", description = "Configuration removed")
132+
@ApiResponse(responseCode = "404", description = "Configuration collection or configuration not found")
133+
public ResponseEntity<Void> removeSpreadsheetConfigFromCollection(
134+
@Parameter(description = "ID of the configuration collection") @PathVariable UUID id,
135+
@Parameter(description = "ID of the configuration to remove") @PathVariable UUID configId) {
136+
spreadsheetConfigService.removeSpreadsheetConfigFromCollection(id, configId);
137+
return ResponseEntity.noContent().build();
138+
}
139+
104140
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public ResponseEntity<UUID> createSpreadsheetConfig(@Parameter(description = "Co
5151
return ResponseEntity.status(HttpStatus.CREATED).body(id);
5252
}
5353

54-
@PostMapping(value = "/duplicate", params = { DUPLICATE_FROM })
54+
@PostMapping(params = { DUPLICATE_FROM })
5555
@Operation(summary = "Duplicate a spreadsheet configuration",
5656
description = "Creates a copy of an existing spreadsheet configuration")
5757
@ApiResponse(responseCode = "201", description = "Configuration duplicated",

src/main/java/org/gridsuite/studyconfig/server/dto/SpreadsheetConfigInfos.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public record SpreadsheetConfigInfos(
2222
@Schema(description = "Spreadsheet configuration ID")
2323
UUID id,
2424

25+
@Schema(description = "Spreadsheet configuration name")
26+
String name,
27+
2528
@NotNull(message = "Sheet type is mandatory")
2629
@Schema(description = "Spreadsheet type")
2730
SheetType sheetType,

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ public class SpreadsheetConfigEntity {
3131
@Column(name = "id")
3232
private UUID id;
3333

34+
@Column(name = "name", nullable = false)
35+
private String name;
36+
3437
@Column(name = "sheet_type", nullable = false)
3538
@Enumerated(EnumType.STRING)
3639
private SheetType sheetType;

src/main/java/org/gridsuite/studyconfig/server/mapper/SpreadsheetConfigMapper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ private SpreadsheetConfigMapper() {
2323
public static SpreadsheetConfigInfos toDto(SpreadsheetConfigEntity entity) {
2424
return new SpreadsheetConfigInfos(
2525
entity.getId(),
26+
entity.getName(),
2627
entity.getSheetType(),
2728
entity.getColumns().stream()
2829
.map(SpreadsheetConfigMapper::toColumnDto)
@@ -36,6 +37,7 @@ public static MetadataInfos toMetadataDto(SpreadsheetConfigEntity entity) {
3637

3738
public static SpreadsheetConfigEntity toEntity(SpreadsheetConfigInfos dto) {
3839
SpreadsheetConfigEntity entity = SpreadsheetConfigEntity.builder()
40+
.name(dto.name())
3941
.sheetType(dto.sheetType())
4042
.build();
4143

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

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@
1818
import org.gridsuite.studyconfig.server.mapper.SpreadsheetConfigMapper;
1919
import org.gridsuite.studyconfig.server.repositories.SpreadsheetConfigCollectionRepository;
2020
import org.gridsuite.studyconfig.server.repositories.SpreadsheetConfigRepository;
21+
import org.springframework.beans.factory.annotation.Value;
22+
import org.springframework.core.io.Resource;
2123
import org.springframework.stereotype.Service;
2224
import org.springframework.transaction.annotation.Transactional;
2325

26+
import com.fasterxml.jackson.databind.ObjectMapper;
27+
28+
import java.io.IOException;
29+
import java.io.InputStream;
2430
import java.util.List;
2531
import java.util.Objects;
2632
import java.util.UUID;
@@ -34,6 +40,10 @@ public class SpreadsheetConfigService {
3440

3541
private final SpreadsheetConfigRepository spreadsheetConfigRepository;
3642
private final SpreadsheetConfigCollectionRepository spreadsheetConfigCollectionRepository;
43+
private final ObjectMapper objectMapper;
44+
45+
@Value("classpath:default-spreadsheet-config-collection.json")
46+
private Resource defaultSpreadsheetConfigCollectionResource;
3747

3848
private static final String SPREADSHEET_CONFIG_COLLECTION_NOT_FOUND = "SpreadsheetConfigCollection not found with id: ";
3949
private static final String COLUMN_NOT_FOUND = "Column not found with id: ";
@@ -53,6 +63,7 @@ public UUID duplicateSpreadsheetConfig(UUID id) {
5363
private SpreadsheetConfigEntity duplicateSpreadsheetConfigEntity(UUID id) {
5464
SpreadsheetConfigEntity entity = findEntityById(id);
5565
SpreadsheetConfigEntity duplicate = SpreadsheetConfigEntity.builder()
66+
.name(entity.getName())
5667
.sheetType(entity.getSheetType())
5768
.build();
5869
List<ColumnEntity> columns = entity.getColumns().stream()
@@ -96,6 +107,7 @@ public void updateSpreadsheetConfig(UUID id, SpreadsheetConfigInfos dto) {
96107
SpreadsheetConfigEntity entity = findEntityById(id);
97108

98109
entity.setSheetType(dto.sheetType());
110+
entity.setName(dto.name());
99111
entity.getColumns().clear();
100112
if (dto.columns() != null) {
101113
entity.getColumns().addAll(dto.columns().stream()
@@ -121,7 +133,6 @@ private EntityNotFoundException entityNotFoundException(UUID id) {
121133
return new EntityNotFoundException("SpreadsheetConfig not found with id: " + id);
122134
}
123135

124-
@Transactional
125136
public UUID createSpreadsheetConfigCollection(SpreadsheetConfigCollectionInfos dto) {
126137
SpreadsheetConfigCollectionEntity entity = new SpreadsheetConfigCollectionEntity();
127138
entity.setSpreadsheetConfigs(dto.spreadsheetConfigs().stream()
@@ -176,6 +187,7 @@ public UUID duplicateSpreadsheetConfigCollection(UUID id) {
176187
duplicate.setSpreadsheetConfigs(entity.getSpreadsheetConfigs().stream()
177188
.map(config -> {
178189
SpreadsheetConfigEntity configDuplicate = SpreadsheetConfigEntity.builder()
190+
.name(config.getName())
179191
.sheetType(config.getSheetType())
180192
.build();
181193
configDuplicate.setColumns(config.getColumns().stream()
@@ -241,4 +253,42 @@ public void deleteColumn(UUID id, UUID columnId) {
241253
spreadsheetConfigRepository.save(entity);
242254
}
243255

256+
private SpreadsheetConfigCollectionInfos readDefaultSpreadsheetConfigCollection() throws IOException {
257+
try (InputStream inputStream = defaultSpreadsheetConfigCollectionResource.getInputStream()) {
258+
return objectMapper.readValue(inputStream, SpreadsheetConfigCollectionInfos.class);
259+
}
260+
}
261+
262+
public UUID createDefaultSpreadsheetConfigCollection() {
263+
try {
264+
SpreadsheetConfigCollectionInfos defaultCollection = readDefaultSpreadsheetConfigCollection();
265+
return createSpreadsheetConfigCollection(defaultCollection);
266+
} catch (IOException e) {
267+
throw new RuntimeException("Failed to read default spreadsheet config collection", e);
268+
}
269+
}
270+
271+
@Transactional
272+
public UUID addSpreadsheetConfigToCollection(UUID collectionId, SpreadsheetConfigInfos dto) {
273+
SpreadsheetConfigCollectionEntity collection = spreadsheetConfigCollectionRepository.findById(collectionId)
274+
.orElseThrow(() -> new EntityNotFoundException(SPREADSHEET_CONFIG_COLLECTION_NOT_FOUND + collectionId));
275+
276+
SpreadsheetConfigEntity newConfig = SpreadsheetConfigMapper.toEntity(dto);
277+
collection.getSpreadsheetConfigs().add(newConfig);
278+
spreadsheetConfigCollectionRepository.flush();
279+
return newConfig.getId();
280+
}
281+
282+
@Transactional
283+
public void removeSpreadsheetConfigFromCollection(UUID collectionId, UUID configId) {
284+
SpreadsheetConfigCollectionEntity collection = spreadsheetConfigCollectionRepository.findById(collectionId)
285+
.orElseThrow(() -> new EntityNotFoundException(SPREADSHEET_CONFIG_COLLECTION_NOT_FOUND + collectionId));
286+
287+
boolean removed = collection.getSpreadsheetConfigs().removeIf(config -> config.getId().equals(configId));
288+
if (!removed) {
289+
throw new EntityNotFoundException("Spreadsheet configuration not found in collection");
290+
}
291+
spreadsheetConfigCollectionRepository.save(collection);
292+
}
293+
244294
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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="labidiayo (generated)" id="1740072547013-1">
4+
<addColumn tableName="spreadsheet_config">
5+
<column name="name" type="varchar(255)">
6+
<constraints nullable="false"/>
7+
</column>
8+
</addColumn>
9+
</changeSet>
10+
</databaseChangeLog>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ databaseChangeLog:
2020
- include:
2121
file: changesets/changelog_20250218T130823Z.xml
2222
relativeToChangelogFile: true
23+
- include:
24+
file: changesets/changelog_20250220T172856Z.xml
25+
relativeToChangelogFile: true

0 commit comments

Comments
 (0)