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

Dynamic Simulation - Rework on model's parameters database #64

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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,14 @@
package org.gridsuite.mapping.server;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.client.HttpClientErrorException;

@ControllerAdvice
public class RestResponseEntityExceptionHandler {
@ExceptionHandler(HttpClientErrorException.class)
protected ResponseEntity<Object> handleHttpClientErrorException(HttpClientErrorException exception) {
return ResponseEntity.status(exception.getStatusCode()).body(exception.getStatusText());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,6 @@ public ResponseEntity<ParametersSetsGroup> saveSimpleParametersSet(@PathVariable
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(modelService.saveParametersSetsGroup(modelName, setsGroup, false));
}

@GetMapping(value = "/{modelName}/parameters/definitions/")
@Operation(summary = "get parameters definitions for a given model")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "parameters definitions of the model")})
public ResponseEntity<List<ModelParameterDefinition>> getParametersDefinitionsFromModelName(@PathVariable("modelName") String modelName) {
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(modelService.getParametersDefinitionsFromModelName(modelName));
}

@GetMapping(value = "/")
@Operation(summary = "get models names")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "names of all models")})
public ResponseEntity<List<SimpleModel>> getModels() {
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(modelService.getModels());
}

@PostMapping(value = "/")
@Operation(summary = "Post a model")
@ApiResponses(value = {
Expand All @@ -92,7 +76,81 @@ public ResponseEntity<ParametersSetsGroup> deleteSet(@PathVariable("modelName")
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(modelService.deleteSet(modelName, groupName, groupType, setName));
}

@GetMapping(value = "/")
@Operation(summary = "get models names")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "names of all models")})
public ResponseEntity<List<SimpleModel>> getModels() {
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(modelService.getModels());
}

// --- BEGIN parameter definition-related endpoints --- //
@GetMapping(value = "/{modelName}/parameters/definitions")
@Operation(summary = "get parameters definitions for a given model")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "parameter definitions of the model")})
public ResponseEntity<List<ModelParameterDefinition>> getParameterDefinitionsFromModel(@PathVariable("modelName") String modelName) {
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(modelService.getParameterDefinitionsFromModel(modelName));
}

@PostMapping(value = "/{modelName}/parameters/definitions")
@Operation(summary = "Add new parameter definitions to model")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "saved model")})
public ResponseEntity<Model> addNewParameterDefinitionsToModel(@PathVariable("modelName") String modelName, @RequestBody List<ModelParameterDefinition> parameterDefinitions) {
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(modelService.addNewParameterDefinitionsToModel(modelName, parameterDefinitions));
}

@PatchMapping(value = "/{modelName}/parameters/definitions/add")
@Operation(summary = "Add existing parameter definitions to model")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "saved model")})
public ResponseEntity<Model> addExistingParameterDefinitionsToModel(@PathVariable("modelName") String modelName, @RequestBody List<String> parameterDefinitionNames) {
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(modelService.addExistingParameterDefinitionsToModel(modelName, parameterDefinitionNames));
}

@PatchMapping(value = "/{modelName}/parameters/definitions/remove")
@Operation(summary = "Remove existing parameter definitions from model")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "saved model")})
public ResponseEntity<Model> removeExistingParameterDefinitionsFromModel(@PathVariable("modelName") String modelName, @RequestBody List<String> parameterDefinitionNames) {
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(modelService.removeExistingParameterDefinitionsFromModel(modelName, parameterDefinitionNames));
}

@PatchMapping(value = "/{modelName}/parameters/definitions/remove-all")
@Operation(summary = "Reset empty parameter definitions on model")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "saved model")})
public ResponseEntity<Model> removeAllParameterDefinitionsFromModel(@PathVariable("modelName") String modelName) {
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(modelService.removeAllParameterDefinitionsOnModel(modelName));
}

@PostMapping(value = "/parameters/definitions")
@Operation(summary = "Save new parameter definitions")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Saved parameter definitions")})
public ResponseEntity<List<ModelParameterDefinition>> saveNewParameterDefinitions(@RequestBody List<ModelParameterDefinition> parameterDefinitions) {
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(modelService.saveNewParameterDefinitions(parameterDefinitions));
}

@DeleteMapping(value = "/parameters/definitions")
@Operation(summary = "Delete parameter definitions")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Deleted parameter definitions")})
public ResponseEntity<List<String>> deleteParameterDefinitions(@RequestBody List<String> parameterDefinitionNames) {
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(modelService.deleteParameterDefinitions(parameterDefinitionNames));
}
// --- END parameter definition-related endpoints --- //

// --- BEGIN variable-related endpoints --- //
@GetMapping(value = "/{modelName}/variables")
@Operation(summary = "get variable definitions for a given model")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "variable definitions of the model")})
public ResponseEntity<List<ModelVariableDefinition>> getVariablesFromModel(@PathVariable("modelName") String modelName) {
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(modelService.getVariableDefinitionsFromModel(modelName));
}

@PostMapping(value = "/{modelName}/variables")
@Operation(summary = "Add new variable definitions to model")
@ApiResponses(value = {
Expand All @@ -113,18 +171,26 @@ public ResponseEntity<Model> addExistingVariablesToModel(@PathVariable("modelNam
@Operation(summary = "Remove existing variable definitions from model")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "saved model")})
public ResponseEntity<Model> removeExistingVariablesToModel(@PathVariable("modelName") String modelName, @RequestBody List<String> variableDefinitionNames) {
public ResponseEntity<Model> removeExistingVariablesFromModel(@PathVariable("modelName") String modelName, @RequestBody List<String> variableDefinitionNames) {
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(modelService.removeExistingVariableDefinitionsFromModel(modelName, variableDefinitionNames));
}

@PatchMapping(value = "/{modelName}/variables/remove-all")
@Operation(summary = "Reset empty variable definitions on model")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "saved model")})
public ResponseEntity<Model> removeAllVariablesToModel(@PathVariable("modelName") String modelName) {
public ResponseEntity<Model> removeAllVariablesOnModel(@PathVariable("modelName") String modelName) {
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(modelService.removeAllVariableDefinitionsOnModel(modelName));
}

@GetMapping(value = "/variables-sets/{variableSetName}/variables")
@Operation(summary = "Add variable definitions from variables set")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "variable definitions of variables set")})
public ResponseEntity<List<ModelVariableDefinition>> getVariablesFromVariablesSet(@PathVariable("variableSetName") String variableSetName) {
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(modelService.getVariableDefinitionsFromVariablesSet(variableSetName));
}

@PostMapping(value = "/variables-sets/{variableSetName}/variables")
@Operation(summary = "Add new variable definitions to variables set")
@ApiResponses(value = {
Expand All @@ -149,6 +215,14 @@ public ResponseEntity<VariablesSet> removeAllVariableDefinitionOnVariablesSet(@P
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(modelService.removeAllVariableDefinitionOnVariablesSet(variableSetName));
}

@GetMapping(value = "/{modelName}/variables-sets")
@Operation(summary = "get variable sets for a given model")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "variable sets of the model")})
public ResponseEntity<List<VariablesSet>> getVariablesSetsFromModel(@PathVariable("modelName") String modelName) {
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(modelService.getVariablesSetsFromModel(modelName));
}

@PostMapping(value = "/{modelName}/variables-sets")
@Operation(summary = "Add new variables set to model")
@ApiResponses(value = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class ParametersSet {
public ParametersSet(ModelParameterSetEntity modelParameterSetEntity) {
name = modelParameterSetEntity.getName();
parameters = modelParameterSetEntity.getParameters().stream().map(ModelParameter::new).collect(Collectors.toList());
lastModifiedDate = modelParameterSetEntity.getLastModifiedDate();
lastModifiedDate = modelParameterSetEntity.getUpdatedDate();
}

public ParametersSet(String name, List<ModelParameter> parameters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ public ParametersSetsGroup(ModelSetsGroupEntity setsGroupEntity) {
name = setsGroupEntity.getName();
type = setsGroupEntity.getType();
sets = setsGroupEntity.getSets().stream().map(ParametersSet::new).collect(Collectors.toList());
modelName = setsGroupEntity.getModelName();
modelName = setsGroupEntity.getModel().getModelName();
}
}
70 changes: 59 additions & 11 deletions src/main/java/org/gridsuite/mapping/server/model/ModelEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,56 @@
import lombok.*;
import org.gridsuite.mapping.server.dto.models.Model;
import org.gridsuite.mapping.server.utils.EquipmentType;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;

import javax.persistence.*;
import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

import static javax.persistence.TemporalType.TIMESTAMP;

/**
* @author Mathieu Scalbert <mathieu.scalbert at rte-france.com>
*/
@Getter
@Setter
@Table(name = "models")
@Entity
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "models")
public class ModelEntity extends AbstractManuallyAssignedIdentifierEntity<String> implements Serializable {

// Could be replaced with UUID, but we lose the ease of use of names
@Id
@EqualsAndHashCode.Include
@Column(name = "model_name")
private String modelName;

@Column(name = "equipment_type")
private EquipmentType equipmentType;

@OneToMany(targetEntity = ModelParameterDefinitionEntity.class, mappedBy = "model", cascade = CascadeType.ALL, orphanRemoval = true)
private List<ModelParameterDefinitionEntity> parameterDefinitions = new ArrayList<>(0);
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH})
@JoinTable(
name = "models_model_parameter_definitions",
joinColumns = {@JoinColumn(name = "model_name")},
inverseJoinColumns = {@JoinColumn(name = "parameter_definition_name", referencedColumnName = "name")}
)
private Set<ModelParameterDefinitionEntity> parameterDefinitions = new LinkedHashSet<>(0);

@OneToMany(targetEntity = ModelSetsGroupEntity.class, mappedBy = "model", cascade = CascadeType.ALL, orphanRemoval = true)
private List<ModelSetsGroupEntity> setsGroups = new ArrayList<>(0);

@ManyToMany(targetEntity = ModelVariableDefinitionEntity.class, mappedBy = "models", cascade = {CascadeType.PERSIST, CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH})
private Set<ModelSetsGroupEntity> setsGroups = new LinkedHashSet<>(0);

// must exclude CascadeType.REMOVE to avoid unexpected cascade on delete a ModelVariableDefinitionEntity
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH})
@JoinTable(
name = "models_model_variable_definitions",
joinColumns = {@JoinColumn(name = "model_name")},
inverseJoinColumns = {@JoinColumn(name = "variable_definition_name")}
)
private Set<ModelVariableDefinitionEntity> variableDefinitions = new LinkedHashSet<>(0);

@ManyToMany(targetEntity = ModelVariableSetEntity.class, mappedBy = "models", cascade = {CascadeType.PERSIST, CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH})
Expand All @@ -55,13 +72,44 @@ public String getId() {
public ModelEntity(Model modelToConvert) {
modelName = modelToConvert.getModelName();
equipmentType = modelToConvert.getEquipmentType();
parameterDefinitions = modelToConvert.getParameterDefinitions() != null ? modelToConvert.getParameterDefinitions().stream().map(parameterDefinition -> new ModelParameterDefinitionEntity(parameterDefinition.getName(), modelToConvert.getModelName(), parameterDefinition.getType(), parameterDefinition.getOrigin(), parameterDefinition.getOriginName(), parameterDefinition.getFixedValue(), this)).collect(Collectors.toList()) : null;
setsGroups = modelToConvert.getSetsGroups() != null ? modelToConvert.getSetsGroups().stream().map(group -> new ModelSetsGroupEntity(this, group)).collect(Collectors.toList()) : null;
parameterDefinitions = modelToConvert.getParameterDefinitions() != null ? modelToConvert.getParameterDefinitions().stream().map(parameterDefinition -> new ModelParameterDefinitionEntity(this, parameterDefinition)).collect(Collectors.toSet()) : null;
setsGroups = modelToConvert.getSetsGroups() != null ? modelToConvert.getSetsGroups().stream().map(group -> new ModelSetsGroupEntity(this, group)).collect(Collectors.toCollection(LinkedHashSet::new)) : null;
variableDefinitions = modelToConvert.getVariableDefinitions() != null ? modelToConvert.getVariableDefinitions().stream().map(variableDefinition -> new ModelVariableDefinitionEntity(this, null, variableDefinition)).collect(Collectors.toCollection(LinkedHashSet::new)) : null;
variableSets = modelToConvert.getVariablesSets() != null ? modelToConvert.getVariablesSets().stream().map(variablesSet -> new ModelVariableSetEntity(this, variablesSet)).collect(Collectors.toCollection(LinkedHashSet::new)) : null;
}

@CreatedDate
@Temporal(TIMESTAMP)
@Column(name = "created_date", updatable = false)
private Date createdDate;

@LastModifiedDate
@Temporal(TIMESTAMP)
@Column(name = "updated_date")
private Date updatedDate;

// --- utils methods --- //

public void addSetsGroup(Collection<ModelSetsGroupEntity> setsGroups) {
setsGroups.forEach(setsGroup -> setsGroup.setModel(this));
this.setsGroups.addAll(setsGroups);
}

public void removeSetsGroup(Collection<ModelSetsGroupEntity> setsGroups) {
setsGroups.forEach(setsGroup -> setsGroup.setModel(null));
this.setsGroups.removeAll(setsGroups);
}

public void addParameterDefinitions(Collection<ModelParameterDefinitionEntity> parameterDefinitions) {
parameterDefinitions.forEach(parameterDefinition -> parameterDefinition.getModels().add(this));
this.parameterDefinitions.addAll(parameterDefinitions);
}

public void removeParameterDefinitions(Collection<ModelParameterDefinitionEntity> parameterDefinitions) {
parameterDefinitions.forEach(parameterDefinition -> parameterDefinition.getModels().remove(this));
this.parameterDefinitions.removeAll(parameterDefinitions);
}

public void addVariableDefinitions(Collection<ModelVariableDefinitionEntity> variableDefinitions) {
variableDefinitions.forEach(variableDefinition -> variableDefinition.getModels().add(this));
this.variableDefinitions.addAll(variableDefinitions);
Expand Down
Loading