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.PermissionService;
import org.oskari.permissions.PermissionServiceMybatisImpl;
import org.oskari.permissions.model.OskariLayerResource;
import org.oskari.permissions.model.Permission;
import org.oskari.permissions.model.Resource;
import org.oskari.permissions.model.ResourceType;

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

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

    public void migrate(Connection connection)
            throws SQLException {
        PermissionService service = new PermissionServiceMybatisImpl();
        for(Resource resToUpdate : getResources()) {
            Optional<Resource> dbRes = service.findResource(ResourceType.maplayer, resToUpdate.getMapping());
            if(dbRes.isPresent()) {
                resToUpdate = dbRes.get();
            }
            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.saveResource(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;
    }
}

@@ -24,199 +24,8 @@
 s;%0A%0A
-import fi.mml.portti.domain.permissions.Permissions;%0Aimport fi.mml.portti.service.db.permissions.PermissionsService;%0Aimport fi.mml.portti.service.db.permissions.PermissionsServiceIbatisImpl;%0A
 impo
@@ -54,16 +54,16 @@
 n.Role;%0A
+
 import f
@@ -484,171 +484,9 @@
 del.
-OskariLayerResource;%0Aimport org.oskari.permissions.model.Permission;%0Aimport org.oskari.permissions.model.Resource;%0Aimport org.oskari.permissions.model.ResourceType
+*
 ;%0A%0Ai
@@ -1261,34 +1261,21 @@
 rmission
-s.PERMISSION_TYPE_
+Type.
 VIEW_LAY
@@ -1473,24 +1473,16 @@
 sion.set
-External
 Type(Per
@@ -1492,94 +1492,13 @@
 sion
-s.EXTERNAL_TYPE_ROLE);%0A                permission.setType(Permissions.PERMISSION_TYPE_
+Type.
 VIEW
@@ -1540,33 +1540,21 @@
 .set
-ExternalId(Long.toString(
+RoleId((int) 
 role
@@ -1562,17 +1562,16 @@
 getId())
-)
 ;%0A      

Replace Ibatis permissions with MyBatis (continued)