Skip to content

Commit f819ca0

Browse files
move parameters setter
1 parent e2f26a1 commit f819ca0

File tree

3 files changed

+67
-12
lines changed

3 files changed

+67
-12
lines changed

src/main/java/org/gridsuite/shortcircuit/server/ShortCircuitController.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.gridsuite.shortcircuit.server.service.ShortCircuitService;
1818
import org.springframework.data.domain.Page;
1919
import org.springframework.data.domain.Pageable;
20+
import org.springframework.http.HttpStatus;
2021
import org.springframework.http.MediaType;
2122
import org.springframework.http.ResponseEntity;
2223
import org.springframework.lang.Nullable;
@@ -165,6 +166,23 @@ public ResponseEntity<UUID> createParameters(@Parameter(description = "The param
165166
return ResponseEntity.ok(shortCircuitService.createParameters(parameters));
166167
}
167168

169+
@PutMapping(value = "/parameters/{parametersUuid}")
170+
@Operation(summary = "Create a entry for parameters")
171+
@ApiResponse(responseCode = "201", description = "A new entry has been created")
172+
@ApiResponse(responseCode = "204", description = "Entry updated with parameters passed")
173+
@ApiResponse(responseCode = "200", description = "Entry reset to default parameters")
174+
public ResponseEntity<Void> createParameters(
175+
@Parameter(description = "Parameters UUID") @PathVariable("parametersUuid") UUID parametersUuid,
176+
@Parameter(description = "The parameters to save. It no body or empty string/json, then default parameters will be used instead.", allowEmptyValue = true) @RequestBody(required = false) @Nullable final ShortCircuitParametersInfos parameters) {
177+
if (shortCircuitService.createParameters(parametersUuid, parameters)) {
178+
return ResponseEntity.status(HttpStatus.CREATED).build();
179+
} else if (parameters == null) {
180+
return ResponseEntity.ok().build();
181+
} else {
182+
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
183+
}
184+
}
185+
168186
@PostMapping(value = "/parameters/{parametersUuid}/duplicate")
169187
@Operation(summary = "Create a entry for parameters")
170188
@ApiResponse(responseCode = "201", description = "A new entry has been created")

src/main/java/org/gridsuite/shortcircuit/server/entities/ShortCircuitParametersEntity.java

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import jakarta.persistence.*;
1212
import lombok.*;
1313
import org.gridsuite.shortcircuit.server.dto.ShortCircuitPredefinedConfiguration;
14+
import org.jetbrains.annotations.Nullable;
1415

1516
import java.util.UUID;
1617

@@ -34,18 +35,8 @@ public ShortCircuitParametersEntity(boolean withLimitViolations, boolean withVol
3435
}
3536

3637
public ShortCircuitParametersEntity(ShortCircuitParametersEntity entity) {
37-
this(entity.withLimitViolations,
38-
entity.withVoltageResult,
39-
entity.withFortescueResult,
40-
entity.withFeederResult,
41-
entity.studyType,
42-
entity.minVoltageDropProportionalThreshold,
43-
entity.predefinedParameters,
44-
entity.withLoads,
45-
entity.withShuntCompensators,
46-
entity.withVscConverterStations,
47-
entity.withNeutralPosition,
48-
entity.initialVoltageProfileMode);
38+
this();
39+
this.updateFrom(entity);
4940
}
5041

5142
@Id
@@ -91,4 +82,38 @@ public ShortCircuitParametersEntity(ShortCircuitParametersEntity entity) {
9182
@Column(name = "initialVoltageProfileMode", columnDefinition = "varchar(15) default \"NOMINAL\"")
9283
@Enumerated(EnumType.STRING)
9384
private InitialVoltageProfileMode initialVoltageProfileMode = InitialVoltageProfileMode.NOMINAL;
85+
86+
/**
87+
* Reset values to defaults ones
88+
* @return this instance
89+
*/
90+
public ShortCircuitParametersEntity resetToDefaults() {
91+
this.updateFrom(new ShortCircuitParametersEntity());
92+
return this;
93+
}
94+
95+
/**
96+
* Update an entity using values of another one
97+
* @param entity the entity to take values from, will reset if {@code null}
98+
* @return this instance
99+
*/
100+
public ShortCircuitParametersEntity updateFrom(@Nullable final ShortCircuitParametersEntity entity) {
101+
if (entity == null) {
102+
this.resetToDefaults();
103+
} else {
104+
this.setWithLimitViolations(entity.withLimitViolations);
105+
this.setWithVoltageResult(entity.withVoltageResult);
106+
this.setWithFortescueResult(entity.withFortescueResult);
107+
this.setWithFeederResult(entity.withFeederResult);
108+
this.setStudyType(entity.studyType);
109+
this.setMinVoltageDropProportionalThreshold(entity.minVoltageDropProportionalThreshold);
110+
this.setPredefinedParameters(entity.predefinedParameters);
111+
this.setWithLoads(entity.withLoads);
112+
this.setWithShuntCompensators(entity.withShuntCompensators);
113+
this.setWithVscConverterStations(entity.withVscConverterStations);
114+
this.setWithNeutralPosition(entity.withNeutralPosition);
115+
this.setInitialVoltageProfileMode(entity.initialVoltageProfileMode);
116+
}
117+
return this;
118+
}
94119
}

src/main/java/org/gridsuite/shortcircuit/server/service/ShortCircuitService.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,18 @@ public UUID createParameters(@Nullable final ShortCircuitParametersInfos paramet
178178
return parametersRepository.save(parametersInfos != null ? EntityDtoUtils.convert(parametersInfos) : new ShortCircuitParametersEntity()).getId();
179179
}
180180

181+
/**
182+
* @return {@code true} if new instance created, {@code false} if updated existing
183+
*/
184+
public boolean createParameters(final UUID parametersUuid, @Nullable final ShortCircuitParametersInfos parametersInfos) {
185+
final ShortCircuitParametersEntity newValues = parametersInfos == null ? null : EntityDtoUtils.convert(parametersInfos);
186+
final Optional<ShortCircuitParametersEntity> entity = parametersRepository.findById(parametersUuid);
187+
parametersRepository.save(entity.map(scpe -> scpe.updateFrom(newValues))
188+
.or(() -> Optional.ofNullable(newValues))
189+
.orElseGet(ShortCircuitParametersEntity::new));
190+
return entity.isEmpty();
191+
}
192+
181193
public UUID duplicateParameters(final UUID parametersUuid) {
182194
return parametersRepository.save(parametersRepository.findById(parametersUuid)
183195
.map(ShortCircuitParametersEntity::new)

0 commit comments

Comments
 (0)