From d158d1128dd3755e578b3bd2312735610fc9fb0c Mon Sep 17 00:00:00 2001 From: Christoph Thelen <christoph.thelen@mni.thm.de> Date: Fri, 15 Feb 2013 17:27:56 +0100 Subject: [PATCH] Fixed counting of interposed questions --- .../QuestionByAudienceController.java | 4 +- .../java/de/thm/arsnova/dao/CouchDBDao.java | 60 +++++++++++++------ .../java/de/thm/arsnova/dao/IDatabaseDao.java | 6 +- .../arsnova/services/IQuestionService.java | 2 + .../thm/arsnova/services/QuestionService.java | 34 +++++++---- .../de/thm/arsnova/dao/StubDatabaseDao.java | 9 ++- 6 files changed, 80 insertions(+), 35 deletions(-) diff --git a/src/main/java/de/thm/arsnova/controller/QuestionByAudienceController.java b/src/main/java/de/thm/arsnova/controller/QuestionByAudienceController.java index 46bfe1457..98eb60d9d 100644 --- a/src/main/java/de/thm/arsnova/controller/QuestionByAudienceController.java +++ b/src/main/java/de/thm/arsnova/controller/QuestionByAudienceController.java @@ -105,13 +105,13 @@ public class QuestionByAudienceController extends AbstractController { throw new BadRequestException(); } - + @RequestMapping(value = "/{questionId}", method = RequestMethod.DELETE) @ResponseBody public final void deleteInterposedQuestion( @PathVariable final String questionId, final HttpServletResponse response ) { - questionService.deleteQuestion(questionId); + questionService.deleteInterposedQuestion(questionId); } } diff --git a/src/main/java/de/thm/arsnova/dao/CouchDBDao.java b/src/main/java/de/thm/arsnova/dao/CouchDBDao.java index 9dfdaffa0..45319bdda 100644 --- a/src/main/java/de/thm/arsnova/dao/CouchDBDao.java +++ b/src/main/java/de/thm/arsnova/dao/CouchDBDao.java @@ -760,13 +760,17 @@ public class CouchDBDao implements IDatabaseDao { public final void deleteQuestion(final Question question) { try { this.deleteAnswers(question); - Document q = this.getDatabase().getDocument(question.get_id()); - this.getDatabase().deleteDocument(q); + this.deleteDocument(question.get_id()); } catch (IOException e) { LOGGER.error("IOException: Could not delete question {}", question.get_id()); } } - + + private void deleteDocument(final String documentId) throws IOException { + Document d = this.getDatabase().getDocument(documentId); + this.getDatabase().deleteDocument(d); + } + @Override public final void deleteAnswers(final Question question) { try { @@ -775,8 +779,7 @@ public class CouchDBDao implements IDatabaseDao { ViewResults results = this.getDatabase().view(view); for (Document d : results.getResults()) { - Document answer = this.getDatabase().getDocument(d.getId()); - this.getDatabase().deleteDocument(answer); + this.deleteDocument(d.getId()); } } catch (IOException e) { LOGGER.error("IOException: Could not delete answers for question {}", question.get_id()); @@ -1047,7 +1050,10 @@ public class CouchDBDao implements IDatabaseDao { return new InterposedReadingCount(); } int read = results.getJSONArray("rows").optJSONObject(0).optInt("value"); - int unread = results.getJSONArray("rows").optJSONObject(1).optInt("value"); + int unread = 0; + if (results.getJSONArray("rows").optJSONObject(1) != null) { + unread = results.getJSONArray("rows").optJSONObject(1).optInt("value"); + } return new InterposedReadingCount(read, unread); } catch (UnsupportedEncodingException e) { LOGGER.error("Error while retrieving interposed question count", e); @@ -1234,22 +1240,29 @@ public class CouchDBDao implements IDatabaseDao { } @Override - public InterposedQuestion getInterposedQuestion(String questionId) throws IOException { - Document document = this.getDatabase().getDocument(questionId); - InterposedQuestion question = (InterposedQuestion) JSONObject.toBean( - document.getJSONObject(), - InterposedQuestion.class - ); - question.setSessionId(getSessionKeyword(question.getSessionId())); - return question; + public InterposedQuestion getInterposedQuestion(String questionId) { + try { + Document document = this.getDatabase().getDocument(questionId); + InterposedQuestion question = (InterposedQuestion) JSONObject.toBean(document.getJSONObject(), + InterposedQuestion.class); + question.setSessionId(getSessionKeyword(question.getSessionId())); + return question; + } catch (IOException e) { + LOGGER.error("Could not load interposed question {}", questionId); + } + return null; } @Override - public void markInterposedQuestionAsRead(InterposedQuestion question) throws IOException { - question.setRead(true); - Document document = this.getDatabase().getDocument(question.get_id()); - document.put("read", question.isRead()); - this.getDatabase().saveDocument(document); + public void markInterposedQuestionAsRead(InterposedQuestion question) { + try { + question.setRead(true); + Document document = this.getDatabase().getDocument(question.get_id()); + document.put("read", question.isRead()); + this.getDatabase().saveDocument(document); + } catch (IOException e) { + LOGGER.error("Coulg not mark interposed question as read {}", question.get_id()); + } } @Override @@ -1329,4 +1342,13 @@ public class CouchDBDao implements IDatabaseDao { LOGGER.error("Could not delete answer {} because of {}", answerId, e.getMessage()); } } + + @Override + public void deleteInterposedQuestion(InterposedQuestion question) { + try { + this.deleteDocument(question.get_id()); + } catch (IOException e) { + LOGGER.error("Could not delete interposed question {} because of {}", question.get_id(), e.getMessage()); + } + } } diff --git a/src/main/java/de/thm/arsnova/dao/IDatabaseDao.java b/src/main/java/de/thm/arsnova/dao/IDatabaseDao.java index 9ef8ba099..2617f6921 100644 --- a/src/main/java/de/thm/arsnova/dao/IDatabaseDao.java +++ b/src/main/java/de/thm/arsnova/dao/IDatabaseDao.java @@ -109,9 +109,9 @@ public interface IDatabaseDao { int countQuestions(); - InterposedQuestion getInterposedQuestion(String questionId) throws IOException; + InterposedQuestion getInterposedQuestion(String questionId); - void markInterposedQuestionAsRead(InterposedQuestion question) throws IOException; + void markInterposedQuestionAsRead(InterposedQuestion question); List<Session> getMyVisitedSessions(User user); @@ -126,4 +126,6 @@ public interface IDatabaseDao { Session getSessionFromId(String sessionId); void deleteAnswer(String answerId); + + void deleteInterposedQuestion(InterposedQuestion question); } diff --git a/src/main/java/de/thm/arsnova/services/IQuestionService.java b/src/main/java/de/thm/arsnova/services/IQuestionService.java index 7c6f91f5f..444d3c01d 100644 --- a/src/main/java/de/thm/arsnova/services/IQuestionService.java +++ b/src/main/java/de/thm/arsnova/services/IQuestionService.java @@ -73,4 +73,6 @@ public interface IQuestionService { void deleteAnswer(String questionId, String answerId); + void deleteInterposedQuestion(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 afdc047be..947bd4e7a 100644 --- a/src/main/java/de/thm/arsnova/services/QuestionService.java +++ b/src/main/java/de/thm/arsnova/services/QuestionService.java @@ -117,6 +117,21 @@ public class QuestionService implements IQuestionService { } databaseDao.deleteQuestion(question); } + + @Override + @Authenticated + public void deleteInterposedQuestion(String questionId) { + InterposedQuestion question = databaseDao.getInterposedQuestion(questionId); + if (question == null) { + throw new NotFoundException(); + } + User user = userService.getCurrentUser(); + Session session = databaseDao.getSessionFromKeyword(question.getSessionId()); + if (user == null || session == null || !session.isCreator(user)) { + throw new UnauthorizedException(); + } + databaseDao.deleteInterposedQuestion(question); + } @Override @Authenticated @@ -206,18 +221,17 @@ public class QuestionService implements IQuestionService { @Override @Authenticated public InterposedQuestion readInterposedQuestion(String questionId) { - try { - InterposedQuestion question = databaseDao.getInterposedQuestion(questionId); - Session session = this.databaseDao.getSessionFromKeyword(question.getSessionId()); - - User user = this.userService.getCurrentUser(); - if (session.isCreator(user)) { - this.databaseDao.markInterposedQuestionAsRead(question); - } - return question; - } catch (IOException e) { + InterposedQuestion question = databaseDao.getInterposedQuestion(questionId); + if (question == null) { throw new NotFoundException(); } + Session session = this.databaseDao.getSessionFromKeyword(question.getSessionId()); + + User user = this.userService.getCurrentUser(); + if (session.isCreator(user)) { + this.databaseDao.markInterposedQuestionAsRead(question); + } + return question; } @Override diff --git a/src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java b/src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java index e80b28c33..46a8abbb4 100644 --- a/src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java +++ b/src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java @@ -338,12 +338,12 @@ public class StubDatabaseDao implements IDatabaseDao { } @Override - public InterposedQuestion getInterposedQuestion(String questionId) throws IOException { + public InterposedQuestion getInterposedQuestion(String questionId) { return this.interposedQuestion; } @Override - public void markInterposedQuestionAsRead(InterposedQuestion question) throws IOException { + public void markInterposedQuestionAsRead(InterposedQuestion question) { this.interposedQuestion.setRead(true); } @@ -408,4 +408,9 @@ public class StubDatabaseDao implements IDatabaseDao { public void deleteAnswer(String answerId) { // TODO Auto-generated method stub } + + @Override + public void deleteInterposedQuestion(InterposedQuestion question) { + // TODO Auto-generated method stub + } } -- GitLab