]> SALOME platform Git repositories - modules/gde.git/blob - projects/GDE_App/GDE-ejb/src/java/com/edf/gde/dao/UserDao.java
Salome HOME
Working on edj->dao separation
[modules/gde.git] / projects / GDE_App / GDE-ejb / src / java / com / edf / gde / dao / UserDao.java
1 /*
2  * (C) 2015 EDF
3  */
4 package com.edf.gde.dao;
5
6 import com.edf.gde.entities.Group;
7 import com.edf.gde.entities.User;
8 import com.edf.gde.entities.UserGroup;
9 import java.util.List;
10 import javax.persistence.EntityManager;
11 import javax.persistence.Query;
12
13 /**
14  *
15  * @author Kavoos
16  */
17 public class UserDao {
18     private final EntityManager em;
19     public UserDao(EntityManager em) {
20         this.em = em;
21     }
22
23     /**
24      * Create a new user
25      *
26      * @param userName
27      * @param password
28      * @return the new User or null if user exists or on error
29      */
30     public User createUser(String userName, String password) {
31
32         if (userExists(userName)) {
33             throw new RuntimeException("Unable to create user " + userName);
34         }
35         User user = new User();
36         user.setName(userName);
37         user.setPassword(password);
38         em.persist(user);
39         return user;
40     }
41
42     /**
43      * Create a new group
44      *
45      * @param groupName
46      * @return the new created Group or null on error
47      */
48     public Group createGroup(String groupName) {
49         if (groupExists(groupName)) {
50             throw new RuntimeException("Unable to create group " + groupName);
51         }
52         Group group = new Group();
53         group.setName(groupName);
54         em.persist(group);
55         return group;
56     }
57
58     /**
59      * Find group
60      *
61      * @param name
62      * @return null if the group does not exists
63      */
64     public Group findGroup(String name) {
65         Group group = null;
66         Query q = em.createNamedQuery("Group.findByName");
67         q.setParameter("name", name);
68         group = (Group) q.getSingleResult();
69         return group;
70     }
71
72     /**
73      *
74      * @param id
75      * @return
76      */
77     public Group findGroup(long id) {
78         Group group = null;
79         Query q = em.createNamedQuery("Group.findById");
80         q.setParameter("id", id);
81         group = (Group) q.getSingleResult();
82         return group;
83     }
84
85     /**
86      *
87      * @param name
88      * @return
89      */
90     private boolean groupExists(String name) {
91         Group group = null;
92         Query q = em.createNamedQuery("Group.findByName");
93         q.setParameter("name", name);
94         try {
95             group = (Group) q.getSingleResult();
96         } catch (Exception ex) {
97             return false;
98         }
99         return true;
100     }
101
102     /**
103      * Add a user to a group
104      *
105      * @param groupId
106      * @param userId
107      * @return
108      */
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);
115         } else {
116             throw new RuntimeException("Unable to add " + userId + " to group " + groupId);
117         }
118     }
119
120     /**
121      * Add users to a group in one transaction
122      *
123      * @param groupId
124      * @param userIds
125      * @return
126      */
127     public void addToGroup(long groupId, List<Long> userIds) {
128         for (Long id : userIds) {
129             addToGroup(groupId, id);
130         }
131     }
132
133     /**
134      *
135      * @param groupId
136      * @param userId
137      * @return
138      */
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();
144         if (l.isEmpty()) {
145             throw new RuntimeException("UserGroup not found");
146         }
147         UserGroup ug = l.get(0);
148         em.remove(ug);
149     }
150
151     /**
152      *
153      * @param groupId
154      * @param userId
155      * @return
156      */
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();
162         return !l.isEmpty();
163     }
164
165     /**
166      * Find user by user name (login)
167      *
168      * @param userName
169      * @return
170      */
171     public User findUser(String userName) {
172         Query q = em.createNamedQuery("User.findByName");
173         q.setParameter("username", userName);
174         User user = null;
175         user = (User) q.getSingleResult();
176         return user;
177     }
178
179     /**
180      *
181      * @param userName
182      * @return
183      */
184     private boolean userExists(String userName) {
185         Query q = em.createNamedQuery("User.findByName");
186         q.setParameter("username", userName);
187         User user = null;
188         try {
189             user = (User) q.getSingleResult();
190         } catch (Exception ex) {
191             return false;
192         }
193         return true;
194     }
195
196     /**
197      *
198      * @param id
199      * @return
200      */
201     public User findUser(long id) {
202         return em.find(User.class, id);
203     }
204
205     /**
206      *
207      * @param userId
208      * @return
209      */
210     public boolean deleteUser(long userId) {
211         User user = findUser(userId);
212         if (user != null) {
213             em.remove(user);
214             return true;
215         }
216         return false;
217     }
218
219     /**
220      *
221      * @param groupId
222      * @return
223      */
224     public boolean deleteGroup(long groupId) {
225         Group group = findGroup(groupId);
226         if (group != null) {
227             em.remove(group);
228             return true;
229         }
230         return false;
231     }
232
233 }