Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for spreadsheet config collections #14

Merged
merged 3 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.gridsuite.spreadsheetconfig.server.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.gridsuite.spreadsheetconfig.server.SpreadsheetConfigApi;
import org.gridsuite.spreadsheetconfig.server.dto.SpreadsheetConfigCollectionInfos;
import org.gridsuite.spreadsheetconfig.server.service.SpreadsheetConfigService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

/**
* @author Ayoub LABIDI <ayoub.labidi at rte-france.com>
*/
@RestController
@RequestMapping(value = "/" + SpreadsheetConfigApi.API_VERSION + "/spreadsheet-config-collections")
@RequiredArgsConstructor
@Tag(name = "Spreadsheet Config Collection", description = "Spreadsheet Configuration Collection API")
public class SpreadsheetConfigCollectionController {

private final SpreadsheetConfigService spreadsheetConfigService;

@PostMapping(consumes = APPLICATION_JSON_VALUE)
@Operation(summary = "Create a new spreadsheet configuration collection",
description = "Creates a new spreadsheet configuration collection and returns its ID")
@ApiResponse(responseCode = "201", description = "Configuration collection created",
content = @Content(schema = @Schema(implementation = UUID.class)))
public ResponseEntity<UUID> createSpreadsheetConfigCollection(@Parameter(description = "Configuration collection to save") @Valid @RequestBody SpreadsheetConfigCollectionInfos dto) {
UUID id = spreadsheetConfigService.createSpreadsheetConfigCollection(dto);
return ResponseEntity.status(HttpStatus.CREATED).body(id);
}

@GetMapping("/{id}")
@Operation(summary = "Get a spreadsheet configuration collection",
description = "Retrieves a spreadsheet configuration collection by its ID")
@ApiResponse(responseCode = "200", description = "Configuration collection found",
content = @Content(schema = @Schema(implementation = SpreadsheetConfigCollectionInfos.class)))
@ApiResponse(responseCode = "404", description = "Configuration collection not found")
public ResponseEntity<SpreadsheetConfigCollectionInfos> getSpreadsheetConfigCollection(
@Parameter(description = "ID of the configuration collection to retrieve") @PathVariable UUID id) {
return ResponseEntity.ok(spreadsheetConfigService.getSpreadsheetConfigCollection(id));
}

@DeleteMapping("/{id}")
@Operation(summary = "Delete a spreadsheet configuration collection",
description = "Deletes an existing spreadsheet configuration collection")
@ApiResponse(responseCode = "204", description = "Configuration collection deleted")
@ApiResponse(responseCode = "404", description = "Configuration collection not found")
public ResponseEntity<Void> deleteSpreadsheetConfigCollection(
@Parameter(description = "ID of the configuration collection to delete") @PathVariable UUID id) {
spreadsheetConfigService.deleteSpreadsheetConfigCollection(id);
return ResponseEntity.noContent().build();
}

@PutMapping("/{id}")
@Operation(summary = "Update a spreadsheet configuration collection",
description = "Updates an existing spreadsheet configuration collection")
@ApiResponse(responseCode = "204", description = "Configuration collection updated")
@ApiResponse(responseCode = "404", description = "Configuration collection not found")
public ResponseEntity<Void> updateSpreadsheetConfigCollection(
@Parameter(description = "ID of the configuration collection to update") @PathVariable UUID id,
@Valid @RequestBody SpreadsheetConfigCollectionInfos dto) {
spreadsheetConfigService.updateSpreadsheetConfigCollection(id, dto);
return ResponseEntity.noContent().build();
}

@PostMapping(value = "/duplicate", params = { "duplicateFrom" })
@Operation(summary = "Duplicate a spreadsheet configuration collection",
description = "Creates a copy of an existing spreadsheet configuration collection")
@ApiResponse(responseCode = "201", description = "Configuration collection duplicated",
content = @Content(schema = @Schema(implementation = UUID.class)))
@ApiResponse(responseCode = "404", description = "Configuration collection not found")
public ResponseEntity<UUID> duplicateSpreadsheetConfigCollection(@Parameter(description = "UUID of the configuration collection to duplicate") @RequestParam(name = "duplicateFrom") UUID id) {
UUID newId = spreadsheetConfigService.duplicateSpreadsheetConfigCollection(id);
return ResponseEntity.status(HttpStatus.CREATED).body(newId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.gridsuite.spreadsheetconfig.server.dto;

import io.swagger.v3.oas.annotations.media.Schema;

import java.util.List;
import java.util.UUID;

/**
* @author Ayoub LABIDI <ayoub.labidi at rte-france.com>
*/
@Schema(name = "SpreadsheetConfigCollectionDto", description = "Spreadsheet configuration collection")
public record SpreadsheetConfigCollectionInfos(

@Schema(description = "Spreadsheet configuration collection ID")
UUID id,

@Schema(description = "List of spreadsheet configurations")
List<SpreadsheetConfigInfos> spreadsheetConfigs
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.gridsuite.spreadsheetconfig.server.entities;

import jakarta.persistence.*;
import lombok.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
* @author Ayoub LABIDI <ayoub.labidi at rte-france.com>
*/
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "spreadsheet_config_collection")
public class SpreadsheetConfigCollectionEntity {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private UUID id;

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "collection_id", foreignKey = @ForeignKey(name = "fk_spreadsheet_config_collection"))
@Builder.Default
private List<SpreadsheetConfigEntity> spreadsheetConfigs = new ArrayList<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public static MetadataInfos toMetadataDto(SpreadsheetConfigEntity entity) {

public static SpreadsheetConfigEntity toEntity(SpreadsheetConfigInfos dto) {
SpreadsheetConfigEntity entity = SpreadsheetConfigEntity.builder()
.id(dto.id())
.sheetType(dto.sheetType())
.build();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.gridsuite.spreadsheetconfig.server.repositories;

import org.gridsuite.spreadsheetconfig.server.entities.SpreadsheetConfigCollectionEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.UUID;

/**
* @author Ayoub LABIDI <ayoub.labidi at rte-france.com>
*/
@Repository
public interface SpreadsheetConfigCollectionRepository extends JpaRepository<SpreadsheetConfigCollectionEntity, UUID> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import org.gridsuite.spreadsheetconfig.server.dto.MetadataInfos;
import org.gridsuite.spreadsheetconfig.server.dto.SpreadsheetConfigCollectionInfos;
import org.gridsuite.spreadsheetconfig.server.dto.SpreadsheetConfigInfos;
import org.gridsuite.spreadsheetconfig.server.entities.CustomColumnEmbeddable;
import org.gridsuite.spreadsheetconfig.server.entities.SpreadsheetConfigCollectionEntity;
import org.gridsuite.spreadsheetconfig.server.entities.SpreadsheetConfigEntity;
import org.gridsuite.spreadsheetconfig.server.mapper.SpreadsheetConfigMapper;
import org.gridsuite.spreadsheetconfig.server.repositories.SpreadsheetConfigCollectionRepository;
import org.gridsuite.spreadsheetconfig.server.repositories.SpreadsheetConfigRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -29,6 +32,9 @@
public class SpreadsheetConfigService {

private final SpreadsheetConfigRepository spreadsheetConfigRepository;
private final SpreadsheetConfigCollectionRepository spreadsheetConfigCollectionRepository;

private static final String SPREADSHEET_CONFIG_COLLECTION_NOT_FOUND = "SpreadsheetConfigCollection not found with id: ";

@Transactional
public UUID createSpreadsheetConfig(SpreadsheetConfigInfos dto) {
Expand Down Expand Up @@ -108,4 +114,64 @@ private EntityNotFoundException entityNotFoundException(UUID id) {
return new EntityNotFoundException("SpreadsheetConfig not found with id: " + id);
}

@Transactional
public UUID createSpreadsheetConfigCollection(SpreadsheetConfigCollectionInfos dto) {
SpreadsheetConfigCollectionEntity entity = new SpreadsheetConfigCollectionEntity();
entity.setSpreadsheetConfigs(dto.spreadsheetConfigs().stream()
.map(SpreadsheetConfigMapper::toEntity)
.toList());
return spreadsheetConfigCollectionRepository.save(entity).getId();
}

@Transactional(readOnly = true)
public SpreadsheetConfigCollectionInfos getSpreadsheetConfigCollection(UUID id) {
SpreadsheetConfigCollectionEntity entity = spreadsheetConfigCollectionRepository.findById(id)
.orElseThrow(() -> new EntityNotFoundException(SPREADSHEET_CONFIG_COLLECTION_NOT_FOUND + id));
return new SpreadsheetConfigCollectionInfos(entity.getId(), entity.getSpreadsheetConfigs().stream()
.map(SpreadsheetConfigMapper::toDto)
.toList());
}

@Transactional
public void deleteSpreadsheetConfigCollection(UUID id) {
if (!spreadsheetConfigCollectionRepository.existsById(id)) {
throw new EntityNotFoundException(SPREADSHEET_CONFIG_COLLECTION_NOT_FOUND + id);
}
spreadsheetConfigCollectionRepository.deleteById(id);
}

@Transactional
public void updateSpreadsheetConfigCollection(UUID id, SpreadsheetConfigCollectionInfos dto) {
SpreadsheetConfigCollectionEntity entity = spreadsheetConfigCollectionRepository.findById(id)
.orElseThrow(() -> new EntityNotFoundException(SPREADSHEET_CONFIG_COLLECTION_NOT_FOUND + id));

entity.getSpreadsheetConfigs().clear();
entity.getSpreadsheetConfigs().addAll(dto.spreadsheetConfigs().stream()
.map(SpreadsheetConfigMapper::toEntity)
.toList());
}

@Transactional
public UUID duplicateSpreadsheetConfigCollection(UUID id) {
SpreadsheetConfigCollectionEntity entity = spreadsheetConfigCollectionRepository.findById(id)
.orElseThrow(() -> new EntityNotFoundException(SPREADSHEET_CONFIG_COLLECTION_NOT_FOUND + id));

SpreadsheetConfigCollectionEntity duplicate = new SpreadsheetConfigCollectionEntity();
duplicate.setSpreadsheetConfigs(entity.getSpreadsheetConfigs().stream()
.map(config -> {
SpreadsheetConfigEntity configDuplicate = SpreadsheetConfigEntity.builder()
.sheetType(config.getSheetType())
.build();
configDuplicate.setCustomColumns(config.getCustomColumns().stream()
.map(column -> CustomColumnEmbeddable.builder()
.name(column.getName())
.formula(column.getFormula())
.build())
.toList());
return configDuplicate;
})
.toList());
return spreadsheetConfigCollectionRepository.save(duplicate).getId();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<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">
<changeSet author="labidiayo (generated)" id="1734360388052-1">
<createTable tableName="spreadsheet_config_collection">
<column name="id" type="UUID">
<constraints nullable="false" primaryKey="true" primaryKeyName="spreadsheet_config_collectionPK"/>
</column>
</createTable>
</changeSet>
<changeSet author="labidiayo (generated)" id="1734360388052-2">
<addColumn tableName="spreadsheet_config">
<column name="collection_id" type="uuid"/>
</addColumn>
</changeSet>
<changeSet author="labidiayo (generated)" id="1734360388052-3">
<addForeignKeyConstraint baseColumnNames="collection_id" baseTableName="spreadsheet_config" constraintName="fk_spreadsheet_config_collection" deferrable="false" initiallyDeferred="false" referencedColumnNames="id" referencedTableName="spreadsheet_config_collection" validate="true"/>
</changeSet>
</databaseChangeLog>
3 changes: 3 additions & 0 deletions src/main/resources/db/changelog/db.changelog-master.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ databaseChangeLog:
- include:
file: changesets/changelog_20241018T115212Z.xml
relativeToChangelogFile: true
- include:
file: changesets/changelog_20241216T144619Z.xml
relativeToChangelogFile: true
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ void testConversionToEntityOfSpreadsheetConfig() {
assertThat(entity)
.as("Entity conversion result")
.satisfies(e -> {
assertThat(e.getId()).isEqualTo(id);
assertThat(e.getSheetType()).isEqualTo(SheetType.BUS);
assertThat(e.getCustomColumns()).hasSize(2);
assertThat(e.getCustomColumns().get(0).getName()).isEqualTo("Column1");
Expand Down
Loading
Loading