@@ -0,0 +1,2628 @@
+package flyway.pti;%0A%0Aimport fi.nls.oskari.util.JSONHelper;%0Aimport org.flywaydb.core.api.migration.BaseJavaMigration;%0Aimport org.flywaydb.core.api.migration.Context;%0Aimport org.json.JSONObject;%0A%0Aimport java.sql.Connection;%0Aimport java.sql.PreparedStatement;%0Aimport java.sql.ResultSet;%0Aimport java.sql.SQLException;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A%0A/**%0A * Removed layer.options.legends object (removing any override admin has given) for layers%0A * returned by getLayersToModify().%0A * This one removes overrides from layers with dataprovider 57 (Tilastokeskus)-%0A */%0Apublic class V3_0_8__remove_legend_overrides extends BaseJavaMigration %7B%0A%0A    class Result %7B%0A        int id;%0A        String options;%0A    %7D%0A%0A    public void migrate(Context context) throws Exception %7B%0A        Connection connection = context.getConnection();%0A        List%3CResult%3E layers = getLayersToModify(connection);%0A        layers.forEach(r -%3E removeLegendOverrides(r));%0A        saveChanges(connection, layers);%0A    %7D%0A%0A    private void removeLegendOverrides(Result result) %7B%0A        if (result.options == null) %7B%0A            return;%0A        %7D%0A        JSONObject options = JSONHelper.createJSONObject(result.options);%0A        if (options == null) %7B%0A            return;%0A        %7D%0A        options.remove(%22legends%22);%0A        result.options = options.toString();%0A    %7D%0A%0A    public int getDataproviderToModify() %7B%0A        return 57;%0A    %7D%0A%0A    public List%3CResult%3E getLayersToModify(Connection conn) throws Exception %7B%0A        List%3CResult%3E layers = new ArrayList%3C%3E();%0A        // remove legends override from layers by %22Tilastokeskus%22%0A        final String sql = %22SELECT id, options FROM oskari_maplayer where dataprovider_id = %22 + getDataproviderToModify();%0A        try(PreparedStatement statement = conn.prepareStatement(sql)) %7B%0A            try (ResultSet rs = statement.executeQuery()) %7B%0A                while(rs.next()) %7B%0A                    Result result = new Result();%0A                    result.id = rs.getInt(%22id%22);%0A                    result.options = rs.getString(%22options%22);%0A                    layers.add(result);%0A                %7D%0A            %7D%0A        %7D%0A        return layers;%0A    %7D%0A%0A    private void saveChanges(Connection conn, List%3CResult%3E layers) throws SQLException %7B%0A        String sql = %22UPDATE oskari_maplayer SET options = ? WHERE id = ?%22;%0A        try (PreparedStatement ps = conn.prepareStatement(sql)) %7B%0A            for (Result layer : layers) %7B%0A                ps.setString(1, layer.options);%0A                ps.setInt(2, layer.id);%0A                ps.addBatch();%0A            %7D%0A            ps.executeBatch();%0A        %7D%0A    %7D%0A%7D%0A

Remove legend overrides from layers from Tilastokeskus