From a78eb7fe84e18b17c7ff274d8cb956d8e36c489a Mon Sep 17 00:00:00 2001
From: Daniel Gerhardt <code@dgerhardt.net>
Date: Mon, 28 Aug 2017 15:57:25 +0200
Subject: [PATCH] Remove obsolete transport entities

With the use of JSON views they are no longer necessary.
---
 .../arsnova/controller/CommentController.java |   6 +-
 .../arsnova/controller/ContentController.java |   2 +-
 .../arsnova/entities/transport/Answer.java    | 165 ------------------
 .../arsnova/entities/transport/Comment.java   | 107 ------------
 .../transport/ImportExportSession.java        |   2 +
 .../entities/transport/ScoreOptions.java      |  61 -------
 .../couchdb/CouchDbSessionRepository.java     |   2 +-
 .../thm/arsnova/services/ContentService.java  |   2 +-
 .../arsnova/services/ContentServiceImpl.java  |  30 ++--
 .../websocket/ArsnovaSocketioServerImpl.java  |  16 +-
 10 files changed, 35 insertions(+), 358 deletions(-)
 delete mode 100644 src/main/java/de/thm/arsnova/entities/transport/Answer.java
 delete mode 100644 src/main/java/de/thm/arsnova/entities/transport/Comment.java
 delete mode 100644 src/main/java/de/thm/arsnova/entities/transport/ScoreOptions.java

diff --git a/src/main/java/de/thm/arsnova/controller/CommentController.java b/src/main/java/de/thm/arsnova/controller/CommentController.java
index 656e54b69..428a6172b 100644
--- a/src/main/java/de/thm/arsnova/controller/CommentController.java
+++ b/src/main/java/de/thm/arsnova/controller/CommentController.java
@@ -17,8 +17,8 @@
  */
 package de.thm.arsnova.controller;
 
+import de.thm.arsnova.entities.Comment;
 import de.thm.arsnova.entities.CommentReadingCount;
-import de.thm.arsnova.entities.transport.Comment;
 import de.thm.arsnova.exceptions.BadRequestException;
 import de.thm.arsnova.services.CommentService;
 import de.thm.arsnova.web.DeprecatedApi;
@@ -74,14 +74,14 @@ public class CommentController extends PaginationController {
 	@RequestMapping(value = "/", method = RequestMethod.GET)
 	@Pagination
 	public List<Comment> getInterposedQuestions(@ApiParam(value = "Session-Key from current session", required = true) @RequestParam final String sessionkey) {
-		return Comment.fromList(commentService.getBySessionKey(sessionkey, offset, limit));
+		return commentService.getBySessionKey(sessionkey, offset, limit);
 	}
 
 	@ApiOperation(value = "Retrieves an Comment",
 			nickname = "getInterposedQuestion")
 	@RequestMapping(value = "/{questionId}", method = RequestMethod.GET)
 	public Comment getInterposedQuestion(@ApiParam(value = "ID of the Comment that needs to be deleted", required = true) @PathVariable final String questionId) {
-		return new Comment(commentService.getAndMarkRead(questionId));
+		return commentService.getAndMarkRead(questionId);
 	}
 
 	@ApiOperation(value = "Creates a new Comment for a Session and returns the Comment's data",
diff --git a/src/main/java/de/thm/arsnova/controller/ContentController.java b/src/main/java/de/thm/arsnova/controller/ContentController.java
index 166c579b4..a0b323ad3 100644
--- a/src/main/java/de/thm/arsnova/controller/ContentController.java
+++ b/src/main/java/de/thm/arsnova/controller/ContentController.java
@@ -458,7 +458,7 @@ public class ContentController extends PaginationController {
 	@RequestMapping(value = "/{questionId}/answer/", method = RequestMethod.POST)
 	public Answer saveAnswer(
 			@PathVariable final String questionId,
-			@RequestBody final de.thm.arsnova.entities.transport.Answer answer,
+			@RequestBody final Answer answer,
 			final HttpServletResponse response
 			) {
 		return contentService.saveAnswer(questionId, answer);
diff --git a/src/main/java/de/thm/arsnova/entities/transport/Answer.java b/src/main/java/de/thm/arsnova/entities/transport/Answer.java
deleted file mode 100644
index 02e1f2fe4..000000000
--- a/src/main/java/de/thm/arsnova/entities/transport/Answer.java
+++ /dev/null
@@ -1,165 +0,0 @@
-/*
- * This file is part of ARSnova Backend.
- * Copyright (C) 2012-2017 The ARSnova Team
- *
- * ARSnova Backend is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * ARSnova Backend is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-package de.thm.arsnova.entities.transport;
-
-import com.fasterxml.jackson.annotation.JsonInclude;
-import com.fasterxml.jackson.annotation.JsonView;
-import de.thm.arsnova.entities.Content;
-import de.thm.arsnova.entities.User;
-import de.thm.arsnova.entities.serialization.View;
-import io.swagger.annotations.ApiModel;
-import io.swagger.annotations.ApiModelProperty;
-
-import java.io.Serializable;
-import java.util.Date;
-
-/**
- * A user's answer to a question.
- */
-@JsonInclude(JsonInclude.Include.NON_DEFAULT)
-@ApiModel(value = "session/answer", description = "the Answer API")
-public class Answer implements Serializable {
-
-	private String answerSubject;
-
-	private String answerSubjectRaw;
-
-	private String answerText;
-	private String answerTextRaw;
-	private double freeTextScore;
-	private boolean successfulFreeTextAnswer;
-
-	private String answerImage;
-
-	private boolean abstention;
-
-	public Answer() {
-
-	}
-
-	public Answer(de.thm.arsnova.entities.Answer a) {
-		answerSubject = a.getAnswerSubject();
-		answerText = a.getAnswerText();
-		answerImage = a.getAnswerImage();
-		abstention = a.isAbstention();
-		successfulFreeTextAnswer = a.isSuccessfulFreeTextAnswer();
-	}
-
-	@ApiModelProperty(required = true, value = "used to display text answer")
-	@JsonView(View.Public.class)
-	public String getAnswerText() {
-		return answerText;
-	}
-
-	public void setAnswerText(String answerText) {
-		this.answerText = answerText;
-	}
-
-	@ApiModelProperty(required = true, value = "used to display subject answer")
-	@JsonView(View.Public.class)
-	public String getAnswerSubject() {
-		return answerSubject;
-	}
-
-	public void setAnswerSubject(String answerSubject) {
-		this.answerSubject = answerSubject;
-	}
-
-	@JsonView(View.Public.class)
-	public final String getAnswerTextRaw() {
-		return this.answerTextRaw;
-	}
-
-	public final void setAnswerTextRaw(final String answerTextRaw) {
-		this.answerTextRaw = answerTextRaw;
-	}
-
-	@JsonView(View.Public.class)
-	public final String getAnswerSubjectRaw() {
-		return this.answerSubjectRaw;
-	}
-
-	public final void setAnswerSubjectRaw(final String answerSubjectRaw) {
-		this.answerSubjectRaw = answerSubjectRaw;
-	}
-
-	@JsonView(View.Public.class)
-	public final double getFreeTextScore() {
-		return this.freeTextScore;
-	}
-
-	public final void setFreeTextScore(final double freeTextScore) {
-		this.freeTextScore = freeTextScore;
-	}
-
-	@ApiModelProperty(required = true, value = "successfulFreeTextAnswer")
-	public final boolean isSuccessfulFreeTextAnswer() {
-		return this.successfulFreeTextAnswer;
-	}
-
-	public final void setSuccessfulFreeTextAnswer(final boolean successfulFreeTextAnswer) {
-		this.successfulFreeTextAnswer = successfulFreeTextAnswer;
-	}
-
-	@ApiModelProperty(required = true, value = "abstention")
-	@JsonView(View.Public.class)
-	public boolean isAbstention() {
-		return abstention;
-	}
-
-	public void setAbstention(boolean abstention) {
-		this.abstention = abstention;
-	}
-
-	public de.thm.arsnova.entities.Answer generateAnswerEntity(final User user, final Content content) {
-		// rewrite all fields so that no manipulated data gets written
-		// only answerText, answerSubject, and abstention are allowed
-		de.thm.arsnova.entities.Answer theAnswer = new de.thm.arsnova.entities.Answer();
-		theAnswer.setAnswerSubject(this.getAnswerSubject());
-		theAnswer.setAnswerText(this.getAnswerText());
-		theAnswer.setAnswerTextRaw(this.getAnswerTextRaw());
-		theAnswer.setSessionId(content.getSessionId());
-		theAnswer.setUser(user.getUsername());
-		theAnswer.setQuestionId(content.getId());
-		theAnswer.setTimestamp(new Date().getTime());
-		theAnswer.setQuestionVariant(content.getQuestionVariant());
-		theAnswer.setAbstention(this.isAbstention());
-		// calculate score value after all properties are set
-		theAnswer.setQuestionValue(content.calculateValue(theAnswer));
-		theAnswer.setAnswerImage(this.getAnswerImage());
-		theAnswer.setSuccessfulFreeTextAnswer(this.isSuccessfulFreeTextAnswer());
-
-		if ("freetext".equals(content.getQuestionType())) {
-			theAnswer.setPiRound(0);
-		} else {
-			theAnswer.setPiRound(content.getPiRound());
-		}
-
-		return theAnswer;
-	}
-
-	@ApiModelProperty(required = true, value = "used to display image answer")
-	@JsonView(View.Public.class)
-	public String getAnswerImage() {
-		return answerImage;
-	}
-
-	public void setAnswerImage(String answerImage) {
-		this.answerImage = answerImage;
-	}
-}
diff --git a/src/main/java/de/thm/arsnova/entities/transport/Comment.java b/src/main/java/de/thm/arsnova/entities/transport/Comment.java
deleted file mode 100644
index e3006b991..000000000
--- a/src/main/java/de/thm/arsnova/entities/transport/Comment.java
+++ /dev/null
@@ -1,107 +0,0 @@
-/*
- * This file is part of ARSnova Backend.
- * Copyright (C) 2012-2017 The ARSnova Team
- *
- * ARSnova Backend is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * ARSnova Backend is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-package de.thm.arsnova.entities.transport;
-
-import com.fasterxml.jackson.annotation.JsonView;
-import de.thm.arsnova.entities.serialization.View;
-import io.swagger.annotations.ApiModel;
-import io.swagger.annotations.ApiModelProperty;
-
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * A question a student is asking. Also known as comment, feedback or audience question.
- */
-@ApiModel(value = "audiencequestion/{questionId}", description = "the comment API")
-public class Comment {
-
-	private String id;
-	private String subject;
-	private String text;
-	private long timestamp;
-	private boolean read;
-
-	public static List<Comment> fromList(List<de.thm.arsnova.entities.Comment> comments) {
-		ArrayList<Comment> transportComments = new ArrayList<>();
-		for (de.thm.arsnova.entities.Comment comment : comments) {
-			transportComments.add(new Comment(comment));
-		}
-		return transportComments;
-	}
-
-	public Comment(de.thm.arsnova.entities.Comment comment) {
-		this.id = comment.getId();
-		this.subject = comment.getSubject();
-		this.text = comment.getText();
-		this.timestamp = comment.getTimestamp();
-		this.read = comment.isRead();
-	}
-
-	public Comment() { }
-
-	@ApiModelProperty(required = true, value = "used to display Id")
-	@JsonView(View.Public.class)
-	public String getId() {
-		return id;
-	}
-
-	public void setId(String id) {
-		this.id = id;
-	}
-
-	@ApiModelProperty(required = true, value = "used to display Subject")
-	@JsonView(View.Public.class)
-	public String getSubject() {
-		return subject;
-	}
-
-	public void setSubject(String subject) {
-		this.subject = subject;
-	}
-
-	@ApiModelProperty(required = true, value = "used to display Text")
-	@JsonView(View.Public.class)
-	public String getText() {
-		return text;
-	}
-
-	public void setText(String text) {
-		this.text = text;
-	}
-
-	@ApiModelProperty(required = true, value = "used to display Timetamp")
-	@JsonView(View.Public.class)
-	public long getTimestamp() {
-		return timestamp;
-	}
-
-	public void setTimestamp(long timestamp) {
-		this.timestamp = timestamp;
-	}
-
-	@ApiModelProperty(required = true, value = "is read")
-	@JsonView(View.Public.class)
-	public boolean isRead() {
-		return read;
-	}
-
-	public void setRead(boolean read) {
-		this.read = read;
-	}
-}
diff --git a/src/main/java/de/thm/arsnova/entities/transport/ImportExportSession.java b/src/main/java/de/thm/arsnova/entities/transport/ImportExportSession.java
index 1ef60232f..ee56a2d13 100644
--- a/src/main/java/de/thm/arsnova/entities/transport/ImportExportSession.java
+++ b/src/main/java/de/thm/arsnova/entities/transport/ImportExportSession.java
@@ -18,6 +18,8 @@
 package de.thm.arsnova.entities.transport;
 
 import com.fasterxml.jackson.annotation.JsonView;
+import de.thm.arsnova.entities.Answer;
+import de.thm.arsnova.entities.Comment;
 import de.thm.arsnova.entities.Content;
 import de.thm.arsnova.entities.Motd;
 import de.thm.arsnova.entities.Session;
diff --git a/src/main/java/de/thm/arsnova/entities/transport/ScoreOptions.java b/src/main/java/de/thm/arsnova/entities/transport/ScoreOptions.java
deleted file mode 100644
index 5bf0b49a8..000000000
--- a/src/main/java/de/thm/arsnova/entities/transport/ScoreOptions.java
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * This file is part of ARSnova Backend.
- * Copyright (C) 2012-2017 The ARSnova Team
- *
- * ARSnova Backend is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * ARSnova Backend is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-package de.thm.arsnova.entities.transport;
-
-/**
- * A session's settings regarding the calculation of the score.
- */
-public class ScoreOptions {
-
-	private String sessionKeyword;
-
-	private String type;
-
-	private String questionVariant;
-
-	public String getSessionKeyword() {
-		return sessionKeyword;
-	}
-
-	public void setSessionKeyword(String sessionKeyword) {
-		this.sessionKeyword = sessionKeyword;
-	}
-
-	public String getType() {
-		return type;
-	}
-
-	public void setType(String type) {
-		this.type = type;
-	}
-
-	public String getQuestionVariant() {
-		return questionVariant;
-	}
-
-	public void setQuestionVariant(String questionVariant) {
-		this.questionVariant = questionVariant;
-	}
-
-	public de.thm.arsnova.entities.ScoreOptions toEntity() {
-		de.thm.arsnova.entities.ScoreOptions entity = new de.thm.arsnova.entities.ScoreOptions();
-		entity.setType(this.getType());
-		entity.setQuestionVariant(this.getQuestionVariant());
-		return entity;
-	}
-}
diff --git a/src/main/java/de/thm/arsnova/persistance/couchdb/CouchDbSessionRepository.java b/src/main/java/de/thm/arsnova/persistance/couchdb/CouchDbSessionRepository.java
index 91be570e1..724755064 100644
--- a/src/main/java/de/thm/arsnova/persistance/couchdb/CouchDbSessionRepository.java
+++ b/src/main/java/de/thm/arsnova/persistance/couchdb/CouchDbSessionRepository.java
@@ -18,12 +18,12 @@
 package de.thm.arsnova.persistance.couchdb;
 
 import de.thm.arsnova.connector.model.Course;
+import de.thm.arsnova.entities.Comment;
 import de.thm.arsnova.entities.LoggedIn;
 import de.thm.arsnova.entities.Session;
 import de.thm.arsnova.entities.SessionInfo;
 import de.thm.arsnova.entities.User;
 import de.thm.arsnova.entities.VisitedSession;
-import de.thm.arsnova.entities.transport.Comment;
 import de.thm.arsnova.entities.transport.ImportExportSession;
 import de.thm.arsnova.exceptions.NotFoundException;
 import de.thm.arsnova.persistance.LogEntryRepository;
diff --git a/src/main/java/de/thm/arsnova/services/ContentService.java b/src/main/java/de/thm/arsnova/services/ContentService.java
index 0c3f13037..6a66cc999 100644
--- a/src/main/java/de/thm/arsnova/services/ContentService.java
+++ b/src/main/java/de/thm/arsnova/services/ContentService.java
@@ -78,7 +78,7 @@ public interface ContentService {
 
 	void deleteAnswers(String questionId);
 
-	Answer saveAnswer(String questionId, de.thm.arsnova.entities.transport.Answer answer);
+	Answer saveAnswer(String questionId, Answer answer);
 
 	Answer updateAnswer(Answer answer);
 
diff --git a/src/main/java/de/thm/arsnova/services/ContentServiceImpl.java b/src/main/java/de/thm/arsnova/services/ContentServiceImpl.java
index becc677ea..8f3c6715b 100644
--- a/src/main/java/de/thm/arsnova/services/ContentServiceImpl.java
+++ b/src/main/java/de/thm/arsnova/services/ContentServiceImpl.java
@@ -712,7 +712,7 @@ public class ContentServiceImpl extends EntityService<Content> implements Conten
 	@Override
 	@PreAuthorize("isAuthenticated()")
 	@CacheEvict(value = "answers", key = "#contentId")
-	public Answer saveAnswer(final String contentId, final de.thm.arsnova.entities.transport.Answer answer) {
+	public Answer saveAnswer(final String contentId, final Answer answer) {
 		final User user = getCurrentUser();
 		final Content content = get(contentId);
 		if (content == null) {
@@ -720,26 +720,32 @@ public class ContentServiceImpl extends EntityService<Content> implements Conten
 		}
 		final Session session = sessionRepository.findOne(content.getSessionId());
 
-		Answer theAnswer = answer.generateAnswerEntity(user, content);
-		theAnswer.setUser(user.getUsername());
-		theAnswer.setQuestionId(content.getId());
-		theAnswer.setSessionId(session.getId());
+		answer.setUser(user.getUsername());
+		answer.setQuestionId(content.getId());
+		answer.setSessionId(session.getId());
+		answer.setQuestionVariant(content.getQuestionVariant());
+		answer.setQuestionValue(content.calculateValue(answer));
+		answer.setTimestamp(new Date().getTime());
+
 		if ("freetext".equals(content.getQuestionType())) {
-			imageUtils.generateThumbnailImage(theAnswer);
+			answer.setPiRound(0);
+			imageUtils.generateThumbnailImage(answer);
 			if (content.isFixedAnswer() && content.getText() != null) {
-				theAnswer.setAnswerTextRaw(theAnswer.getAnswerText());
+				answer.setAnswerTextRaw(answer.getAnswerText());
 
 				if (content.isStrictMode()) {
-					content.checkTextStrictOptions(theAnswer);
+					content.checkTextStrictOptions(answer);
 				}
-				theAnswer.setQuestionValue(content.evaluateCorrectAnswerFixedText(theAnswer.getAnswerTextRaw()));
-				theAnswer.setSuccessfulFreeTextAnswer(content.isSuccessfulFreeTextAnswer(theAnswer.getAnswerTextRaw()));
+				answer.setQuestionValue(content.evaluateCorrectAnswerFixedText(answer.getAnswerTextRaw()));
+				answer.setSuccessfulFreeTextAnswer(content.isSuccessfulFreeTextAnswer(answer.getAnswerTextRaw()));
 			}
+		} else {
+			answer.setPiRound(content.getPiRound());
 		}
 
-		this.answerQueue.offer(new AnswerQueueElement(session, content, theAnswer, user));
+		this.answerQueue.offer(new AnswerQueueElement(session, content, answer, user));
 
-		return theAnswer;
+		return answer;
 	}
 
 	@Override
diff --git a/src/main/java/de/thm/arsnova/websocket/ArsnovaSocketioServerImpl.java b/src/main/java/de/thm/arsnova/websocket/ArsnovaSocketioServerImpl.java
index a473408f0..8cb3664e1 100644
--- a/src/main/java/de/thm/arsnova/websocket/ArsnovaSocketioServerImpl.java
+++ b/src/main/java/de/thm/arsnova/websocket/ArsnovaSocketioServerImpl.java
@@ -29,8 +29,8 @@ import com.corundumstudio.socketio.listener.DisconnectListener;
 import com.corundumstudio.socketio.protocol.Packet;
 import com.corundumstudio.socketio.protocol.PacketType;
 import de.thm.arsnova.entities.Comment;
+import de.thm.arsnova.entities.ScoreOptions;
 import de.thm.arsnova.entities.User;
-import de.thm.arsnova.entities.transport.ScoreOptions;
 import de.thm.arsnova.events.*;
 import de.thm.arsnova.exceptions.NoContentException;
 import de.thm.arsnova.exceptions.NotFoundException;
@@ -189,15 +189,16 @@ public class ArsnovaSocketioServerImpl implements ArsnovaSocketioServer, Arsnova
 			}
 		});
 
+		/* TODO: This listener expects a Comment entity but only uses the ID. Reduce transmitted data. */
 		server.addEventListener(
 				"readInterposedQuestion",
-				de.thm.arsnova.entities.transport.Comment.class,
-				new DataListener<de.thm.arsnova.entities.transport.Comment>() {
+				Comment.class,
+				new DataListener<Comment>() {
 			@Override
 			@Timed(name = "readInterposedQuestionEvent.onData")
 			public void onData(
 					SocketIOClient client,
-					de.thm.arsnova.entities.transport.Comment comment,
+					Comment comment,
 					AckRequest ackRequest) {
 				final User user = userService.getUser2SocketId(client.getSessionId());
 				try {
@@ -228,11 +229,12 @@ public class ArsnovaSocketioServerImpl implements ArsnovaSocketioServer, Arsnova
 			@Timed(name = "setLearningProgressOptionsEvent.onData")
 			public void onData(SocketIOClient client, ScoreOptions scoreOptions, AckRequest ack) {
 				final User user = userService.getUser2SocketId(client.getSessionId());
-				final de.thm.arsnova.entities.Session session = sessionService.getInternal(scoreOptions.getSessionKeyword(), user);
+				final String sessionKey = userService.getSessionByUsername(user.getUsername());
+				final de.thm.arsnova.entities.Session session = sessionService.getInternal(sessionKey, user);
 				if (session.isCreator(user)) {
-					session.setLearningProgressOptions(scoreOptions.toEntity());
+					session.setLearningProgressOptions(scoreOptions);
 					sessionService.updateInternal(session, user);
-					broadcastInSession(session.getKeyword(), "learningProgressOptions", scoreOptions.toEntity());
+					broadcastInSession(session.getKeyword(), "learningProgressOptions", scoreOptions);
 				}
 			}
 		});
-- 
GitLab