/* ======================================================================
   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.Collections;
import java.util.Iterator;

import org.bodington.server.realm.Group;
import org.bodington.server.realm.User;

/**
 * Class to contain group resource permissions.
 * @author buckett
 */
public class AclGroupResourcePermission extends AclResourcePermission
{
    private Group group; 
    private User user;

    /**
     * Gets the user associated with this permission
     * @see org.bodington.servlet.facilities.AclResourcePermission#getUser()
     */
    public User getUser()
    {
        return user;
    }

    public Iterator getGroups()
    {
        return Collections.singletonList(group).iterator();
    }
    
    public Group getGroup()
    {
        return this.group;
    }
    
    public void setGroup(Group group)
    {
        this.group = group;
        this.user = new User();
        this.user.setName(group.getDescription());
    }
    
    /**
     * Compare the group names.
     */
    public int compareTo(Object other)
    {
        if (other instanceof AclGroupResourcePermission)
        {
            AclGroupResourcePermission otherGroup = (AclGroupResourcePermission)other;
            return getGroup().getName().compareTo(otherGroup.getGroup().getName());
        }
        else
        {
            return super.compareTo((AclResourcePermission)other);
        }
    }

}
