1 package com.edf.gde.ejb;
3 import com.edf.gde.entities.Group;
4 import com.edf.gde.entities.User;
5 import com.edf.gde.entities.UserGroup;
7 import javax.ejb.Stateless;
8 import javax.ejb.LocalBean;
9 import javax.persistence.EntityManager;
10 import javax.persistence.PersistenceContext;
11 import javax.persistence.Query;
19 public class UserDAO {
21 @PersistenceContext(unitName = "GDE-ejbPU")
22 private EntityManager em;
29 * @return the new User or null if user exists or on error
31 public User createUser(String userName, String password) {
33 if (userExists(userName)) {
34 throw new RuntimeException("Unable to create user " + userName);
36 User user = new User();
37 user.setName(userName);
38 user.setPassword(password);
47 * @return the new created Group or null on error
49 public Group createGroup(String groupName) {
50 if (groupExists(groupName)) {
51 throw new RuntimeException("Unable to create group " + groupName);
53 Group group = new Group();
54 group.setName(groupName);
63 * @return null if the group does not exists
65 public Group findGroup(String name) {
67 Query q = em.createNamedQuery("Group.findByName");
68 q.setParameter("name", name);
69 group = (Group) q.getSingleResult();
78 public Group findGroup(long id) {
80 Query q = em.createNamedQuery("Group.findById");
81 q.setParameter("id", id);
82 group = (Group) q.getSingleResult();
91 private boolean groupExists(String name) {
93 Query q = em.createNamedQuery("Group.findByName");
94 q.setParameter("name", name);
96 group = (Group) q.getSingleResult();
97 } catch (Exception ex) {
104 * Add a user to a group
110 public void addToGroup(long groupId, long userId) {
111 if (!isInGroup(groupId, userId)) {
112 UserGroup userGroup = new UserGroup();
113 userGroup.setGroupId(groupId);
114 userGroup.setUserId(userId);
115 em.persist(userGroup);
117 throw new RuntimeException("Unable to add " + userId + " to group " + groupId);
122 * Add users to a group in one transaction
128 public void addToGroup(long groupId, List<Long> userIds) {
129 for (Long id : userIds) {
130 addToGroup(groupId, id);
140 public void removeFromGroup(long groupId, long userId) {
141 Query q = em.createNamedQuery("UserGroup.findByGroupIdUserId");
142 q.setParameter("groupId", groupId);
143 q.setParameter("userId", userId);
144 List<UserGroup> l = q.getResultList();
146 throw new RuntimeException("UserGroup not found");
148 UserGroup ug = l.get(0);
158 public boolean isInGroup(long groupId, long userId) {
159 Query q = em.createNamedQuery("UserGroup.findByGroupIdUserId");
160 q.setParameter("groupId", groupId);
161 q.setParameter("userId", userId);
162 List<UserGroup> l = q.getResultList();
167 * Find user by user name (login)
172 public User findUser(String userName) {
173 Query q = em.createNamedQuery("User.findByName");
174 q.setParameter("username", userName);
176 user = (User) q.getSingleResult();
185 private boolean userExists(String userName) {
186 Query q = em.createNamedQuery("User.findByName");
187 q.setParameter("username", userName);
190 user = (User) q.getSingleResult();
191 } catch (Exception ex) {
202 public User findUser(long id) {
203 return em.find(User.class, id);
211 public boolean deleteUser(long userId) {
212 User user = findUser(userId);
225 public boolean deleteGroup(long groupId) {
226 Group group = findGroup(groupId);