From cdf309b2d58c63f4370d78b3c6076d40599ad2c6 Mon Sep 17 00:00:00 2001 From: basseche Date: Fri, 8 Aug 2025 13:15:56 +0200 Subject: [PATCH] refactor migration Signed-off-by: basseche --- .../migration/MergeLimitSetsGroupsTables.java | 252 ++++++++++-------- 1 file changed, 143 insertions(+), 109 deletions(-) diff --git a/src/main/java/org/gridsuite/modification/server/migration/MergeLimitSetsGroupsTables.java b/src/main/java/org/gridsuite/modification/server/migration/MergeLimitSetsGroupsTables.java index dfa415070..12db49296 100644 --- a/src/main/java/org/gridsuite/modification/server/migration/MergeLimitSetsGroupsTables.java +++ b/src/main/java/org/gridsuite/modification/server/migration/MergeLimitSetsGroupsTables.java @@ -12,6 +12,7 @@ import liquibase.statement.core.DeleteStatement; import liquibase.statement.core.InsertStatement; import liquibase.statement.core.UpdateStatement; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -48,58 +49,73 @@ private int getLength(ResultSet resultSet) throws SQLException { return length; } - private boolean compareOperationalLimitsInfos(JdbcConnection connection, String operationalLimitsIds1, String operationalLimitsIds2) throws DatabaseException, SQLException { + private String getCurrentsLimitId(JdbcConnection connection, String operationalLimitsId) throws DatabaseException, SQLException { final String query = "select current_limits_id from operational_limits_group where uuid =''"; - ResultSet currentLimits1 = connection.createStatement().executeQuery(query.replace("", operationalLimitsIds1)); - ResultSet currentLimits2 = connection.createStatement().executeQuery(query.replace("", operationalLimitsIds2)); - - if (currentLimits1.next() && currentLimits2.next()) { - String currentLimitsQuery = "select permanent_limit from current_limits where id = ''"; - String currentLimitsId1 = currentLimits1.getString(CURRENT_LIMITS_ID_COL); - String currentLimitsId2 = currentLimits2.getString(CURRENT_LIMITS_ID_COL); - ResultSet permanentLimit = connection.createStatement().executeQuery(currentLimitsQuery.replace("", currentLimitsId1)); - ResultSet permanentLimit2 = connection.createStatement().executeQuery(currentLimitsQuery.replace("", currentLimitsId2)); - - boolean haspermanentLimit = permanentLimit.next(); - boolean haspermanentLimit2 = permanentLimit2.next(); - if (haspermanentLimit != haspermanentLimit2) { - return false; - } - if (haspermanentLimit && !Objects.equals(permanentLimit.getString("permanent_limit"), - permanentLimit2.getString("permanent_limit"))) { - return false; + try (ResultSet currentLimits = connection.createStatement().executeQuery(query.replace("", operationalLimitsId))) { + if (currentLimits.next()) { + return currentLimits.getString(CURRENT_LIMITS_ID_COL); } + } + return null; + } - final String queryTemporary = "select * from current_temporary_limits where id =''"; - Statement statement1 = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); - Statement statement2 = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); - ResultSet temporaryLimit = statement1.executeQuery(queryTemporary.replace("", currentLimitsId1)); - ResultSet temporaryLimit2 = statement2.executeQuery(queryTemporary.replace("", currentLimitsId2)); - if (getLength(temporaryLimit) != getLength(temporaryLimit2)) { - return false; + private String getPermanentLimit(JdbcConnection connection, String currentLimitsId) throws DatabaseException, SQLException { + String currentLimitsQuery = "select permanent_limit from current_limits where id = ''"; + try (ResultSet permanentLimit = connection.createStatement().executeQuery(currentLimitsQuery.replace("", currentLimitsId))) { + if (permanentLimit.next()) { + return permanentLimit.getString("permanent_limit"); } + } + return null; + } + + private boolean compareTemporaryLimitsInfos(JdbcConnection connection, String currentLimitsId1, String currentLimitsId2) throws DatabaseException, SQLException { + final String queryTemporary = "select * from current_temporary_limits where id =''"; + try (Statement statement1 = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY)) { + try (Statement statement2 = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY)) { + ResultSet temporaryLimit = statement1.executeQuery(queryTemporary.replace("", currentLimitsId1)); + ResultSet temporaryLimit2 = statement2.executeQuery(queryTemporary.replace("", currentLimitsId2)); + if (getLength(temporaryLimit) != getLength(temporaryLimit2)) { + return false; + } - while (temporaryLimit.next()) { - String name = temporaryLimit.getString("name"); - boolean found = false; - temporaryLimit2.beforeFirst(); - while (temporaryLimit2.next()) { - if (Objects.equals(temporaryLimit2.getString("name"), name)) { - if (!Objects.equals(temporaryLimit.getString("value_"), temporaryLimit2.getString("value_")) || - !Objects.equals(temporaryLimit.getString("acceptable_duration"), temporaryLimit2.getString("acceptable_duration"))) { - return false; + while (temporaryLimit.next()) { + String name = temporaryLimit.getString("name"); + boolean found = false; + temporaryLimit2.beforeFirst(); + while (temporaryLimit2.next()) { + if (Objects.equals(temporaryLimit2.getString("name"), name)) { + if (!Objects.equals(temporaryLimit.getString("value_"), temporaryLimit2.getString("value_")) || + !Objects.equals(temporaryLimit.getString("acceptable_duration"), temporaryLimit2.getString("acceptable_duration"))) { + return false; + } + found = true; + break; } - found = true; - break; } - } - // temporary limit not found - if (!found) { - return false; + // temporary limit not found + if (!found) { + return false; + } } } } + return true; + } + + private boolean compareOperationalLimitsInfos(JdbcConnection connection, String operationalLimitsId1, String operationalLimitsId2) throws DatabaseException, SQLException { + String currentLimitsId1 = getCurrentsLimitId(connection, operationalLimitsId1); + String currentLimitsId2 = getCurrentsLimitId(connection, operationalLimitsId2); + + if (!StringUtils.isEmpty(currentLimitsId1) && !StringUtils.isEmpty(currentLimitsId2)) { + String permanentLimit = getPermanentLimit(connection, currentLimitsId1); + String permanentLimit2 = getPermanentLimit(connection, currentLimitsId2); + if (!Objects.equals(permanentLimit, permanentLimit2)) { + return false; + } + return compareTemporaryLimitsInfos(connection, currentLimitsId1, currentLimitsId2); + } return true; } @@ -113,6 +129,88 @@ private void addOperationalLimitsGroupApplicability(Database database, ResultSet } } + private String getBranchCreationOpLimitsGroup2Id(JdbcConnection connection, String branchCreationOpLimitsGroups2Table, + String branchId, String branchCreationOpLimitsGroup1Pos) throws SQLException, DatabaseException { + try (ResultSet branchCreationOpLimitsGroups2 = connection.createStatement() + .executeQuery(createQueryLineOpLimitsGroupsWithPos(branchCreationOpLimitsGroups2Table, branchId, + branchCreationOpLimitsGroup1Pos))) { + + branchCreationOpLimitsGroups2.next(); + return branchCreationOpLimitsGroups2.getString(OPERATIONAL_LG_ID_COL); + } + } + + private void migrateBranch(Database database, JdbcConnection connection, List statements, + ResultSet branches, String branchCreationOpLimitsGroups1Table, + String branchCreationOpLimitsGroups2Table, String branchCreationOpLimitsGroupsTable) throws SQLException, DatabaseException { + int position = 0; + //get operational limits groups1 + try (ResultSet branchCreationOpLimitsGroups1 = connection.createStatement() + .executeQuery(createQueryLineOpLimitsGroups(branches.getString(ID_COL), branchCreationOpLimitsGroups1Table))) { + + while (branchCreationOpLimitsGroups1.next()) { + String branchCreationOpLimitsGroup1Id = branchCreationOpLimitsGroups1.getString(OPERATIONAL_LG_ID_COL); + String branchCreationOpLimitsGroup1Pos = branchCreationOpLimitsGroups1.getString(POS_OP_LG_COL); + + String branchCreationOpLimitsGroup2Id = getBranchCreationOpLimitsGroup2Id(connection, branchCreationOpLimitsGroups2Table, + branches.getString(ID_COL), branchCreationOpLimitsGroup1Pos); + + // Compare Both limitsGroups 1 and 2 limits + String query = "select * from operational_limits_group where uuid = ''"; + try (ResultSet operationalLimitsGroups1 = connection.createStatement().executeQuery(query.replace("", branchCreationOpLimitsGroup1Id))) { + + if (compareOperationalLimitsInfos(connection, branchCreationOpLimitsGroup1Id, branchCreationOpLimitsGroup2Id)) { + String query2 = "select current_limits_id from operational_limits_group where uuid = ''"; + try (ResultSet operationalLimitsGroups2 = connection.createStatement().executeQuery(query2.replace("", branchCreationOpLimitsGroup2Id))) { + + // - remove line from operational_limits_group + // - remove related permanent limit from current_limits + // - remove all other related limits from current_temporary_limits + if (operationalLimitsGroups2.next()) { + statements.add(new DeleteStatement(database.getDefaultCatalogName(), database.getDefaultSchemaName(), OPERATIONAL_LIMITS_GROUPS_TABLE) + .setWhere(UUID_COL + " = '" + branchCreationOpLimitsGroup2Id + "'")); + String currentLimitId = operationalLimitsGroups2.getString(CURRENT_LIMITS_ID_COL); + statements.add(new DeleteStatement(database.getDefaultCatalogName(), database.getDefaultSchemaName(), "current_temporary_limits") + .setWhere(ID_COL + " = '" + currentLimitId + "'")); + statements.add(new DeleteStatement(database.getDefaultCatalogName(), database.getDefaultSchemaName(), "current_limits") + .setWhere(ID_COL + " = '" + currentLimitId + "'")); + } + } + + addOperationalLimitsGroupApplicability(database, operationalLimitsGroups1, statements, "EQUIPMENT"); + + // if they are equal then add only one limitGroup in new Table with application side = equipment (both) + statements.add(new InsertStatement(database.getDefaultCatalogName(), database.getDefaultSchemaName(), branchCreationOpLimitsGroupsTable) + .addColumnValue(BRANCH_ID_COL, branches.getString(ID_COL)) + .addColumnValue(OPERATIONAL_LG_ID_COL, branchCreationOpLimitsGroup1Id) + .addColumnValue(POS_OP_LG_COL, position++)); + } else { + // change Applicability side 1 + addOperationalLimitsGroupApplicability(database, operationalLimitsGroups1, statements, "SIDE1"); + + // Add to merged table + statements.add(new InsertStatement(database.getDefaultCatalogName(), database.getDefaultSchemaName(), branchCreationOpLimitsGroupsTable) + .addColumnValue(BRANCH_ID_COL, branches.getString(ID_COL)) + .addColumnValue(OPERATIONAL_LG_ID_COL, branchCreationOpLimitsGroup1Id) + .addColumnValue(POS_OP_LG_COL, position++)); + + // Change Applicability side 2 + String query3 = "select * from operational_limits_group where uuid = ''"; + try (ResultSet operationalLimitsGroups2 = connection.createStatement().executeQuery(query3.replace("", branchCreationOpLimitsGroup2Id))) { + addOperationalLimitsGroupApplicability(database, operationalLimitsGroups2, statements, "SIDE2"); + } + + // Add to merged table + statements.add(new InsertStatement(database.getDefaultCatalogName(), database.getDefaultSchemaName(), branchCreationOpLimitsGroupsTable) + .addColumnValue(BRANCH_ID_COL, branches.getString(ID_COL)) + .addColumnValue(OPERATIONAL_LG_ID_COL, branchCreationOpLimitsGroup2Id) + .addColumnValue(POS_OP_LG_COL, position++)); + } + } + } + } + } + @Override public SqlStatement[] generateStatements(Database database) throws CustomChangeException { JdbcConnection connection = (JdbcConnection) database.getConnection(); @@ -133,72 +231,8 @@ public SqlStatement[] generateStatements(Database database) throws CustomChangeE String branchesToProcess = "Select id, selected_operational_limits_group_id1, selected_operational_limits_group_id2 from "; try (ResultSet branches = connection.createStatement().executeQuery(branchesToProcess.replace("
", branchCreationTable))) { while (branches.next()) { - int position = 0; - //get operational limits groups1 - ResultSet branchCreationOpLimitsGroups1 = connection.createStatement() - .executeQuery(createQueryLineOpLimitsGroups(branches.getString(ID_COL), branchCreationOpLimitsGroups1Table)); - - while (branchCreationOpLimitsGroups1.next()) { - String branchCreationOpLimitsGroup1Id = branchCreationOpLimitsGroups1.getString(OPERATIONAL_LG_ID_COL); - String branchCreationOpLimitsGroup1Pos = branchCreationOpLimitsGroups1.getString(POS_OP_LG_COL); - - ResultSet branchCreationOpLimitsGroups2 = connection.createStatement() - .executeQuery(createQueryLineOpLimitsGroupsWithPos(branchCreationOpLimitsGroups2Table, branches.getString(ID_COL), - branchCreationOpLimitsGroup1Pos)); - - branchCreationOpLimitsGroups2.next(); - String branchCreationOpLimitsGroup2Id = branchCreationOpLimitsGroups2.getString(OPERATIONAL_LG_ID_COL); - - // Compare Both limitsGroups 1 and 2 limits - String query = "select * from operational_limits_group where uuid = ''"; - ResultSet operationalLimitsGroups1 = connection.createStatement().executeQuery(query.replace("", branchCreationOpLimitsGroup1Id)); - - if (compareOperationalLimitsInfos(connection, branchCreationOpLimitsGroup1Id, branchCreationOpLimitsGroup2Id)) { - String query2 = "select current_limits_id from operational_limits_group where uuid = ''"; - ResultSet operationalLimitsGroups2 = connection.createStatement().executeQuery(query2.replace("", branchCreationOpLimitsGroup2Id)); - - // - remove line from operational_limits_group - // - remove related permanent limit from current_limits - // - remove all other related limits from current_temporary_limits - if (operationalLimitsGroups2.next()) { - statements.add(new DeleteStatement(database.getDefaultCatalogName(), database.getDefaultSchemaName(), OPERATIONAL_LIMITS_GROUPS_TABLE) - .setWhere(UUID_COL + " = '" + branchCreationOpLimitsGroup2Id + "'")); - String currentLimitId = operationalLimitsGroups2.getString(CURRENT_LIMITS_ID_COL); - statements.add(new DeleteStatement(database.getDefaultCatalogName(), database.getDefaultSchemaName(), "current_temporary_limits") - .setWhere(ID_COL + " = '" + currentLimitId + "'")); - statements.add(new DeleteStatement(database.getDefaultCatalogName(), database.getDefaultSchemaName(), "current_limits") - .setWhere(ID_COL + " = '" + currentLimitId + "'")); - } - - addOperationalLimitsGroupApplicability(database, operationalLimitsGroups1, statements, "EQUIPMENT"); - - // if they are equal then add only one limitGroup in new Table with application side = equipment (both) - statements.add(new InsertStatement(database.getDefaultCatalogName(), database.getDefaultSchemaName(), branchCreationOpLimitsGroupsTable) - .addColumnValue(BRANCH_ID_COL, branches.getString(ID_COL)) - .addColumnValue(OPERATIONAL_LG_ID_COL, branchCreationOpLimitsGroup1Id) - .addColumnValue(POS_OP_LG_COL, position++)); - } else { - // change Applicability side 1 - addOperationalLimitsGroupApplicability(database, operationalLimitsGroups1, statements, "SIDE1"); - - // Add to merged table - statements.add(new InsertStatement(database.getDefaultCatalogName(), database.getDefaultSchemaName(), branchCreationOpLimitsGroupsTable) - .addColumnValue(BRANCH_ID_COL, branches.getString(ID_COL)) - .addColumnValue(OPERATIONAL_LG_ID_COL, branchCreationOpLimitsGroup1Id) - .addColumnValue(POS_OP_LG_COL, position++)); - - // Change Applicability side 2 - String query3 = "select * from operational_limits_group where uuid = ''"; - ResultSet operationalLimitsGroups2 = connection.createStatement().executeQuery(query3.replace("", branchCreationOpLimitsGroup2Id)); - addOperationalLimitsGroupApplicability(database, operationalLimitsGroups2, statements, "SIDE2"); - - // Add to merged table - statements.add(new InsertStatement(database.getDefaultCatalogName(), database.getDefaultSchemaName(), branchCreationOpLimitsGroupsTable) - .addColumnValue(BRANCH_ID_COL, branches.getString(ID_COL)) - .addColumnValue(OPERATIONAL_LG_ID_COL, branchCreationOpLimitsGroup2Id) - .addColumnValue(POS_OP_LG_COL, position++)); - } - } + migrateBranch(database, connection, statements, branches, branchCreationOpLimitsGroups1Table, + branchCreationOpLimitsGroups2Table, branchCreationOpLimitsGroupsTable); } } } @@ -216,12 +250,12 @@ public String getConfirmationMessage() { @Override public void setUp() throws SetupException { - + // Nothing special } @Override public void setFileOpener(ResourceAccessor resourceAccessor) { - + // Nothing special } @Override