diff --git a/pom.xml b/pom.xml
index e82f2161a..3c0c1f0ab 100644
--- a/pom.xml
+++ b/pom.xml
@@ -210,7 +210,6 @@
org.liquibase
liquibase-core
- runtime
org.springframework.cloud
diff --git a/src/main/java/org/gridsuite/modification/server/NetworkModificationController.java b/src/main/java/org/gridsuite/modification/server/NetworkModificationController.java
index 9396298c8..f8396eeee 100644
--- a/src/main/java/org/gridsuite/modification/server/NetworkModificationController.java
+++ b/src/main/java/org/gridsuite/modification/server/NetworkModificationController.java
@@ -241,7 +241,7 @@ public ResponseEntity stashNetworkModifications(
networkModificationService.stashNetworkModifications(groupUuid, networkModificationUuids);
networkModificationService.reorderNetworkModifications(groupUuid, Boolean.FALSE);
} else {
- networkModificationService.restoreNetworkModifications(networkModificationUuids, groupUuid);
+ networkModificationService.restoreNetworkModifications(groupUuid, networkModificationUuids);
networkModificationService.reorderNetworkModifications(groupUuid, Boolean.TRUE);
}
return ResponseEntity.ok().build();
diff --git a/src/main/java/org/gridsuite/modification/server/migration/ModificationOrderMigration.java b/src/main/java/org/gridsuite/modification/server/migration/ModificationOrderMigration.java
new file mode 100644
index 000000000..2223973d4
--- /dev/null
+++ b/src/main/java/org/gridsuite/modification/server/migration/ModificationOrderMigration.java
@@ -0,0 +1,94 @@
+package org.gridsuite.modification.server.migration;
+
+import liquibase.change.custom.CustomTaskChange;
+import liquibase.database.Database;
+import liquibase.database.jvm.JdbcConnection;
+import liquibase.exception.CustomChangeException;
+import liquibase.exception.DatabaseException;
+import liquibase.exception.SetupException;
+import liquibase.exception.ValidationErrors;
+import liquibase.resource.ResourceAccessor;
+import org.apache.commons.lang3.tuple.Pair;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.UUID;
+import java.util.stream.IntStream;
+
+public class ModificationOrderMigration implements CustomTaskChange {
+
+ @Override
+ public void execute(Database database) throws CustomChangeException {
+ JdbcConnection connection = (JdbcConnection) database.getConnection();
+ try {
+ ResultSet groupIds = connection.createStatement().executeQuery("select distinct group_id from modification");
+ while (groupIds.next()) {
+ UUID groupId = UUID.fromString(groupIds.getString(1));
+ reorderNetworkModifications(groupId, true, connection);
+ reorderNetworkModifications(groupId, false, connection);
+ }
+ } catch (SQLException | DatabaseException e) {
+ throw new CustomChangeException(e);
+ }
+ }
+
+ private void reorderNetworkModifications(UUID groupId, boolean stashed, JdbcConnection connection) throws SQLException, DatabaseException {
+ List entities = findAllByGroupId(groupId, stashed, connection);
+ List> entitiesToUpdate = new ArrayList<>();
+ if (!entities.isEmpty()) {
+ if (Boolean.TRUE.equals(stashed)) {
+ IntStream.range(1, entities.size() + 1)
+ .forEach(i -> entitiesToUpdate.add(Pair.of(entities.get(i - 1), -i)));
+ } else {
+ IntStream.range(0, entities.size())
+ .forEach(i -> entitiesToUpdate.add(Pair.of(entities.get(i), i)));
+ }
+ }
+ saveAll(entitiesToUpdate, connection);
+ }
+
+ private List findAllByGroupId(UUID groupId, boolean stashed, JdbcConnection connection) throws DatabaseException, SQLException {
+ ResultSet resultSet = connection.createStatement().executeQuery(String.format("SELECT id FROM modification m WHERE m.group_id = '%s' AND m.stashed = %b order by modifications_order", groupId, stashed));
+ List entities = new ArrayList<>();
+ while (resultSet.next()) {
+ entities.add(UUID.fromString(resultSet.getString(1)));
+ }
+ return entities;
+ }
+
+ private void saveAll(List> entities, JdbcConnection connection) throws DatabaseException, SQLException {
+ Statement statement = connection.createStatement();
+ entities.forEach(pair -> {
+ try {
+ statement.addBatch(String.format("UPDATE modification SET modifications_order=%d WHERE id = '%s'", pair.getValue(), pair.getKey()));
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+
+ });
+ statement.executeBatch();
+ }
+
+ @Override
+ public String getConfirmationMessage() {
+ return "";
+ }
+
+ @Override
+ public void setUp() throws SetupException {
+
+ }
+
+ @Override
+ public void setFileOpener(ResourceAccessor resourceAccessor) {
+
+ }
+
+ @Override
+ public ValidationErrors validate(Database database) {
+ return null;
+ }
+}
diff --git a/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java
index 4b051a117..1568bd632 100644
--- a/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java
+++ b/src/main/java/org/gridsuite/modification/server/repositories/ModificationRepository.java
@@ -32,7 +32,7 @@ public interface ModificationRepository extends JpaRepository findAllBaseByGroupIdReverse(UUID uuid);
@Query(value = "SELECT m FROM ModificationEntity m WHERE m.group.id = ?1 AND m.stashed = ?2 order by m.modificationsOrder")
- List findAllStashedByGroupId(@Param("groupId") UUID groupId, @Param("stashed") Boolean stashed);
+ List findAllByGroupId(@Param("groupId") UUID groupId, @Param("stashed") Boolean stashed);
@Query(value = "SELECT new ModificationEntity(m.id, m.type) FROM ModificationEntity m WHERE m.id IN (?1)")
List findMetadataIn(List uuids);
diff --git a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java
index 255403667..1b56810e7 100644
--- a/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java
+++ b/src/main/java/org/gridsuite/modification/server/repositories/NetworkModificationRepository.java
@@ -89,9 +89,6 @@ private void saveModificationsNonTransactional(UUID groupUuid, List extends Mo
for (ModificationEntity m : modifications) {
modificationGroupEntity.addModification(m);
m.setModificationsOrder(order);
- // We need here to call the save() method on the modification entity cause the ids of the ModificationEntity's are used in further treatments in the same transaction.
- // As we generate the id in Java with @GeneratedValue(strategy = GenerationType.AUTO), saving the entity in the JPA world is enough to generate the id (no need to flush it).
- // Without the saving, the id generation would be done only at the flush() and wouldn't be available for the further treatments.
order++;
}
modificationRepository.saveAll(modifications);
@@ -420,36 +417,37 @@ public List getActiveModificationsInfos(@NonNull UUID groupUu
@Transactional
public void stashNetworkModifications(@NonNull List modificationUuids, int stashedModificationCount) {
int stashModificationOrder = -stashedModificationCount - 1;
+ List modificationEntities = new ArrayList<>();
for (UUID modificationUuid : modificationUuids) {
ModificationEntity modificationEntity = this.modificationRepository
.findById(modificationUuid)
.orElseThrow(() -> new NetworkModificationException(MODIFICATION_NOT_FOUND, String.format(MODIFICATION_NOT_FOUND_MESSAGE, modificationUuid)));
modificationEntity.setStashed(true);
modificationEntity.setModificationsOrder(stashModificationOrder);
- this.modificationRepository.save(modificationEntity);
+ modificationEntities.add(modificationEntity);
stashModificationOrder--;
}
+ this.modificationRepository.saveAll(modificationEntities);
}
@Transactional
public void reorderNetworkModifications(UUID groupId, Boolean stashed) {
- List entities = this.modificationRepository.findAllStashedByGroupId(groupId, stashed);
- if (entities.isEmpty()) {
- return;
- }
- if (Boolean.TRUE.equals(stashed)) {
- IntStream.range(1, entities.size() + 1)
- .forEach(i -> entities.get(i - 1).setModificationsOrder(-i));
- } else {
- IntStream.range(0, entities.size())
- .forEach(i -> entities.get(i).setModificationsOrder(i));
+ List entities = this.modificationRepository.findAllByGroupId(groupId, stashed);
+ if (!entities.isEmpty()) {
+ if (Boolean.TRUE.equals(stashed)) {
+ IntStream.range(1, entities.size() + 1)
+ .forEach(i -> entities.get(i - 1).setModificationsOrder(-i));
+ } else {
+ IntStream.range(0, entities.size())
+ .forEach(i -> entities.get(i).setModificationsOrder(i));
+ }
}
this.modificationRepository.saveAll(entities);
}
@Transactional
- public void restoreNetworkModifications(@NonNull List modificationUuids, int unStashedSize) {
- int modificationOrder = unStashedSize;
+ public void restoreNetworkModifications(@NonNull List modificationUuids, int unstashedSize) {
+ int modificationOrder = unstashedSize;
List modifications = modificationRepository.findAllByIdInReverse(modificationUuids);
if (modifications.size() != modificationUuids.size()) {
throw new NetworkModificationException(MODIFICATION_NOT_FOUND);
diff --git a/src/main/java/org/gridsuite/modification/server/service/NetworkModificationService.java b/src/main/java/org/gridsuite/modification/server/service/NetworkModificationService.java
index 9115202a5..2ee61becf 100644
--- a/src/main/java/org/gridsuite/modification/server/service/NetworkModificationService.java
+++ b/src/main/java/org/gridsuite/modification/server/service/NetworkModificationService.java
@@ -128,7 +128,7 @@ public void reorderNetworkModifications(UUID groupId, Boolean stashed) {
}
@Transactional
- public void restoreNetworkModifications(@NonNull List modificationUuids, UUID groupUuid) {
+ public void restoreNetworkModifications(UUID groupUuid, @NonNull List modificationUuids) {
networkModificationRepository.restoreNetworkModifications(modificationUuids,
networkModificationRepository.getModificationsCount(groupUuid, false));
}
diff --git a/src/main/resources/db/changelog/changesets/changelog_20241015T130742Z.xml b/src/main/resources/db/changelog/changesets/changelog_20241015T130742Z.xml
new file mode 100644
index 000000000..da8d73207
--- /dev/null
+++ b/src/main/resources/db/changelog/changesets/changelog_20241015T130742Z.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
diff --git a/src/main/resources/db/changelog/db.changelog-master.yaml b/src/main/resources/db/changelog/db.changelog-master.yaml
index 4ef96dc04..32d3e8bde 100644
--- a/src/main/resources/db/changelog/db.changelog-master.yaml
+++ b/src/main/resources/db/changelog/db.changelog-master.yaml
@@ -315,3 +315,6 @@ databaseChangeLog:
- include:
relativeToChangelogFile: true
file: changesets/changelog_20240912T130742Z.xml
+ - include:
+ relativeToChangelogFile: true
+ file: changesets/changelog_20241015T130742Z.xml
\ No newline at end of file
diff --git a/src/test/java/org/gridsuite/modification/server/service/ModificationRepositoryTest.java b/src/test/java/org/gridsuite/modification/server/service/ModificationRepositoryTest.java
index 402531564..84f4c033c 100644
--- a/src/test/java/org/gridsuite/modification/server/service/ModificationRepositoryTest.java
+++ b/src/test/java/org/gridsuite/modification/server/service/ModificationRepositoryTest.java
@@ -1316,7 +1316,7 @@ public void testStaticVarCompensatorCreation() {
.build().toEntity();
networkModificationRepository.saveModifications(TEST_GROUP_ID, List.of(createStaticVarCompensator1, createStaticVarCompensator2, createStaticVarCompensator3));
- assertRequestsCount(1, 3, 1, 0);
+ assertRequestsCount(2, 3, 0, 0);
List modificationInfos = networkModificationRepository.getModifications(TEST_GROUP_ID, true, true);
assertEquals(3, modificationInfos.size());
@@ -1331,7 +1331,7 @@ public void testStaticVarCompensatorCreation() {
SQLStatementCountValidator.reset();
networkModificationRepository.deleteModifications(TEST_GROUP_ID, List.of(createStaticVarCompensator2.getId(), createStaticVarCompensator3.getId()));
- assertRequestsCount(4, 0, 1, 2);
+ assertRequestsCount(4, 0, 0, 2);
SQLStatementCountValidator.reset();
assertEquals(1, networkModificationRepository.getModifications(TEST_GROUP_ID, true, true).size());