From 0296407ab2b16d529e990dca1601e53b4b6ada01 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Sven=20K=C3=A4stle?= <sven.kaestle@gmx.de>
Date: Thu, 6 Sep 2018 16:30:01 +0200
Subject: [PATCH] feat: Add 'targetCategory' to annotation object

---
 .../unipotsdam/gf/interfaces/IAnnotation.java | 10 +++++----
 .../controller/AnnotationController.java      | 14 +++++++------
 .../modules/annotation/model/Annotation.java  | 15 ++++++++++++-
 .../annotation/model/AnnotationMessage.java   | 13 ++++++++++++
 .../model/AnnotationPostRequest.java          | 19 ++++++++++++++---
 .../annotation/view/AnnotationService.java    |  7 ++++---
 .../src/scripts/dbschema/fltrail.sql          |  1 +
 .../gf/interfaces/AnnotationTest.java         | 21 ++++++++++---------
 8 files changed, 73 insertions(+), 27 deletions(-)

diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/interfaces/IAnnotation.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/interfaces/IAnnotation.java
index d8957a0d..9ae417fd 100644
--- a/gemeinsamforschen/src/main/java/unipotsdam/gf/interfaces/IAnnotation.java
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/interfaces/IAnnotation.java
@@ -3,6 +3,7 @@ package unipotsdam.gf.interfaces;
 import unipotsdam.gf.modules.annotation.model.Annotation;
 import unipotsdam.gf.modules.annotation.model.AnnotationPatchRequest;
 import unipotsdam.gf.modules.annotation.model.AnnotationPostRequest;
+import unipotsdam.gf.modules.peer2peerfeedback.Category;
 
 import java.util.ArrayList;
 
@@ -44,12 +45,13 @@ public interface IAnnotation {
     Annotation getAnnotation(String annotationId);
 
     /**
-     * Returns all annotations from a target
+     * Returns all annotations for a specific target id and category
      *
-     * @param targetId the target id
-     * @return Returns all annotations
+     * @param targetId The if of the target
+     * @param targetCategory The category of the target
+     * @return Returns all annotations for a target
      */
-    ArrayList<Annotation> getAnnotations(int targetId);
+    ArrayList<Annotation> getAnnotations(int targetId, Category targetCategory);
 
     /**
      * Checks if an annotation id already exists in the database
diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/controller/AnnotationController.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/controller/AnnotationController.java
index ce29362c..91f9625c 100644
--- a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/controller/AnnotationController.java
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/controller/AnnotationController.java
@@ -7,6 +7,7 @@ import unipotsdam.gf.modules.annotation.model.Annotation;
 import unipotsdam.gf.modules.annotation.model.AnnotationBody;
 import unipotsdam.gf.modules.annotation.model.AnnotationPatchRequest;
 import unipotsdam.gf.modules.annotation.model.AnnotationPostRequest;
+import unipotsdam.gf.modules.peer2peerfeedback.Category;
 
 import java.time.ZonedDateTime;
 import java.util.ArrayList;
@@ -27,8 +28,8 @@ public class AnnotationController implements IAnnotation {
         connection.connect();
 
         // build and execute request
-        String request = "INSERT INTO annotations (`id`, `userToken`, `targetId`, `title`, `comment`, `startCharacter`, `endCharacter`) VALUES (?,?,?,?,?,?,?);";
-        connection.issueInsertOrDeleteStatement(request, uuid, annotationPostRequest.getUserToken(), annotationPostRequest.getTargetId(), annotationPostRequest.getBody().getTitle(), annotationPostRequest.getBody().getComment(),annotationPostRequest.getBody().getStartCharacter(), annotationPostRequest.getBody().getEndCharacter());
+        String request = "INSERT INTO annotations (`id`, `userToken`, `targetId`, `targetCategory`, `title`, `comment`, `startCharacter`, `endCharacter`) VALUES (?,?,?,?,?,?,?,?);";
+        connection.issueInsertOrDeleteStatement(request, uuid, annotationPostRequest.getUserToken(), annotationPostRequest.getTargetId(), annotationPostRequest.getTargetCategory().toString().toUpperCase(), annotationPostRequest.getBody().getTitle(), annotationPostRequest.getBody().getComment(),annotationPostRequest.getBody().getStartCharacter(), annotationPostRequest.getBody().getEndCharacter());
 
         // close connection
         connection.close();
@@ -104,7 +105,7 @@ public class AnnotationController implements IAnnotation {
     }
 
     @Override
-    public ArrayList<Annotation> getAnnotations(int targetId) {
+    public ArrayList<Annotation> getAnnotations(int targetId, Category category) {
 
         // declare annotation ArrayList
         ArrayList<Annotation> annotations = new ArrayList<>();
@@ -114,8 +115,8 @@ public class AnnotationController implements IAnnotation {
         connection.connect();
 
         // build and execute request
-        String request = "SELECT * FROM annotations WHERE targetId = ?;";
-        VereinfachtesResultSet rs = connection.issueSelectStatement(request, targetId);
+        String request = "SELECT * FROM annotations WHERE targetId = ? AND targetCategory = ?;";
+        VereinfachtesResultSet rs = connection.issueSelectStatement(request, targetId, category.toString().toUpperCase());
 
         while (rs.next()) {
             annotations.add(getAnnotationFromResultSet(rs));
@@ -172,6 +173,7 @@ public class AnnotationController implements IAnnotation {
         long timestamp = rs.getTimestamp(2).getTime();
         String userToken = rs.getString("userToken");
         int targetId = rs.getInt("targetId");
+        Category targetCategory = Category.valueOf(rs.getString("targetCategory"));
 
         // initialize new annotation body
         String title = rs.getString("title");
@@ -180,7 +182,7 @@ public class AnnotationController implements IAnnotation {
         int endCharacter = rs.getInt("endCharacter");
         AnnotationBody body = new AnnotationBody(title, comment, startCharacter, endCharacter);
 
-        return new Annotation(id, timestamp, userToken, targetId, body);
+        return new Annotation(id, timestamp, userToken, targetId, targetCategory, body);
 
     }
 }
diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/model/Annotation.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/model/Annotation.java
index 699ec3ed..13dbd820 100644
--- a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/model/Annotation.java
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/model/Annotation.java
@@ -1,5 +1,7 @@
 package unipotsdam.gf.modules.annotation.model;
 
+import unipotsdam.gf.modules.peer2peerfeedback.Category;
+
 /**
  * @author Sven Kästle
  * skaestle@uni-potsdam.de
@@ -11,14 +13,16 @@ public class Annotation {
     private long timestamp;
     private String userToken;
     private int targetId;
+    private Category targetCategory;
     private AnnotationBody body;
 
     // constructor
-    public Annotation(String id, long timestamp, String userToken, int targetId, AnnotationBody body) {
+    public Annotation(String id, long timestamp, String userToken, int targetId, Category targetCategory, AnnotationBody body) {
         this.id = id;
         this.timestamp = timestamp;
         this.userToken = userToken;
         this.targetId = targetId;
+        this.targetCategory = targetCategory;
         this.body = body;
     }
 
@@ -55,6 +59,14 @@ public class Annotation {
         this.targetId = targetId;
     }
 
+    public Category getTargetCategory() {
+        return targetCategory;
+    }
+
+    public void setTargetCategory(Category targetCategory) {
+        this.targetCategory = targetCategory;
+    }
+
     public AnnotationBody getBody() {
         return body;
     }
@@ -70,6 +82,7 @@ public class Annotation {
                 ", timestamp=" + timestamp +
                 ", userToken='" + userToken + '\'' +
                 ", targetId=" + targetId +
+                ", targetCategory=" + targetCategory +
                 ", body=" + body +
                 '}';
     }
diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/model/AnnotationMessage.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/model/AnnotationMessage.java
index 60d895b6..f8e3f913 100644
--- a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/model/AnnotationMessage.java
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/model/AnnotationMessage.java
@@ -1,9 +1,12 @@
 package unipotsdam.gf.modules.annotation.model;
 
+import unipotsdam.gf.modules.peer2peerfeedback.Category;
+
 public class AnnotationMessage {
     // variables
     private String from;
     private String targetId;
+    private Category targetCategory;
     private AnnotationMessageType type;
     private String annotationId;
 
@@ -30,6 +33,14 @@ public class AnnotationMessage {
         this.targetId = targetId;
     }
 
+    public Category getTargetCategory() {
+        return targetCategory;
+    }
+
+    public void setTargetCategory(Category targetCategory) {
+        this.targetCategory = targetCategory;
+    }
+
     public AnnotationMessageType getType() {
         return type;
     }
@@ -51,8 +62,10 @@ public class AnnotationMessage {
         return "AnnotationMessage{" +
                 "from='" + from + '\'' +
                 ", targetId='" + targetId + '\'' +
+                ", targetCategory=" + targetCategory +
                 ", type=" + type +
                 ", annotationId='" + annotationId + '\'' +
                 '}';
     }
+
 }
diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/model/AnnotationPostRequest.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/model/AnnotationPostRequest.java
index 5dfe6e20..f4967695 100644
--- a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/model/AnnotationPostRequest.java
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/model/AnnotationPostRequest.java
@@ -1,5 +1,7 @@
 package unipotsdam.gf.modules.annotation.model;
 
+import unipotsdam.gf.modules.peer2peerfeedback.Category;
+
 /**
  * @author Sven Kästle
  * skaestle@uni-potsdam.de
@@ -9,12 +11,14 @@ public class AnnotationPostRequest {
     // variables
     private String userToken;
     private int targetId;
+    private Category targetCategory;
     private AnnotationBody body;
 
     // constructors
-    public AnnotationPostRequest(String userToken, int targetId, AnnotationBody body) {
+    public AnnotationPostRequest(String userToken, int targetId, Category targetCategory, AnnotationBody body) {
         this.userToken = userToken;
         this.targetId = targetId;
+        this.targetCategory = targetCategory;
         this.body = body;
     }
 
@@ -38,6 +42,14 @@ public class AnnotationPostRequest {
         this.targetId = targetId;
     }
 
+    public Category getTargetCategory() {
+        return targetCategory;
+    }
+
+    public void setTargetCategory(Category targetCategory) {
+        this.targetCategory = targetCategory;
+    }
+
     public AnnotationBody getBody() {
         return body;
     }
@@ -51,8 +63,9 @@ public class AnnotationPostRequest {
         return "AnnotationPostRequest{" +
                 "userToken='" + userToken + '\'' +
                 ", targetId=" + targetId +
-                ", body=" + body.toString() +
+                ", targetCategory=" + targetCategory +
+                ", body=" + body +
                 '}';
     }
-
+    
 }
diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/view/AnnotationService.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/view/AnnotationService.java
index 1072cd55..25049551 100644
--- a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/view/AnnotationService.java
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/view/AnnotationService.java
@@ -6,6 +6,7 @@ import unipotsdam.gf.modules.annotation.model.Annotation;
 import unipotsdam.gf.modules.annotation.model.AnnotationPatchRequest;
 import unipotsdam.gf.modules.annotation.model.AnnotationPostRequest;
 import unipotsdam.gf.modules.annotation.model.AnnotationResponse;
+import unipotsdam.gf.modules.peer2peerfeedback.Category;
 
 import javax.ws.rs.*;
 import javax.ws.rs.core.MediaType;
@@ -108,12 +109,12 @@ public class AnnotationService {
     }
 
     @GET
-    @Path("/target/{id}")
-    public Response getAnnotations(@PathParam("id") int targetId) {
+    @Path("/targetid/{id}/targetcategory/{category}")
+    public Response getAnnotations(@PathParam("id") int targetId, @PathParam("category") String category) {
 
         // receive the annotation
         AnnotationController controller = new AnnotationController();
-        ArrayList<Annotation> annotations = controller.getAnnotations(targetId);
+        ArrayList<Annotation> annotations = controller.getAnnotations(targetId, Category.valueOf(category.toUpperCase()));
 
         if (!annotations.isEmpty()) {
             return Response.ok(annotations).build();
diff --git a/gemeinsamforschen/src/scripts/dbschema/fltrail.sql b/gemeinsamforschen/src/scripts/dbschema/fltrail.sql
index 978bd4b9..2ceb83f7 100644
--- a/gemeinsamforschen/src/scripts/dbschema/fltrail.sql
+++ b/gemeinsamforschen/src/scripts/dbschema/fltrail.sql
@@ -114,6 +114,7 @@ CREATE TABLE if not exists `annotations` (
   `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
   `userToken` varchar(120) DEFAULT NULL,
   `targetId` int(11) DEFAULT NULL,
+  `targetCategory` VARCHAR(30) NOT NULL,
   `title` varchar(120) DEFAULT NULL,
   `comment` varchar(400) DEFAULT NULL,
   `startCharacter` int(11) DEFAULT NULL,
diff --git a/gemeinsamforschen/src/test/java/unipotsdam/gf/interfaces/AnnotationTest.java b/gemeinsamforschen/src/test/java/unipotsdam/gf/interfaces/AnnotationTest.java
index d0fde6c5..2dda63f5 100644
--- a/gemeinsamforschen/src/test/java/unipotsdam/gf/interfaces/AnnotationTest.java
+++ b/gemeinsamforschen/src/test/java/unipotsdam/gf/interfaces/AnnotationTest.java
@@ -7,6 +7,7 @@ import unipotsdam.gf.modules.annotation.model.Annotation;
 import unipotsdam.gf.modules.annotation.model.AnnotationBody;
 import unipotsdam.gf.modules.annotation.model.AnnotationPatchRequest;
 import unipotsdam.gf.modules.annotation.model.AnnotationPostRequest;
+import unipotsdam.gf.modules.peer2peerfeedback.Category;
 
 import java.util.ArrayList;
 
@@ -37,7 +38,7 @@ public class AnnotationTest {
         String comment = "comment_testAddAnnotation";
 
         // prepare and execute request
-        AnnotationPostRequest annotationPostRequest = new AnnotationPostRequest("userToken", 1, new AnnotationBody(title, comment, 1, 2));
+        AnnotationPostRequest annotationPostRequest = new AnnotationPostRequest("userToken", 1, Category.TITEL, new AnnotationBody(title, comment, 1, 2));
         Annotation response = controller.addAnnotation(annotationPostRequest);
 
         // the new annotation should be found in the database
@@ -58,7 +59,7 @@ public class AnnotationTest {
         String commentNew = "commentNew_testAlterAnnotation";
 
         // save new annotation in database
-        AnnotationPostRequest annotationPostRequest = new AnnotationPostRequest("userToken", 0, new AnnotationBody(titleOld, commentOld, 1, 2));
+        AnnotationPostRequest annotationPostRequest = new AnnotationPostRequest("userToken", 0, Category.TITEL, new AnnotationBody(titleOld, commentOld, 1, 2));
         Annotation response = controller.addAnnotation(annotationPostRequest);
 
         // the new annotation should be found in the database
@@ -96,7 +97,7 @@ public class AnnotationTest {
         String comment = "comment_testDeleteAnnotation";
 
         // prepare and execute request
-        AnnotationPostRequest annotationPostRequest = new AnnotationPostRequest("userToken", 1, new AnnotationBody(title, comment, 1, 2));
+        AnnotationPostRequest annotationPostRequest = new AnnotationPostRequest("userToken", 1, Category.TITEL, new AnnotationBody(title, comment, 1, 2));
         Annotation response = controller.addAnnotation(annotationPostRequest);
 
         // the new annotation should be found in the database
@@ -118,7 +119,7 @@ public class AnnotationTest {
         String comment = "comment_testGetAnnotation";
 
         // prepare and execute request
-        AnnotationPostRequest annotationPostRequest = new AnnotationPostRequest("userToken", 1, new AnnotationBody(title, comment, 1, 2));
+        AnnotationPostRequest annotationPostRequest = new AnnotationPostRequest("userToken", 1, Category.TITEL, new AnnotationBody(title, comment, 1, 2));
         Annotation response = controller.addAnnotation(annotationPostRequest);
 
         // receive the new annotation
@@ -148,21 +149,21 @@ public class AnnotationTest {
         targetIds.add(-2);
 
         // save new annotations in database
-        AnnotationPostRequest request1 = new AnnotationPostRequest("userToken", targetIds.get(0), new AnnotationBody(title, comment, 1, 2));
-        AnnotationPostRequest request2 = new AnnotationPostRequest("userToken", targetIds.get(1), new AnnotationBody(title, comment, 1, 2));
-        AnnotationPostRequest request3 = new AnnotationPostRequest("userToken", targetIds.get(1), new AnnotationBody(title, comment, 1, 2));
+        AnnotationPostRequest request1 = new AnnotationPostRequest("userToken", targetIds.get(0), Category.TITEL, new AnnotationBody(title, comment, 1, 2));
+        AnnotationPostRequest request2 = new AnnotationPostRequest("userToken", targetIds.get(1), Category.TITEL, new AnnotationBody(title, comment, 1, 2));
+        AnnotationPostRequest request3 = new AnnotationPostRequest("userToken", targetIds.get(1), Category.TITEL, new AnnotationBody(title, comment, 1, 2));
         controller.addAnnotation(request1);
         controller.addAnnotation(request2);
         controller.addAnnotation(request3);
 
         // receive the new annotations with targetId = -2
-        ArrayList<Annotation> getResponse = controller.getAnnotations(targetIds.get(1));
+        ArrayList<Annotation> getResponse = controller.getAnnotations(targetIds.get(1), Category.TITEL);
 
         // the size of the getResponse should be 2
         assertEquals("The size of the response should be 2 but was " + getResponse.size(), 2, getResponse.size());
 
         // receive the new annotations with targetId = -1
-        ArrayList<Annotation> getResponseNew = controller.getAnnotations(targetIds.get(0));
+        ArrayList<Annotation> getResponseNew = controller.getAnnotations(targetIds.get(0), Category.TITEL);
 
         // the size of the getResponse should be 1
         assertEquals("The size of the response should be 1 but was " + getResponseNew.size(), 1, getResponseNew.size());
@@ -183,7 +184,7 @@ public class AnnotationTest {
         String badId = "badId";
 
         // save new annotation in database
-        AnnotationPostRequest annotationPostRequest = new AnnotationPostRequest("userToken", 0, new AnnotationBody(title, comment, 1, 2));
+        AnnotationPostRequest annotationPostRequest = new AnnotationPostRequest("userToken", 0, Category.TITEL, new AnnotationBody(title, comment, 1, 2));
         Annotation response = controller.addAnnotation(annotationPostRequest);
 
         // the annotation shouldn't be found in the database
-- 
GitLab