Skip to content

Commit f9b4eab

Browse files
authored
Add support for spreadsheet config collections (#14)
Signed-off-by: Ayoub LABIDI <[email protected]>
1 parent 0f3febb commit f9b4eab

File tree

10 files changed

+461
-2
lines changed

10 files changed

+461
-2
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* Copyright (c) 2024, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
package org.gridsuite.spreadsheetconfig.server.controller;
8+
9+
import io.swagger.v3.oas.annotations.Operation;
10+
import io.swagger.v3.oas.annotations.Parameter;
11+
import io.swagger.v3.oas.annotations.media.Content;
12+
import io.swagger.v3.oas.annotations.media.Schema;
13+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
14+
import io.swagger.v3.oas.annotations.tags.Tag;
15+
import jakarta.validation.Valid;
16+
import lombok.RequiredArgsConstructor;
17+
import org.gridsuite.spreadsheetconfig.server.SpreadsheetConfigApi;
18+
import org.gridsuite.spreadsheetconfig.server.dto.SpreadsheetConfigCollectionInfos;
19+
import org.gridsuite.spreadsheetconfig.server.service.SpreadsheetConfigService;
20+
import org.springframework.http.HttpStatus;
21+
import org.springframework.http.ResponseEntity;
22+
import org.springframework.web.bind.annotation.*;
23+
24+
import java.util.UUID;
25+
26+
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
27+
28+
/**
29+
* @author Ayoub LABIDI <ayoub.labidi at rte-france.com>
30+
*/
31+
@RestController
32+
@RequestMapping(value = "/" + SpreadsheetConfigApi.API_VERSION + "/spreadsheet-config-collections")
33+
@RequiredArgsConstructor
34+
@Tag(name = "Spreadsheet Config Collection", description = "Spreadsheet Configuration Collection API")
35+
public class SpreadsheetConfigCollectionController {
36+
37+
private final SpreadsheetConfigService spreadsheetConfigService;
38+
39+
@PostMapping(consumes = APPLICATION_JSON_VALUE)
40+
@Operation(summary = "Create a new spreadsheet configuration collection",
41+
description = "Creates a new spreadsheet configuration collection and returns its ID")
42+
@ApiResponse(responseCode = "201", description = "Configuration collection created",
43+
content = @Content(schema = @Schema(implementation = UUID.class)))
44+
public ResponseEntity<UUID> createSpreadsheetConfigCollection(@Parameter(description = "Configuration collection to save") @Valid @RequestBody SpreadsheetConfigCollectionInfos dto) {
45+
UUID id = spreadsheetConfigService.createSpreadsheetConfigCollection(dto);
46+
return ResponseEntity.status(HttpStatus.CREATED).body(id);
47+
}
48+
49+
@GetMapping("/{id}")
50+
@Operation(summary = "Get a spreadsheet configuration collection",
51+
description = "Retrieves a spreadsheet configuration collection by its ID")
52+
@ApiResponse(responseCode = "200", description = "Configuration collection found",
53+
content = @Content(schema = @Schema(implementation = SpreadsheetConfigCollectionInfos.class)))
54+
@ApiResponse(responseCode = "404", description = "Configuration collection not found")
55+
public ResponseEntity<SpreadsheetConfigCollectionInfos> getSpreadsheetConfigCollection(
56+
@Parameter(description = "ID of the configuration collection to retrieve") @PathVariable UUID id) {
57+
return ResponseEntity.ok(spreadsheetConfigService.getSpreadsheetConfigCollection(id));
58+
}
59+
60+
@DeleteMapping("/{id}")
61+
@Operation(summary = "Delete a spreadsheet configuration collection",
62+
description = "Deletes an existing spreadsheet configuration collection")
63+
@ApiResponse(responseCode = "204", description = "Configuration collection deleted")
64+
@ApiResponse(responseCode = "404", description = "Configuration collection not found")
65+
public ResponseEntity<Void> deleteSpreadsheetConfigCollection(
66+
@Parameter(description = "ID of the configuration collection to delete") @PathVariable UUID id) {
67+
spreadsheetConfigService.deleteSpreadsheetConfigCollection(id);
68+
return ResponseEntity.noContent().build();
69+
}
70+
71+
@PutMapping("/{id}")
72+
@Operation(summary = "Update a spreadsheet configuration collection",
73+
description = "Updates an existing spreadsheet configuration collection")
74+
@ApiResponse(responseCode = "204", description = "Configuration collection updated")
75+
@ApiResponse(responseCode = "404", description = "Configuration collection not found")
76+
public ResponseEntity<Void> updateSpreadsheetConfigCollection(
77+
@Parameter(description = "ID of the configuration collection to update") @PathVariable UUID id,
78+
@Valid @RequestBody SpreadsheetConfigCollectionInfos dto) {
79+
spreadsheetConfigService.updateSpreadsheetConfigCollection(id, dto);
80+
return ResponseEntity.noContent().build();
81+
}
82+
83+
@PostMapping(value = "/duplicate", params = { "duplicateFrom" })
84+
@Operation(summary = "Duplicate a spreadsheet configuration collection",
85+
description = "Creates a copy of an existing spreadsheet configuration collection")
86+
@ApiResponse(responseCode = "201", description = "Configuration collection duplicated",
87+
content = @Content(schema = @Schema(implementation = UUID.class)))
88+
@ApiResponse(responseCode = "404", description = "Configuration collection not found")
89+
public ResponseEntity<UUID> duplicateSpreadsheetConfigCollection(@Parameter(description = "UUID of the configuration collection to duplicate") @RequestParam(name = "duplicateFrom") UUID id) {
90+
UUID newId = spreadsheetConfigService.duplicateSpreadsheetConfigCollection(id);
91+
return ResponseEntity.status(HttpStatus.CREATED).body(newId);
92+
}
93+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright (c) 2024, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
package org.gridsuite.spreadsheetconfig.server.dto;
8+
9+
import io.swagger.v3.oas.annotations.media.Schema;
10+
11+
import java.util.List;
12+
import java.util.UUID;
13+
14+
/**
15+
* @author Ayoub LABIDI <ayoub.labidi at rte-france.com>
16+
*/
17+
@Schema(name = "SpreadsheetConfigCollectionDto", description = "Spreadsheet configuration collection")
18+
public record SpreadsheetConfigCollectionInfos(
19+
20+
@Schema(description = "Spreadsheet configuration collection ID")
21+
UUID id,
22+
23+
@Schema(description = "List of spreadsheet configurations")
24+
List<SpreadsheetConfigInfos> spreadsheetConfigs
25+
) {
26+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright (c) 2024, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
package org.gridsuite.spreadsheetconfig.server.entities;
8+
9+
import jakarta.persistence.*;
10+
import lombok.*;
11+
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
import java.util.UUID;
15+
16+
/**
17+
* @author Ayoub LABIDI <ayoub.labidi at rte-france.com>
18+
*/
19+
@Builder
20+
@NoArgsConstructor
21+
@AllArgsConstructor
22+
@Getter
23+
@Setter
24+
@Entity
25+
@Table(name = "spreadsheet_config_collection")
26+
public class SpreadsheetConfigCollectionEntity {
27+
28+
@Id
29+
@GeneratedValue(strategy = GenerationType.AUTO)
30+
@Column(name = "id")
31+
private UUID id;
32+
33+
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
34+
@JoinColumn(name = "collection_id", foreignKey = @ForeignKey(name = "fk_spreadsheet_config_collection"))
35+
@Builder.Default
36+
private List<SpreadsheetConfigEntity> spreadsheetConfigs = new ArrayList<>();
37+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public static MetadataInfos toMetadataDto(SpreadsheetConfigEntity entity) {
3636

3737
public static SpreadsheetConfigEntity toEntity(SpreadsheetConfigInfos dto) {
3838
SpreadsheetConfigEntity entity = SpreadsheetConfigEntity.builder()
39-
.id(dto.id())
4039
.sheetType(dto.sheetType())
4140
.build();
4241

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright (c) 2024, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
package org.gridsuite.spreadsheetconfig.server.repositories;
8+
9+
import org.gridsuite.spreadsheetconfig.server.entities.SpreadsheetConfigCollectionEntity;
10+
import org.springframework.data.jpa.repository.JpaRepository;
11+
import org.springframework.stereotype.Repository;
12+
13+
import java.util.UUID;
14+
15+
/**
16+
* @author Ayoub LABIDI <ayoub.labidi at rte-france.com>
17+
*/
18+
@Repository
19+
public interface SpreadsheetConfigCollectionRepository extends JpaRepository<SpreadsheetConfigCollectionEntity, UUID> {
20+
}

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
import jakarta.persistence.EntityNotFoundException;
1010
import lombok.RequiredArgsConstructor;
1111
import org.gridsuite.spreadsheetconfig.server.dto.MetadataInfos;
12+
import org.gridsuite.spreadsheetconfig.server.dto.SpreadsheetConfigCollectionInfos;
1213
import org.gridsuite.spreadsheetconfig.server.dto.SpreadsheetConfigInfos;
1314
import org.gridsuite.spreadsheetconfig.server.entities.CustomColumnEmbeddable;
15+
import org.gridsuite.spreadsheetconfig.server.entities.SpreadsheetConfigCollectionEntity;
1416
import org.gridsuite.spreadsheetconfig.server.entities.SpreadsheetConfigEntity;
1517
import org.gridsuite.spreadsheetconfig.server.mapper.SpreadsheetConfigMapper;
18+
import org.gridsuite.spreadsheetconfig.server.repositories.SpreadsheetConfigCollectionRepository;
1619
import org.gridsuite.spreadsheetconfig.server.repositories.SpreadsheetConfigRepository;
1720
import org.springframework.stereotype.Service;
1821
import org.springframework.transaction.annotation.Transactional;
@@ -29,6 +32,9 @@
2932
public class SpreadsheetConfigService {
3033

3134
private final SpreadsheetConfigRepository spreadsheetConfigRepository;
35+
private final SpreadsheetConfigCollectionRepository spreadsheetConfigCollectionRepository;
36+
37+
private static final String SPREADSHEET_CONFIG_COLLECTION_NOT_FOUND = "SpreadsheetConfigCollection not found with id: ";
3238

3339
@Transactional
3440
public UUID createSpreadsheetConfig(SpreadsheetConfigInfos dto) {
@@ -108,4 +114,64 @@ private EntityNotFoundException entityNotFoundException(UUID id) {
108114
return new EntityNotFoundException("SpreadsheetConfig not found with id: " + id);
109115
}
110116

117+
@Transactional
118+
public UUID createSpreadsheetConfigCollection(SpreadsheetConfigCollectionInfos dto) {
119+
SpreadsheetConfigCollectionEntity entity = new SpreadsheetConfigCollectionEntity();
120+
entity.setSpreadsheetConfigs(dto.spreadsheetConfigs().stream()
121+
.map(SpreadsheetConfigMapper::toEntity)
122+
.toList());
123+
return spreadsheetConfigCollectionRepository.save(entity).getId();
124+
}
125+
126+
@Transactional(readOnly = true)
127+
public SpreadsheetConfigCollectionInfos getSpreadsheetConfigCollection(UUID id) {
128+
SpreadsheetConfigCollectionEntity entity = spreadsheetConfigCollectionRepository.findById(id)
129+
.orElseThrow(() -> new EntityNotFoundException(SPREADSHEET_CONFIG_COLLECTION_NOT_FOUND + id));
130+
return new SpreadsheetConfigCollectionInfos(entity.getId(), entity.getSpreadsheetConfigs().stream()
131+
.map(SpreadsheetConfigMapper::toDto)
132+
.toList());
133+
}
134+
135+
@Transactional
136+
public void deleteSpreadsheetConfigCollection(UUID id) {
137+
if (!spreadsheetConfigCollectionRepository.existsById(id)) {
138+
throw new EntityNotFoundException(SPREADSHEET_CONFIG_COLLECTION_NOT_FOUND + id);
139+
}
140+
spreadsheetConfigCollectionRepository.deleteById(id);
141+
}
142+
143+
@Transactional
144+
public void updateSpreadsheetConfigCollection(UUID id, SpreadsheetConfigCollectionInfos dto) {
145+
SpreadsheetConfigCollectionEntity entity = spreadsheetConfigCollectionRepository.findById(id)
146+
.orElseThrow(() -> new EntityNotFoundException(SPREADSHEET_CONFIG_COLLECTION_NOT_FOUND + id));
147+
148+
entity.getSpreadsheetConfigs().clear();
149+
entity.getSpreadsheetConfigs().addAll(dto.spreadsheetConfigs().stream()
150+
.map(SpreadsheetConfigMapper::toEntity)
151+
.toList());
152+
}
153+
154+
@Transactional
155+
public UUID duplicateSpreadsheetConfigCollection(UUID id) {
156+
SpreadsheetConfigCollectionEntity entity = spreadsheetConfigCollectionRepository.findById(id)
157+
.orElseThrow(() -> new EntityNotFoundException(SPREADSHEET_CONFIG_COLLECTION_NOT_FOUND + id));
158+
159+
SpreadsheetConfigCollectionEntity duplicate = new SpreadsheetConfigCollectionEntity();
160+
duplicate.setSpreadsheetConfigs(entity.getSpreadsheetConfigs().stream()
161+
.map(config -> {
162+
SpreadsheetConfigEntity configDuplicate = SpreadsheetConfigEntity.builder()
163+
.sheetType(config.getSheetType())
164+
.build();
165+
configDuplicate.setCustomColumns(config.getCustomColumns().stream()
166+
.map(column -> CustomColumnEmbeddable.builder()
167+
.name(column.getName())
168+
.formula(column.getFormula())
169+
.build())
170+
.toList());
171+
return configDuplicate;
172+
})
173+
.toList());
174+
return spreadsheetConfigCollectionRepository.save(duplicate).getId();
175+
}
176+
111177
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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="1734360388052-1">
4+
<createTable tableName="spreadsheet_config_collection">
5+
<column name="id" type="UUID">
6+
<constraints nullable="false" primaryKey="true" primaryKeyName="spreadsheet_config_collectionPK"/>
7+
</column>
8+
</createTable>
9+
</changeSet>
10+
<changeSet author="labidiayo (generated)" id="1734360388052-2">
11+
<addColumn tableName="spreadsheet_config">
12+
<column name="collection_id" type="uuid"/>
13+
</addColumn>
14+
</changeSet>
15+
<changeSet author="labidiayo (generated)" id="1734360388052-3">
16+
<addForeignKeyConstraint baseColumnNames="collection_id" baseTableName="spreadsheet_config" constraintName="fk_spreadsheet_config_collection" deferrable="false" initiallyDeferred="false" referencedColumnNames="id" referencedTableName="spreadsheet_config_collection" validate="true"/>
17+
</changeSet>
18+
</databaseChangeLog>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ databaseChangeLog:
55
- include:
66
file: changesets/changelog_20241018T115212Z.xml
77
relativeToChangelogFile: true
8+
- include:
9+
file: changesets/changelog_20241216T144619Z.xml
10+
relativeToChangelogFile: true

src/test/java/org/gridsuite/spreadsheetconfig/server/DtoConverterTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ void testConversionToEntityOfSpreadsheetConfig() {
7171
assertThat(entity)
7272
.as("Entity conversion result")
7373
.satisfies(e -> {
74-
assertThat(e.getId()).isEqualTo(id);
7574
assertThat(e.getSheetType()).isEqualTo(SheetType.BUS);
7675
assertThat(e.getCustomColumns()).hasSize(2);
7776
assertThat(e.getCustomColumns().get(0).getName()).isEqualTo("Column1");

0 commit comments

Comments
 (0)