package org.oskari.permissions;

import java.util.List;
import java.util.Optional;
import java.util.Set;

import fi.nls.oskari.domain.User;
import fi.nls.oskari.util.ConversionHelper;
import fi.nls.oskari.util.PropertyUtil;
import org.oskari.permissions.model.PermissionType;
import org.oskari.permissions.model.Resource;
import org.oskari.permissions.model.ResourceType;

import fi.nls.oskari.service.OskariComponent;

public abstract class PermissionService extends OskariComponent {

    private Set<String> DYNAMIC_PERMISSIONS;

    public PermissionService() {
        // add any additional permissions
        DYNAMIC_PERMISSIONS = ConversionHelper.asSet(PropertyUtil.getCommaSeparatedList("permission.types"));
    }
    /**
     * Configure additional permissions with oskari-ext.properties:
     *
     *   permission.types = EDIT_LAYER_CONTENT
     *   permission.EDIT_LAYER_CONTENT.name.fi=Muokkaa tasoa
     *   permission.EDIT_LAYER_CONTENT.name.en=Edit layer
     * @return
     */
    public Set<String> getAdditionalPermissions() {
        return DYNAMIC_PERMISSIONS;
    }

    public abstract Optional<Resource> findResource(int id);
    public abstract Optional<Resource> findResource(ResourceType type, String mapping);
    public abstract Optional<Resource> findResource(String type, String mapping);
    public abstract List<Resource> findResourcesByUser(User user, ResourceType type);


    public abstract Set<String>
        getResourcesWithGrantedPermissions(String resourceType, User user, String permissionsType);

    public Set<String>
        getResourcesWithGrantedPermissions(ResourceType resourceType, User user, PermissionType permissionType) {
        return getResourcesWithGrantedPermissions(resourceType.name(), user, permissionType.name());
    }

    public abstract void insertResource(Resource resource);
    public abstract void saveResource(Resource resource);
    public abstract void deleteResource(Resource resource);

}

@@ -49,16 +49,38 @@
 l.List;%0A
+import java.util.Map;%0A
 import j
@@ -98,16 +98,16 @@
 tional;%0A
-
 import j
@@ -1098,32 +1098,843 @@
 ISSIONS;%0A    %7D%0A%0A
+    /**%0A     * Names can be configured as instructed in getAdditionalPermissions() or with%0A     *   permission.EDIT_LAYER_CONTENT.name = Name for all languages%0A     *%0A     * @param permissionId%0A     * @param lang%0A     * @return%0A     */%0A    public String getPermissionName(String permissionId, String lang) %7B%0A        final String property = %22permission.%22 + permissionId + %22.name%22;%0A        final Object obj = PropertyUtil.getLocalizableProperty(property, permissionId);%0A        if(obj instanceof String) %7B%0A            return (String) obj;%0A        %7D%0A        else if(obj instanceof Map) %7B%0A            String tmp = (String)((Map) obj).get(lang);%0A            if(tmp != null) %7B%0A                return tmp;%0A            %7D%0A            // TODO: Should we try other languages?%0A        %7D%0A        return permissionId;%0A    %7D%0A%0A
     public abstr
@@ -2140,32 +2140,32 @@
 tring mapping);%0A
-
     public abstr
@@ -2234,16 +2234,91 @@
  type);%0A
+    public abstract List%3CResource%3E findResourcesByType(ResourceType type);%0A
 %0A%0A    pu

Add permission localization