RoleBean.java 2.14 KB
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fi.insomnia.bortal.beans;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fi.insomnia.bortal.facade.RoleFacade;
import fi.insomnia.bortal.model.Role;

/**
 * 
 * @author tuukka
 */
@Stateless
public class RoleBean implements RoleBeanLocal {

//    public static final String[] DECLARED_ROLES = 
//    {
//        BeanRole.SUPERADMIN.toString(),
//        BeanRole.ADMIN_BASE.name(), 
//        BeanRole.USER_BASE.name() 
//        };

    @EJB
    private RoleFacade roleFacade;
    private static final Logger logger = LoggerFactory.getLogger(RoleBean.class);

    public List<Role> listRoles() {
        return roleFacade.findAll();
    }

    public Role mergeChanges(Role role) {
        return roleFacade.merge(role);
    }

    public Role create(Role role) {

        roleFacade.create(role);

        return role;
    }

    public List<Role> getPossibleParents(Role role) {
        List<Role> roleList = listRoles();

        if (role == null)
            return roleList;

        List<Role> children = getAllChilds(role, new HashSet<Role>());

        for (Role unit : children) {
            if (roleList.contains(unit)) {
                roleList.remove(unit);
            }
        }

        return roleList;
    }

    private static List<Role> getAllChilds(Role role, Set<Role> checkedRoles) {

        List<Role> returnList = new ArrayList<Role>();

        if (checkedRoles.contains(role) || role == null) {
            return returnList;
        }

        for (Role unit : role.getChildren()) {
            List<Role> someList = getAllChilds(unit, checkedRoles);

            returnList.addAll(someList);
        }
        checkedRoles.add(role);

        return returnList;
    }

//    public static String[] getDeclaredRoles() {
//        return DECLARED_ROLES;
//    }

    

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
}