2013-03-28 17:07:37 +01:00
|
|
|
package com.commafeed.backend.dao;
|
|
|
|
|
|
2013-03-30 09:22:49 +01:00
|
|
|
import java.util.List;
|
2013-03-29 12:59:21 +01:00
|
|
|
import java.util.Set;
|
2013-03-28 17:07:37 +01:00
|
|
|
|
|
|
|
|
import javax.ejb.Stateless;
|
2013-06-08 16:15:11 +02:00
|
|
|
import javax.persistence.criteria.CriteriaQuery;
|
|
|
|
|
import javax.persistence.criteria.JoinType;
|
|
|
|
|
import javax.persistence.criteria.Root;
|
2013-03-28 17:07:37 +01:00
|
|
|
|
|
|
|
|
import com.commafeed.backend.model.User;
|
|
|
|
|
import com.commafeed.backend.model.UserRole;
|
2013-03-30 19:06:32 +01:00
|
|
|
import com.commafeed.backend.model.UserRole.Role;
|
2013-04-12 10:44:41 +02:00
|
|
|
import com.commafeed.backend.model.UserRole_;
|
2013-03-29 12:59:21 +01:00
|
|
|
import com.google.common.collect.Sets;
|
2013-03-28 17:07:37 +01:00
|
|
|
|
|
|
|
|
@Stateless
|
2013-04-11 20:49:08 +02:00
|
|
|
public class UserRoleDAO extends GenericDAO<UserRole> {
|
2013-03-28 17:07:37 +01:00
|
|
|
|
2013-05-15 16:26:18 +02:00
|
|
|
@Override
|
|
|
|
|
public List<UserRole> findAll() {
|
2013-06-08 16:15:11 +02:00
|
|
|
CriteriaQuery<UserRole> query = builder.createQuery(getType());
|
|
|
|
|
Root<UserRole> root = query.from(getType());
|
|
|
|
|
query.distinct(true);
|
|
|
|
|
|
|
|
|
|
root.fetch(UserRole_.user, JoinType.LEFT);
|
|
|
|
|
|
|
|
|
|
return em.createQuery(query).getResultList();
|
2013-05-15 16:26:18 +02:00
|
|
|
}
|
|
|
|
|
|
2013-03-30 09:22:49 +01:00
|
|
|
public List<UserRole> findAll(User user) {
|
2013-04-12 10:44:41 +02:00
|
|
|
return findByField(UserRole_.user, user);
|
2013-03-30 09:22:49 +01:00
|
|
|
}
|
|
|
|
|
|
2013-04-11 20:49:08 +02:00
|
|
|
public Set<Role> findRoles(User user) {
|
2013-03-30 19:06:32 +01:00
|
|
|
Set<Role> list = Sets.newHashSet();
|
2013-04-12 10:44:41 +02:00
|
|
|
for (UserRole role : findByField(UserRole_.user, user)) {
|
2013-03-28 17:07:37 +01:00
|
|
|
list.add(role.getRole());
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
}
|