diff --git a/src/main/java/de/thm/arsnova/dao/CouchDBDao.java b/src/main/java/de/thm/arsnova/dao/CouchDBDao.java index a799fc4f1874258b5c794ed6bd9590ac67f63e01..3740e71bfe374b832096d33882953bfa83dd5c2c 100644 --- a/src/main/java/de/thm/arsnova/dao/CouchDBDao.java +++ b/src/main/java/de/thm/arsnova/dao/CouchDBDao.java @@ -53,6 +53,7 @@ import de.thm.arsnova.entities.Session; import de.thm.arsnova.entities.User; import de.thm.arsnova.services.ISessionService; import de.thm.arsnova.services.IUserService; +import de.thm.arsnova.socket.message.Question; @Component public class CouchDBDao implements IDatabaseDao { @@ -145,17 +146,7 @@ public class CouchDBDao implements IDatabaseDao { @Override public Session getSession(String keyword) { - View view = new View("session/by_keyword"); - view.setKey(URLEncoder.encode("\"" + keyword + "\"")); - ViewResults results = this.getDatabase().view(view); - - if (results.getJSONArray("rows").optJSONObject(0) == null) - return null; - - Session result = (Session) JSONObject.toBean( - results.getJSONArray("rows").optJSONObject(0) - .optJSONObject("value"), Session.class); - + Session result = this.getSessionFromKeyword(keyword); if (result.isActive() || result.getCreator().equals(this.actualUserName())) { sessionService.addUserToSessionMap(this.actualUserName(), keyword); return result; @@ -164,6 +155,23 @@ public class CouchDBDao implements IDatabaseDao { return null; } + @Override + public Session getSessionFromKeyword(String keyword) { + try { + View view = new View("session/by_keyword"); + view.setKey(URLEncoder.encode("\"" + keyword + "\"", "UTF-8")); + ViewResults results = this.getDatabase().view(view); + + if (results.getJSONArray("rows").optJSONObject(0) == null) + return null; + + return (Session) JSONObject.toBean( + results.getJSONArray("rows").optJSONObject(0).optJSONObject("value"), Session.class); + } catch (UnsupportedEncodingException e) { + return null; + } + } + @Override public Session saveSession(Session session) { @@ -373,4 +381,25 @@ public class CouchDBDao implements IDatabaseDao { return database; } + + @Override + public boolean saveQuestion(Session session, Question question) { + Document q = new Document(); + q.put("type", "skill_question"); + q.put("questionType", question.getQuestionType()); + q.put("sessionId", session.get_id()); + q.put("subject", question.getSubject()); + q.put("text", question.getText()); + q.put("active", question.isActive()); + q.put("number", 0); // TODO: This number has to get incremented automatically + q.put("releasedFor", question.getReleasedFor()); + q.put("possibleAnswers", question.getPossibleAnswers()); + q.put("noCorrect", question.isNoCorrect()); + try { + database.saveDocument(q); + } catch (IOException e) { + logger.error("Could not save question {}", question); + } + return false; + } } diff --git a/src/main/java/de/thm/arsnova/dao/IDatabaseDao.java b/src/main/java/de/thm/arsnova/dao/IDatabaseDao.java index 4d41043b543089feeaac2ff8c2b65afadcec9352..84bd0a7b8639ea78bce800f266167ba9aeb238aa 100644 --- a/src/main/java/de/thm/arsnova/dao/IDatabaseDao.java +++ b/src/main/java/de/thm/arsnova/dao/IDatabaseDao.java @@ -22,12 +22,16 @@ package de.thm.arsnova.dao; import de.thm.arsnova.entities.Feedback; import de.thm.arsnova.entities.Session; import de.thm.arsnova.entities.User; +import de.thm.arsnova.socket.message.Question; public interface IDatabaseDao { public void cleanFeedbackVotes(int cleanupFeedbackDelay); + public Session getSessionFromKeyword(String keyword); public Session getSession(String keyword); public Session saveSession(Session session); public Feedback getFeedback(String keyword); public boolean saveFeedback(String keyword, int value, User user); public boolean sessionKeyAvailable(String keyword); + + public boolean saveQuestion(Session session, Question question); } \ No newline at end of file diff --git a/src/main/java/de/thm/arsnova/entities/Session.java b/src/main/java/de/thm/arsnova/entities/Session.java index ad486b70bd97fa1b39cc3b57c2d58c1a1a4cfece..ba107fe106dc9bd5d70863f46458a55ed9894b55 100644 --- a/src/main/java/de/thm/arsnova/entities/Session.java +++ b/src/main/java/de/thm/arsnova/entities/Session.java @@ -67,7 +67,12 @@ public class Session { this.active = active; } - public void set_id(String id) {} - public void set_rev(String rev) {} + public void set_id(String id) { + _id = id; + } + public String get_id() { + return _id; + } + public void set_rev(String rev) {} } diff --git a/src/main/java/de/thm/arsnova/services/ISessionService.java b/src/main/java/de/thm/arsnova/services/ISessionService.java index cbcd5c85631fb76924f0e42837eb97bfee643a62..0dfb1c8dee82d3eeed5f946e8fcd1be47b256d2b 100644 --- a/src/main/java/de/thm/arsnova/services/ISessionService.java +++ b/src/main/java/de/thm/arsnova/services/ISessionService.java @@ -27,6 +27,8 @@ import de.thm.arsnova.entities.Feedback; import de.thm.arsnova.entities.Session; import de.thm.arsnova.entities.User; +import de.thm.arsnova.socket.message.Question; + public interface ISessionService { @@ -41,4 +43,6 @@ public interface ISessionService { public boolean isUserInSession(User user, String keyword); public List<String> getUsersInSession(String keyword); public void broadcastFeedbackChanges(Map<String, Set<String>> affectedUsers, Set<String> allAffectedSessions); + + public boolean saveQuestion(Question question); } \ No newline at end of file diff --git a/src/main/java/de/thm/arsnova/services/SessionService.java b/src/main/java/de/thm/arsnova/services/SessionService.java index 4edd169b5e5bceac05261c6889c9802720a546fb..e169cb00977e9a902561cb966d373ac97394edb6 100644 --- a/src/main/java/de/thm/arsnova/services/SessionService.java +++ b/src/main/java/de/thm/arsnova/services/SessionService.java @@ -38,6 +38,7 @@ import de.thm.arsnova.entities.Feedback; import de.thm.arsnova.entities.Session; import de.thm.arsnova.entities.User; import de.thm.arsnova.socket.ARSnovaSocketIOServer; +import de.thm.arsnova.socket.message.Question; @Service public class SessionService implements ISessionService { @@ -141,4 +142,10 @@ public class SessionService implements ISessionService { if (this.sessionKeyAvailable(keyword)) return keyword; return generateKeyword(); } + + @Override + public boolean saveQuestion(Question question) { + Session session = this.databaseDao.getSessionFromKeyword(question.getSession()); + return this.databaseDao.saveQuestion(session, question); + } } diff --git a/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java b/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java index ccf4f80ceea5aaead7e4279d1954afce620f8e89..d7485bfc57ebd62411f8dd9a369a609722483a69 100644 --- a/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java +++ b/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java @@ -25,6 +25,7 @@ import com.corundumstudio.socketio.listener.DisconnectListener; import de.thm.arsnova.entities.User; import de.thm.arsnova.services.ISessionService; import de.thm.arsnova.socket.message.Feedback; +import de.thm.arsnova.socket.message.Question; public class ARSnovaSocketIOServer { @@ -95,9 +96,13 @@ public class ARSnovaSocketIOServer { } } }); - - + server.addEventListener("arsnova/question/create", Question.class, new DataListener<Question>(){ + @Override + public void onData(SocketIOClient client, Question question) { + sessionService.saveQuestion(question); + } + }); server.addConnectListener(new ConnectListener() { @Override diff --git a/src/main/java/de/thm/arsnova/socket/message/PossibleAnswer.java b/src/main/java/de/thm/arsnova/socket/message/PossibleAnswer.java new file mode 100644 index 0000000000000000000000000000000000000000..c25f64025f3459819a0193e8d6f56f7b3d278f0a --- /dev/null +++ b/src/main/java/de/thm/arsnova/socket/message/PossibleAnswer.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2012 THM webMedia + * + * This file is part of ARSnova. + * + * ARSnova 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 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.socket.message; + +public class PossibleAnswer { + + private String text; + private boolean correct; + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public boolean isCorrect() { + return correct; + } + + public void setCorrect(boolean correct) { + this.correct = correct; + } + + @Override + public String toString() { + return "PossibleAnswer [text=" + text + ", correct=" + correct + "]"; + } +} diff --git a/src/main/java/de/thm/arsnova/socket/message/Question.java b/src/main/java/de/thm/arsnova/socket/message/Question.java new file mode 100644 index 0000000000000000000000000000000000000000..e271fcc43f742acc63da1f60599ea06fb1a00815 --- /dev/null +++ b/src/main/java/de/thm/arsnova/socket/message/Question.java @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2012 THM webMedia + * + * This file is part of ARSnova. + * + * ARSnova 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 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.socket.message; + +import java.util.List; + +public class Question { + + private String questionType; + private String subject; + private String text; + private boolean active; + private String releasedFor; + private List<PossibleAnswer> possibleAnswers; + private boolean noCorrect; + private String session; + + public String getQuestionType() { + return questionType; + } + + public void setQuestionType(String questionType) { + this.questionType = questionType; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public boolean isActive() { + return active; + } + + public void setActive(boolean active) { + this.active = active; + } + + public String getReleasedFor() { + return releasedFor; + } + + public void setReleasedFor(String releasedFor) { + this.releasedFor = releasedFor; + } + + public List<PossibleAnswer> getPossibleAnswers() { + return possibleAnswers; + } + + public void setPossibleAnswers(List<PossibleAnswer> possibleAnswers) { + this.possibleAnswers = possibleAnswers; + } + + public boolean isNoCorrect() { + return noCorrect; + } + + public void setNoCorrect(boolean noCorrect) { + this.noCorrect = noCorrect; + } + + public String getSession() { + return session; + } + + public void setSession(String session) { + this.session = session; + } + + @Override + public String toString() { + return "Question type '" + this.questionType + "': " + this.subject + ";\n" + this.text; + } +}