package flyway.sample;

import java.sql.Connection;
import java.sql.PreparedStatement;

import org.flywaydb.core.api.migration.jdbc.JdbcMigration;

import fi.nls.oskari.util.FlywayHelper;
import fi.nls.oskari.util.PropertyUtil;
import fi.nls.oskari.map.view.ViewService;
import fi.nls.oskari.map.view.ViewServiceIbatisImpl;

public class V1_0_12__add_download_basket_to_views implements JdbcMigration {

	private static final ViewService VIEW_SERVICE = new ViewServiceIbatisImpl();
	private static final String DOWNLOAD_BASKET = "download-basket";

	public void migrate(Connection connection) throws Exception {
        if(PropertyUtil.getOptional("flyway.sample.1_0_12.skip", true)) {
            return;
        }
		long viewId = VIEW_SERVICE.getDefaultViewId();
		makeInsert(viewId,connection);
	}

	private void makeInsert(long viewId, Connection connection) throws Exception {

		final PreparedStatement statement = connection.prepareStatement("INSERT INTO portti_view_bundle_seq"
				+ "(view_id, bundle_id, seqno, config, state, startup, bundleinstance) " + "VALUES (" + "?, "
				+ "(SELECT id FROM portti_bundle WHERE name=?), "
				+ "(SELECT max(seqno)+1 FROM portti_view_bundle_seq WHERE view_id=?), " + "?, ?, "
				+ "(SELECT startup FROM portti_bundle WHERE name=?), " + "?)");

		statement.setLong(1, viewId);
		statement.setString(2, DOWNLOAD_BASKET);
		statement.setLong(3, viewId);
		statement.setString(4, "{}");
		statement.setString(5, "{}");
		statement.setString(6, DOWNLOAD_BASKET);
		statement.setString(7, DOWNLOAD_BASKET);

		try {
			statement.execute();
		} finally {
			statement.close();
		}
	}
}

@@ -28,301 +28,366 @@
 ort 
-java.sql.Connection;%0Aimport java.sql.PreparedStatement;%0A%0Aimport org.flywaydb.core.api.migration.jdbc.JdbcMigration;%0A%0Aimport fi.nls.oskari.util.FlywayHelper;%0Aimport fi.nls.oskari.util.PropertyUtil;%0Aimport fi.nls.oskari.map.view.ViewService;%0Aimport fi.nls.oskari.map.view.ViewS
+fi.nls.oskari.util.FlywayHelper;%0Aimport fi.nls.oskari.util.PropertyUtil;%0Aimport org.flywaydb.core.api.migration.jdbc.JdbcMigration;%0A%0Aimport java.sql.Connection;%0Aimport java.util.List;%0A%0A/**%0A * Migration is skipped by default!%0A *%0A * Add %22flyway.sample.1_0_12.skip=false%22 in oskari-ext.properties to add%0A * download-basket for all default and us
 er
+ 
 vi
-ceIbatisImpl;%0A
+ews.%0A *%0A */
 %0Apub
@@ -466,87 +466,12 @@
  %7B%0A%0A
-%09private static final ViewService VIEW_SERVICE = new ViewServiceIbatisImpl();%0A%09
+    
 priv
@@ -494,31 +494,25 @@
  String 
-DOWNLOAD_BASKET
+BUNDLE_ID
  = %22down
@@ -526,17 +526,20 @@
 sket%22;%0A%0A
-%09
+    
 public v
@@ -602,16 +602,17 @@
       if
+ 
 (Propert
@@ -701,917 +701,344 @@
   %7D%0A
-%09%09l
+%0A        final List%3CL
 ong
+%3E
  view
-Id = VIEW_SERVICE.getDefaultViewId();%0A%09%09makeInsert(viewId,connection);%0A%09%7D%0A%0A%09private void makeInsert(long viewId, Connection connection) throws Exception %7B%0A%0A%09%09final PreparedStatement statement = connection.prepareStatement(%22INSERT INTO portti_view_bundle_seq%22%0A%09%09%09%09+ %22(view_id, bundle_id, seqno, config, state, startup, bundleinstance) %22 + %22VALUES (%22 + %22?, %22%0A%09%09%09%09+ %22(SELECT id FROM portti_bundle WHERE name=?), %22%0A%09%09%09%09+ %22(SELECT max(seqno)+1 FROM portti_view_bundle_seq WHERE view_id=?), %22 + %22?, ?, %22%0A%09%09%09%09+ %22(SELECT startup FROM portti_bundle WHERE name=?), %22 + %22?)%22);%0A%0A%09%09statement.setLong(1, viewId);%0A%09%09statement.setString(2, DOWNLOAD_BASKET);%0A%09%09statement.setLong(3, viewId);%0A%09%09statement.setString(4, %22%7B%7D%22);%0A%09%09statement.setString(5, %22%7B%7D%22);%0A%09%09statement.setString(6, DOWNLOAD_BASKET);%0A%09%09statement.setString(7, DOWNLOAD_BASKET);%0A%0A%09%09try %7B%0A%09%09%09statement.execute();%0A%09%09%7D finally %7B%0A%09%09%09statement.close();%0A%09%09%7D%0A%09
+s = FlywayHelper.getUserAndDefaultViewIds(connection);%0A        for (Long viewId : views) %7B%0A            if (FlywayHelper.viewContainsBundle(connection, BUNDLE_ID, viewId)) %7B%0A                continue;%0A            %7D%0A            FlywayHelper.addBundleWithDefaults(connection, viewId, BUNDLE_ID);%0A        %7D%0A    
 %7D%0A%7D%0A

Add download-basket to personal views as well as default view