RoleFacade.java 2.8 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.facade;

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

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import fi.codecrew.moya.beans.EventBeanLocal;
import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.LanEvent;
import fi.codecrew.moya.model.Role;
import fi.codecrew.moya.model.Role_;

@Stateless
@LocalBean
public class RoleFacade extends IntegerPkGenericFacade<Role> {

	@EJB
	private EventBeanLocal eventbean;

	public RoleFacade() {

		super(Role.class);
	}

	// public Role findByName(String name, LanEvent event) {
	// TypedQuery<Role> q = em.createNamedQuery("Role.findByRoleName",
	// Role.class);
	// q.setParameter("name", name);
	// q.setParameter("event", event);
	// return getSingleNullableResult(q);
	// }

	public List<Role> findForUser(EventUser user) {
		CriteriaBuilder cb = getEm().getCriteriaBuilder();
		CriteriaQuery<Role> cq = cb.createQuery(Role.class);
		Root<Role> root = cq.from(Role.class);
		cq.where(cb.equal(root.get(Role_.event), eventbean.getCurrentEvent()), cb.isMember(user, root.get(Role_.users)));

		return getEm().createQuery(cq).getResultList();
	}

	public Role createRole(LanEvent event, String rolename) {
		Role ret = new Role(event);
		ret.setName(rolename);
		create(ret);
		return ret;
	}

	public List<Role> findAll() {
		CriteriaBuilder cb = getEm().getCriteriaBuilder();
		CriteriaQuery<Role> cq = cb.createQuery(Role.class);
		Root<Role> root = cq.from(Role.class);
		cq.where(cb.equal(root.get(Role_.event), eventbean.getCurrentEvent()));

		return getEm().createQuery(cq).getResultList();
	}

	public List<Role> findUserSelectableRoles() {
		CriteriaBuilder cb = getEm().getCriteriaBuilder();
		CriteriaQuery<Role> cq = cb.createQuery(Role.class);
		Root<Role> root = cq.from(Role.class);
		cq.where(cb.equal(root.get(Role_.event), eventbean.getCurrentEvent()),
				cb.equal(root.get(Role_.userSelectableRole), Boolean.TRUE));
		
		List<Role> roles = getEm().createQuery(cq).getResultList();
		
		return roles;
	}

}