Added UserGroupDao methods and tests.

Change-Id: I71d224c5d236cc57ef74749e06d4b28d0bc2cc6a
diff --git a/full/src/main/java/de/ids_mannheim/korap/constant/PredefinedRole.java b/full/src/main/java/de/ids_mannheim/korap/constant/PredefinedRole.java
index 0c79d6d..29dd9d4 100644
--- a/full/src/main/java/de/ids_mannheim/korap/constant/PredefinedRole.java
+++ b/full/src/main/java/de/ids_mannheim/korap/constant/PredefinedRole.java
@@ -1,15 +1,22 @@
 package de.ids_mannheim.korap.constant;
 
 public enum PredefinedRole {
-    GROUP_ADMIN(1), GROUP_MEMBER(2), VC_ADMIN(3), VC_MEMBER(3);
+    GROUP_ADMIN(1), GROUP_MEMBER(2), VC_ADMIN(3), VC_MEMBER(4);
     
     private int id;
+    private String name;
 
     PredefinedRole (int i) {
         this.id = i;
+        this.name = name().toLowerCase().replace("_", " "); 
     }
     
     public int getId () {
         return id;
     }
+    
+    @Override
+    public String toString () {
+        return this.name;
+    }
 }
diff --git a/full/src/main/java/de/ids_mannheim/korap/dao/RoleDao.java b/full/src/main/java/de/ids_mannheim/korap/dao/RoleDao.java
index ac73e26..fdf1c46 100644
--- a/full/src/main/java/de/ids_mannheim/korap/dao/RoleDao.java
+++ b/full/src/main/java/de/ids_mannheim/korap/dao/RoleDao.java
@@ -1,12 +1,15 @@
 package de.ids_mannheim.korap.dao;
 
 import java.util.List;
+import java.util.Set;
 
 import javax.persistence.EntityManager;
 import javax.persistence.PersistenceContext;
 import javax.persistence.Query;
 import javax.persistence.criteria.CriteriaBuilder;
 import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.ListJoin;
+import javax.persistence.criteria.Predicate;
 import javax.persistence.criteria.Root;
 
 import org.springframework.beans.factory.annotation.Autowired;
@@ -17,6 +20,8 @@
 import de.ids_mannheim.korap.entity.Privilege;
 import de.ids_mannheim.korap.entity.Role;
 import de.ids_mannheim.korap.entity.Role_;
+import de.ids_mannheim.korap.entity.UserGroupMember;
+import de.ids_mannheim.korap.entity.UserGroupMember_;
 
 @Transactional
 @Repository
@@ -40,12 +45,12 @@
         entityManager.remove(r);
     }
 
-    public void updateRoleName (int roleId, String name) {
+    public void editRoleName (int roleId, String name) {
         Role r = retrieveRoleById(roleId);
         r.setName(name);
         entityManager.persist(r);
     }
-    
+
     public Role retrieveRoleById (int roleId) {
         CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
         CriteriaQuery<Role> query = criteriaBuilder.createQuery(Role.class);
@@ -70,4 +75,19 @@
         return (Role) q.getSingleResult();
     }
 
+    public List<Role> retrieveRoleByGroupMemberId (int userId) {
+        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
+        CriteriaQuery<Role> query = criteriaBuilder.createQuery(Role.class);
+
+        Root<Role> root = query.from(Role.class);
+        ListJoin<Role, UserGroupMember> memberRole =
+                root.join(Role_.userGroupMembers);
+
+        query.select(root);
+        query.where(criteriaBuilder.equal(memberRole.get(UserGroupMember_.id),
+                userId));
+        Query q = entityManager.createQuery(query);
+        return q.getResultList();
+    }
+
 }
diff --git a/full/src/main/java/de/ids_mannheim/korap/dao/UserGroupDao.java b/full/src/main/java/de/ids_mannheim/korap/dao/UserGroupDao.java
index f96cbd5..915a1ce 100644
--- a/full/src/main/java/de/ids_mannheim/korap/dao/UserGroupDao.java
+++ b/full/src/main/java/de/ids_mannheim/korap/dao/UserGroupDao.java
@@ -1,14 +1,17 @@
 package de.ids_mannheim.korap.dao;
 
-import java.util.HashSet;
+import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
-import java.util.Set;
 
 import javax.persistence.EntityManager;
+import javax.persistence.NoResultException;
 import javax.persistence.PersistenceContext;
 import javax.persistence.Query;
 import javax.persistence.criteria.CriteriaBuilder;
 import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.Expression;
+import javax.persistence.criteria.Join;
 import javax.persistence.criteria.ListJoin;
 import javax.persistence.criteria.Predicate;
 import javax.persistence.criteria.Root;
@@ -27,7 +30,12 @@
 import de.ids_mannheim.korap.entity.UserGroupMember_;
 import de.ids_mannheim.korap.entity.UserGroup_;
 import de.ids_mannheim.korap.entity.VirtualCorpus;
-import de.ids_mannheim.korap.entity.VirtualCorpusAccessGroup;
+import de.ids_mannheim.korap.entity.VirtualCorpusAccess;
+import de.ids_mannheim.korap.entity.VirtualCorpusAccess_;
+import de.ids_mannheim.korap.entity.VirtualCorpus_;
+import de.ids_mannheim.korap.exceptions.KustvaktException;
+import de.ids_mannheim.korap.exceptions.StatusCodes;
+import de.ids_mannheim.korap.utils.ParameterChecker;
 
 @Transactional
 @Repository
@@ -41,28 +49,38 @@
     @Autowired
     private RoleDao roleDao;
 
-    public void createGroup (String name, String createdBy,
-            UserGroupStatus status) {
+    public int createGroup (String name, String createdBy,
+            UserGroupStatus status) throws KustvaktException {
+        ParameterChecker.checkStringValue(name, "name");
+        ParameterChecker.checkStringValue(createdBy, "createdBy");
+        ParameterChecker.checkObjectValue(status, "UserGroupStatus");
+
         UserGroup group = new UserGroup();
         group.setName(name);
         group.setStatus(status);
         group.setCreatedBy(createdBy);
         entityManager.persist(group);
 
-        Set<Role> roles = new HashSet<Role>(2);
+        List<Role> roles = new ArrayList<Role>(2);
         roles.add(roleDao.retrieveRoleById(PredefinedRole.GROUP_ADMIN.getId()));
         roles.add(roleDao.retrieveRoleById(PredefinedRole.VC_ADMIN.getId()));
 
         UserGroupMember owner = new UserGroupMember();
+        owner.setUserId(createdBy);
         owner.setCreatedBy(createdBy);
         owner.setStatus(GroupMemberStatus.ACTIVE);
         owner.setGroup(group);
         owner.setRoles(roles);
         entityManager.persist(owner);
+
+        return group.getId();
     }
 
     public void deleteGroup (int groupId, String deletedBy,
-            boolean isSoftDelete) {
+            boolean isSoftDelete) throws KustvaktException {
+        ParameterChecker.checkIntegerValue(groupId, "groupId");
+        ParameterChecker.checkStringValue(deletedBy, "deletedBy");
+
         UserGroup group = retrieveGroupById(groupId);
         if (isSoftDelete) {
             group.setStatus(UserGroupStatus.DELETED);
@@ -74,7 +92,25 @@
         }
     }
 
-    public UserGroup retrieveGroupById (int groupId) {
+    public void editGroupName (int groupId, String name)
+            throws KustvaktException {
+        UserGroup group = retrieveGroupById(groupId);
+        group.setName(name);
+        entityManager.persist(group);
+    }
+
+    /** Retrieves the UserGroup by the given group id. This methods does not 
+     *  fetch group members because only group admin is allowed to see them. 
+     *  Group members have to be retrieved separately.
+     * 
+     * @see UserGroupMember
+     * @param groupId group id
+     * @return UserGroup
+     * @throws KustvaktException 
+     */
+    public UserGroup retrieveGroupById (int groupId) throws KustvaktException {
+        ParameterChecker.checkIntegerValue(groupId, "groupId");
+
         CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
         CriteriaQuery<UserGroup> query =
                 criteriaBuilder.createQuery(UserGroup.class);
@@ -83,26 +119,69 @@
         query.select(root);
         query.where(criteriaBuilder.equal(root.get(UserGroup_.id), groupId));
         Query q = entityManager.createQuery(query);
-        return (UserGroup) q.getSingleResult();
+
+        UserGroup userGroup;
+        try {
+            userGroup = (UserGroup) q.getSingleResult();
+        }
+        catch (NoResultException e) {
+            throw new KustvaktException(StatusCodes.NO_RESULT_FOUND,
+                    "No result found for query: retrieve group by id "
+                            + groupId,
+                    String.valueOf(groupId), e);
+        }
+        return userGroup;
     }
 
-    public List<UserGroup> retrieveGroupByUserId (String userId) {
+    public UserGroup retrieveGroupWithMemberById (int groupId)
+            throws KustvaktException {
+
+        ParameterChecker.checkIntegerValue(groupId, "groupId");
+
+        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
+        CriteriaQuery<UserGroup> query =
+                criteriaBuilder.createQuery(UserGroup.class);
+
+        Root<UserGroup> root = query.from(UserGroup.class);
+        root.fetch(UserGroup_.members);
+        query.select(root);
+        query.where(criteriaBuilder.equal(root.get(UserGroup_.id), groupId));
+        Query q = entityManager.createQuery(query);
+
+        UserGroup userGroup;
+        try {
+            userGroup = (UserGroup) q.getSingleResult();
+        }
+        catch (NoResultException e) {
+            throw new KustvaktException(StatusCodes.NO_RESULT_FOUND,
+                    "No result found for query: retrieve group by id "
+                            + groupId,
+                    String.valueOf(groupId), e);
+        }
+        return userGroup;
+    }
+
+    /** Retrieves only user-groups that are active (not hidden or deleted).
+     * 
+     * @param userId user id
+     * @return a list of UserGroup
+     * @throws KustvaktException
+     */
+    public List<UserGroup> retrieveGroupByUserId (String userId)
+            throws KustvaktException {
+        ParameterChecker.checkStringValue(userId, "userId");
+
         CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
         CriteriaQuery<UserGroup> query =
                 criteriaBuilder.createQuery(UserGroup.class);
 
         Root<UserGroup> root = query.from(UserGroup.class);
 
-        Predicate allUserGroup = criteriaBuilder.and(
-                criteriaBuilder.equal(root.get(UserGroup_.name),
-                        USER_GROUP_ALL),
-                criteriaBuilder.notEqual(root.get(UserGroup_.status),
-                        UserGroupStatus.DELETED));
-
-
         ListJoin<UserGroup, UserGroupMember> members =
                 root.join(UserGroup_.members);
-        Predicate memberships = criteriaBuilder.and(
+        Predicate restrictions = criteriaBuilder.and(
+                criteriaBuilder.equal(root.get(UserGroup_.status),
+                        UserGroupStatus.ACTIVE),
                 criteriaBuilder.equal(members.get(UserGroupMember_.userId),
                         userId),
                 criteriaBuilder.equal(members.get(UserGroupMember_.status),
@@ -110,14 +189,14 @@
 
 
         query.select(root);
-        query.where(criteriaBuilder.and(allUserGroup, memberships));
+        query.where(restrictions);
         Query q = entityManager.createQuery(query);
         return q.getResultList();
     }
 
     public void addVCToGroup (VirtualCorpus virtualCorpus, String createdBy,
             VirtualCorpusAccessStatus status, UserGroup group) {
-        VirtualCorpusAccessGroup accessGroup = new VirtualCorpusAccessGroup();
+        VirtualCorpusAccess accessGroup = new VirtualCorpusAccess();
         accessGroup.setCreatedBy(createdBy);
         accessGroup.setStatus(status);
         accessGroup.setUserGroup(group);
@@ -134,4 +213,26 @@
         }
     }
 
+    public void deleteVCFromGroup (int virtualCorpusId, int groupId) throws KustvaktException {
+        ParameterChecker.checkIntegerValue(virtualCorpusId, "virtualCorpusId");
+        ParameterChecker.checkIntegerValue(groupId, "groupId");
+
+        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
+        CriteriaQuery<VirtualCorpusAccess> query =
+                criteriaBuilder.createQuery(VirtualCorpusAccess.class);
+
+        Root<VirtualCorpusAccess> root = query.from(VirtualCorpusAccess.class);
+        Join<VirtualCorpusAccess, VirtualCorpus> vc = root.join(VirtualCorpusAccess_.virtualCorpus);
+        Join<VirtualCorpusAccess, UserGroup> group = root.join(VirtualCorpusAccess_.userGroup);
+
+        Predicate virtualCorpus = criteriaBuilder.equal(vc.get(VirtualCorpus_.id), virtualCorpusId);
+        Predicate userGroup = criteriaBuilder.equal(group.get(UserGroup_.id), groupId);
+        
+        query.select(root);
+        query.where(criteriaBuilder.and(virtualCorpus, userGroup));
+        Query q = entityManager.createQuery(query);
+        VirtualCorpusAccess vcAccess = (VirtualCorpusAccess) q.getSingleResult();
+        entityManager.remove(vcAccess);
+    }
+
 }
diff --git a/full/src/main/java/de/ids_mannheim/korap/dao/UserGroupMemberDao.java b/full/src/main/java/de/ids_mannheim/korap/dao/UserGroupMemberDao.java
index f0789ac..1a80b92 100644
--- a/full/src/main/java/de/ids_mannheim/korap/dao/UserGroupMemberDao.java
+++ b/full/src/main/java/de/ids_mannheim/korap/dao/UserGroupMemberDao.java
@@ -23,26 +23,25 @@
 
 @Transactional
 @Repository
-public class UserGroupMemberDao<X> {
+public class UserGroupMemberDao {
 
     @PersistenceContext
     private EntityManager entityManager;
 
-    public void addMember (UserGroupMember member)
-            throws KustvaktException {
+    public void addMember (UserGroupMember member) throws KustvaktException {
         ParameterChecker.checkObjectValue(member, "userGroupMember");
         entityManager.persist(member);
     }
-    
+
     public void addMembers (List<UserGroupMember> members)
             throws KustvaktException {
         ParameterChecker.checkObjectValue(members, "List<UserGroupMember>");
 
-        for (UserGroupMember member: members){
+        for (UserGroupMember member : members) {
             addMember(member);
         }
     }
-    
+
     public void approveMember (String userId, int groupId)
             throws KustvaktException {
         ParameterChecker.checkStringValue(userId, "userId");
@@ -80,8 +79,10 @@
         Root<UserGroupMember> root = query.from(UserGroupMember.class);
 
         Predicate predicate = criteriaBuilder.and(
-                criteriaBuilder.equal(root.get(UserGroupMember_.group), groupId),
-                criteriaBuilder.equal(root.get(UserGroupMember_.userId), userId));
+                criteriaBuilder.equal(root.get(UserGroupMember_.group),
+                        groupId),
+                criteriaBuilder.equal(root.get(UserGroupMember_.userId),
+                        userId));
 
         query.select(root);
         query.where(predicate);
@@ -89,16 +90,18 @@
         return (UserGroupMember) q.getSingleResult();
     }
 
-    public List<UserGroupMember> retrieveMemberByRole (int groupId, int roleId) {
+    public List<UserGroupMember> retrieveMemberByRole (int groupId,
+            int roleId) {
         CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
         CriteriaQuery<UserGroupMember> query =
                 criteriaBuilder.createQuery(UserGroupMember.class);
 
         Root<UserGroupMember> root = query.from(UserGroupMember.class);
         Join<UserGroupMember, Role> memberRole = root.join("roles");
-        
+
         Predicate predicate = criteriaBuilder.and(
-                criteriaBuilder.equal(root.get(UserGroupMember_.group), groupId),
+                criteriaBuilder.equal(root.get(UserGroupMember_.group),
+                        groupId),
                 criteriaBuilder.equal(memberRole.get("role_id"), roleId));
 
         query.select(root);
@@ -106,4 +109,20 @@
         Query q = entityManager.createQuery(query);
         return q.getResultList();
     }
+
+    public List<UserGroupMember> retrieveMemberByGroupId (int groupId) {
+        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
+        CriteriaQuery<UserGroupMember> query =
+                criteriaBuilder.createQuery(UserGroupMember.class);
+
+        Root<UserGroupMember> root = query.from(UserGroupMember.class);
+
+        Predicate predicate = criteriaBuilder.and(criteriaBuilder
+                .equal(root.get(UserGroupMember_.group), groupId));
+
+        query.select(root);
+        query.where(predicate);
+        Query q = entityManager.createQuery(query);
+        return q.getResultList();
+    }
 }
diff --git a/full/src/main/java/de/ids_mannheim/korap/dao/VirtualCorpusDao.java b/full/src/main/java/de/ids_mannheim/korap/dao/VirtualCorpusDao.java
index 266a788..a57ae75 100644
--- a/full/src/main/java/de/ids_mannheim/korap/dao/VirtualCorpusDao.java
+++ b/full/src/main/java/de/ids_mannheim/korap/dao/VirtualCorpusDao.java
@@ -11,6 +11,7 @@
 import javax.persistence.criteria.CriteriaBuilder;
 import javax.persistence.criteria.CriteriaQuery;
 import javax.persistence.criteria.Join;
+import javax.persistence.criteria.ListJoin;
 import javax.persistence.criteria.Predicate;
 import javax.persistence.criteria.Root;
 
@@ -22,10 +23,17 @@
 import de.ids_mannheim.korap.constant.VirtualCorpusAccessStatus;
 import de.ids_mannheim.korap.constant.VirtualCorpusType;
 import de.ids_mannheim.korap.entity.UserGroup;
+import de.ids_mannheim.korap.entity.UserGroupMember;
+import de.ids_mannheim.korap.entity.UserGroupMember_;
+import de.ids_mannheim.korap.entity.UserGroup_;
 import de.ids_mannheim.korap.entity.VirtualCorpus;
-import de.ids_mannheim.korap.entity.VirtualCorpusAccessGroup;
+import de.ids_mannheim.korap.entity.VirtualCorpusAccess;
+import de.ids_mannheim.korap.entity.VirtualCorpusAccess_;
+import de.ids_mannheim.korap.entity.VirtualCorpus_;
 import de.ids_mannheim.korap.exceptions.KustvaktException;
 import de.ids_mannheim.korap.exceptions.StatusCodes;
+import de.ids_mannheim.korap.user.User.CorpusAccess;
+import de.ids_mannheim.korap.utils.ParameterChecker;
 
 /** VirtualCorpusDao manages SQL queries regarding virtual corpora, 
  *  e.g. retrieving and storing virtual corpora.
@@ -40,8 +48,22 @@
     @PersistenceContext
     private EntityManager entityManager;
 
-    public void storeVirtualCorpus (VirtualCorpus virtualCorpus) {
-        entityManager.persist(virtualCorpus);
+    public int createVirtualCorpus (String name, VirtualCorpusType type,
+            CorpusAccess requiredAccess, String collectionQuery, String definition,
+            String description, String status, String createdBy) {
+
+        VirtualCorpus vc = new VirtualCorpus();
+        vc.setName(name);
+        vc.setType(type);
+        vc.setRequiredAccess(requiredAccess);
+        vc.setCollectionQuery(collectionQuery);
+        vc.setDefinition(definition);
+        vc.setDescription(description);
+        vc.setStatus(status);
+        vc.setCreatedBy(createdBy);
+        
+        entityManager.persist(vc);
+        return vc.getId();
     }
 
     public void deleteVirtualCorpus (int id) throws KustvaktException {
@@ -51,32 +73,30 @@
 
     public List<VirtualCorpus> retrieveVCByType (VirtualCorpusType type)
             throws KustvaktException {
-        if (type == null) {
-            throw new KustvaktException(StatusCodes.MISSING_ARGUMENT, "type",
-                    "null");
-        }
+        ParameterChecker.checkObjectValue(type, "type");
+
         CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
         CriteriaQuery<VirtualCorpus> query =
                 criteriaBuilder.createQuery(VirtualCorpus.class);
         Root<VirtualCorpus> virtualCorpus = query.from(VirtualCorpus.class);
         query.select(virtualCorpus);
-        query.where(criteriaBuilder.equal(virtualCorpus.get("type"), type));
+        query.where(criteriaBuilder
+                .equal(virtualCorpus.get(VirtualCorpus_.type), type));
         Query q = entityManager.createQuery(query);
         return q.getResultList();
     }
 
 
     public VirtualCorpus retrieveVCById (int id) throws KustvaktException {
-        if (id == 0) {
-            throw new KustvaktException(StatusCodes.MISSING_ARGUMENT, "id",
-                    String.valueOf(id));
-        }
+        ParameterChecker.checkIntegerValue(id, "id");
+
         CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
         CriteriaQuery<VirtualCorpus> query =
                 criteriaBuilder.createQuery(VirtualCorpus.class);
         Root<VirtualCorpus> virtualCorpus = query.from(VirtualCorpus.class);
         query.select(virtualCorpus);
-        query.where(criteriaBuilder.equal(virtualCorpus.get("id"), id));
+        query.where(criteriaBuilder.equal(virtualCorpus.get(VirtualCorpus_.id),
+                id));
 
         VirtualCorpus vc = null;
         try {
@@ -95,17 +115,16 @@
 
     public List<VirtualCorpus> retrievePrivateVC (String userId)
             throws KustvaktException {
-        if (userId == null || userId.isEmpty()) {
-            throw new KustvaktException(StatusCodes.MISSING_ARGUMENT, "userId",
-                    userId);
-        }
+        ParameterChecker.checkStringValue(userId, "userId");
+
         CriteriaBuilder builder = entityManager.getCriteriaBuilder();
         CriteriaQuery<VirtualCorpus> query =
                 builder.createQuery(VirtualCorpus.class);
 
         Root<VirtualCorpus> virtualCorpus = query.from(VirtualCorpus.class);
         query.select(virtualCorpus);
-        query.where(builder.equal(virtualCorpus.get("createdBy"), userId));
+        query.where(builder.equal(virtualCorpus.get(VirtualCorpus_.createdBy),
+                userId));
 
         Query q = entityManager.createQuery(query);
         return q.getResultList();
@@ -114,35 +133,33 @@
 
     public List<VirtualCorpus> retrieveGroupVCByUser (String userId)
             throws KustvaktException {
-        if (userId == null || userId.isEmpty()) {
-            throw new KustvaktException(StatusCodes.MISSING_ARGUMENT, "userId",
-                    userId);
-        }
+        ParameterChecker.checkStringValue(userId, "userId");
 
         CriteriaBuilder builder = entityManager.getCriteriaBuilder();
         CriteriaQuery<VirtualCorpus> query =
                 builder.createQuery(VirtualCorpus.class);
 
         Root<VirtualCorpus> virtualCorpus = query.from(VirtualCorpus.class);
-        Join<VirtualCorpus, VirtualCorpusAccessGroup> accessGroup =
-                virtualCorpus.join("accessGroup");
+        Join<VirtualCorpus, VirtualCorpusAccess> access =
+                virtualCorpus.join(VirtualCorpus_.virtualCorpusAccess);
 
         Predicate corpusStatus = builder.and(
-                builder.notEqual(accessGroup.get("status"),
+                builder.notEqual(access.get(VirtualCorpusAccess_.status),
                         VirtualCorpusAccessStatus.HIDDEN),
-                builder.notEqual(accessGroup.get("status"),
+                builder.notEqual(access.get(VirtualCorpusAccess_.status),
                         VirtualCorpusAccessStatus.DELETED));
-        
+
         Predicate userGroupStatus =
-                builder.notEqual(accessGroup.get("userGroup").get("status"),
-                        UserGroupStatus.DELETED);
-        Join<VirtualCorpusAccessGroup, UserGroup> userGroupMembers =
-                accessGroup.join("userGroup").join("members");
-        
-        Predicate memberStatus = builder.equal(userGroupMembers.get("status"),
-                GroupMemberStatus.ACTIVE);
-        
-        Predicate user = builder.equal(userGroupMembers.get("userId"), userId);
+                builder.notEqual(access.get(VirtualCorpusAccess_.userGroup)
+                        .get(UserGroup_.status), UserGroupStatus.DELETED);
+        Join<UserGroup, UserGroupMember> members = access
+                .join(VirtualCorpusAccess_.userGroup).join(UserGroup_.members);
+
+        Predicate memberStatus = builder.equal(
+                members.get(UserGroupMember_.status), GroupMemberStatus.ACTIVE);
+
+        Predicate user =
+                builder.equal(members.get(UserGroupMember_.userId), userId);
 
         query.select(virtualCorpus);
         query.where(
@@ -155,18 +172,17 @@
 
     public Set<VirtualCorpus> retrieveVCByUser (String userId)
             throws KustvaktException {
-        if (userId == null || userId.isEmpty()) {
-            throw new KustvaktException(StatusCodes.MISSING_ARGUMENT, "userId",
-                    userId);
-        }
+        ParameterChecker.checkStringValue(userId, "userId");
+
         CriteriaBuilder builder = entityManager.getCriteriaBuilder();
         CriteriaQuery<VirtualCorpus> query =
                 builder.createQuery(VirtualCorpus.class);
 
         Root<VirtualCorpus> virtualCorpus = query.from(VirtualCorpus.class);
         Predicate predicate = builder.or(
-                builder.equal(virtualCorpus.get("createdBy"), userId),
-                builder.equal(virtualCorpus.get("type"),
+                builder.equal(virtualCorpus.get(VirtualCorpus_.createdBy),
+                        userId),
+                builder.equal(virtualCorpus.get(VirtualCorpus_.type),
                         VirtualCorpusType.PREDEFINED));
 
 
@@ -183,10 +199,31 @@
         return vcSet;
     }
 
+    public List<VirtualCorpus> retrieveVCByGroup (int groupId)
+            throws KustvaktException {
+        ParameterChecker.checkIntegerValue(groupId, "groupId");
+
+        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
+        CriteriaQuery<VirtualCorpus> query =
+                builder.createQuery(VirtualCorpus.class);
+
+        Root<VirtualCorpus> virtualCorpus = query.from(VirtualCorpus.class);
+        ListJoin<VirtualCorpus, VirtualCorpusAccess> corpusAccess =
+                virtualCorpus.join(VirtualCorpus_.virtualCorpusAccess);
+        Join<VirtualCorpusAccess, UserGroup> accessGroup =
+                corpusAccess.join(VirtualCorpusAccess_.userGroup);
+
+        query.select(virtualCorpus);
+        query.where(builder.equal(accessGroup.get(UserGroup_.id), groupId));
+        Query q = entityManager.createQuery(query);
+        return q.getResultList();
+    }
 
     // EM: what is needed for admin?
     public List<VirtualCorpus> retrieveVirtualCorpusByAdmin () {
         return null;
 
     }
+
+
 }
diff --git a/full/src/main/java/de/ids_mannheim/korap/entity/Role.java b/full/src/main/java/de/ids_mannheim/korap/entity/Role.java
index 685bf92..2d2571e 100644
--- a/full/src/main/java/de/ids_mannheim/korap/entity/Role.java
+++ b/full/src/main/java/de/ids_mannheim/korap/entity/Role.java
@@ -35,10 +35,10 @@
     @ManyToMany(mappedBy = "roles", fetch = FetchType.LAZY)
     private List<UserGroupMember> userGroupMembers;
 
-    @OneToMany(mappedBy = "role", fetch = FetchType.LAZY)
+    @OneToMany(mappedBy = "role", fetch = FetchType.EAGER)
     private List<Privilege> privileges;
 
     public String toString () {
-        return "id=" + id + "name=" + name;
+        return "id=" + id + ", name=" + name;
     }
 }
diff --git a/full/src/main/java/de/ids_mannheim/korap/entity/UserGroup.java b/full/src/main/java/de/ids_mannheim/korap/entity/UserGroup.java
index 7dc49b8..1ff0425 100644
--- a/full/src/main/java/de/ids_mannheim/korap/entity/UserGroup.java
+++ b/full/src/main/java/de/ids_mannheim/korap/entity/UserGroup.java
@@ -13,9 +13,6 @@
 import javax.persistence.OneToMany;
 import javax.persistence.Table;
 
-import org.hibernate.annotations.Fetch;
-import org.hibernate.annotations.FetchMode;
-
 import de.ids_mannheim.korap.constant.UserGroupStatus;
 import lombok.Getter;
 import lombok.Setter;
@@ -48,12 +45,11 @@
     @Enumerated(EnumType.STRING)
     private UserGroupStatus status;
 
-    @Fetch(FetchMode.SELECT)
-    @OneToMany(mappedBy="group")//, fetch = FetchType.LAZY)
+    @OneToMany(mappedBy="group", fetch = FetchType.LAZY)
     List<UserGroupMember> members;
 
     @OneToMany(mappedBy = "userGroup", fetch = FetchType.LAZY)
-    private List<VirtualCorpusAccessGroup> virtualCorpusAccessGroup;
+    private List<VirtualCorpusAccess> virtualCorpusAccess;
 
 
     @Override
diff --git a/full/src/main/java/de/ids_mannheim/korap/entity/UserGroupMember.java b/full/src/main/java/de/ids_mannheim/korap/entity/UserGroupMember.java
index 696c5c7..a4e9598 100644
--- a/full/src/main/java/de/ids_mannheim/korap/entity/UserGroupMember.java
+++ b/full/src/main/java/de/ids_mannheim/korap/entity/UserGroupMember.java
@@ -1,6 +1,6 @@
 package de.ids_mannheim.korap.entity;
 
-import java.util.Set;
+import java.util.List;
 
 import javax.persistence.Column;
 import javax.persistence.Entity;
@@ -19,15 +19,17 @@
 import javax.persistence.UniqueConstraint;
 
 import de.ids_mannheim.korap.constant.GroupMemberStatus;
-import de.ids_mannheim.korap.exceptions.KustvaktException;
+import de.ids_mannheim.korap.constant.PredefinedRole;
 import lombok.Getter;
 import lombok.Setter;
 
-/** Describes members of user groups.
+/** Describes members of user groups. Only member of predefined role 
+ *  group admin can see the rest of members.
  * 
  *  @author margaretha
  *  @see UserGroup
  *  @see Role
+ *  @see PredefinedRole
  */
 @Setter
 @Getter
@@ -45,7 +47,7 @@
     private String createdBy;
     @Column(name = "deleted_by")
     private String deletedBy;
-    
+
     @Enumerated(EnumType.STRING)
     private GroupMemberStatus status;
 
@@ -53,7 +55,10 @@
     @JoinColumn(name = "group_id")
     private UserGroup group;
 
-    @ManyToMany(fetch = FetchType.LAZY)
+    /** Information about roles is deemed always necessary to describe a member.
+     * 
+     */
+    @ManyToMany(fetch = FetchType.EAGER)
     @JoinTable(name = "group_member_role",
             joinColumns = @JoinColumn(name = "group_member_id",
                     referencedColumnName = "id"),
@@ -61,23 +66,8 @@
                     referencedColumnName = "id"),
             uniqueConstraints = @UniqueConstraint(
                     columnNames = { "group_member_id", "role_id" }))
-    private Set<Role> roles;
+    private List<Role> roles;
 
-
-    public UserGroupMember createMember (String userId, String createdBy,
-            UserGroup userGroup, Set<Role> roles) throws KustvaktException {
-//        checkStringValue(userId, "userId");
-//        checkStringValue(createdBy, "createdBy");
-        
-        UserGroupMember member = new UserGroupMember();
-        member.setUserId(userId);
-        member.setCreatedBy(createdBy);
-        member.setGroup(userGroup);
-        member.setStatus(GroupMemberStatus.PENDING);
-        member.setRoles(roles);
-        return member;
-    }
-    
     @Override
     public String toString () {
         return "id=" + id + ", group= " + group + ", userId= " + userId
diff --git a/full/src/main/java/de/ids_mannheim/korap/entity/VirtualCorpus.java b/full/src/main/java/de/ids_mannheim/korap/entity/VirtualCorpus.java
index deccdb8..8413c24 100644
--- a/full/src/main/java/de/ids_mannheim/korap/entity/VirtualCorpus.java
+++ b/full/src/main/java/de/ids_mannheim/korap/entity/VirtualCorpus.java
@@ -14,6 +14,7 @@
 import javax.persistence.Table;
 
 import de.ids_mannheim.korap.constant.VirtualCorpusType;
+import de.ids_mannheim.korap.user.User.CorpusAccess;
 import lombok.Getter;
 import lombok.Setter;
 
@@ -25,7 +26,7 @@
  *   
  * @author margaretha
  *
- * @see VirtualCorpusAccessGroup
+ * @see VirtualCorpusAccess
  * @see UserGroup
  */
 @Setter
@@ -42,8 +43,9 @@
     private VirtualCorpusType type;
     private String status;
     private String description;
+    @Enumerated(EnumType.STRING)
     @Column(name = "required_access")
-    private String requiredAccess;
+    private CorpusAccess requiredAccess;
     @Column(name = "collection_query")
     private String collectionQuery;
     private String definition;
@@ -51,7 +53,7 @@
     private String createdBy;
 
     @OneToMany(mappedBy = "virtualCorpus", fetch=FetchType.LAZY)
-    private List<VirtualCorpusAccessGroup> accessGroup;
+    private List<VirtualCorpusAccess> virtualCorpusAccess;
 
     @Override
     public String toString () {
diff --git a/full/src/main/java/de/ids_mannheim/korap/entity/VirtualCorpusAccess.java b/full/src/main/java/de/ids_mannheim/korap/entity/VirtualCorpusAccess.java
index 6fbedf6..873aac7 100644
--- a/full/src/main/java/de/ids_mannheim/korap/entity/VirtualCorpusAccess.java
+++ b/full/src/main/java/de/ids_mannheim/korap/entity/VirtualCorpusAccess.java
@@ -28,7 +28,7 @@
 @Getter
 @Entity
 @Table(name = "virtual_corpus_access")
-public class VirtualCorpusAccessGroup {
+public class VirtualCorpusAccess {
 
     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
diff --git a/full/src/main/resources/db/insert/V3.1__insert_virtual_corpus.sql b/full/src/main/resources/db/insert/V3.1__insert_virtual_corpus.sql
index 442a39c..db77480 100644
--- a/full/src/main/resources/db/insert/V3.1__insert_virtual_corpus.sql
+++ b/full/src/main/resources/db/insert/V3.1__insert_virtual_corpus.sql
@@ -2,16 +2,16 @@
 
 -- user groups
 INSERT INTO user_group(name,status,created_by) 
-	VALUES ("dory group","active","dory");
+	VALUES ("dory group","ACTIVE","dory");
 
 INSERT INTO user_group(name,status,created_by) 
-	VALUES ("auto group","hidden","system");
+	VALUES ("auto group","HIDDEN","system");
 
 INSERT INTO user_group(name,status,created_by) 
-	VALUES ("all users","hidden","system");
+	VALUES ("all users","HIDDEN","system");
 
 INSERT INTO user_group(name,status,created_by) 
-	VALUES ("deleted group","deleted","dory");
+	VALUES ("deleted group","DELETED","dory");
 
 
 
diff --git a/full/src/main/resources/db/insert/V3.3__insert_member_roles.sql b/full/src/main/resources/db/insert/V3.3__insert_member_roles.sql
new file mode 100644
index 0000000..459b5e2
--- /dev/null
+++ b/full/src/main/resources/db/insert/V3.3__insert_member_roles.sql
@@ -0,0 +1,28 @@
+-- member roles
+-- dory group
+INSERT INTO group_member_role(group_member_id,role_id)
+SELECT
+	(SELECT id FROM user_group_member WHERE user_id="dory" AND group_id=1),
+	(SELECT id FROM role WHERE name = "group admin");
+	
+INSERT INTO group_member_role(group_member_id,role_id)
+SELECT
+	(SELECT id FROM user_group_member WHERE user_id="dory" AND group_id=1),
+	(SELECT id FROM role WHERE name = "vc admin");
+	
+INSERT INTO group_member_role(group_member_id,role_id)
+SELECT
+	(SELECT id FROM user_group_member WHERE user_id="nemo" AND group_id=1),
+	(SELECT id FROM role WHERE name = "group member");
+	
+INSERT INTO group_member_role(group_member_id,role_id)
+SELECT
+	(SELECT id FROM user_group_member WHERE user_id="nemo" AND group_id=1),
+	(SELECT id FROM role WHERE name = "vc member");
+
+-- auto group
+INSERT INTO group_member_role(group_member_id,role_id)
+SELECT
+	(SELECT id FROM user_group_member WHERE user_id="pearl" AND group_id=2),
+	(SELECT id FROM role WHERE name = "vc member");
+
diff --git a/full/src/main/resources/db/new-sqlite/V1.1__create_virtual_corpus_tables.sql b/full/src/main/resources/db/new-sqlite/V1.1__create_virtual_corpus_tables.sql
index 27fb035..91b01a7 100644
--- a/full/src/main/resources/db/new-sqlite/V1.1__create_virtual_corpus_tables.sql
+++ b/full/src/main/resources/db/new-sqlite/V1.1__create_virtual_corpus_tables.sql
@@ -21,7 +21,8 @@
   id INTEGER PRIMARY KEY AUTOINCREMENT,
   name varchar(100) NOT NULL,
   status varchar(100) NOT NULL,
-  created_by varchar(100) NOT NULL
+  created_by varchar(100) NOT NULL,
+  deleted_by varchar(100) DEFAULT NULL
 );
 
 CREATE INDEX user_group_index ON user_group(status);
diff --git a/full/src/main/resources/db/predefined/V3.2__insert_predefined_roles.sql b/full/src/main/resources/db/predefined/V3.2__insert_predefined_roles.sql
index fdb8031..c9409ea 100644
--- a/full/src/main/resources/db/predefined/V3.2__insert_predefined_roles.sql
+++ b/full/src/main/resources/db/predefined/V3.2__insert_predefined_roles.sql
@@ -1,8 +1,8 @@
 -- roles
 INSERT INTO role(name) VALUES ("group admin");
 INSERT INTO role(name) VALUES ("group member");
-INSERT INTO role(name) VALUES ("VC admin");
-INSERT INTO role(name) VALUES ("VC member");
+INSERT INTO role(name) VALUES ("vc admin");
+INSERT INTO role(name) VALUES ("vc member");
 
 -- privileges
 INSERT INTO privilege(name,role_id)
diff --git a/full/src/main/resources/jdbc.properties b/full/src/main/resources/jdbc.properties
index 65fba6b..9b888f0 100644
--- a/full/src/main/resources/jdbc.properties
+++ b/full/src/main/resources/jdbc.properties
@@ -9,7 +9,7 @@
 #jdbc.password=password
 
 # db.insert contains test data, omit it in production setting
-#jdbc.schemaPath=db.new-mysql, db.insert
+#jdbc.schemaPath=db.new-mysql, db.insert, db.predefined
 
 
 #-------------------------------------------------------------------------------
@@ -22,4 +22,4 @@
 jdbc.username=pc
 jdbc.password=pc
 # db.insert contains test data, omit it in production setting
-jdbc.schemaPath=db.new-sqlite, db.insert
+jdbc.schemaPath=db.new-sqlite, db.insert, db.predefined