Skip to content
Snippets Groups Projects
Commit 21702ad2 authored by Sven Kästle's avatar Sven Kästle
Browse files

refactor: The attribute 'targetId' is now a String

* and removed in front end
parent 370b29dd
No related branches found
No related tags found
No related merge requests found
Showing
with 89 additions and 37 deletions
......@@ -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
......
......@@ -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
......
......@@ -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;
}
......
......@@ -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;
}
......
......@@ -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();
......
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() +
'}';
}
}
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);
......
// 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,
......
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);
......
......@@ -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,
......
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment