OrgRoleBean.java 2.73 KB
/*
 * Copyright Codecrew Ry
 * 
 * All rights reserved.
 * 
 * This license applies to any software containing a notice placed by the 
 * copyright holder. Such software is herein referred to as the Software. 
 * This license covers modification, distribution and use of the Software. 
 * 
 * Any distribution and use in source and binary forms, with or without 
 * modification is not permitted without explicit written permission from the 
 * copyright owner. 
 * 
 * A non-exclusive royalty-free right is granted to the copyright owner of the 
 * Software to use, modify and distribute all modifications to the Software in 
 * future versions of the Software. 
 * 
 */
package fi.codecrew.moya.beans;

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

import javax.annotation.security.DeclareRoles;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

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

import fi.codecrew.moya.facade.OrgRoleFacade;
import fi.codecrew.moya.beans.EventBeanLocal;
import fi.codecrew.moya.beans.OrgRoleBeanLocal;
import fi.codecrew.moya.enums.apps.UserPermission;
import fi.codecrew.moya.model.OrgRole;

/**
 * Session Bean implementation class OrgRoleBean
 */
@Stateless
@LocalBean
@DeclareRoles({ UserPermission.S_READ_ORGROLES, UserPermission.S_WRITE_ORGROLES })
public class OrgRoleBean implements OrgRoleBeanLocal {

	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory
			.getLogger(OrgRoleBean.class);

	@EJB
	private EventBeanLocal eventBean;

	@EJB
	private OrgRoleFacade orgRoleFacade;

	public OrgRoleBean() {
	}

	@Override
	public List<OrgRole> listOrgRoles() {
		return orgRoleFacade.findByOrganizer(eventBean.getCurrentEvent()
				.getOrganiser());
	}

	@Override
	public OrgRole find(Integer id) {
		return orgRoleFacade.find(id);
	}

	@Override
	public List<OrgRole> getPossibleParents(OrgRole orgRole) {

		List<OrgRole> roleList = orgRoleFacade.findAll();

		if (orgRole == null) {
			return roleList;
		}

		List<OrgRole> children = getAllChilds(orgRole, new HashSet<OrgRole>());

		for (OrgRole unit : children) {
			if (roleList.contains(unit)) {
				roleList.remove(unit);
			}
		}
		roleList.remove(orgRole);
		return roleList;
	}

	private List<OrgRole> getAllChilds(OrgRole orgRole,
			HashSet<OrgRole> checkedRoles) {
		List<OrgRole> returnList = new ArrayList<OrgRole>();

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

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

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

		return returnList;
	}

	@Override
	public void create(OrgRole orgRole) {
		orgRoleFacade.create(orgRole);
	}

}