]> SALOME platform Git repositories - modules/gde.git/blob - projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/UserDAO.java
Salome HOME
- Group management, final commit.
[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.PersistenceContext;
11 import javax.persistence.Query;
12
13 /**
14  *
15  * @author Kavoos
16  */
17 @Stateless
18 @LocalBean
19 public class UserDAO {
20
21     @PersistenceContext(unitName = "GDE-ejbPU")
22     private EntityManager em;
23
24     /**
25      * Create a new user
26      *
27      * @param userName
28      * @param password
29      * @return the new User or null if user exists or on error
30      */
31     public User createUser(String userName, String password) {
32
33         if (userExists(userName)) {
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 (groupExists(groupName)) {
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         group = (Group) q.getSingleResult();
70         return group;
71     }
72
73     /**
74      *
75      * @param id
76      * @return
77      */
78     public Group findGroup(long id) {
79         Group group = null;
80         Query q = em.createNamedQuery("Group.findById");
81         q.setParameter("id", id);
82         group = (Group) q.getSingleResult();
83         return group;
84     }
85
86     /**
87      *
88      * @param name
89      * @return
90      */
91     private boolean groupExists(String name) {
92         Group group = null;
93         Query q = em.createNamedQuery("Group.findByName");
94         q.setParameter("name", name);
95         try {
96             group = (Group) q.getSingleResult();
97         } catch (Exception ex) {
98             return false;
99         }
100         return true;
101     }
102
103     /**
104      * Add a user to a group
105      *
106      * @param groupId
107      * @param userId
108      * @return
109      */
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);
116         } else {
117             throw new RuntimeException("Unable to add " + userId + " to group " + groupId);
118         }
119     }
120
121     /**
122      * Add users to a group in one transaction
123      *
124      * @param groupId
125      * @param userIds
126      * @return
127      */
128     public void addToGroup(long groupId, List<Long> userIds) {
129         for (Long id : userIds) {
130             addToGroup(groupId, id);
131         }
132     }
133
134     /**
135      *
136      * @param groupId
137      * @param userId
138      * @return
139      */
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();
145         if (l.isEmpty()) {
146             throw new RuntimeException("UserGroup not found");
147         }
148         UserGroup ug = l.get(0);
149         em.remove(ug);
150     }
151
152     /**
153      *
154      * @param groupId
155      * @param userId
156      * @return
157      */
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();
163         return !l.isEmpty();
164     }
165
166     /**
167      * Find user by user name (login)
168      *
169      * @param userName
170      * @return
171      */
172     public User findUser(String userName) {
173         Query q = em.createNamedQuery("User.findByName");
174         q.setParameter("username", userName);
175         User user = null;
176         user = (User) q.getSingleResult();
177         return user;
178     }
179
180     /**
181      *
182      * @param userName
183      * @return
184      */
185     private boolean userExists(String userName) {
186         Query q = em.createNamedQuery("User.findByName");
187         q.setParameter("username", userName);
188         User user = null;
189         try {
190             user = (User) q.getSingleResult();
191         } catch (Exception ex) {
192             return false;
193         }
194         return true;
195     }
196
197     /**
198      *
199      * @param id
200      * @return
201      */
202     public User findUser(long id) {
203         return em.find(User.class, id);
204     }
205
206     /**
207      *
208      * @param userId
209      * @return
210      */
211     public boolean deleteUser(long userId) {
212         User user = findUser(userId);
213         if (user != null) {
214             em.remove(user);
215             return true;
216         }
217         return false;
218     }
219
220     /**
221      *
222      * @param groupId
223      * @return
224      */
225     public boolean deleteGroup(long groupId) {
226         Group group = findGroup(groupId);
227         if (group != null) {
228             em.remove(group);
229             return true;
230         }
231         return false;
232     }
233 }