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;
/**
* Find group
+ *
* @param name
* @return null if the group does not exists
*/
}
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)
*