4 package com.edf.gde.dao;
6 import com.edf.gde.entities.Group;
7 import com.edf.gde.entities.User;
8 import com.edf.gde.entities.UserGroup;
10 import javax.persistence.EntityManager;
11 import javax.persistence.Query;
17 public class UserDao {
18 private final EntityManager em;
19 public UserDao(EntityManager em) {
28 * @return the new User or null if user exists or on error
30 public User createUser(String userName, String password) {
32 if (userExists(userName)) {
33 throw new RuntimeException("Unable to create user " + userName);
35 User user = new User();
36 user.setName(userName);
37 user.setPassword(password);
46 * @return the new created Group or null on error
48 public Group createGroup(String groupName) {
49 if (groupExists(groupName)) {
50 throw new RuntimeException("Unable to create group " + groupName);
52 Group group = new Group();
53 group.setName(groupName);
62 * @return null if the group does not exists
64 public Group findGroup(String name) {
66 Query q = em.createNamedQuery("Group.findByName");
67 q.setParameter("name", name);
68 group = (Group) q.getSingleResult();
77 public Group findGroup(long id) {
79 Query q = em.createNamedQuery("Group.findById");
80 q.setParameter("id", id);
81 group = (Group) q.getSingleResult();
90 private boolean groupExists(String name) {
92 Query q = em.createNamedQuery("Group.findByName");
93 q.setParameter("name", name);
95 group = (Group) q.getSingleResult();
96 } catch (Exception ex) {
103 * Add a user to a group
109 public void addToGroup(long groupId, long userId) {
110 if (!isInGroup(groupId, userId)) {
111 UserGroup userGroup = new UserGroup();
112 userGroup.setGroupId(groupId);
113 userGroup.setUserId(userId);
114 em.persist(userGroup);
116 throw new RuntimeException("Unable to add " + userId + " to group " + groupId);
121 * Add users to a group in one transaction
127 public void addToGroup(long groupId, List<Long> userIds) {
128 for (Long id : userIds) {
129 addToGroup(groupId, id);
139 public void removeFromGroup(long groupId, long userId) {
140 Query q = em.createNamedQuery("UserGroup.findByGroupIdUserId");
141 q.setParameter("groupId", groupId);
142 q.setParameter("userId", userId);
143 List<UserGroup> l = q.getResultList();
145 throw new RuntimeException("UserGroup not found");
147 UserGroup ug = l.get(0);
157 public boolean isInGroup(long groupId, long userId) {
158 Query q = em.createNamedQuery("UserGroup.findByGroupIdUserId");
159 q.setParameter("groupId", groupId);
160 q.setParameter("userId", userId);
161 List<UserGroup> l = q.getResultList();
166 * Find user by user name (login)
171 public User findUser(String userName) {
172 Query q = em.createNamedQuery("User.findByName");
173 q.setParameter("username", userName);
175 user = (User) q.getSingleResult();
184 private boolean userExists(String userName) {
185 Query q = em.createNamedQuery("User.findByName");
186 q.setParameter("username", userName);
189 user = (User) q.getSingleResult();
190 } catch (Exception ex) {
201 public User findUser(long id) {
202 return em.find(User.class, id);
210 public boolean deleteUser(long userId) {
211 User user = findUser(userId);
224 public boolean deleteGroup(long groupId) {
225 Group group = findGroup(groupId);