/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.servlet.facilities;

import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;

import org.bodington.server.realm.AclEntry;
import org.bodington.server.realm.Permission;
import org.bodington.server.realm.User;


/**
 * Class to store the summary permission data that a principal has for
 * a resource.
 * @author buckett
 */
abstract public class AclResourcePermission implements Comparable {
   
    private HashSet permissions = new HashSet();

    /**
     * Get the user associated with this permission set.
     * @return The user.
     */
    abstract public User getUser();
    
    /**
     * Get the groups that are associated with this permission set.
     * @return An iterator returning groups.
     */
    abstract public Iterator getGroups();
    
    public void addPermission(Permission permission) {
        permissions.add(permission);
    }
    
    public boolean hasPermission(Permission permission) {
        return permissions.contains(permission);
    }
    
    public void addPermissions(AclEntry entry)
    {
        Enumeration permissions = entry.permissions();
        while(permissions.hasMoreElements())
        {
            addPermission((Permission)permissions.nextElement());
        }
    }
    
    public void addPermissions(AclResourcePermission aclPermission)
    {
        Enumeration permissions = Permission.permissions();
        while (permissions.hasMoreElements())
        {
            Permission permission = (Permission)permissions.nextElement();
            if (aclPermission.hasPermission(permission))
            {
                addPermission(permission);
            }
        }
    }
    
    public void addAdminPermissions(AclEntry entry)
    {
        if (entry.checkPermission(Permission.ADMINISTER))
            addPermission(Permission.ADMINISTER);
        if (entry.checkPermission(Permission.SYSADMIN))
            addPermission(Permission.SYSADMIN);
    }
    
    
    /**
     * Compare to another AclResourcePermission. This is used as the
     * fallback for comparing objects of different types when sorting in child classes.
     * At the moment it puts AclUserResourcePermission objects before all others.
     * @param other The class we are comparing against.
     * @return 1 If other is a AclUserResourcePermission.
     */
    public int compareTo(AclResourcePermission other)
    {
        if(other instanceof AclUserResourcePermission)
        {
            return -1;
        }
        else
        {
            return 1;
        }
    }

    
    
}
