@@ -0,0 +1,3194 @@
+package flyway.oskari;%0A%0Aimport fi.nls.oskari.util.JSONHelper;%0Aimport org.flywaydb.core.api.migration.BaseJavaMigration;%0Aimport org.flywaydb.core.api.migration.Context;%0Aimport org.json.JSONArray;%0Aimport org.json.JSONException;%0Aimport org.json.JSONObject;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.sql.Connection;%0Aimport java.sql.PreparedStatement;%0Aimport java.sql.ResultSet;%0Aimport java.sql.SQLException;%0A%0Apublic class V2_0_9__migrate_legend_image extends BaseJavaMigration %7B%0A%0A    @Override%0A    public void migrate(Context context) throws Exception %7B%0A        Connection connection = context.getConnection();%0A        List%3CLayerConfig%3E layers = getLayers(connection);%0A        for (LayerConfig layer : layers) %7B%0A            migrateLegendImages(layer);%0A            updateOptions(connection, layer.layerId, layer.options);%0A        %7D%0A%0A    %7D%0A    private void migrateLegendImages (LayerConfig layer) throws JSONException %7B%0A        String legendImage = layer.legendImage;%0A        JSONArray styleList = JSONHelper.getEmptyIfNull(layer.capabilities.optJSONArray(%22styles%22));%0A        JSONObject legends = new JSONObject();%0A        if (styleList == null %7C%7C styleList.length() == 0) %7B%0A            JSONHelper.putValue(legends, %22legendImage%22, legendImage);%0A        %7D else %7B%0A            for (int i = 0; i %3C styleList.length(); i++ ) %7B%0A                JSONObject style = styleList.getJSONObject(i);%0A                // copy legend image to every style%0A                JSONHelper.putValue(legends, style.getString(%22name%22), legendImage);%0A            %7D%0A        %7D%0A        JSONHelper.putValue(layer.options, %22legends%22, legends);%0A%0A    %7D%0A    private void updateOptions (Connection conn, int layerId, JSONObject options) throws SQLException %7B%0A        final String sql = %22UPDATE oskari_maplayer SET options=? where id=?%22;%0A        try(PreparedStatement statement = conn.prepareStatement(sql)) %7B%0A            statement.setString(1, options.toString());%0A            statement.setInt(2, layerId);%0A            statement.execute();%0A        %7D%0A    %7D%0A    private List%3CLayerConfig%3E getLayers(Connection conn) throws SQLException %7B%0A        List%3CLayerConfig%3E layers = new ArrayList%3C%3E();%0A        // only process layers which haves legend_image (mostly wms and wmts layers)%0A        final String sql = %22SELECT id, legend_image, options, capabilities FROM oskari_maplayer where legend_image %3C%3E '' and legend_image is not null%22;%0A        try(PreparedStatement statement = conn.prepareStatement(sql)) %7B%0A            try (ResultSet rs = statement.executeQuery()) %7B%0A                while(rs.next()) %7B%0A                    LayerConfig layer = new LayerConfig();%0A                    layer.layerId = rs.getInt(%22id%22);%0A                    layer.legendImage = rs.getString(%22legend_image%22);%0A                    layer.options = JSONHelper.createJSONObject(rs.getString(%22options%22));%0A                    layer.capabilities = JSONHelper.createJSONObject(rs.getString(%22capabilities%22));%0A                    layers.add(layer);%0A                %7D%0A            %7D%0A        %7D%0A        return layers;%0A    %7D%0A    class LayerConfig %7B%0A        int layerId;%0A        String legendImage;%0A        JSONObject options;%0A        JSONObject capabilities;%0A    %7D%0A%7D%0A

add flyway migration for legend_image