From 21702ad2960309aa4e55bd20bd1e428cba0cf6c8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Sven=20K=C3=A4stle?= <sven.kaestle@gmx.de>
Date: Sat, 8 Sep 2018 14:18:46 +0200
Subject: [PATCH] refactor: The attribute 'targetId' is now a String

* and removed in front end
---
 .../unipotsdam/gf/interfaces/IAnnotation.java |  2 +-
 .../controller/AnnotationController.java      |  4 +-
 .../modules/annotation/model/Annotation.java  |  8 ++--
 .../model/AnnotationPostRequest.java          |  8 ++--
 .../annotation/view/AnnotationService.java    |  2 +-
 .../websocket/AnnotationWSTarget.java         | 46 +++++++++++++++++++
 .../AnnotationWebSocketEndpoint.java          | 17 ++++---
 .../main/webapp/assets/js/annotationScript.js | 17 ++++---
 .../webapp/assets/js/annotationWebsocket.js   |  4 +-
 .../src/scripts/dbschema/fltrail.sql          |  2 +-
 .../gf/interfaces/AnnotationTest.java         | 16 +++----
 11 files changed, 89 insertions(+), 37 deletions(-)
 create mode 100644 gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/websocket/AnnotationWSTarget.java

diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/interfaces/IAnnotation.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/interfaces/IAnnotation.java
index 9ae417fd..0378153d 100644
--- a/gemeinsamforschen/src/main/java/unipotsdam/gf/interfaces/IAnnotation.java
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/interfaces/IAnnotation.java
@@ -51,7 +51,7 @@ public interface IAnnotation {
      * @param targetCategory The category of the target
      * @return Returns all annotations for a target
      */
-    ArrayList<Annotation> getAnnotations(int targetId, Category targetCategory);
+    ArrayList<Annotation> getAnnotations(String 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 91f9625c..394ccf69 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
@@ -105,7 +105,7 @@ public class AnnotationController implements IAnnotation {
     }
 
     @Override
-    public ArrayList<Annotation> getAnnotations(int targetId, Category category) {
+    public ArrayList<Annotation> getAnnotations(String targetId, Category category) {
 
         // declare annotation ArrayList
         ArrayList<Annotation> annotations = new ArrayList<>();
@@ -172,7 +172,7 @@ public class AnnotationController implements IAnnotation {
         String id = rs.getString("id");
         long timestamp = rs.getTimestamp(2).getTime();
         String userToken = rs.getString("userToken");
-        int targetId = rs.getInt("targetId");
+        String targetId = rs.getString("targetId");
         Category targetCategory = Category.valueOf(rs.getString("targetCategory"));
 
         // initialize new annotation 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 13dbd820..950959d3 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
@@ -12,12 +12,12 @@ public class Annotation {
     private String id;
     private long timestamp;
     private String userToken;
-    private int targetId;
+    private String targetId;
     private Category targetCategory;
     private AnnotationBody body;
 
     // constructor
-    public Annotation(String id, long timestamp, String userToken, int targetId, Category targetCategory, AnnotationBody body) {
+    public Annotation(String id, long timestamp, String userToken, String targetId, Category targetCategory, AnnotationBody body) {
         this.id = id;
         this.timestamp = timestamp;
         this.userToken = userToken;
@@ -51,11 +51,11 @@ public class Annotation {
         this.userToken = userToken;
     }
 
-    public int getTargetId() {
+    public String getTargetId() {
         return targetId;
     }
 
-    public void setTargetId(int targetId) {
+    public void setTargetId(String targetId) {
         this.targetId = targetId;
     }
 
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 f4967695..1ae3c2b6 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
@@ -10,12 +10,12 @@ public class AnnotationPostRequest {
 
     // variables
     private String userToken;
-    private int targetId;
+    private String targetId;
     private Category targetCategory;
     private AnnotationBody body;
 
     // constructors
-    public AnnotationPostRequest(String userToken, int targetId, Category targetCategory, AnnotationBody body) {
+    public AnnotationPostRequest(String userToken, String targetId, Category targetCategory, AnnotationBody body) {
         this.userToken = userToken;
         this.targetId = targetId;
         this.targetCategory = targetCategory;
@@ -34,11 +34,11 @@ public class AnnotationPostRequest {
         this.userToken = userToken;
     }
 
-    public int getTargetId() {
+    public String getTargetId() {
         return targetId;
     }
 
-    public void setTargetId(int targetId) {
+    public void setTargetId(String targetId) {
         this.targetId = targetId;
     }
 
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 25049551..9a4656e6 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
@@ -110,7 +110,7 @@ public class AnnotationService {
 
     @GET
     @Path("/targetid/{id}/targetcategory/{category}")
-    public Response getAnnotations(@PathParam("id") int targetId, @PathParam("category") String category) {
+    public Response getAnnotations(@PathParam("id") String targetId, @PathParam("category") String category) {
 
         // receive the annotation
         AnnotationController controller = new AnnotationController();
diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/websocket/AnnotationWSTarget.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/websocket/AnnotationWSTarget.java
new file mode 100644
index 00000000..09409309
--- /dev/null
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/websocket/AnnotationWSTarget.java
@@ -0,0 +1,46 @@
+package unipotsdam.gf.modules.annotation.websocket;
+
+import unipotsdam.gf.modules.peer2peerfeedback.Category;
+
+/**
+ * @author Sven Kästle
+ * skaestle@uni-potsdam.de
+ */
+public class AnnotationWSTarget {
+
+    // variables
+    String targetId;
+    Category targetCategory;
+
+    // constructor
+    public AnnotationWSTarget(String targetId, Category targetCategory) {
+        this.targetId = targetId;
+        this.targetCategory = targetCategory;
+    }
+
+    // methods
+    public String getTargetId() {
+        return targetId;
+    }
+
+    public void setTargetId(String targetId) {
+        this.targetId = targetId;
+    }
+
+    public Category getTargetCategory() {
+        return targetCategory;
+    }
+
+    public void setTargetCategory(Category targetCategory) {
+        this.targetCategory = targetCategory;
+    }
+
+    @Override
+    public String toString() {
+        return "AnnotationWSTarget{" +
+                "targetId='" + targetId + '\'' +
+                ", targetCategory=" + targetCategory.toString() +
+                '}';
+    }
+
+}
diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/websocket/AnnotationWebSocketEndpoint.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/websocket/AnnotationWebSocketEndpoint.java
index 29c2d3f6..b3074391 100644
--- a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/websocket/AnnotationWebSocketEndpoint.java
+++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/websocket/AnnotationWebSocketEndpoint.java
@@ -1,6 +1,7 @@
 package unipotsdam.gf.modules.annotation.websocket;
 
 import unipotsdam.gf.modules.annotation.model.AnnotationMessage;
+import unipotsdam.gf.modules.peer2peerfeedback.Category;
 
 import javax.websocket.*;
 import javax.websocket.server.PathParam;
@@ -10,26 +11,27 @@ import java.util.HashMap;
 import java.util.Set;
 import java.util.concurrent.CopyOnWriteArraySet;
 
-@ServerEndpoint(value = "/ws/annotation/{targetId}", decoders = AnnotationMessageDecoder.class, encoders = AnnotationMessageEncoder.class)
+@ServerEndpoint(value = "/ws/annotation/{targetId}/{targetCategory}", decoders = AnnotationMessageDecoder.class, encoders = AnnotationMessageEncoder.class)
 public class AnnotationWebSocketEndpoint {
 
     private Session session;
     private static final Set<AnnotationWebSocketEndpoint> endpoints = new CopyOnWriteArraySet<>();
-    private static HashMap<String, String> targets = new HashMap<>();
+    private static HashMap<String, AnnotationWSTarget> targets = new HashMap<>();
 
     @OnOpen
-    public void onOpen(Session session, @PathParam("targetId") String targetId) throws IOException {
+    public void onOpen(Session session, @PathParam("targetId") String targetId, @PathParam("targetCategory") String targetCategory) throws IOException {
         // initialize session
         this.session = session;
         // save endpoint in set of endpoints
         endpoints.add(this);
-        // save mapping of session and target id
-        targets.put(session.getId(), targetId);
+        // save mapping of session and target (id + category)
+        targets.put(session.getId(), new AnnotationWSTarget(targetId, Category.valueOf(targetCategory.toUpperCase())));
     }
 
     @OnMessage
     public void onMessage(Session session, AnnotationMessage annotationMessage) throws IOException, EncodeException {
-        annotationMessage.setTargetId(targets.get(session.getId()));
+        annotationMessage.setTargetId(targets.get(session.getId()).getTargetId());
+        annotationMessage.setTargetCategory(targets.get(session.getId()).getTargetCategory());
         annotationMessage.setFrom(session.getId());
         broadcast(annotationMessage);
 
@@ -49,7 +51,8 @@ public class AnnotationWebSocketEndpoint {
         endpoints.forEach(endpoint -> {
             synchronized (endpoint) {
                 try {
-                    if (targets.get(endpoint.session.getId()).equals(annotationMessage.getTargetId())
+                    if (targets.get(endpoint.session.getId()).getTargetId().equals(annotationMessage.getTargetId())
+                            && targets.get(endpoint.session.getId()).getTargetCategory() == annotationMessage.getTargetCategory()
                             && !endpoint.session.getId().equals(annotationMessage.getFrom())) {
                         System.out.println("Send message to session" + endpoint.session.getId() + " from session " + annotationMessage.getFrom());
                         endpoint.session.getBasicRemote().sendObject(annotationMessage);
diff --git a/gemeinsamforschen/src/main/webapp/assets/js/annotationScript.js b/gemeinsamforschen/src/main/webapp/assets/js/annotationScript.js
index 8a5e0c99..662568a5 100644
--- a/gemeinsamforschen/src/main/webapp/assets/js/annotationScript.js
+++ b/gemeinsamforschen/src/main/webapp/assets/js/annotationScript.js
@@ -1,9 +1,6 @@
-// initialize userToken, userColors and targetId
-var userToken = getUserTokenFromUrl();
+// initialize userToken, userColors
 var userColors = new Map();
 var userColorsDark = new Map();
-var targetId = 200;
-var targetCategory = "TITEL";
 
 // declare document text, start and end character
 var startCharacter, endCharacter;
@@ -48,7 +45,7 @@ $(document).ready(function() {
     });
 
     // connect to websocket on page ready
-    connect(targetId);
+    connect(fullSubmissionId, category);
 
     /**
      * Context menu handler
@@ -237,7 +234,7 @@ $(document).ready(function() {
     });
 
     // fetch annotations from server on page start
-    getAnnotations(targetId, targetCategory, function (response) {
+    getAnnotations(fullSubmissionId, category, function (response) {
         // iterate over annotations and display each
         $.each(response, function (i, annotation) {
             displayAnnotation(annotation);
@@ -338,7 +335,7 @@ function displayAnnotation(annotation) {
                             .append(
                                 // edit
                                 function () {
-                                    if (userToken == annotation.userToken) {
+                                    if (getUserTokenFromUrl() === annotation.userToken) {
                                         return $('<div>').attr('class', 'annotation-footer-edit')
                                             .append(
                                                 $('<i>').attr('class', editIcon)
@@ -610,6 +607,12 @@ function toggleButtonHandler(id) {
  * @param endCharacter The endCharacter based on the annotated text
  */
 function saveNewAnnotation(title, comment, startCharacter, endCharacter) {
+
+    // initialize target
+    let targetId = getValueFromUrl("fullSubmissionId");
+    let targetCategory = getValueFromUrl("category");
+    let userToken = getUserTokenFromUrl();
+
     // build annotationPostRequest
     var annotationPostRequest = {
         userToken: userToken,
diff --git a/gemeinsamforschen/src/main/webapp/assets/js/annotationWebsocket.js b/gemeinsamforschen/src/main/webapp/assets/js/annotationWebsocket.js
index 62bbd2f3..5c3ced1b 100644
--- a/gemeinsamforschen/src/main/webapp/assets/js/annotationWebsocket.js
+++ b/gemeinsamforschen/src/main/webapp/assets/js/annotationWebsocket.js
@@ -1,10 +1,10 @@
 var ws;
 
-function connect(targetId) {
+function connect(targetId, targetCategory) {
     var host = document.location.host;
     var pathname = document.location.pathname;
 
-    ws = new WebSocket("ws://" + host  + "/ws/annotation/" + targetId);
+    ws = new WebSocket("ws://" + host  + "/ws/annotation/" + targetId + "/" + targetCategory);
 
     ws.onmessage = function (e) {
         var message = JSON.parse(e.data);
diff --git a/gemeinsamforschen/src/scripts/dbschema/fltrail.sql b/gemeinsamforschen/src/scripts/dbschema/fltrail.sql
index 5ef4d315..881287e2 100644
--- a/gemeinsamforschen/src/scripts/dbschema/fltrail.sql
+++ b/gemeinsamforschen/src/scripts/dbschema/fltrail.sql
@@ -113,7 +113,7 @@ CREATE TABLE if not exists `annotations` (
   `id` varchar(120) NOT NULL,
   `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
   `userToken` varchar(120) DEFAULT NULL,
-  `targetId` int(11) DEFAULT NULL,
+  `targetId` varchar(120) DEFAULT NULL,
   `targetCategory` VARCHAR(30) NOT NULL,
   `title` varchar(120) DEFAULT NULL,
   `comment` varchar(400) 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 2dda63f5..7faee341 100644
--- a/gemeinsamforschen/src/test/java/unipotsdam/gf/interfaces/AnnotationTest.java
+++ b/gemeinsamforschen/src/test/java/unipotsdam/gf/interfaces/AnnotationTest.java
@@ -38,7 +38,7 @@ public class AnnotationTest {
         String comment = "comment_testAddAnnotation";
 
         // prepare and execute request
-        AnnotationPostRequest annotationPostRequest = new AnnotationPostRequest("userToken", 1, Category.TITEL, 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
@@ -59,7 +59,7 @@ public class AnnotationTest {
         String commentNew = "commentNew_testAlterAnnotation";
 
         // save new annotation in database
-        AnnotationPostRequest annotationPostRequest = new AnnotationPostRequest("userToken", 0, Category.TITEL, 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
@@ -97,7 +97,7 @@ public class AnnotationTest {
         String comment = "comment_testDeleteAnnotation";
 
         // prepare and execute request
-        AnnotationPostRequest annotationPostRequest = new AnnotationPostRequest("userToken", 1, Category.TITEL, 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
@@ -119,7 +119,7 @@ public class AnnotationTest {
         String comment = "comment_testGetAnnotation";
 
         // prepare and execute request
-        AnnotationPostRequest annotationPostRequest = new AnnotationPostRequest("userToken", 1, Category.TITEL, 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
@@ -144,9 +144,9 @@ public class AnnotationTest {
         String comment = "comment_testGetAnnotations";
 
         // initialize targetIds
-        ArrayList<Integer> targetIds = new ArrayList<>();
-        targetIds.add(-1);
-        targetIds.add(-2);
+        ArrayList<String> targetIds = new ArrayList<>();
+        targetIds.add("-1");
+        targetIds.add("-2");
 
         // save new annotations in database
         AnnotationPostRequest request1 = new AnnotationPostRequest("userToken", targetIds.get(0), Category.TITEL, new AnnotationBody(title, comment, 1, 2));
@@ -184,7 +184,7 @@ public class AnnotationTest {
         String badId = "badId";
 
         // save new annotation in database
-        AnnotationPostRequest annotationPostRequest = new AnnotationPostRequest("userToken", 0, Category.TITEL, 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