package flyway.asdistats;

import org.flywaydb.core.api.migration.BaseJavaMigration;
import org.flywaydb.core.api.migration.Context;
import org.oskari.helpers.AppSetupHelper;

import java.sql.Connection;
import java.util.List;

/**
 * Add statsgrid bundle to views that don't have it.
 */
public class V1_0__add_statsgrid_to_geoportal extends BaseJavaMigration {
    private static final String BUNDLE_ID = "statsgrid";

    public void migrate(Context context) throws Exception {
        Connection connection = context.getConnection();
        final List<Long> views = AppSetupHelper.getSetupsForUserAndDefaultType(connection);
        for (Long viewId : views) {
            if (AppSetupHelper.appContainsBundle(connection, viewId, BUNDLE_ID)) {
                continue;
            }
            AppSetupHelper.addBundleToApp(connection, viewId, BUNDLE_ID);
        }
    }
}

@@ -20,16 +20,61 @@
 stats;%0A%0A
+import fi.nls.oskari.domain.map.view.Bundle;%0A
 import o
@@ -119,24 +119,24 @@
 aMigration;%0A
-
 import org.f
@@ -218,60 +218,8 @@
 r;%0A%0A
-import java.sql.Connection;%0Aimport java.util.List;%0A%0A
 /**%0A
@@ -352,65 +352,8 @@
 on %7B
-%0A    private static final String BUNDLE_ID = %22statsgrid%22;
 %0A%0A  
@@ -422,391 +422,120 @@
     
-Connection connection = context.getConnection();%0A        final List%3CLong%3E views = AppSetupHelper.getSetupsForUserAndDefaultType(connection);%0A        for (Long viewId : views) %7B%0A            if (AppSetupHelper.appContainsBundle(connection, viewId, BUNDLE_ID)) %7B%0A                continue;%0A            %7D%0A            AppSetupHelper.addBundleToApp(connection, viewId, BUNDLE_ID);%0A        %7D
+AppSetupHelper.addBundleToDefaultAndUserApps(%0A                context.getConnection(), new Bundle(%22statsgrid%22));
 %0A   

Refactor bundle addition