Skip to content

Commit 9209f08

Browse files
authored
Replace Custom columns with new column entity and add column endpoints (#28)
Signed-off-by: Ayoub LABIDI <[email protected]>
1 parent 2a96b11 commit 9209f08

File tree

12 files changed

+346
-112
lines changed

12 files changed

+346
-112
lines changed

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import jakarta.validation.Valid;
1616
import lombok.RequiredArgsConstructor;
1717
import org.gridsuite.studyconfig.server.StudyConfigApi;
18+
import org.gridsuite.studyconfig.server.dto.ColumnInfos;
1819
import org.gridsuite.studyconfig.server.dto.MetadataInfos;
1920
import org.gridsuite.studyconfig.server.dto.SpreadsheetConfigInfos;
2021
import org.gridsuite.studyconfig.server.service.SpreadsheetConfigService;
@@ -112,4 +113,47 @@ public ResponseEntity<Void> deleteSpreadsheetConfig(
112113
spreadsheetConfigService.deleteSpreadsheetConfig(id);
113114
return ResponseEntity.noContent().build();
114115
}
116+
117+
@GetMapping("/{id}/columns/{columnId}")
118+
@Operation(summary = "Get a column", description = "Retrieves a column by its ID")
119+
@ApiResponse(responseCode = "200", description = "Column found")
120+
@ApiResponse(responseCode = "404", description = "Column not found")
121+
public ResponseEntity<ColumnInfos> getColumn(
122+
@Parameter(description = "ID of the spreadsheet config") @PathVariable UUID id,
123+
@Parameter(description = "ID of the column to retrieve") @PathVariable UUID columnId) {
124+
return ResponseEntity.ok(spreadsheetConfigService.getColumn(id, columnId));
125+
}
126+
127+
@PostMapping("/{id}/columns")
128+
@Operation(summary = "Create a column", description = "Creates a new column")
129+
@ApiResponse(responseCode = "201", description = "Column created")
130+
public ResponseEntity<UUID> createColumn(
131+
@Parameter(description = "ID of the spreadsheet config") @PathVariable UUID id,
132+
@Valid @RequestBody ColumnInfos dto) {
133+
UUID columnId = spreadsheetConfigService.createColumn(id, dto);
134+
return ResponseEntity.status(HttpStatus.CREATED).body(columnId);
135+
}
136+
137+
@PutMapping("/{id}/columns/{columnId}")
138+
@Operation(summary = "Update a column", description = "Updates an existing column")
139+
@ApiResponse(responseCode = "204", description = "Column updated")
140+
public ResponseEntity<Void> updateColumn(
141+
@Parameter(description = "ID of the spreadsheet config") @PathVariable UUID id,
142+
@Parameter(description = "ID of the column to update") @PathVariable UUID columnId,
143+
@Valid @RequestBody ColumnInfos dto) {
144+
spreadsheetConfigService.updateColumn(id, columnId, dto);
145+
return ResponseEntity.noContent().build();
146+
}
147+
148+
@DeleteMapping("/{id}/columns/{columnId}")
149+
@Operation(summary = "Delete a column", description = "Deletes an existing column")
150+
@ApiResponse(responseCode = "204", description = "Column deleted")
151+
@ApiResponse(responseCode = "404", description = "Column not found")
152+
public ResponseEntity<Void> deleteColumn(
153+
@Parameter(description = "ID of the spreadsheet config") @PathVariable UUID id,
154+
@Parameter(description = "ID of the column to delete") @PathVariable UUID columnId) {
155+
spreadsheetConfigService.deleteColumn(id, columnId);
156+
return ResponseEntity.noContent().build();
157+
}
158+
115159
}

src/main/java/org/gridsuite/studyconfig/server/dto/CustomColumnInfos.java renamed to src/main/java/org/gridsuite/studyconfig/server/dto/ColumnInfos.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
*/
77
package org.gridsuite.studyconfig.server.dto;
88

9+
import java.util.UUID;
10+
911
import org.gridsuite.studyconfig.server.constants.ColumnType;
1012

1113
import io.swagger.v3.oas.annotations.media.Schema;
@@ -14,8 +16,11 @@
1416
/**
1517
* @author Achour BERRAHMA <achour.berrahma at rte-france.com>
1618
*/
17-
@Schema(name = "CustomColumnDto", description = "Custom column configuration")
18-
public record CustomColumnInfos(
19+
@Schema(name = "ColumnDto", description = "Column configuration")
20+
public record ColumnInfos(
21+
22+
@Schema(description = "Column UUID")
23+
UUID uuid,
1924

2025
@NotNull(message = "Column name is mandatory")
2126
@Schema(description = "Column name")

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ public record SpreadsheetConfigInfos(
2626
@Schema(description = "Spreadsheet type")
2727
SheetType sheetType,
2828

29-
@Schema(description = "Custom columns")
30-
List<CustomColumnInfos> customColumns
29+
@Schema(description = "Columns")
30+
List<ColumnInfos> columns
3131
) { }
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
*/
77
package org.gridsuite.studyconfig.server.entities;
88

9+
import java.util.UUID;
10+
911
import org.gridsuite.studyconfig.server.constants.ColumnType;
1012

1113
import jakarta.persistence.*;
@@ -14,13 +16,19 @@
1416
/**
1517
* @author Achour BERRAHMA <achour.berrahma at rte-france.com>
1618
*/
17-
@Embeddable
19+
@Entity
20+
@Table(name = "spreadsheet_column")
1821
@Getter
1922
@Setter
2023
@NoArgsConstructor
2124
@AllArgsConstructor
2225
@Builder
23-
public class CustomColumnEmbeddable {
26+
public class ColumnEntity {
27+
28+
@Id
29+
@GeneratedValue(strategy = GenerationType.AUTO)
30+
@Column(name = "uuid")
31+
private UUID uuid;
2432

2533
@Column(name = "name", nullable = false, columnDefinition = "varchar(255)")
2634
private String name;

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,10 @@ public class SpreadsheetConfigEntity {
3535
@Enumerated(EnumType.STRING)
3636
private SheetType sheetType;
3737

38-
@ElementCollection
39-
@CollectionTable(
40-
name = "spreadsheet_custom_column",
41-
joinColumns = @JoinColumn(name = "spreadsheet_config_id"),
42-
uniqueConstraints = {
43-
@UniqueConstraint(name = "UK_config_id_column_id", columnNames = {"spreadsheet_config_id", "column_id"})
44-
}
45-
)
38+
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
39+
@JoinColumn(name = "spreadsheet_config_id", foreignKey = @ForeignKey(name = "fk_spreadsheet_config_column"))
4640
@OrderColumn(name = "column_order")
4741
@Builder.Default
48-
private List<CustomColumnEmbeddable> customColumns = new ArrayList<>();
42+
private List<ColumnEntity> columns = new ArrayList<>();
4943

5044
}

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
import org.gridsuite.studyconfig.server.dto.MetadataInfos;
1010
import org.gridsuite.studyconfig.server.dto.SpreadsheetConfigInfos;
11-
import org.gridsuite.studyconfig.server.dto.CustomColumnInfos;
12-
import org.gridsuite.studyconfig.server.entities.CustomColumnEmbeddable;
11+
import org.gridsuite.studyconfig.server.dto.ColumnInfos;
12+
import org.gridsuite.studyconfig.server.entities.ColumnEntity;
1313
import org.gridsuite.studyconfig.server.entities.SpreadsheetConfigEntity;
1414

1515
/**
@@ -24,8 +24,8 @@ public static SpreadsheetConfigInfos toDto(SpreadsheetConfigEntity entity) {
2424
return new SpreadsheetConfigInfos(
2525
entity.getId(),
2626
entity.getSheetType(),
27-
entity.getCustomColumns().stream()
28-
.map(SpreadsheetConfigMapper::toCustomColumnDto)
27+
entity.getColumns().stream()
28+
.map(SpreadsheetConfigMapper::toColumnDto)
2929
.toList()
3030
);
3131
}
@@ -39,20 +39,27 @@ public static SpreadsheetConfigEntity toEntity(SpreadsheetConfigInfos dto) {
3939
.sheetType(dto.sheetType())
4040
.build();
4141

42-
if (dto.customColumns() != null) {
43-
entity.setCustomColumns(dto.customColumns().stream()
44-
.map(SpreadsheetConfigMapper::toCustomColumnEmbeddable)
42+
if (dto.columns() != null) {
43+
entity.setColumns(dto.columns().stream()
44+
.map(SpreadsheetConfigMapper::toColumnEntity)
4545
.toList());
4646
}
4747

4848
return entity;
4949
}
5050

51-
public static CustomColumnInfos toCustomColumnDto(CustomColumnEmbeddable entity) {
52-
return new CustomColumnInfos(entity.getName(), entity.getType(), entity.getPrecision(), entity.getFormula(), entity.getDependencies(), entity.getId());
51+
public static ColumnInfos toColumnDto(ColumnEntity entity) {
52+
return new ColumnInfos(entity.getUuid(), entity.getName(), entity.getType(), entity.getPrecision(), entity.getFormula(), entity.getDependencies(), entity.getId());
5353
}
5454

55-
public static CustomColumnEmbeddable toCustomColumnEmbeddable(CustomColumnInfos dto) {
56-
return new CustomColumnEmbeddable(dto.name(), dto.type(), dto.precision(), dto.formula(), dto.dependencies(), dto.id());
55+
public static ColumnEntity toColumnEntity(ColumnInfos dto) {
56+
return ColumnEntity.builder()
57+
.name(dto.name())
58+
.type(dto.type())
59+
.precision(dto.precision())
60+
.formula(dto.formula())
61+
.dependencies(dto.dependencies())
62+
.id(dto.id())
63+
.build();
5764
}
5865
}

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

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88

99
import jakarta.persistence.EntityNotFoundException;
1010
import lombok.RequiredArgsConstructor;
11+
import org.gridsuite.studyconfig.server.dto.ColumnInfos;
1112
import org.gridsuite.studyconfig.server.dto.MetadataInfos;
1213
import org.gridsuite.studyconfig.server.dto.SpreadsheetConfigCollectionInfos;
1314
import org.gridsuite.studyconfig.server.dto.SpreadsheetConfigInfos;
14-
import org.gridsuite.studyconfig.server.entities.CustomColumnEmbeddable;
15+
import org.gridsuite.studyconfig.server.entities.ColumnEntity;
1516
import org.gridsuite.studyconfig.server.entities.SpreadsheetConfigCollectionEntity;
1617
import org.gridsuite.studyconfig.server.entities.SpreadsheetConfigEntity;
1718
import org.gridsuite.studyconfig.server.mapper.SpreadsheetConfigMapper;
@@ -35,6 +36,7 @@ public class SpreadsheetConfigService {
3536
private final SpreadsheetConfigCollectionRepository spreadsheetConfigCollectionRepository;
3637

3738
private static final String SPREADSHEET_CONFIG_COLLECTION_NOT_FOUND = "SpreadsheetConfigCollection not found with id: ";
39+
private static final String COLUMN_NOT_FOUND = "Column not found with id: ";
3840

3941
@Transactional
4042
public UUID createSpreadsheetConfig(SpreadsheetConfigInfos dto) {
@@ -53,8 +55,8 @@ private SpreadsheetConfigEntity duplicateSpreadsheetConfigEntity(UUID id) {
5355
SpreadsheetConfigEntity duplicate = SpreadsheetConfigEntity.builder()
5456
.sheetType(entity.getSheetType())
5557
.build();
56-
List<CustomColumnEmbeddable> customColumns = entity.getCustomColumns().stream()
57-
.map(column -> CustomColumnEmbeddable.builder()
58+
List<ColumnEntity> columns = entity.getColumns().stream()
59+
.map(column -> ColumnEntity.builder()
5860
.name(column.getName())
5961
.type(column.getType())
6062
.precision(column.getPrecision())
@@ -63,7 +65,7 @@ private SpreadsheetConfigEntity duplicateSpreadsheetConfigEntity(UUID id) {
6365
.id(column.getId())
6466
.build())
6567
.toList();
66-
duplicate.setCustomColumns(customColumns);
68+
duplicate.setColumns(columns);
6769
return duplicate;
6870
}
6971

@@ -94,10 +96,10 @@ public void updateSpreadsheetConfig(UUID id, SpreadsheetConfigInfos dto) {
9496
SpreadsheetConfigEntity entity = findEntityById(id);
9597

9698
entity.setSheetType(dto.sheetType());
97-
entity.getCustomColumns().clear();
98-
if (dto.customColumns() != null) {
99-
entity.getCustomColumns().addAll(dto.customColumns().stream()
100-
.map(SpreadsheetConfigMapper::toCustomColumnEmbeddable)
99+
entity.getColumns().clear();
100+
if (dto.columns() != null) {
101+
entity.getColumns().addAll(dto.columns().stream()
102+
.map(SpreadsheetConfigMapper::toColumnEntity)
101103
.toList());
102104
}
103105
}
@@ -176,8 +178,8 @@ public UUID duplicateSpreadsheetConfigCollection(UUID id) {
176178
SpreadsheetConfigEntity configDuplicate = SpreadsheetConfigEntity.builder()
177179
.sheetType(config.getSheetType())
178180
.build();
179-
configDuplicate.setCustomColumns(config.getCustomColumns().stream()
180-
.map(column -> CustomColumnEmbeddable.builder()
181+
configDuplicate.setColumns(config.getColumns().stream()
182+
.map(column -> ColumnEntity.builder()
181183
.name(column.getName())
182184
.type(column.getType())
183185
.precision(column.getPrecision())
@@ -192,4 +194,51 @@ public UUID duplicateSpreadsheetConfigCollection(UUID id) {
192194
return spreadsheetConfigCollectionRepository.save(duplicate).getId();
193195
}
194196

197+
@Transactional(readOnly = true)
198+
public ColumnInfos getColumn(UUID id, UUID columnId) {
199+
SpreadsheetConfigEntity entity = findEntityById(id);
200+
return entity.getColumns().stream()
201+
.filter(column -> column.getUuid().equals(columnId))
202+
.findFirst()
203+
.map(SpreadsheetConfigMapper::toColumnDto)
204+
.orElseThrow(() -> new EntityNotFoundException(COLUMN_NOT_FOUND + columnId));
205+
}
206+
207+
@Transactional
208+
public UUID createColumn(UUID id, ColumnInfos dto) {
209+
SpreadsheetConfigEntity entity = findEntityById(id);
210+
ColumnEntity columnEntity = SpreadsheetConfigMapper.toColumnEntity(dto);
211+
entity.getColumns().add(columnEntity);
212+
spreadsheetConfigRepository.flush();
213+
return columnEntity.getUuid();
214+
}
215+
216+
@Transactional
217+
public void updateColumn(UUID id, UUID columnId, ColumnInfos dto) {
218+
SpreadsheetConfigEntity entity = findEntityById(id);
219+
ColumnEntity columnEntity = entity.getColumns().stream()
220+
.filter(column -> column.getUuid().equals(columnId))
221+
.findFirst()
222+
.orElseThrow(() -> new EntityNotFoundException(COLUMN_NOT_FOUND + columnId));
223+
224+
columnEntity.setName(dto.name());
225+
columnEntity.setType(dto.type());
226+
columnEntity.setPrecision(dto.precision());
227+
columnEntity.setFormula(dto.formula());
228+
columnEntity.setDependencies(dto.dependencies());
229+
columnEntity.setId(dto.id());
230+
231+
spreadsheetConfigRepository.save(entity);
232+
}
233+
234+
@Transactional
235+
public void deleteColumn(UUID id, UUID columnId) {
236+
SpreadsheetConfigEntity entity = findEntityById(id);
237+
boolean removed = entity.getColumns().removeIf(column -> column.getUuid().equals(columnId));
238+
if (!removed) {
239+
throw new EntityNotFoundException(COLUMN_NOT_FOUND + columnId);
240+
}
241+
spreadsheetConfigRepository.save(entity);
242+
}
243+
195244
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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" id="1739884113021-1">
4+
<sql dbms="postgresql">
5+
TRUNCATE TABLE spreadsheet_config CASCADE;
6+
</sql>
7+
</changeSet>
8+
<changeSet author="labidiayo" id="1739884113021-2">
9+
<sql dbms="postgresql">
10+
TRUNCATE TABLE spreadsheet_config_collection CASCADE;
11+
</sql>
12+
</changeSet>
13+
<changeSet author="labidiayo (generated)" id="1739884113021-3">
14+
<createTable tableName="spreadsheet_column">
15+
<column name="uuid" type="UUID">
16+
<constraints nullable="false" primaryKey="true" primaryKeyName="spreadsheet_columnPK"/>
17+
</column>
18+
<column name="dependencies" type="CLOB"/>
19+
<column name="formula" type="CLOB"/>
20+
<column name="column_id" type="VARCHAR(255)">
21+
<constraints nullable="false"/>
22+
</column>
23+
<column name="name" type="VARCHAR(255)">
24+
<constraints nullable="false"/>
25+
</column>
26+
<column name="precision" type="INT"/>
27+
<column name="type" type="VARCHAR(255)">
28+
<constraints nullable="false"/>
29+
</column>
30+
<column name="spreadsheet_config_id" type="UUID"/>
31+
<column name="column_order" type="INT"/>
32+
</createTable>
33+
</changeSet>
34+
<changeSet author="labidiayo (generated)" id="1739884113021-4">
35+
<addForeignKeyConstraint baseColumnNames="spreadsheet_config_id" baseTableName="spreadsheet_column" constraintName="fk_spreadsheet_config_column" deferrable="false" initiallyDeferred="false" referencedColumnNames="id" referencedTableName="spreadsheet_config" validate="true"/>
36+
</changeSet>
37+
<changeSet author="labidiayo (generated)" id="1739884113021-5">
38+
<dropForeignKeyConstraint baseTableName="SPREADSHEET_CUSTOM_COLUMN" constraintName="SPREADSHEET_CONFIG_ID_FK_CONSTRAINT"/>
39+
</changeSet>
40+
<changeSet author="labidiayo (generated)" id="1739884113021-6">
41+
<dropTable tableName="SPREADSHEET_CUSTOM_COLUMN"/>
42+
</changeSet>
43+
</databaseChangeLog>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ databaseChangeLog:
1717
- include:
1818
file: changesets/changelog_20250201T194706Z.xml
1919
relativeToChangelogFile: true
20+
- include:
21+
file: changesets/changelog_20250218T130823Z.xml
22+
relativeToChangelogFile: true

0 commit comments

Comments
 (0)