Skip to content
Snippets Groups Projects
Commit 717a5bb5 authored by Christoph Thelen's avatar Christoph Thelen
Browse files

Implemented #4032, #4033: 'interposed question' functionality

parent 391e8b23
Branches
Tags
No related merge requests found
......@@ -268,8 +268,17 @@ public class QuestionController extends AbstractController {
) {
return questionService.getInterposedQuestions(sessionKey);
}
@RequestMapping(value = "/session/{sessionKey}/interposed/{questionId}", method = RequestMethod.GET)
@ResponseBody
public final InterposedQuestion getInterposedQuestions(
@PathVariable final String sessionKey,
@PathVariable final String questionId,
final HttpServletResponse response
) {
return questionService.readInterposedQuestion(sessionKey, questionId);
}
@RequestMapping(value = "/session/{sessionkey}/interposed", method = RequestMethod.POST)
@ResponseBody
public final void postInterposedQuestion(
......@@ -277,7 +286,7 @@ public class QuestionController extends AbstractController {
@RequestBody final InterposedQuestion question,
final HttpServletResponse response
) {
if (!sessionkey.equals(question.getSession())) {
if (!sessionkey.equals(question.getSessionId())) {
response.setStatus(HttpStatus.PRECONDITION_FAILED.value());
return;
}
......@@ -290,5 +299,15 @@ public class QuestionController extends AbstractController {
response.setStatus(HttpStatus.BAD_REQUEST.value());
return;
}
@RequestMapping(value = "/session/{sessionkey}/interposed/{questionId}", method = RequestMethod.DELETE)
@ResponseBody
public final void deleteInterposedQuestion(
@PathVariable final String sessionkey,
@PathVariable final String questionId,
final HttpServletResponse response
) {
questionService.deleteQuestion(sessionkey, questionId);
}
}
......@@ -481,20 +481,12 @@ public class CouchDBDao implements IDatabaseDao {
return results.getJSONArray("rows").optJSONObject(0).optJSONObject("value").getString("_id");
}
private String getSessionKeyword(final String internalSessionId) {
try {
View view = new View("session/by_id");
view.setKey(URLEncoder.encode("\"" + internalSessionId + "\"", "UTF-8"));
ViewResults results = this.getDatabase().view(view);
for (Document d : results.getResults()) {
Document arsSession = this.getDatabase().getDocument(d.getId());
return arsSession.get("keyword").toString();
}
} catch (UnsupportedEncodingException e) {
return null;
} catch (IOException e) {
return null;
private String getSessionKeyword(final String internalSessionId) throws IOException {
Document document = this.getDatabase().getDocument(internalSessionId);
if (document.has("keyword")) {
return (String) document.get("keyword");
}
LOGGER.error("No session found for internal id: {}", internalSessionId);
return null;
}
......@@ -1066,7 +1058,7 @@ public class CouchDBDao implements IDatabaseDao {
document.getJSONObject().getJSONObject("value"),
InterposedQuestion.class
);
question.setSession(sessionKey);
question.setSessionId(sessionKey);
question.set_id(document.getId());
result.add(question);
}
......@@ -1206,4 +1198,23 @@ public class CouchDBDao implements IDatabaseDao {
}
return 0;
}
@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;
}
@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);
}
}
......@@ -19,6 +19,7 @@
package de.thm.arsnova.dao;
import java.io.IOException;
import java.util.List;
import de.thm.arsnova.entities.Answer;
......@@ -105,4 +106,8 @@ public interface IDatabaseDao {
int countQuestions();
InterposedQuestion getInterposedQuestion(String questionId) throws IOException;
void markInterposedQuestionAsRead(InterposedQuestion question) throws IOException;
}
......@@ -27,7 +27,7 @@ public class InterposedQuestion {
private String type;
private String subject;
private String text;
private String session;
private String sessionId;
private long timestamp;
private boolean read;
......@@ -67,11 +67,11 @@ public class InterposedQuestion {
public void setText(String text) {
this.text = text;
}
public String getSession() {
return session;
public String getSessionId() {
return sessionId;
}
public void setSession(String session) {
this.session = session;
public void setSessionId(String sessionId) {
this.sessionId = sessionId;
}
public long getTimestamp() {
return timestamp;
......
......@@ -101,4 +101,8 @@ public class Session {
public String get_rev() {
return _rev;
}
public boolean isCreator(User user) {
return user.getUsername().equals(this.creator);
}
}
......@@ -19,6 +19,8 @@
package de.thm.arsnova.services;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import de.thm.arsnova.entities.Answer;
......@@ -27,7 +29,7 @@ import de.thm.arsnova.entities.Question;
public interface IQuestionService {
boolean saveQuestion(Question question);
boolean saveQuestion(InterposedQuestion question);
Question getQuestion(String id, String sessionkey);
......@@ -58,4 +60,6 @@ public interface IQuestionService {
List<InterposedQuestion> getInterposedQuestions(String sessionKey);
InterposedQuestion readInterposedQuestion(String sessionKey, String questionId);
}
/*
* Copyright (C) 2012 THM webMedia
*
*
* This file is part of ARSnova.
*
* ARSnova is free software: you can redistribute it and/or modify
......@@ -19,6 +19,7 @@
package de.thm.arsnova.services;
import java.io.IOException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -30,7 +31,9 @@ import de.thm.arsnova.entities.Answer;
import de.thm.arsnova.entities.InterposedQuestion;
import de.thm.arsnova.entities.Question;
import de.thm.arsnova.entities.Session;
import de.thm.arsnova.entities.User;
import de.thm.arsnova.exceptions.NoContentException;
import de.thm.arsnova.exceptions.NotFoundException;
@Service
public class QuestionService implements IQuestionService {
......@@ -38,6 +41,9 @@ public class QuestionService implements IQuestionService {
@Autowired
private IDatabaseDao databaseDao;
@Autowired
private IUserService userService;
public void setDatabaseDao(IDatabaseDao databaseDao) {
this.databaseDao = databaseDao;
}
......@@ -68,17 +74,16 @@ public class QuestionService implements IQuestionService {
@Override
@Authenticated
public boolean saveQuestion(InterposedQuestion question) {
Session session = this.databaseDao.getSessionFromKeyword(question.getSession());
Session session = this.databaseDao.getSessionFromKeyword(question.getSessionId());
return this.databaseDao.saveQuestion(session, question);
}
@Override
@Authenticated
public Question getQuestion(String id, String sessionKey) {
return databaseDao.getQuestion(id, sessionKey);
}
@Override
@Authenticated
public List<String> getQuestionIds(String sessionKey) {
......@@ -89,7 +94,6 @@ public class QuestionService implements IQuestionService {
@Authenticated
public void deleteQuestion(String sessionKey, String questionId) {
databaseDao.deleteQuestion(sessionKey, questionId);
}
@Override
......@@ -109,40 +113,59 @@ public class QuestionService implements IQuestionService {
public List<Answer> getAnswers(String sessionKey, String questionId) {
return databaseDao.getAnswers(sessionKey, questionId);
}
@Override
@Authenticated
public int getAnswerCount(String sessionKey, String questionId) {
return databaseDao.getAnswerCount(sessionKey, questionId);
}
@Override
@Authenticated
public List<Answer> getFreetextAnswers(String sessionKey, String questionId) {
return databaseDao.getFreetextAnswers(sessionKey, questionId);
}
@Override
@Authenticated
public List<Answer> getMytAnswers(String sessionKey) {
return databaseDao.getMyAnswers(sessionKey);
}
@Override
@Authenticated
public int getTotalAnswerCount(String sessionKey) {
return databaseDao.getTotalAnswerCount(sessionKey);
}
@Override
@Authenticated
public int getInterposedCount(String sessionKey) {
return databaseDao.getInterposedCount(sessionKey);
}
@Override
@Authenticated
public List<InterposedQuestion> getInterposedQuestions(String sessionKey) {
return databaseDao.getInterposedQuestions(sessionKey);
}
@Override
public InterposedQuestion readInterposedQuestion(String sessionKey, String questionId) {
try {
InterposedQuestion question = databaseDao.getInterposedQuestion(questionId);
Session session = this.databaseDao.getSessionFromKeyword(sessionKey);
if (session == null || !session.getKeyword().equals(question.getSessionId())) {
throw new NotFoundException();
}
User user = this.userService.getCurrentUser();
if (session.isCreator(user)) {
this.databaseDao.markInterposedQuestionAsRead(question);
}
return question;
} catch (IOException e) {
throw new NotFoundException();
}
}
}
package de.thm.arsnova.dao;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
......@@ -330,4 +331,16 @@ public class StubDatabaseDao implements IDatabaseDao {
// TODO Auto-generated method stub
return false;
}
@Override
public InterposedQuestion getInterposedQuestion(String questionId) throws IOException {
// TODO Auto-generated method stub
return null;
}
@Override
public void markInterposedQuestionAsRead(InterposedQuestion question) throws IOException {
// TODO Auto-generated method stub
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment