Salome HOME
- Code refactoring and simplification
[modules/gde.git] / projects / GDE_App / GDE-ejb / src / java / com / edf / gde / ejb / UserDAO.java
1 package com.edf.gde.ejb;
2
3 import com.edf.gde.entities.Group;
4 import com.edf.gde.entities.User;
5 import com.edf.gde.entities.UserGroup;
6 import java.util.List;
7 import javax.ejb.Stateless;
8 import javax.ejb.LocalBean;
9 import javax.persistence.EntityManager;
10 import javax.persistence.NoResultException;
11 import javax.persistence.PersistenceContext;
12 import javax.persistence.Query;
13
14 /**
15  *
16  * @author Kavoos
17  */
18 @Stateless
19 @LocalBean
20 public class UserDAO {
21
22     @PersistenceContext(unitName = "GDE-ejbPU")
23     private EntityManager em;
24
25     /**
26      * Create a new user
27      *
28      * @param userName
29      * @param password
30      * @return the new User or null if user exists or on error
31      */
32     public User createUser(String userName, String password) {
33         if (findUser(userName) != null) {
34             throw new RuntimeException("Unable to create user " + userName);
35         }
36         User user = new User();
37         user.setName(userName);
38         user.setPassword(password);
39         em.persist(user);
40         return user;
41     }
42
43     /**
44      * Create a new group
45      *
46      * @param groupName
47      * @return the new created Group or null on error
48      */
49     public Group createGroup(String groupName) {
50         if (findGroup(groupName) != null) {
51             throw new RuntimeException("Unable to create group " + groupName);
52         }
53         Group group = new Group();
54         group.setName(groupName);
55         em.persist(group);
56         return group;
57     }
58
59     /**
60      * Find group
61      *
62      * @param name
63      * @return null if the group does not exists
64      */
65     public Group findGroup(String name) {
66         Group group = null;
67         Query q = em.createNamedQuery("Group.findByName");
68         q.setParameter("name", name);
69         try {
70             group = (Group) q.getSingleResult();
71         } catch (NoResultException ex) {
72             return null;
73         }
74         return group;
75     }
76
77     /**
78      * Add a user to a group
79      * @param groupId
80      * @param userId
81      * @return
82      */
83     public void addToGroup(long groupId, long userId) {
84         if (!isInGroup(groupId, userId)) {
85             UserGroup userGroup = new UserGroup();
86             userGroup.setGroupId(groupId);
87             userGroup.setUserId(userId);
88             em.persist(userGroup);
89         } else {
90             throw new RuntimeException("Unable to add "+userId+" to group "+groupId);
91         }
92     }
93
94     /**
95      * Add users to a group in one transaction
96      *
97      * @param groupId
98      * @param userIds 
99      * @return
100      */
101     public void addToGroup(long groupId, List<Long> userIds) {
102         for (Long id : userIds) {
103             addToGroup(groupId, id);
104         }
105     }
106
107     /**
108      *
109      * @param groupId
110      * @param userId
111      * @return
112      */
113     public void removeFromGroup(long groupId, long userId) {
114         Query q = em.createNamedQuery("UserGroup.findByGroupIdUserId");
115         q.setParameter("groupId", groupId);
116         q.setParameter("userId", userId);
117         List<UserGroup> l = q.getResultList();
118         if (l.isEmpty()) {
119             throw new RuntimeException("UserGroup not found");
120         }
121         UserGroup ug = l.get(0);
122         em.remove(ug);
123     }
124
125     /**
126      *
127      * @param groupId
128      * @param userId
129      * @return
130      */
131     public boolean isInGroup(long groupId, long userId) {
132         Query q = em.createNamedQuery("UserGroup.findByGroupIdUserId");
133         q.setParameter("groupId", groupId);
134         q.setParameter("userId", userId);
135         List<UserGroup> l = q.getResultList();
136         return !l.isEmpty();
137     }
138
139     /**
140      * Find user by user name (login)
141      *
142      * @param userName
143      * @return
144      */
145     public User findUser(String userName) {
146         Query q = em.createNamedQuery("User.findByName");
147         User user = null;
148         try {
149             user = (User) q.getSingleResult();
150         } catch (NoResultException e) {
151             return null;
152         }
153         return user;
154     }
155
156     /**
157      *
158      * @param id
159      * @return
160      */
161     public User findUser(long id) {
162         return em.find(User.class, id);
163     }
164
165     /**
166      *
167      * @param userId
168      * @return
169      */
170     public boolean deleteUser(long userId) {
171         User user = findUser(userId);
172         if (user != null) {
173             em.remove(user);
174             return true;
175         }
176         return false;
177     }
178 }