]> SALOME platform Git repositories - modules/gde.git/commitdiff
Salome HOME
Group management
authormordicus <mordicus@HP-8560w>
Mon, 3 Aug 2015 15:55:01 +0000 (17:55 +0200)
committermordicus <mordicus@HP-8560w>
Mon, 3 Aug 2015 15:55:01 +0000 (17:55 +0200)
projects/GDE_App/GDE-ejb/src/java/com/edf/gde/ejb/UserDAO.java

index bc3276bd09086b6616f2e3fead69c0b1af8f2f88..48016c0d5a18dbbff1b5a6a116e3f9a969b152eb 100644 (file)
@@ -2,6 +2,8 @@ package com.edf.gde.ejb;
 
 import com.edf.gde.entities.Group;
 import com.edf.gde.entities.User;
+import com.edf.gde.entities.UserGroup;
+import java.util.List;
 import javax.ejb.Stateless;
 import javax.ejb.LocalBean;
 import javax.persistence.EntityManager;
@@ -56,6 +58,7 @@ public class UserDAO {
 
     /**
      * Find group
+     *
      * @param name
      * @return null if the group does not exists
      */
@@ -72,15 +75,37 @@ public class UserDAO {
     }
 
     public boolean addToGroup(long groupId, long userId) {
-        /* TODO : add code ! */
-        return true;
+        if (!isInGroup(groupId, userId)) {
+            UserGroup userGroup = new UserGroup();
+            userGroup.setGroupId(groupId);
+            userGroup.setUserId(userId);
+            em.persist(userGroup);
+            return true;
+        }
+        return false;
     }
-    
+
     public boolean removeFromGroup(long groupId, long userId) {
-        /* TODO : add code ! */
+        Query q = em.createNamedQuery("UserGroup.findByGroupIdUserId");
+        q.setParameter("groupId", groupId);
+        q.setParameter("userId", userId);
+        List<UserGroup> l = q.getResultList();
+        if (l.isEmpty()) {
+            return false;
+        }
+        UserGroup ug = l.get(0);
+        em.remove(ug);
         return true;
     }
-    
+
+    public boolean isInGroup(long groupId, long userId) {
+        Query q = em.createNamedQuery("UserGroup.findByGroupIdUserId");
+        q.setParameter("groupId", groupId);
+        q.setParameter("userId", userId);
+        List<UserGroup> l = q.getResultList();
+        return !l.isEmpty();
+    }
+
     /**
      * Find user by user name (login)
      *