Fixed delete-on-cascade on one-to-many relations.

Change-Id: Ib5332cb04c36bb3a7ccf7b3896ecb4f59823248f
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 2d2571e..2058abf 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
@@ -2,6 +2,7 @@
 
 import java.util.List;
 
+import javax.persistence.CascadeType;
 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.FetchType;
@@ -29,13 +30,14 @@
     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     private int id;
-    @Column(unique=true)
+    @Column(unique = true)
     private String name;
 
     @ManyToMany(mappedBy = "roles", fetch = FetchType.LAZY)
     private List<UserGroupMember> userGroupMembers;
 
-    @OneToMany(mappedBy = "role", fetch = FetchType.EAGER)
+    @OneToMany(mappedBy = "role", fetch = FetchType.EAGER,
+            cascade = CascadeType.REMOVE)
     private List<Privilege> privileges;
 
     public String toString () {
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 1ff0425..9034ffa 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
@@ -2,6 +2,7 @@
 
 import java.util.List;
 
+import javax.persistence.CascadeType;
 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.EnumType;
@@ -41,14 +42,16 @@
     private String createdBy;
     @Column(name = "deleted_by")
     private String deletedBy;
-    
+
     @Enumerated(EnumType.STRING)
     private UserGroupStatus status;
 
-    @OneToMany(mappedBy="group", fetch = FetchType.LAZY)
-    List<UserGroupMember> members;
+    @OneToMany(mappedBy = "group", fetch = FetchType.LAZY,
+            cascade = CascadeType.REMOVE)
+    private List<UserGroupMember> members;
 
-    @OneToMany(mappedBy = "userGroup", fetch = FetchType.LAZY)
+    @OneToMany(mappedBy = "userGroup", fetch = FetchType.LAZY,
+            cascade = CascadeType.REMOVE)
     private List<VirtualCorpusAccess> virtualCorpusAccess;
 
 
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 e62af0c..f3060ba 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
@@ -2,6 +2,7 @@
 
 import java.util.List;
 
+import javax.persistence.CascadeType;
 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.EnumType;
@@ -52,7 +53,8 @@
     @Column(name = "created_by")
     private String createdBy;
 
-    @OneToMany(mappedBy = "virtualCorpus", fetch=FetchType.LAZY)
+    @OneToMany(mappedBy = "virtualCorpus", fetch = FetchType.LAZY,
+            cascade = CascadeType.REMOVE)
     private List<VirtualCorpusAccess> virtualCorpusAccess;
 
     @Override
@@ -62,7 +64,7 @@
                 + requiredAccess + ", corpusQuery= " + corpusQuery
                 + ", definition= " + definition + ", createdBy= " + createdBy;
     }
-    
+
     @Override
     public int hashCode () {
         int prime = 37;
@@ -72,7 +74,7 @@
         result = prime * result + createdBy.hashCode();
         return result;
     }
-    
+
     @Override
     public boolean equals (Object obj) {
         VirtualCorpus vc = (VirtualCorpus) obj;
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 c3bba6e..ba80f59 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
@@ -43,12 +43,11 @@
     @Enumerated(EnumType.STRING)
     private VirtualCorpusAccessStatus status;
 
-    @ManyToOne(fetch=FetchType.EAGER)
-    @JoinColumn(name = "virtual_corpus_id",
-            referencedColumnName = "id")
+    @ManyToOne(fetch = FetchType.EAGER)
+    @JoinColumn(name = "virtual_corpus_id", referencedColumnName = "id")
     private VirtualCorpus virtualCorpus;
 
-    @ManyToOne(fetch=FetchType.EAGER)
+    @ManyToOne(fetch = FetchType.EAGER)
     @JoinColumn(name = "user_group_id", referencedColumnName = "id")
     private UserGroup userGroup;
 
diff --git a/full/src/main/java/de/ids_mannheim/korap/service/VirtualCorpusService.java b/full/src/main/java/de/ids_mannheim/korap/service/VirtualCorpusService.java
index dcaaeb0..e7dd923 100644
--- a/full/src/main/java/de/ids_mannheim/korap/service/VirtualCorpusService.java
+++ b/full/src/main/java/de/ids_mannheim/korap/service/VirtualCorpusService.java
@@ -231,8 +231,9 @@
 
     private void publishVC (int vcId) throws KustvaktException {
 
+        VirtualCorpusAccess access = accessDao.retrieveHiddenAccess(vcId);
         // check if hidden access exists
-        if (accessDao.retrieveHiddenAccess(vcId) != null) {
+        if (access == null) {
             VirtualCorpus vc = vcDao.retrieveVCById(vcId);
             // create and assign a hidden group
             int groupId = userGroupService.createAutoHiddenGroup(vcId);
diff --git a/full/src/test/java/de/ids_mannheim/korap/service/VirtualCorpusServiceTest.java b/full/src/test/java/de/ids_mannheim/korap/service/VirtualCorpusServiceTest.java
index c2ca100..4576ddf 100644
--- a/full/src/test/java/de/ids_mannheim/korap/service/VirtualCorpusServiceTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/service/VirtualCorpusServiceTest.java
@@ -25,19 +25,25 @@
 
     @Test
     public void createPublishVC () throws KustvaktException {
-
+        String username = "VirtualCorpusServiceTest";
+        
         VirtualCorpusJson vc = new VirtualCorpusJson();
         vc.setCorpusQuery("corpusSigle=GOE");
-        vc.setCreatedBy("VirtualCorpusServiceTest");
+        vc.setCreatedBy(username);
         vc.setName("new published vc");
         vc.setType(VirtualCorpusType.PUBLISHED);
         int vcId = vcService.storeVC(vc, "VirtualCorpusServiceTest");
 
         List<VirtualCorpusAccess> accesses = vcService.retrieveAllVCAccess(vcId);
-        assertEquals(2, accesses.size());
-        for (VirtualCorpusAccess access : accesses) {
-            assertEquals(VirtualCorpusAccessStatus.HIDDEN, access.getStatus());
-        }
+        assertEquals(1, accesses.size());
+        
+        VirtualCorpusAccess access = accesses.get(0);
+        assertEquals(VirtualCorpusAccessStatus.HIDDEN, access.getStatus());
+        
+        // delete VC
+        vcService.deleteVC(username, vcId);
+        accesses = vcService.retrieveAllVCAccess(vcId);
+        assertEquals(0, accesses.size());
     }
 
 }
diff --git a/full/src/test/java/de/ids_mannheim/korap/web/controller/VirtualCorpusControllerTest.java b/full/src/test/java/de/ids_mannheim/korap/web/controller/VirtualCorpusControllerTest.java
index 1e48889..8f49630 100644
--- a/full/src/test/java/de/ids_mannheim/korap/web/controller/VirtualCorpusControllerTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/web/controller/VirtualCorpusControllerTest.java
@@ -68,7 +68,7 @@
                 .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32")
                 .get(ClientResponse.class);
         String entity = response.getEntity(String.class);
-
+//        System.out.println(entity);
         assertEquals(Status.OK.getStatusCode(), response.getStatus());
 
         JsonNode node = JsonUtils.readTree(entity);
@@ -715,11 +715,17 @@
     }
 
     // share VC
-    @Test
-    public void testCreateDeleteAccess () {
-        // TODO Auto-generated method stub
-
-    }
+//    @Test
+//    public void testCreateDeleteAccess () {
+//        // create VC
+//        
+//        // create group
+//        
+//        // share VC
+//        
+//        // delete 
+//
+//    }
 
 
 }