/************************************************
 * Copyright (c) Shen Li. All rights reserved.  *
 * http://joyaop.sourceforge.net                *
 * -------------------------------------------- *
 * Distributable under LGPL license.            *
 * See terms of license at gnu.org.             *
 ************************************************/
package net.sf.joyaop.demo.rbac.model;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * @author Shen Li
 */
public abstract class RoleOwnerImpl implements RoleOwner, Child {
    private Set roles = new HashSet();

    public Set getAllRoles() {
        Set allRoles = new HashSet(roles);
        for (Iterator i = getParents().iterator(); i.hasNext();) {
            allRoles.addAll(((RoleOwner) i.next()).getAllRoles());
        }
        return allRoles;
    }

    public Set getRoles() {
        return roles;
    }

    public void setRoles(Set roles) {
        this.roles = roles;
    }

    public void addRole(Role role) {
        roles.add(role);
    }

    public boolean hasRole(Role role) {
        return roles.contains(role);
    }

    public void removeRole(Role role) {
        roles.remove(role);
    }

    public boolean hasPermission(Permission permission) {        
        for (Iterator i = getAllRoles().iterator(); i.hasNext();) {
            if (((Role) i.next()).hasPermission(permission)) {
                return true;
            }
        }
        return false;
    }
}
