/************************************************
 * 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 class ParentImpl implements Parent {
    private Set children = new HashSet();

    public Set getChildren() {
        return children;
    }

    public void setChildren(Set children) {
        this.children = children;
    }

    public void addChild(Child child) {
        children.add(child);
    }

    public void removeChildren(Child child) {
        children.remove(child);
    }

    public boolean hasChild(Child child) {
        // TODO maybe it's a bug of hibernate
        if (new HashSet(children).contains(child)) {
            return true;
        }
        for (Iterator i = children.iterator(); i.hasNext();) {
            Object object = i.next();
            if (object instanceof Parent && (((Parent) object).hasChild(child))) {
                return true;
            }
        }
        return false;
    }
}
