diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/interfaces/IAnnotation.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/interfaces/IAnnotation.java index 9ae417fdd7cc9e5ea1b6b1bd4511993b2af1096a..0378153de66a916d195a5b498f4654a88d49d671 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 91f9625c83d388e6fb64c958a23316a6150f7950..394ccf69df9e554a136704c2221a0f4e1b3a4bfe 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 13dbd82071abda17602b788c87a828096c62bb21..950959d31d5820e480aeefb57852c9dbecb4cfa4 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 f49676950341f2bac2e68ff6a9ff7546a84c8aab..1ae3c2b6b5137b7877367c3342b1f2eaab7584f4 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 2504955171b3f1293246ae0dafda871fb2e43890..9a4656e6dc7098c0c7563a57a162ef5d3bc782ae 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 0000000000000000000000000000000000000000..09409309c304231d50a4e472cba639dd60d5dd6a --- /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 29c2d3f61ebc6da8e99be59dfdff206c8e1810b0..b30743913672d6770337c6d4b5ca5dcebfeda1a5 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 8a5e0c9969b609ae60efef8c4d4be44ce23f2c24..662568a5a3bb822154dcdf510a1c05819871d1be 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 62bbd2f33c69daada2c18e08614c9ab718ad6c00..5c3ced1b8d2017cf5e7365a17306d90c6ba37d49 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 5ef4d315577afa6631281e4a9aaef5f5504e2963..881287e2e1b793d0e0a4e6059eb2edc7c8873676 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 2dda63f5fb34ed1ccb47611f00eeeaebff0d2c37..7faee3414dc619219b3c9e98604dd4b69c4670bb 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