diff --git a/network-store-server/pom.xml b/network-store-server/pom.xml
index 4a1c9f8e..a8e37c4a 100644
--- a/network-store-server/pom.xml
+++ b/network-store-server/pom.xml
@@ -107,7 +107,6 @@
org.liquibase
liquibase-core
- runtime
org.slf4j
diff --git a/network-store-server/src/main/java/com/powsybl/network/store/server/NetworkStoreRepository.java b/network-store-server/src/main/java/com/powsybl/network/store/server/NetworkStoreRepository.java
index d17a66f6..e397a5a8 100644
--- a/network-store-server/src/main/java/com/powsybl/network/store/server/NetworkStoreRepository.java
+++ b/network-store-server/src/main/java/com/powsybl/network/store/server/NetworkStoreRepository.java
@@ -6,9 +6,9 @@
*/
package com.powsybl.network.store.server;
-import com.fasterxml.jackson.databind.DeserializationFeature;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.SerializationFeature;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.google.common.base.Stopwatch;
import com.google.common.collect.Lists;
@@ -22,6 +22,8 @@
import com.powsybl.network.store.server.dto.PermanentLimitAttributes;
import com.powsybl.network.store.server.exceptions.JsonApiErrorResponseException;
import com.powsybl.network.store.server.exceptions.UncheckedSqlException;
+import com.powsybl.network.store.server.json.PermanentLimitSqlData;
+import com.powsybl.network.store.server.json.TemporaryLimitSqlData;
import com.powsybl.ws.commons.LogUtils;
import org.apache.commons.lang3.mutable.MutableInt;
import org.apache.commons.lang3.tuple.Pair;
@@ -30,10 +32,8 @@
import org.springframework.stereotype.Repository;
import javax.sql.DataSource;
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
+import java.io.UncheckedIOException;
+import java.sql.*;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@@ -1770,6 +1770,24 @@ public Map> getTemporaryLimitsWithInCl
}
}
+ public Map> getOldTemporaryLimitsWithInClause(UUID networkUuid, int variantNum, String columnNameForWhereClause, List valuesForInClause) {
+ if (valuesForInClause.isEmpty()) {
+ return Collections.emptyMap();
+ }
+ try (var connection = dataSource.getConnection()) {
+ var preparedStmt = connection.prepareStatement(QueryCatalog.buildOldTemporaryLimitWithInClauseQuery(columnNameForWhereClause, valuesForInClause.size()));
+ preparedStmt.setString(1, networkUuid.toString());
+ preparedStmt.setInt(2, variantNum);
+ for (int i = 0; i < valuesForInClause.size(); i++) {
+ preparedStmt.setString(3 + i, valuesForInClause.get(i));
+ }
+
+ return innerGetOldTemporaryLimits(preparedStmt);
+ } catch (SQLException e) {
+ throw new UncheckedSqlException(e);
+ }
+ }
+
public Map> getPermanentLimitsWithInClause(UUID networkUuid, int variantNum, String columnNameForWhereClause, List valuesForInClause) {
if (valuesForInClause.isEmpty()) {
return Collections.emptyMap();
@@ -1788,13 +1806,58 @@ public Map> getPermanentLimitsWithInCl
}
}
+ public Map> getOldPermanentLimitsWithInClause(UUID networkUuid, int variantNum, String columnNameForWhereClause, List valuesForInClause) {
+ if (valuesForInClause.isEmpty()) {
+ return Collections.emptyMap();
+ }
+ try (var connection = dataSource.getConnection()) {
+ var preparedStmt = connection.prepareStatement(QueryCatalog.buildOldPermanentLimitWithInClauseQuery(columnNameForWhereClause, valuesForInClause.size()));
+ preparedStmt.setString(1, networkUuid.toString());
+ preparedStmt.setInt(2, variantNum);
+ for (int i = 0; i < valuesForInClause.size(); i++) {
+ preparedStmt.setString(3 + i, valuesForInClause.get(i));
+ }
+
+ return innerGetOldPermanentLimits(preparedStmt);
+ } catch (SQLException e) {
+ throw new UncheckedSqlException(e);
+ }
+ }
+
public Map getLimitsInfos(UUID networkUuid, int variantNum, String columnNameForWhereClause, String valueForWhereClause) {
+ // migrate data from old limit table to new limit table
+ Map> oldTemporaryLimits = getOldTemporaryLimits(networkUuid, variantNum, columnNameForWhereClause, valueForWhereClause);
+ insertTemporaryLimitsAttributes(oldTemporaryLimits);
+ Map> oldPermanentLimits = getOldPermanentLimits(networkUuid, variantNum, columnNameForWhereClause, valueForWhereClause);
+ insertPermanentLimitsAttributes(oldPermanentLimits);
+
+ // delete in old table
+ if (!oldPermanentLimits.keySet().isEmpty()) {
+ deleteOldPermanentLimits(networkUuid, variantNum, oldPermanentLimits.keySet().stream().map(OwnerInfo::getEquipmentId).toList());
+ }
+ if (!oldTemporaryLimits.keySet().isEmpty()) {
+ deleteOldTemporaryLimits(networkUuid, variantNum, oldTemporaryLimits.keySet().stream().map(OwnerInfo::getEquipmentId).toList());
+ }
+
+ // get data from the new limit table
Map> temporaryLimits = getTemporaryLimits(networkUuid, variantNum, columnNameForWhereClause, valueForWhereClause);
Map> permanentLimits = getPermanentLimits(networkUuid, variantNum, columnNameForWhereClause, valueForWhereClause);
return mergeLimitsIntoLimitsInfos(temporaryLimits, permanentLimits);
}
public Map getLimitsInfosWithInClause(UUID networkUuid, int variantNum, String columnNameForWhereClause, List valuesForInClause) {
+ // migrate data from old limit table to new limit table
+ Map> oldTemporaryLimits = getOldTemporaryLimitsWithInClause(networkUuid, variantNum, columnNameForWhereClause, valuesForInClause);
+ insertTemporaryLimitsAttributes(oldTemporaryLimits);
+ Map> oldPermanentLimits = getOldPermanentLimitsWithInClause(networkUuid, variantNum, columnNameForWhereClause, valuesForInClause);
+ insertPermanentLimitsAttributes(oldPermanentLimits);
+ // delete in old table
+ if (!oldPermanentLimits.keySet().isEmpty()) {
+ deleteOldPermanentLimits(networkUuid, variantNum, oldPermanentLimits.keySet().stream().map(OwnerInfo::getEquipmentId).toList());
+ }
+ if (!oldTemporaryLimits.keySet().isEmpty()) {
+ deleteOldTemporaryLimits(networkUuid, variantNum, oldTemporaryLimits.keySet().stream().map(OwnerInfo::getEquipmentId).toList());
+ }
Map> temporaryLimits = getTemporaryLimitsWithInClause(networkUuid, variantNum, columnNameForWhereClause, valuesForInClause);
Map> permanentLimits = getPermanentLimitsWithInClause(networkUuid, variantNum, columnNameForWhereClause, valuesForInClause);
return mergeLimitsIntoLimitsInfos(temporaryLimits, permanentLimits);
@@ -1814,8 +1877,24 @@ private Map mergeLimitsIntoLimitsInfos(Map> getOldTemporaryLimits(UUID networkUuid, int variantNum, String columnNameForWhereClause, String valueForWhereClause) {
+ try (var connection = dataSource.getConnection()) {
+
+ // get from old table
+ var preparedStmt = connection.prepareStatement(QueryCatalog.buildOldTemporaryLimitQuery(columnNameForWhereClause));
+ preparedStmt.setString(1, networkUuid.toString());
+ preparedStmt.setInt(2, variantNum);
+ preparedStmt.setString(3, valueForWhereClause);
+
+ return innerGetOldTemporaryLimits(preparedStmt);
+ } catch (SQLException e) {
+ throw new UncheckedSqlException(e);
+ }
+ }
+
public Map> getTemporaryLimits(UUID networkUuid, int variantNum, String columnNameForWhereClause, String valueForWhereClause) {
try (var connection = dataSource.getConnection()) {
+ // get from new table
var preparedStmt = connection.prepareStatement(QueryCatalog.buildTemporaryLimitQuery(columnNameForWhereClause));
preparedStmt.setString(1, networkUuid.toString());
preparedStmt.setInt(2, variantNum);
@@ -1827,6 +1906,19 @@ public Map> getTemporaryLimits(UUID ne
}
}
+ public Map> getOldPermanentLimits(UUID networkUuid, int variantNum, String columnNameForWhereClause, String valueForWhereClause) {
+ try (var connection = dataSource.getConnection()) {
+ var preparedStmt = connection.prepareStatement(QueryCatalog.buildOldPermanentLimitQuery(columnNameForWhereClause));
+ preparedStmt.setString(1, networkUuid.toString());
+ preparedStmt.setInt(2, variantNum);
+ preparedStmt.setString(3, valueForWhereClause);
+
+ return innerGetOldPermanentLimits(preparedStmt);
+ } catch (SQLException e) {
+ throw new UncheckedSqlException(e);
+ }
+ }
+
public Map> getPermanentLimits(UUID networkUuid, int variantNum, String columnNameForWhereClause, String valueForWhereClause) {
try (var connection = dataSource.getConnection()) {
var preparedStmt = connection.prepareStatement(QueryCatalog.buildPermanentLimitQuery(columnNameForWhereClause));
@@ -1844,7 +1936,30 @@ private Map> innerGetTemporaryLimits(P
try (ResultSet resultSet = preparedStmt.executeQuery()) {
Map> map = new HashMap<>();
while (resultSet.next()) {
+ OwnerInfo owner = new OwnerInfo();
+ // In order, from the QueryCatalog.buildTemporaryLimitQuery SQL query :
+ // equipmentId, equipmentType, networkUuid, variantNum, side, limitType, name, value, acceptableDuration, fictitious
+ owner.setEquipmentId(resultSet.getString(1));
+ owner.setEquipmentType(ResourceType.valueOf(resultSet.getString(2)));
+ owner.setNetworkUuid(UUID.fromString(resultSet.getString(3)));
+ owner.setVariantNum(resultSet.getInt(4));
+ String temporaryLimitData = resultSet.getString(5);
+ List parsedTemporaryLimitData = mapper.readValue(temporaryLimitData, new TypeReference<>() { });
+ List temporaryLimits = parsedTemporaryLimitData.stream().map(TemporaryLimitSqlData::toTemporaryLimitAttributes).toList();
+ if (!temporaryLimits.isEmpty()) {
+ map.put(owner, temporaryLimits);
+ }
+ }
+ return map;
+ } catch (JsonProcessingException e) {
+ throw new UncheckedIOException(e);
+ }
+ }
+ private Map> innerGetOldTemporaryLimits(PreparedStatement preparedStmt) throws SQLException {
+ try (ResultSet resultSet = preparedStmt.executeQuery()) {
+ Map> map = new HashMap<>();
+ while (resultSet.next()) {
OwnerInfo owner = new OwnerInfo();
TemporaryLimitAttributes temporaryLimit = new TemporaryLimitAttributes();
// In order, from the QueryCatalog.buildTemporaryLimitQuery SQL query :
@@ -1860,7 +1975,6 @@ private Map> innerGetTemporaryLimits(P
temporaryLimit.setValue(resultSet.getDouble(9));
temporaryLimit.setAcceptableDuration(resultSet.getInt(10));
temporaryLimit.setFictitious(resultSet.getBoolean(11));
-
map.computeIfAbsent(owner, k -> new ArrayList<>());
map.get(owner).add(temporaryLimit);
}
@@ -1872,7 +1986,28 @@ private Map> innerGetPermanentLimits(P
try (ResultSet resultSet = preparedStmt.executeQuery()) {
Map> map = new HashMap<>();
while (resultSet.next()) {
+ OwnerInfo owner = new OwnerInfo();
+ owner.setEquipmentId(resultSet.getString(1));
+ owner.setEquipmentType(ResourceType.valueOf(resultSet.getString(2)));
+ owner.setNetworkUuid(UUID.fromString(resultSet.getString(3)));
+ owner.setVariantNum(resultSet.getInt(4));
+ String permanentLimitData = resultSet.getString(5);
+ List parsedTemporaryLimitData = mapper.readValue(permanentLimitData, new TypeReference<>() { });
+ List permanentLimits = parsedTemporaryLimitData.stream().map(PermanentLimitSqlData::toPermanentLimitAttributes).toList();
+ if (!permanentLimits.isEmpty()) {
+ map.put(owner, permanentLimits);
+ }
+ }
+ return map;
+ } catch (JsonProcessingException e) {
+ throw new UncheckedIOException(e);
+ }
+ }
+ private Map> innerGetOldPermanentLimits(PreparedStatement preparedStmt) throws SQLException {
+ try (ResultSet resultSet = preparedStmt.executeQuery()) {
+ Map> map = new HashMap<>();
+ while (resultSet.next()) {
OwnerInfo owner = new OwnerInfo();
PermanentLimitAttributes permanentLimit = new PermanentLimitAttributes();
// In order, from the QueryCatalog.buildTemporaryLimitQuery SQL query :
@@ -1914,29 +2049,27 @@ protected Map limitsInfos) {
Map> temporaryLimits = limitsInfos.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().getTemporaryLimits()));
+ insertTemporaryLimitsAttributes(temporaryLimits);
+ }
+
+ public void insertTemporaryLimitsAttributes(Map> temporaryLimits) {
try (var connection = dataSource.getConnection()) {
try (var preparedStmt = connection.prepareStatement(QueryCatalog.buildInsertTemporaryLimitsQuery())) {
- List