|
| 1 | +package org.gridsuite.modification.server.migration; |
| 2 | + |
| 3 | +import liquibase.change.custom.CustomSqlChange; |
| 4 | +import liquibase.database.Database; |
| 5 | +import liquibase.database.jvm.JdbcConnection; |
| 6 | +import liquibase.exception.CustomChangeException; |
| 7 | +import liquibase.exception.DatabaseException; |
| 8 | +import liquibase.exception.SetupException; |
| 9 | +import liquibase.exception.ValidationErrors; |
| 10 | +import liquibase.resource.ResourceAccessor; |
| 11 | +import liquibase.statement.SqlStatement; |
| 12 | +import liquibase.statement.core.DeleteStatement; |
| 13 | +import liquibase.statement.core.InsertStatement; |
| 14 | +import liquibase.statement.core.UpdateStatement; |
| 15 | +import org.slf4j.Logger; |
| 16 | +import org.slf4j.LoggerFactory; |
| 17 | + |
| 18 | +import java.sql.ResultSet; |
| 19 | +import java.sql.SQLException; |
| 20 | +import java.sql.Statement; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Objects; |
| 24 | + |
| 25 | +public class MergeLimitSetsGroupsTables implements CustomSqlChange { |
| 26 | + |
| 27 | + private static final Logger LOGGER = LoggerFactory.getLogger(MergeLimitSetsGroupsTables.class); |
| 28 | + private static final String UUID_COL = "uuid"; |
| 29 | + private static final String ID_COL = "id"; |
| 30 | + private static final String OPERATIONAL_LG_ID_COL = "operational_limits_groups_id"; |
| 31 | + private static final String CURRENT_LIMITS_ID_COL = "current_limits_id"; |
| 32 | + private static final String POS_OP_LG_COL = "pos_operational_limits_groups"; |
| 33 | + private static final String OPERATIONAL_LIMITS_GROUPS_TABLE = "operational_limits_group"; |
| 34 | + private static final String BRANCH_ID_COL = "branch_id"; |
| 35 | + |
| 36 | + private String createQueryLineOpLimitsGroups(String id, String tableName) { |
| 37 | + return "select * from " + tableName + " where branch_id = '" + id + "'"; |
| 38 | + } |
| 39 | + |
| 40 | + private String createQueryLineOpLimitsGroupsWithPos(String tableName, String id, String pos) { |
| 41 | + return "select * from " + tableName + " where branch_id = '" + id + "' and pos_operational_limits_groups = " + pos; |
| 42 | + } |
| 43 | + |
| 44 | + private int getLength(ResultSet resultSet) throws SQLException { |
| 45 | + resultSet.last(); |
| 46 | + int length = resultSet.getRow(); |
| 47 | + resultSet.beforeFirst(); |
| 48 | + return length; |
| 49 | + } |
| 50 | + |
| 51 | + private boolean compareOperationalLimitsInfos(JdbcConnection connection, String operationalLimitsIds1, String operationalLimitsIds2) throws DatabaseException, SQLException { |
| 52 | + final String query = "select current_limits_id from operational_limits_group where uuid ='<id>'"; |
| 53 | + ResultSet currentLimits1 = connection.createStatement().executeQuery(query.replace("<id>", operationalLimitsIds1)); |
| 54 | + ResultSet currentLimits2 = connection.createStatement().executeQuery(query.replace("<id>", operationalLimitsIds2)); |
| 55 | + |
| 56 | + if (currentLimits1.next() && currentLimits2.next()) { |
| 57 | + String currentLimitsQuery = "select permanent_limit from current_limits where id = '<id>'"; |
| 58 | + String currentLimitsId1 = currentLimits1.getString(CURRENT_LIMITS_ID_COL); |
| 59 | + String currentLimitsId2 = currentLimits2.getString(CURRENT_LIMITS_ID_COL); |
| 60 | + ResultSet permanentLimit = connection.createStatement().executeQuery(currentLimitsQuery.replace("<id>", currentLimitsId1)); |
| 61 | + ResultSet permanentLimit2 = connection.createStatement().executeQuery(currentLimitsQuery.replace("<id>", currentLimitsId2)); |
| 62 | + |
| 63 | + boolean haspermanentLimit = permanentLimit.next(); |
| 64 | + boolean haspermanentLimit2 = permanentLimit2.next(); |
| 65 | + if (haspermanentLimit != haspermanentLimit2) { |
| 66 | + return false; |
| 67 | + } |
| 68 | + if (haspermanentLimit && !Objects.equals(permanentLimit.getString("permanent_limit"), |
| 69 | + permanentLimit2.getString("permanent_limit"))) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | + final String queryTemporary = "select * from current_temporary_limits where id ='<id>'"; |
| 74 | + Statement statement1 = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); |
| 75 | + Statement statement2 = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); |
| 76 | + ResultSet temporaryLimit = statement1.executeQuery(queryTemporary.replace("<id>", currentLimitsId1)); |
| 77 | + ResultSet temporaryLimit2 = statement2.executeQuery(queryTemporary.replace("<id>", currentLimitsId2)); |
| 78 | + if (getLength(temporaryLimit) != getLength(temporaryLimit2)) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + while (temporaryLimit.next()) { |
| 83 | + String name = temporaryLimit.getString("name"); |
| 84 | + boolean found = false; |
| 85 | + temporaryLimit2.beforeFirst(); |
| 86 | + while (temporaryLimit2.next()) { |
| 87 | + if (Objects.equals(temporaryLimit2.getString("name"), name)) { |
| 88 | + if (!Objects.equals(temporaryLimit.getString("value_"), temporaryLimit2.getString("value_")) || |
| 89 | + !Objects.equals(temporaryLimit.getString("acceptable_duration"), temporaryLimit2.getString("acceptable_duration"))) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + found = true; |
| 93 | + break; |
| 94 | + } |
| 95 | + } |
| 96 | + // temporary limit not found |
| 97 | + if (!found) { |
| 98 | + return false; |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return true; |
| 104 | + } |
| 105 | + |
| 106 | + private void addOperationalLimitsGroupApplicability(Database database, ResultSet operationalLimitsGroups, |
| 107 | + List<SqlStatement> statements, String applicability) throws SQLException { |
| 108 | + |
| 109 | + if (operationalLimitsGroups.next()) { |
| 110 | + statements.add(new UpdateStatement(database.getDefaultCatalogName(), database.getDefaultSchemaName(), OPERATIONAL_LIMITS_GROUPS_TABLE) |
| 111 | + .setWhereClause(UUID_COL + " = '" + operationalLimitsGroups.getString(UUID_COL) + "'") |
| 112 | + .addNewColumnValue("applicability", applicability)); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public SqlStatement[] generateStatements(Database database) throws CustomChangeException { |
| 118 | + JdbcConnection connection = (JdbcConnection) database.getConnection(); |
| 119 | + |
| 120 | + List<SqlStatement> statements = new ArrayList<>(); |
| 121 | + |
| 122 | + try { |
| 123 | + for (int i = 0; i < 2; i++) { |
| 124 | + // Define tables names ( i= 0 => lines, i=1 => twoWindingsTransformers) |
| 125 | + final String branchCreationTable = i == 0 ? "line_creation" : "two_windings_transformer_creation"; |
| 126 | + final String branchCreationOpLimitsGroups1Table = i == 0 ? "line_creation_operational_limits_groups1" |
| 127 | + : "two_windings_transformer_creation_operational_limits_groups1"; |
| 128 | + final String branchCreationOpLimitsGroups2Table = i == 0 ? "line_creation_operational_limits_groups2" |
| 129 | + : "two_windings_transformer_creation_operational_limits_groups2"; |
| 130 | + final String branchCreationOpLimitsGroupsTable = i == 0 ? "line_creation_operational_limits_groups" |
| 131 | + : "two_windings_transformer_creation_operational_limits_groups"; |
| 132 | + |
| 133 | + String branchesToProcess = "Select id, selected_operational_limits_group_id1, selected_operational_limits_group_id2 from <Table>"; |
| 134 | + try (ResultSet branches = connection.createStatement().executeQuery(branchesToProcess.replace("<Table>", branchCreationTable))) { |
| 135 | + while (branches.next()) { |
| 136 | + int position = 0; |
| 137 | + //get operational limits groups1 |
| 138 | + ResultSet branchCreationOpLimitsGroups1 = connection.createStatement() |
| 139 | + .executeQuery(createQueryLineOpLimitsGroups(branches.getString(ID_COL), branchCreationOpLimitsGroups1Table)); |
| 140 | + |
| 141 | + while (branchCreationOpLimitsGroups1.next()) { |
| 142 | + String branchCreationOpLimitsGroup1Id = branchCreationOpLimitsGroups1.getString(OPERATIONAL_LG_ID_COL); |
| 143 | + String branchCreationOpLimitsGroup1Pos = branchCreationOpLimitsGroups1.getString(POS_OP_LG_COL); |
| 144 | + |
| 145 | + ResultSet branchCreationOpLimitsGroups2 = connection.createStatement() |
| 146 | + .executeQuery(createQueryLineOpLimitsGroupsWithPos(branchCreationOpLimitsGroups2Table, branches.getString(ID_COL), |
| 147 | + branchCreationOpLimitsGroup1Pos)); |
| 148 | + |
| 149 | + branchCreationOpLimitsGroups2.next(); |
| 150 | + String branchCreationOpLimitsGroup2Id = branchCreationOpLimitsGroups2.getString(OPERATIONAL_LG_ID_COL); |
| 151 | + |
| 152 | + // Compare Both limitsGroups 1 and 2 limits |
| 153 | + String query = "select * from operational_limits_group where uuid = '<id>'"; |
| 154 | + ResultSet operationalLimitsGroups1 = connection.createStatement().executeQuery(query.replace("<id>", branchCreationOpLimitsGroup1Id)); |
| 155 | + |
| 156 | + if (compareOperationalLimitsInfos(connection, branchCreationOpLimitsGroup1Id, branchCreationOpLimitsGroup2Id)) { |
| 157 | + String query2 = "select current_limits_id from operational_limits_group where uuid = '<id>'"; |
| 158 | + ResultSet operationalLimitsGroups2 = connection.createStatement().executeQuery(query2.replace("<id>", branchCreationOpLimitsGroup2Id)); |
| 159 | + |
| 160 | + // - remove line from operational_limits_group |
| 161 | + // - remove related permanent limit from current_limits |
| 162 | + // - remove all other related limits from current_temporary_limits |
| 163 | + if (operationalLimitsGroups2.next()) { |
| 164 | + statements.add(new DeleteStatement(database.getDefaultCatalogName(), database.getDefaultSchemaName(), OPERATIONAL_LIMITS_GROUPS_TABLE) |
| 165 | + .setWhere(UUID_COL + " = '" + branchCreationOpLimitsGroup2Id + "'")); |
| 166 | + String currentLimitId = operationalLimitsGroups2.getString(CURRENT_LIMITS_ID_COL); |
| 167 | + statements.add(new DeleteStatement(database.getDefaultCatalogName(), database.getDefaultSchemaName(), "current_temporary_limits") |
| 168 | + .setWhere(ID_COL + " = '" + currentLimitId + "'")); |
| 169 | + statements.add(new DeleteStatement(database.getDefaultCatalogName(), database.getDefaultSchemaName(), "current_limits") |
| 170 | + .setWhere(ID_COL + " = '" + currentLimitId + "'")); |
| 171 | + } |
| 172 | + |
| 173 | + addOperationalLimitsGroupApplicability(database, operationalLimitsGroups1, statements, "EQUIPMENT"); |
| 174 | + |
| 175 | + // if they are equal then add only one limitGroup in new Table with application side = equipment (both) |
| 176 | + statements.add(new InsertStatement(database.getDefaultCatalogName(), database.getDefaultSchemaName(), branchCreationOpLimitsGroupsTable) |
| 177 | + .addColumnValue(BRANCH_ID_COL, branches.getString(ID_COL)) |
| 178 | + .addColumnValue(OPERATIONAL_LG_ID_COL, branchCreationOpLimitsGroup1Id) |
| 179 | + .addColumnValue(POS_OP_LG_COL, position++)); |
| 180 | + } else { |
| 181 | + // change Applicability side 1 |
| 182 | + addOperationalLimitsGroupApplicability(database, operationalLimitsGroups1, statements, "SIDE1"); |
| 183 | + |
| 184 | + // Add to merged table |
| 185 | + statements.add(new InsertStatement(database.getDefaultCatalogName(), database.getDefaultSchemaName(), branchCreationOpLimitsGroupsTable) |
| 186 | + .addColumnValue(BRANCH_ID_COL, branches.getString(ID_COL)) |
| 187 | + .addColumnValue(OPERATIONAL_LG_ID_COL, branchCreationOpLimitsGroup1Id) |
| 188 | + .addColumnValue(POS_OP_LG_COL, position++)); |
| 189 | + |
| 190 | + // Change Applicability side 2 |
| 191 | + String query3 = "select * from operational_limits_group where uuid = '<id>'"; |
| 192 | + ResultSet operationalLimitsGroups2 = connection.createStatement().executeQuery(query3.replace("<id>", branchCreationOpLimitsGroup2Id)); |
| 193 | + addOperationalLimitsGroupApplicability(database, operationalLimitsGroups2, statements, "SIDE2"); |
| 194 | + |
| 195 | + // Add to merged table |
| 196 | + statements.add(new InsertStatement(database.getDefaultCatalogName(), database.getDefaultSchemaName(), branchCreationOpLimitsGroupsTable) |
| 197 | + .addColumnValue(BRANCH_ID_COL, branches.getString(ID_COL)) |
| 198 | + .addColumnValue(OPERATIONAL_LG_ID_COL, branchCreationOpLimitsGroup2Id) |
| 199 | + .addColumnValue(POS_OP_LG_COL, position++)); |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + } catch (Exception throwables) { |
| 206 | + LOGGER.error(throwables.getMessage()); |
| 207 | + return new SqlStatement[0]; // If any exception occurs don't do any migration |
| 208 | + } |
| 209 | + return statements.toArray(new SqlStatement[0]); |
| 210 | + } |
| 211 | + |
| 212 | + @Override |
| 213 | + public String getConfirmationMessage() { |
| 214 | + return "tables line_creation_operational_limits_group1 and 2, merged successfully into line_creation_operational_limits_group"; |
| 215 | + } |
| 216 | + |
| 217 | + @Override |
| 218 | + public void setUp() throws SetupException { |
| 219 | + |
| 220 | + } |
| 221 | + |
| 222 | + @Override |
| 223 | + public void setFileOpener(ResourceAccessor resourceAccessor) { |
| 224 | + |
| 225 | + } |
| 226 | + |
| 227 | + @Override |
| 228 | + public ValidationErrors validate(Database database) { |
| 229 | + return null; |
| 230 | + } |
| 231 | +} |
0 commit comments