package org.oskari.permissions.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import fi.nls.oskari.domain.Role;
import fi.nls.oskari.domain.User;

/**
 * A generic mapping for resource. Reflects DB table oskari_resource.
 */
public class Resource {

    private int id = -1;
    private String  type;
    private String mapping;
    private List<Permission> permissions;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type =  type;
    }

    public void setType(ResourceType type) {
        setType(type.name());
    }

    public String getMapping() {
        return mapping;
    }

    public void setMapping(String mapping) {
        this.mapping = mapping;
    }

    public void setMapping(String namespace, String name) {
        setMapping(namespace + "+" + name);
    }

    public List<Permission> getPermissions() {
        if (permissions == null) {
            return Collections.emptyList();
        }
        return permissions;
    }

    public void setPermissions(List<Permission> permissions) {
        this.permissions = permissions;
    }

    public void addPermission(Permission permission) {
        if (permission == null) {
            return;
        }
        if (permissions == null) {
            permissions = new ArrayList<>();
        }
        permissions.add(permission);
    }

    public Set<String> getPermissionTypes() {
        return getPermissions().stream()
                .map(p -> p.getType())
                .collect(Collectors.toSet());
    }

    public boolean hasPermission(User user, PermissionType permissionType) {
        return hasPermission(user, permissionType.name());
    }

    public boolean hasPermission(User user, String permissionType) {
        boolean userHasRoleWithId = getPermissions().stream()
                .filter(p -> p.isOfType(permissionType))
                .filter(p -> p.getExternalType() == PermissionExternalType.ROLE)
                .filter(p -> user.hasRoleWithId(p.getExternalId()))
                .findAny()
                .isPresent();

        if (userHasRoleWithId) {
            return true;
        }

        return getPermissions().stream()
                .filter(p -> p.isOfType(permissionType))
                .filter(p -> p.getExternalType() == PermissionExternalType.USER)
                .filter(p -> p.getExternalId() == user.getId())
                .findAny()
                .isPresent();
    }

    public boolean hasPermission(Role role, PermissionType permissionType) {
        return getPermissions().stream()
                .filter(p -> p.isOfType(permissionType))
                .filter(p -> p.getExternalType() == PermissionExternalType.ROLE)
                .filter(p -> p.getExternalId() == role.getId())
                .findAny()
                .isPresent();
    }

    public void removePermissionsOfType(String permissionType) {
        removePermissionsOfType(PermissionType.valueOf(permissionType));
    }

    public void removePermissionsOfType(PermissionType permissionType) {
        getPermissions().removeIf(p -> p.isOfType(permissionType));
    }

    public boolean isOfType(ResourceType type) {
        return isOfType(type.name());
    }

    public boolean isOfType(String permissionType) {
        return type.equals(permissionType);
    }
}

@@ -2746,32 +2746,166 @@
 rmissionType) %7B%0A
+        return hasPermission(role, permissionType.name());%0A    %7D%0A    public boolean hasPermission(Role role, String permissionType) %7B%0A
         return g

Add compatibility method