Defined relationships between entities and added indexes & comments.

Change-Id: I8a176d141eef0349e1b3d819f849488c94f0cb9c
diff --git a/full/src/main/java/de/ids_mannheim/korap/dto/ResourceDto.java b/full/src/main/java/de/ids_mannheim/korap/dto/ResourceDto.java
index acf2001..8b03ec9 100644
--- a/full/src/main/java/de/ids_mannheim/korap/dto/ResourceDto.java
+++ b/full/src/main/java/de/ids_mannheim/korap/dto/ResourceDto.java
@@ -1,6 +1,5 @@
 package de.ids_mannheim.korap.dto;
 
-import java.util.List;
 import java.util.Map;
 
 import lombok.Getter;
diff --git a/full/src/main/java/de/ids_mannheim/korap/entity/Annotation.java b/full/src/main/java/de/ids_mannheim/korap/entity/Annotation.java
index 1fbe3fb..fb6939f 100644
--- a/full/src/main/java/de/ids_mannheim/korap/entity/Annotation.java
+++ b/full/src/main/java/de/ids_mannheim/korap/entity/Annotation.java
@@ -10,6 +10,12 @@
 import lombok.Getter;
 import lombok.Setter;
 
+/** Describes annotation tags available in the system / used in 
+ *  annotating corpus data. 
+ * 
+ *  @author margaretha
+ *
+ */
 @Setter
 @Getter
 @Entity
diff --git a/full/src/main/java/de/ids_mannheim/korap/entity/AnnotationPair.java b/full/src/main/java/de/ids_mannheim/korap/entity/AnnotationPair.java
index ca580c3..fdd0ea8 100644
--- a/full/src/main/java/de/ids_mannheim/korap/entity/AnnotationPair.java
+++ b/full/src/main/java/de/ids_mannheim/korap/entity/AnnotationPair.java
@@ -21,11 +21,18 @@
 import lombok.Getter;
 import lombok.Setter;
 
+/** Describes annotations as a pair, e.g. foundry and layer where 
+ *  foundry denotes where the annotation comes from e.g. Tree tagger 
+ *  parser, and layer denotes the annotation layer e.g. part of speech.
+ * 
+ *  @author margaretha
+ *  @see Annotation
+ */
 @Setter
 @Getter
 @Entity
-@Table(name = "annotation_pair", 
-    uniqueConstraints=@UniqueConstraint(columnNames={"annotation1","annotation2"}))
+@Table(name = "annotation_pair", uniqueConstraints = @UniqueConstraint(
+        columnNames = { "annotation1", "annotation2" }))
 public class AnnotationPair {
     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
@@ -41,28 +48,29 @@
     @ManyToOne //(fetch=FetchType.LAZY) 
     @JoinColumn(name = "annotation1", insertable = false, updatable = false)
     private Annotation annotation1;
-    
+
     @Fetch(FetchMode.SELECT)
     @ManyToOne //(fetch=FetchType.LAZY) 
     @JoinColumn(name = "annotation2", insertable = false, updatable = false)
     private Annotation annotation2;
 
-    @ManyToMany(fetch=FetchType.LAZY) //(fetch=FetchType.EAGER)
-    @JoinTable(
-            name="annotation_pair_value",
-            joinColumns=@JoinColumn(name="pair_id", referencedColumnName="id"),
-            inverseJoinColumns=@JoinColumn(name="value_id", referencedColumnName="id"),
-            uniqueConstraints = @UniqueConstraint(columnNames = {
-                            "pair_id", "value_id" })
-    )
+    @ManyToMany(fetch = FetchType.LAZY) //(fetch=FetchType.EAGER)
+    @JoinTable(name = "annotation_pair_value",
+            joinColumns = @JoinColumn(name = "pair_id",
+                    referencedColumnName = "id"),
+            inverseJoinColumns = @JoinColumn(name = "value_id",
+                    referencedColumnName = "id"),
+            uniqueConstraints = @UniqueConstraint(
+                    columnNames = { "pair_id", "value_id" }))
     private Set<Annotation> values;
 
+
     @Override
     public String toString () {
         return "id=" + id + ", annotation1=" + annotationId1 + ", annotation2="
                 + annotationId1 + ", description=" + description
-                + ", annotation1= "+ annotation1
-                + ", annotation2= "+ annotation2;
-                
+                + ", annotation1= " + annotation1 + ", annotation2= "
+                + annotation2;
+
     }
 }
diff --git a/full/src/main/java/de/ids_mannheim/korap/entity/Privilege.java b/full/src/main/java/de/ids_mannheim/korap/entity/Privilege.java
new file mode 100644
index 0000000..dd9fbd1
--- /dev/null
+++ b/full/src/main/java/de/ids_mannheim/korap/entity/Privilege.java
@@ -0,0 +1,12 @@
+package de.ids_mannheim.korap.entity;
+
+/** Simple definitions of privileges.
+ *  When needed can be adopted into a database table. 
+ * 
+ * @author margaretha
+ *
+ */
+public enum Privilege {
+    
+    READ, WRITE, DELETE;
+}
diff --git a/full/src/main/java/de/ids_mannheim/korap/entity/Resource.java b/full/src/main/java/de/ids_mannheim/korap/entity/Resource.java
index 18cec9e..f08b5b3 100644
--- a/full/src/main/java/de/ids_mannheim/korap/entity/Resource.java
+++ b/full/src/main/java/de/ids_mannheim/korap/entity/Resource.java
@@ -14,6 +14,12 @@
 import lombok.Getter;
 import lombok.Setter;
 
+/** Describes resources having free licenses. Primarily for accommodating 
+ *  clients in providing data without login such as KorapSRU. 
+ *  
+ * @author margaretha
+ *
+ */
 @Getter
 @Setter
 @Entity
@@ -28,22 +34,24 @@
 
     @Column(name = "en_title")
     private String englishTitle;
-    
+
     @Column(name = "en_description")
     private String englishDescription;
 
-    @ManyToMany(fetch=FetchType.LAZY)
-    @JoinTable(
-            name="resource_layer",
-            joinColumns=@JoinColumn(name="resource_id", referencedColumnName="id"),
-            inverseJoinColumns=@JoinColumn(name="layer_id", referencedColumnName="id")
-    )
+    @ManyToMany(fetch = FetchType.LAZY)
+    @JoinTable(name = "resource_layer",
+            joinColumns = @JoinColumn(name = "resource_id",
+                    referencedColumnName = "id"),
+            inverseJoinColumns = @JoinColumn(name = "layer_id",
+                    referencedColumnName = "id"))
     private Set<AnnotationPair> layers;
-    
+
+
     @Override
     public String toString () {
         return "id=" + id + ", germanTitle=" + germanTitle + ", englishTitle="
-                + englishTitle + ", description="+englishDescription+ ", layers= "+layers;
+                + englishTitle + ", description=" + englishDescription
+                + ", layers= " + layers;
     }
 
 }
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
new file mode 100644
index 0000000..7461fda
--- /dev/null
+++ b/full/src/main/java/de/ids_mannheim/korap/entity/Role.java
@@ -0,0 +1,37 @@
+package de.ids_mannheim.korap.entity;
+
+import java.util.List;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.Id;
+import javax.persistence.ManyToMany;
+import javax.persistence.Table;
+
+import lombok.Getter;
+import lombok.Setter;
+
+/** Describes user roles for example in managing a group or 
+ *  virtual corpora of a group.
+ *  
+ * @author margaretha
+ * @see Privilege
+ */
+@Setter
+@Getter
+@Entity
+@Table(name = "role")
+public class Role {
+    @Id
+    @Column(unique = true)
+    private String id;
+    private String privilege;
+
+    @ManyToMany(mappedBy = "roles", fetch = FetchType.LAZY)
+    private List<UserGroupMember> userGroupMembers;
+
+    public String toString () {
+        return "id=" + id + ", privilege= " + privilege;
+    }
+}
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 91cf76f..7f29477 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
@@ -1,15 +1,28 @@
 package de.ids_mannheim.korap.entity;
 
+import java.util.List;
+
 import javax.persistence.Column;
 import javax.persistence.Entity;
+import javax.persistence.FetchType;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
+import javax.persistence.OneToMany;
 import javax.persistence.Table;
 
 import lombok.Getter;
 import lombok.Setter;
 
+/** Describes user groups.
+ * 
+ * Any user may create a user group and send invitations to group 
+ * member by username. Any group member may reject the invitation
+ * or unsubscribe from the group.
+ * 
+ * @author margaretha
+ * @see UserGroupMember
+ */
 @Setter
 @Getter
 @Entity
@@ -21,12 +34,18 @@
     private int id;
     @Column(name = "name")
     private String name;
-    @Column(name = "owner_id")
-    private int ownerId;
-    
+    @Column(name = "created_by")
+    private String createdBy;
+
+    @OneToMany(fetch = FetchType.LAZY)
+    List<UserGroupMember> members;
+
+    @OneToMany(mappedBy = "virtualCorpus", fetch = FetchType.LAZY)
+    private List<VirtualCorpusAccessGroup> virtualCorpusAccessGroup;
+
+
     @Override
     public String toString () {
-        return "id=" + id + ", name= " + name + ", ownerId= "
-                + ownerId;
+        return "id=" + id + ", name= " + name + ", createdBy= " + createdBy;
     }
 }
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 9f1389f..6abe32b 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,33 +1,67 @@
 package de.ids_mannheim.korap.entity;
 
+import java.util.Set;
+
 import javax.persistence.Column;
 import javax.persistence.Entity;
+import javax.persistence.FetchType;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
+import javax.persistence.Index;
+import javax.persistence.JoinColumn;
+import javax.persistence.JoinTable;
+import javax.persistence.ManyToMany;
+import javax.persistence.ManyToOne;
 import javax.persistence.Table;
+import javax.persistence.UniqueConstraint;
 
 import lombok.Getter;
 import lombok.Setter;
 
+/** Describes members of user groups.
+ * 
+ *  @author margaretha
+ *  @see UserGroup
+ *  @see Role
+ */
 @Setter
 @Getter
 @Entity
-@Table(name = "user_group_member")
+@Table(name = "user_group_member",
+        indexes = { @Index(unique = true, columnList = "user_id, group_id") })
 public class UserGroupMember {
 
     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     private int id;
-    @Column(name = "group_id")
-    private int groupId;
     @Column(name = "user_id")
     private int userId;
     private String status;
-    
+    @Column(name = "created_by")
+    private String createdBy;
+    @Column(name = "deleted_by")
+    private String deletedBy;
+
+    @ManyToOne(fetch = FetchType.LAZY)
+    @JoinColumn(name = "group_id")
+    private UserGroup group;
+
+    @ManyToMany(fetch = FetchType.LAZY)
+    @JoinTable(name = "group_member_role",
+            joinColumns = @JoinColumn(name = "group_member_id",
+                    referencedColumnName = "id"),
+            inverseJoinColumns = @JoinColumn(name = "role_id",
+                    referencedColumnName = "id"),
+            uniqueConstraints = @UniqueConstraint(
+                    columnNames = { "group_member_id", "role_id" }))
+    private Set<Role> roles;
+
+
     @Override
     public String toString () {
-        return "id=" + id + ", groupId= " + groupId + ", userId= "
-                + userId;
+        return "id=" + id + ", group= " + group + ", userId= " + userId
+                + ", createdBy= " + createdBy + ", deletedBy= " + deletedBy
+                + ", roles=" + roles;
     }
 }
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 dd050fc..b100600 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
@@ -1,15 +1,29 @@
 package de.ids_mannheim.korap.entity;
 
+import java.util.List;
+
 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
+import javax.persistence.OneToMany;
 import javax.persistence.Table;
 
 import lombok.Getter;
 import lombok.Setter;
 
+/** Describes virtual corpora.
+ * 
+ *  Any user may create a virtual corpus and share it to a user group.
+ *  However, if the user is not a user-group admin, the virtual corpus 
+ *  will not be shared until a user-group admin accept his/her request.
+ *   
+ * @author margaretha
+ *
+ * @see VirtualCorpusAccessGroup
+ * @see UserGroup
+ */
 @Setter
 @Getter
 @Entity
@@ -28,8 +42,11 @@
     @Column(name = "collection_query")
     private String collectionQuery;
     private String definition;
-    @Column(name = "owner_id")
-    private String ownerId;
+    @Column(name = "created_by")
+    private String createdBy;
+
+    @OneToMany(mappedBy = "userGroup")
+    List<VirtualCorpusAccessGroup> accessGroup;
 
 
     @Override
@@ -37,6 +54,6 @@
         return "id=" + id + ", name= " + name + ", type= " + type + ", status= "
                 + status + ", description=" + description + ", requiredAccess="
                 + requiredAccess + ", collectionQuery= " + collectionQuery
-                + ", definition= " + definition + ", ownerId= " + ownerId;
+                + ", definition= " + definition + ", createdBy= " + createdBy;
     }
 }
diff --git a/full/src/main/java/de/ids_mannheim/korap/entity/VirtualCorpusAccessGroup.java b/full/src/main/java/de/ids_mannheim/korap/entity/VirtualCorpusAccessGroup.java
index 9b4ba9a..eb1fcb1 100644
--- a/full/src/main/java/de/ids_mannheim/korap/entity/VirtualCorpusAccessGroup.java
+++ b/full/src/main/java/de/ids_mannheim/korap/entity/VirtualCorpusAccessGroup.java
@@ -5,29 +5,53 @@
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
+import javax.persistence.Index;
+import javax.persistence.ManyToOne;
+import javax.persistence.PrimaryKeyJoinColumn;
 import javax.persistence.Table;
 
 import lombok.Getter;
 import lombok.Setter;
 
+/** Describes the relationship between virtual corpora and user groups, 
+ *  i.e. which groups may access which virtual corpora, and the history 
+ *  of group-access management.  
+ * 
+ * @author margaretha
+ * @see VirtualCorpus
+ * @see UserGroup
+ */
 @Setter
 @Getter
 @Entity
-@Table(name = "vc_access_group")
+@Table(name = "vc_access_group", indexes = {
+        @Index(unique = true, columnList = "virtual_corpus_id, group_id") })
 public class VirtualCorpusAccessGroup {
 
     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     private int id;
-    @Column(name = "vc_id")
-    private int virtualCorpusId;
-    @Column(name = "group_id")
-    private int groupId;
+    private String status;
+    @Column(name = "created_by")
+    private String createdBy;
+    @Column(name = "approved_by")
+    private String approvedBy;
+    @Column(name = "deleted_by")
+    private String deletedBy;
+
+    @ManyToOne
+    @PrimaryKeyJoinColumn(name = "virtual_corpus_id",
+            referencedColumnName = "id")
+    private VirtualCorpus virtualCorpus;
+
+    @ManyToOne
+    @PrimaryKeyJoinColumn(name = "group_id", referencedColumnName = "id")
+    private UserGroup userGroup;
 
 
     @Override
     public String toString () {
-        return "id=" + id + ", virtualCorpusId= " + virtualCorpusId + ", groupId= "
-                + groupId;
+        return "id=" + id + ", virtualCorpus= " + virtualCorpus
+                + ", userGroup= " + userGroup;
     }
 }