Skip to content

Commit 228c24e

Browse files
authored
clean contingency list script (#130)
* clean contingency list script --------- Signed-off-by: SOUISSI Maissa (Externe) <[email protected]>
1 parent fa972f1 commit 228c24e

File tree

11 files changed

+37
-1115
lines changed

11 files changed

+37
-1115
lines changed

src/main/java/org/gridsuite/actions/server/ContingencyListController.java

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,6 @@ public ContingencyListController(ContingencyListService service) {
3838
this.service = service;
3939
}
4040

41-
@GetMapping(value = "/script-contingency-lists", produces = MediaType.APPLICATION_JSON_VALUE)
42-
@Operation(summary = "Get all script contingency lists")
43-
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "All script contingency lists")})
44-
public ResponseEntity<List<PersistentContingencyList>> getScriptContingencyLists() {
45-
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(service.getScriptContingencyLists());
46-
}
47-
4841
@GetMapping(value = "/form-contingency-lists", produces = MediaType.APPLICATION_JSON_VALUE)
4942
@Operation(summary = "Get all form contingency lists")
5043
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "All form contingency lists")})
@@ -59,17 +52,6 @@ public ResponseEntity<List<ContingencyListMetadata>> getContingencyListsMetadata
5952
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(service.getContingencyListsMetadata());
6053
}
6154

62-
@GetMapping(value = "/script-contingency-lists/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
63-
@Operation(summary = "Get script contingency list by id")
64-
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The script contingency list"),
65-
@ApiResponse(responseCode = "404", description = "The script contingency list does not exists")})
66-
public ResponseEntity<PersistentContingencyList> getScriptContingencyList(@PathVariable("id") UUID id) {
67-
return service.getScriptContingencyList(id).map(contingencyList -> ResponseEntity.ok()
68-
.contentType(MediaType.APPLICATION_JSON)
69-
.body(contingencyList))
70-
.orElse(ResponseEntity.notFound().build());
71-
}
72-
7355
@GetMapping(value = "/form-contingency-lists/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
7456
@Operation(summary = "Get form contingency list by id")
7557
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The form contingency list"),
@@ -110,42 +92,6 @@ public ResponseEntity<List<ContingencyInfos>> exportContingencyInfosList(@Reques
11092
return ResponseEntity.ok().body(service.exportContingencyInfosList(ids, networkUuid, variantId));
11193
}
11294

113-
@PostMapping(value = "/script-contingency-lists", consumes = MediaType.APPLICATION_JSON_VALUE)
114-
@Operation(summary = "Create a script contingency list")
115-
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The script contingency list have been created successfully")})
116-
public ResponseEntity<PersistentContingencyList> createScriptContingencyList(@RequestParam(required = false, value = "id") UUID id,
117-
@RequestBody ScriptContingencyList script) {
118-
return ResponseEntity.ok()
119-
.contentType(MediaType.APPLICATION_JSON)
120-
.body(service.createScriptContingencyList(id, script));
121-
}
122-
123-
@PostMapping(value = "/script-contingency-lists")
124-
@Operation(summary = "Create a script contingency list from another existing one")
125-
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The script contingency list have been duplicated successfully"),
126-
@ApiResponse(responseCode = "404", description = "Source script contingency list not found")})
127-
public ResponseEntity<UUID> duplicateScriptContingencyList(@RequestParam("duplicateFrom") UUID scriptContingencyListsId) {
128-
return service.duplicateScriptContingencyList(scriptContingencyListsId).map(contingencyList -> ResponseEntity.ok()
129-
.contentType(MediaType.APPLICATION_JSON)
130-
.body(contingencyList))
131-
.orElse(ResponseEntity.notFound().build());
132-
}
133-
134-
@PutMapping(value = "/script-contingency-lists/{id}", consumes = MediaType.APPLICATION_JSON_VALUE)
135-
@Operation(summary = "Modify a script contingency list")
136-
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The script contingency list have been modified successfully")})
137-
public ResponseEntity<Void> modifyScriptContingencyList(
138-
@PathVariable UUID id,
139-
@RequestBody(required = false) ScriptContingencyList script,
140-
@RequestHeader("userId") String userId) {
141-
try {
142-
service.modifyScriptContingencyList(id, script, userId);
143-
return ResponseEntity.ok().build();
144-
} catch (EntityNotFoundException ignored) {
145-
return ResponseEntity.notFound().build();
146-
}
147-
}
148-
14995
@PostMapping(value = "/form-contingency-lists", consumes = MediaType.APPLICATION_JSON_VALUE)
15096
@Operation(summary = "Create a form contingency list")
15197
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The form contingency list have been created successfully")})
@@ -241,25 +187,6 @@ public ResponseEntity<Void> deleteContingencyList(@PathVariable("id") UUID id) {
241187
}
242188
}
243189

244-
@PostMapping(value = "/form-contingency-lists/{id}/replace-with-script")
245-
@Operation(summary = "Replace a form contingency list with a script contingency list")
246-
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The form contingency list have been replaced successfully")})
247-
public ResponseEntity<PersistentContingencyList> replaceFormContingencyListWithScript(@PathVariable("id") UUID id, @RequestHeader("userId") String userId) {
248-
return ResponseEntity.ok()
249-
.contentType(MediaType.APPLICATION_JSON)
250-
.body(service.replaceFormContingencyListWithScript(id, userId));
251-
}
252-
253-
@PostMapping(value = "/form-contingency-lists/{id}/new-script")
254-
@Operation(summary = "Create a new script contingency list from a form contingency list")
255-
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The script contingency list have been created successfully")})
256-
public ResponseEntity<PersistentContingencyList> newScriptFromFormContingencyList(@PathVariable("id") UUID id,
257-
@RequestParam(required = false, value = "newId") UUID newId) {
258-
return ResponseEntity.ok()
259-
.contentType(MediaType.APPLICATION_JSON)
260-
.body(service.newScriptFromFormContingencyList(id, newId));
261-
}
262-
263190
@GetMapping(value = "/contingency-lists/metadata", produces = MediaType.APPLICATION_JSON_VALUE)
264191
@Operation(summary = "Get contingency lists metadata")
265192
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "contingency lists metadata"),

src/main/java/org/gridsuite/actions/server/ContingencyListService.java

Lines changed: 3 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.gridsuite.actions.server.entities.*;
2525
import org.gridsuite.actions.server.repositories.FormContingencyListRepository;
2626
import org.gridsuite.actions.server.repositories.IdBasedContingencyListRepository;
27-
import org.gridsuite.actions.server.repositories.ScriptContingencyListRepository;
2827
import org.gridsuite.actions.server.utils.ContingencyListType;
2928
import org.springframework.context.annotation.ComponentScan;
3029
import org.springframework.dao.EmptyResultDataAccessException;
@@ -46,8 +45,6 @@
4645
@Service
4746
public class ContingencyListService {
4847

49-
private final ScriptContingencyListRepository scriptContingencyListRepository;
50-
5148
private final FormContingencyListRepository formContingencyListRepository;
5249

5350
private final IdBasedContingencyListRepository idBasedContingencyListRepository;
@@ -56,43 +53,29 @@ public class ContingencyListService {
5653

5754
private final NotificationService notificationService;
5855

59-
private final FormToGroovyScript formToScript = new FormToGroovyScript();
60-
61-
public ContingencyListService(ScriptContingencyListRepository scriptContingencyListRepository,
62-
FormContingencyListRepository formContingencyListRepository,
56+
public ContingencyListService(FormContingencyListRepository formContingencyListRepository,
6357
IdBasedContingencyListRepository idBasedContingencyListRepository,
6458
NetworkStoreService networkStoreService,
6559
NotificationService notificationService) {
66-
this.scriptContingencyListRepository = scriptContingencyListRepository;
6760
this.formContingencyListRepository = formContingencyListRepository;
6861
this.idBasedContingencyListRepository = idBasedContingencyListRepository;
6962
this.networkStoreService = networkStoreService;
7063
this.notificationService = notificationService;
7164
}
7265

73-
private static ScriptContingencyList fromScriptContingencyListEntity(ScriptContingencyListEntity entity) {
74-
return new ScriptContingencyList(entity.getId(), entity.getModificationDate(), entity.getScript() != null ? entity.getScript() : "");
75-
}
76-
7766
private static FormContingencyList fromFormContingencyListEntity(FormContingencyListEntity entity) {
7867
return new FormContingencyList(entity.getId(), entity.getModificationDate(), entity.getEquipmentType(), NumericalFilterEntity.convert(entity.getNominalVoltage()), NumericalFilterEntity.convert(entity.getNominalVoltage1()), NumericalFilterEntity.convert(entity.getNominalVoltage2()),
7968
entity.getCountries() == null ? entity.getCountries() : Set.copyOf(entity.getCountries()),
8069
entity.getCountries1() == null ? entity.getCountries1() : Set.copyOf(entity.getCountries1()),
8170
entity.getCountries2() == null ? entity.getCountries2() : Set.copyOf(entity.getCountries2()));
8271
}
8372

84-
List<PersistentContingencyList> getScriptContingencyLists() {
85-
return scriptContingencyListRepository.findAll().stream().map(ContingencyListService::fromScriptContingencyListEntity).collect(Collectors.toList());
86-
}
87-
8873
ContingencyListMetadata fromContingencyListEntity(AbstractContingencyEntity entity, ContingencyListType type) {
8974
return new ContingencyListMetadataImpl(entity.getId(), type, entity.getModificationDate());
9075
}
9176

9277
List<ContingencyListMetadata> getContingencyListsMetadata() {
9378
return Stream.of(
94-
scriptContingencyListRepository.findAll().stream().map(scriptContingencyListEntity ->
95-
fromContingencyListEntity(scriptContingencyListEntity, ContingencyListType.SCRIPT)),
9679
formContingencyListRepository.findAll().stream().map(formContingencyListEntity ->
9780
fromContingencyListEntity(formContingencyListEntity, ContingencyListType.FORM)),
9881
idBasedContingencyListRepository.findAll().stream().map(idBasedContingencyListEntity ->
@@ -102,8 +85,6 @@ List<ContingencyListMetadata> getContingencyListsMetadata() {
10285

10386
List<ContingencyListMetadata> getContingencyListsMetadata(List<UUID> ids) {
10487
return Stream.of(
105-
scriptContingencyListRepository.findAllById(ids).stream().map(scriptContingencyListEntity ->
106-
fromContingencyListEntity(scriptContingencyListEntity, ContingencyListType.SCRIPT)),
10788
formContingencyListRepository.findAllById(ids).stream().map(formContingencyListEntity ->
10889
fromContingencyListEntity(formContingencyListEntity, ContingencyListType.FORM)),
10990
idBasedContingencyListRepository.findAllById(ids).stream().map(idBasedContingencyListEntity ->
@@ -116,11 +97,6 @@ public List<PersistentContingencyList> getFormContingencyLists() {
11697
return formContingencyListRepository.findAllWithCountries().stream().map(ContingencyListService::fromFormContingencyListEntity).collect(Collectors.toList());
11798
}
11899

119-
Optional<PersistentContingencyList> getScriptContingencyList(UUID id) {
120-
Objects.requireNonNull(id);
121-
return scriptContingencyListRepository.findById(id).map(ContingencyListService::fromScriptContingencyListEntity);
122-
}
123-
124100
private Optional<FormContingencyListEntity> doGetFormContingencyListWithPreFetchedCountries(UUID name) {
125101
return formContingencyListRepository.findById(name).map(entity -> {
126102
@SuppressWarnings("unused")
@@ -203,8 +179,7 @@ private PersistentContingencyList findContingencyList(UUID id, Network network)
203179

204180
private Optional<PersistentContingencyList> getAnyContingencyList(UUID id, Network network) {
205181
return doGetFormContingencyList(id)
206-
.or(() -> doGetIdBasedContingencyList(id, network))
207-
.or(() -> getScriptContingencyList(id));
182+
.or(() -> doGetIdBasedContingencyList(id, network));
208183
}
209184

210185
private List<ContingencyInfos> evaluateContingencyList(PersistentContingencyList persistentContingencyList, Network network) {
@@ -266,27 +241,6 @@ private Network getNetworkFromUuid(UUID networkUuid, String variantId) {
266241
return network;
267242
}
268243

269-
ScriptContingencyList createScriptContingencyList(UUID id, ScriptContingencyList script) {
270-
ScriptContingencyListEntity entity = new ScriptContingencyListEntity(script);
271-
entity.setId(id == null ? UUID.randomUUID() : id);
272-
return fromScriptContingencyListEntity(scriptContingencyListRepository.save(entity));
273-
}
274-
275-
Optional<UUID> duplicateScriptContingencyList(UUID sourceListId) {
276-
Optional<ScriptContingencyList> scriptContingencyList = getScriptContingencyList(sourceListId).map(s -> createScriptContingencyList(null, (ScriptContingencyList) s));
277-
if (!scriptContingencyList.isPresent()) {
278-
throw createNotFoundException(sourceListId.toString(), "Script contingency list");
279-
} else {
280-
return Optional.of(scriptContingencyList.get().getId());
281-
}
282-
}
283-
284-
@Transactional
285-
public void modifyScriptContingencyList(UUID id, ScriptContingencyList script, String userId) {
286-
scriptContingencyListRepository.save(scriptContingencyListRepository.getReferenceById(id).update(script));
287-
notificationService.emitElementUpdated(id, userId);
288-
}
289-
290244
@Transactional
291245
public FormContingencyList createFormContingencyList(UUID id, FormContingencyList formContingencyList) {
292246
return doCreateFormContingencyList(id, formContingencyList);
@@ -338,49 +292,11 @@ public void deleteContingencyList(UUID id) throws EmptyResultDataAccessException
338292
// if there is no form contingency list by this Id, deleted count == 0
339293
if (formContingencyListRepository.deleteFormContingencyListEntityById(id) == 0) {
340294
if (idBasedContingencyListRepository.deleteIdBasedContingencyListEntityById(id) == 0) {
341-
if (scriptContingencyListRepository.deleteScriptContingencyListById(id) == 0) {
342-
throw new EmptyResultDataAccessException("No element found", 1);
343-
}
295+
throw new EmptyResultDataAccessException("No element found", 1);
344296
}
345297
}
346298
}
347299

348-
private String generateGroovyScriptFromForm(FormContingencyList formContingencyList) {
349-
return formToScript.generateGroovyScriptFromForm(formContingencyList);
350-
}
351-
352-
@Transactional
353-
public ScriptContingencyList replaceFormContingencyListWithScript(UUID id, String userId) {
354-
Objects.requireNonNull(id);
355-
Optional<FormContingencyListEntity> formContingencyList = doGetFormContingencyListWithPreFetchedCountries(id);
356-
ScriptContingencyList result = formContingencyList.map(entity -> {
357-
String script = generateGroovyScriptFromForm(fromFormContingencyListEntity(entity));
358-
var scriptContingencyListEntity = new ScriptContingencyListEntity(new ScriptContingencyList(id, null, script));
359-
scriptContingencyListEntity.setId(id);
360-
var res = fromScriptContingencyListEntity(scriptContingencyListRepository.save(scriptContingencyListEntity));
361-
formContingencyListRepository.deleteById(id);
362-
return res;
363-
}).orElseThrow(() -> {
364-
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Contingency list " + id + " not found");
365-
});
366-
notificationService.emitElementUpdated(id, userId);
367-
return result;
368-
}
369-
370-
@Transactional
371-
public ScriptContingencyList newScriptFromFormContingencyList(UUID id, UUID newId) {
372-
Objects.requireNonNull(id);
373-
Optional<FormContingencyListEntity> formContingencyList = doGetFormContingencyListWithPreFetchedCountries(id);
374-
return formContingencyList.map(entity -> {
375-
String script = generateGroovyScriptFromForm(fromFormContingencyListEntity(entity));
376-
ScriptContingencyListEntity scriptEntity = new ScriptContingencyListEntity(new ScriptContingencyList(script));
377-
scriptEntity.setId(newId == null ? UUID.randomUUID() : newId);
378-
return fromScriptContingencyListEntity(scriptContingencyListRepository.save(scriptEntity));
379-
}).orElseThrow(() -> {
380-
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Contingency list " + id + " not found");
381-
});
382-
}
383-
384300
private static IdBasedContingencyList fromIdBasedContingencyListEntity(IdBasedContingencyListEntity entity, Network network) {
385301
List<NetworkElementIdentifier> listOfNetworkElementIdentifierList = new ArrayList<>();
386302
Map<String, Set<String>> notFoundElements = new HashMap<>();

0 commit comments

Comments
 (0)