From ae601fac86adf6891207b86cb46e377030c81a40 Mon Sep 17 00:00:00 2001 From: Christoph Thelen <christoph.thelen@mni.thm.de> Date: Thu, 28 May 2015 11:41:25 +0200 Subject: [PATCH] Fix for #7658: Flag answers as 'read' via socket --- .../java/de/thm/arsnova/dao/CouchDBDao.java | 23 ++++++++++++++++++- .../java/de/thm/arsnova/dao/IDatabaseDao.java | 2 ++ .../java/de/thm/arsnova/entities/Answer.java | 9 ++++++++ .../arsnova/services/IQuestionService.java | 2 ++ .../thm/arsnova/services/QuestionService.java | 16 +++++++++++++ .../arsnova/socket/ARSnovaSocketIOServer.java | 12 ++++++++++ .../de/thm/arsnova/dao/StubDatabaseDao.java | 6 +++++ 7 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/thm/arsnova/dao/CouchDBDao.java b/src/main/java/de/thm/arsnova/dao/CouchDBDao.java index cad6b9d1e..aadd3d1e7 100644 --- a/src/main/java/de/thm/arsnova/dao/CouchDBDao.java +++ b/src/main/java/de/thm/arsnova/dao/CouchDBDao.java @@ -870,6 +870,27 @@ public class CouchDBDao implements IDatabaseDao, ApplicationEventPublisherAware ); } + @SuppressWarnings("unchecked") + @Override + public <T> T getObjectFromId(final String documentId, final Class<T> klass) { + T obj = null; + try { + final Document doc = getDatabase().getDocument(documentId); + if (doc == null) { + return null; + } + // TODO: This needs some more error checking... + obj = (T) JSONObject.toBean(doc.getJSONObject(), klass); + } catch (IOException e) { + return null; + } catch (ClassCastException e) { + return null; + } catch (net.sf.json.JSONException e) { + return null; + } + return obj; + } + @Override public List<Answer> getAnswers(final Question question, final int piRound) { final String questionId = question.get_id(); @@ -1375,8 +1396,8 @@ public class CouchDBDao implements IDatabaseDao, ApplicationEventPublisherAware a.put("abstention", answer.isAbstention()); a.put("questionValue", answer.getQuestionValue()); a.put("answerImage", answer.getAnswerImage()); - a.put("answerThumbnailImage", answer.getAnswerThumbnailImage()); + a.put("read", answer.isRead()); database.saveDocument(a); answer.set_rev(a.getRev()); return answer; diff --git a/src/main/java/de/thm/arsnova/dao/IDatabaseDao.java b/src/main/java/de/thm/arsnova/dao/IDatabaseDao.java index 833761262..f910ecbba 100644 --- a/src/main/java/de/thm/arsnova/dao/IDatabaseDao.java +++ b/src/main/java/de/thm/arsnova/dao/IDatabaseDao.java @@ -222,4 +222,6 @@ public interface IDatabaseDao { void setVotingAdmissions(Session session, boolean disableVoting, List<Question> questions); List<Question> setVotingAdmissionForAllQuestions(Session session, boolean disableVoting); + + <T> T getObjectFromId(String documentId, Class<T> klass); } diff --git a/src/main/java/de/thm/arsnova/entities/Answer.java b/src/main/java/de/thm/arsnova/entities/Answer.java index ef2d18d82..4aa859228 100644 --- a/src/main/java/de/thm/arsnova/entities/Answer.java +++ b/src/main/java/de/thm/arsnova/entities/Answer.java @@ -38,6 +38,7 @@ public class Answer { private int piRound; private String user; private long timestamp; + private boolean read; private int answerCount = 1; private boolean abstention; private int abstentionCount; @@ -150,6 +151,14 @@ public class Answer { this.timestamp = timestamp; } + public boolean isRead() { + return read; + } + + public void setRead(boolean read) { + this.read = read; + } + public final int getAnswerCount() { return answerCount; } diff --git a/src/main/java/de/thm/arsnova/services/IQuestionService.java b/src/main/java/de/thm/arsnova/services/IQuestionService.java index 0a2f1fce3..a21d84244 100644 --- a/src/main/java/de/thm/arsnova/services/IQuestionService.java +++ b/src/main/java/de/thm/arsnova/services/IQuestionService.java @@ -59,6 +59,8 @@ public interface IQuestionService { Answer getMyAnswer(String questionId); + void readFreetextAnswer(String answerId, User user); + List<Answer> getAnswers(String questionId, int piRound); List<Answer> getAnswers(String questionId); diff --git a/src/main/java/de/thm/arsnova/services/QuestionService.java b/src/main/java/de/thm/arsnova/services/QuestionService.java index 450456526..5edb106f5 100644 --- a/src/main/java/de/thm/arsnova/services/QuestionService.java +++ b/src/main/java/de/thm/arsnova/services/QuestionService.java @@ -462,6 +462,22 @@ public class QuestionService implements IQuestionService, ApplicationEventPublis return databaseDao.getMyAnswer(userService.getCurrentUser(), questionId, question.getPiRound()); } + @Override + public void readFreetextAnswer(final String answerId, final User user) { + final Answer answer = databaseDao.getObjectFromId(answerId, Answer.class); + if (answer == null) { + throw new NotFoundException(); + } + if (answer.isRead()) { + return; + } + final Session session = databaseDao.getSessionFromId(answer.getSessionId()); + if (session.isCreator(user)) { + answer.setRead(true); + databaseDao.updateAnswer(answer); + } + } + @Override @PreAuthorize("isAuthenticated()") public List<Answer> getAnswers(final String questionId, final int piRound) { diff --git a/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java b/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java index b764d11f0..62b3b23ff 100644 --- a/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java +++ b/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java @@ -221,6 +221,18 @@ public class ARSnovaSocketIOServer implements ARSnovaSocket, NovaEventVisitor { } }); + server.addEventListener("readFreetextAnswer", String.class, new DataListener<String>() { + @Override + public void onData(SocketIOClient client, String answerId, AckRequest ackRequest) { + final User user = userService.getUser2SocketId(client.getSessionId()); + try { + questionService.readFreetextAnswer(answerId, user); + } catch (NotFoundException | UnauthorizedException e) { + LOGGER.error("Marking answer {} as read failed for user {} with exception {}", answerId, user, e.getMessage()); + } + } + }); + server.addEventListener( "setLearningProgressOptions", LearningProgressOptions.class, diff --git a/src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java b/src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java index 1c3604d8f..32ec1204a 100644 --- a/src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java +++ b/src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java @@ -640,4 +640,10 @@ public class StubDatabaseDao implements IDatabaseDao { // TODO Auto-generated method stub return null; } + + @Override + public <T> T getObjectFromId(String documentId, Class<T> klass) { + // TODO Auto-generated method stub + return null; + } } -- GitLab