package flyway.asdistats;

import fi.mml.portti.domain.permissions.Permissions;
import fi.mml.portti.service.db.permissions.PermissionsService;
import fi.mml.portti.service.db.permissions.PermissionsServiceIbatisImpl;
import fi.nls.oskari.domain.Role;
import fi.nls.oskari.domain.User;
import fi.nls.oskari.domain.map.OskariLayer;
import fi.nls.oskari.service.ServiceException;
import fi.nls.oskari.service.ServiceRuntimeException;
import fi.nls.oskari.service.UserService;
import org.flywaydb.core.api.migration.jdbc.JdbcMigration;
import org.oskari.permissions.model.OskariLayerResource;
import org.oskari.permissions.model.Permission;
import org.oskari.permissions.model.Resource;

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

/**
 * Add permissions for regionset: countries
 */
public class V1_2__add_permission_for_regionset implements JdbcMigration {

    public void migrate(Connection connection)
            throws SQLException {
        PermissionsService service = new PermissionsServiceIbatisImpl();
        for(Resource resToUpdate : getResources()) {
            Resource dbRes = service.findResource(resToUpdate);
            if(dbRes != null) {
                resToUpdate = dbRes;
            }
            for(Role role : getRoles()) {
                if(resToUpdate.hasPermission(role, Permissions.PERMISSION_TYPE_VIEW_LAYER)) {
                    // already had the permission
                    continue;
                }
                final Permission permission = new Permission();
                permission.setExternalType(Permissions.EXTERNAL_TYPE_ROLE);
                permission.setType(Permissions.PERMISSION_TYPE_VIEW_LAYER);
                permission.setExternalId(Long.toString(role.getId()));
                resToUpdate.addPermission(permission);
            }
            service.saveResourcePermissions(resToUpdate);
        }
    }

    // statslayers described as layer resources for permissions handling
    protected List<Resource> getResources() {
        List<Resource> list = new ArrayList<>();
        list.add(new OskariLayerResource(OskariLayer.TYPE_STATS, "resources://regionsets/ne_110m_countries-3575.json", "ne_110m_countries-3575"));
        return list;
    }

    private List<Role> getRoles() {
        List<Role> list = new ArrayList<>();
        try {
            // "logged in" user
            list.add(Role.getDefaultUserRole());
            // guest user
            User guest = UserService.getInstance().getGuestUser();
            list.addAll(guest.getRoles());
        } catch (ServiceException ex) {
            throw new ServiceRuntimeException("Unable to get roles");
        }
        return list;
    }
}

@@ -522,24 +522,133 @@
 cMigration;%0A
+import org.oskari.permissions.PermissionService;%0Aimport org.oskari.permissions.PermissionServiceMybatisImpl;%0A
 import org.o
@@ -785,16 +785,66 @@
 esource;
+%0Aimport org.oskari.permissions.model.ResourceType;
 %0A%0Aimport
@@ -945,16 +945,43 @@
 il.List;
+%0Aimport java.util.Optional;
 %0A%0A/**%0A *
@@ -1197,25 +1197,24 @@
   Permission
-s
 Service serv
@@ -1229,33 +1229,33 @@
 w Permission
-s
 Service
-I
+My
 batisImpl();
@@ -1320,24 +1320,33 @@
         
+Optional%3C
 Resource
  dbRes =
@@ -1337,16 +1337,17 @@
 Resource
+%3E
  dbRes =
@@ -1368,27 +1368,63 @@
 esource(
-resToUpdate
+ResourceType.maplayer, resToUpdate.getMapping()
 );%0A     
@@ -1442,16 +1442,20 @@
 bRes
- != null
+.isPresent()
 ) %7B%0A
@@ -1489,16 +1489,22 @@
  = dbRes
+.get()
 ;%0A      
@@ -2133,27 +2133,16 @@
 Resource
-Permissions
 (resToUp

Replace Ibatis permissions with MyBatis