Commit c18ffc9b authored by Daniel Gerhardt's avatar Daniel Gerhardt

Separate answer persistance code and migrate it to Ektorp

parent 1a7b2dcd
......@@ -23,6 +23,7 @@ import com.fasterxml.jackson.databind.SerializationFeature;
import de.thm.arsnova.ImageUtils;
import de.thm.arsnova.connector.client.ConnectorClient;
import de.thm.arsnova.connector.client.ConnectorClientImpl;
import de.thm.arsnova.entities.Answer;
import de.thm.arsnova.entities.Comment;
import de.thm.arsnova.entities.DbUser;
import de.thm.arsnova.entities.LogEntry;
......@@ -32,12 +33,14 @@ import de.thm.arsnova.entities.Session;
import de.thm.arsnova.entities.serialization.CouchDbDocumentModule;
import de.thm.arsnova.entities.serialization.CouchDbObjectMapperFactory;
import de.thm.arsnova.entities.serialization.View;
import de.thm.arsnova.persistance.AnswerRepository;
import de.thm.arsnova.persistance.CommentRepository;
import de.thm.arsnova.persistance.ContentRepository;
import de.thm.arsnova.persistance.LogEntryRepository;
import de.thm.arsnova.persistance.MotdRepository;
import de.thm.arsnova.persistance.SessionRepository;
import de.thm.arsnova.persistance.UserRepository;
import de.thm.arsnova.persistance.couchdb.CouchDbAnswerRepository;
import de.thm.arsnova.persistance.couchdb.CouchDbCommentRepository;
import de.thm.arsnova.persistance.couchdb.CouchDbContentRepository;
import de.thm.arsnova.persistance.couchdb.CouchDbLogEntryRepository;
......@@ -308,6 +311,11 @@ public class AppConfig extends WebMvcConfigurerAdapter {
return new CouchDbContentRepository(Content.class, couchDbConnector(), false);
}
@Bean
public AnswerRepository answerRepository() throws Exception {
return new CouchDbAnswerRepository(Answer.class, couchDbConnector(), false);
}
@Bean
public UserRepository userRepository() throws Exception {
return new CouchDbUserRepository(DbUser.class, couchDbConnector(), false);
......
......@@ -26,48 +26,11 @@ import java.util.List;
* All methods the database must support.
*/
public interface IDatabaseDao {
Answer getMyAnswer(User me, String questionId, int piRound);
List<Answer> getAnswers(Content content, int piRound);
List<Answer> getAnswers(Content content);
List<Answer> getAllAnswers(Content content);
int getAnswerCount(Content content, int piRound);
int getTotalAnswerCountByQuestion(Content content);
int getAbstentionAnswerCount(String questionId);
List<Answer> getFreetextAnswers(String questionId, final int start, final int limit);
List<Answer> getMyAnswers(User me, Session session);
int getTotalAnswerCount(String sessionKey);
int deleteAnswers(Content content);
Answer saveAnswer(Answer answer, User user, Content content, Session session);
Answer updateAnswer(Answer answer);
void deleteAnswer(String answerId);
int deleteInactiveGuestVisitedSessionLists(long lastActivityBefore);
int countLectureQuestionAnswers(Session session);
int countPreparationQuestionAnswers(Session session);
int deleteAllQuestionsAnswers(Session session);
CourseScore getLearningProgress(Session session);
int deleteAllPreparationAnswers(Session session);
int deleteAllLectureAnswers(Session session);
Statistics getStatistics();
<T> T getObjectFromId(String documentId, Class<T> klass);
......@@ -75,6 +38,4 @@ public interface IDatabaseDao {
MotdList getMotdListForUser(final String username);
MotdList createOrUpdateMotdList(MotdList motdlist);
int[] deleteAllAnswersWithQuestions(List<Content> contents);
}
......@@ -18,6 +18,8 @@
package de.thm.arsnova.entities;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonView;
import de.thm.arsnova.entities.serialization.View;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
......@@ -29,10 +31,9 @@ import java.io.Serializable;
* This class has additional fields to transport generated answer statistics.
*/
@ApiModel(value = "Answer", description = "the answer entity")
public class Answer implements Serializable {
private String _id;
private String _rev;
public class Answer implements Entity {
private String id;
private String rev;
private String type;
private String sessionId;
private String questionId;
......@@ -58,143 +59,158 @@ public class Answer implements Serializable {
}
@ApiModelProperty(required = true, value = "the couchDB ID")
public final String get_id() {
return _id;
}
public final void set_id(String _id) {
this._id = _id;
}
public final String get_rev() {
return _rev;
@JsonView({View.Persistence.class, View.Public.class})
public String getId() {
return id;
}
public final void set_rev(final String _rev) {
this._rev = _rev;
@JsonView({View.Persistence.class, View.Public.class})
public void setId(final String id) {
this.id = id;
}
@ApiModelProperty(required = true, value = "\"skill_question_answer\" - used to filter in the couchDB")
public final String getType() {
return type;
@JsonView({View.Persistence.class, View.Public.class})
public void setRevision(final String rev) {
this.rev = rev;
}
public final void setType(final String type) {
this.type = type;
@JsonView({View.Persistence.class, View.Public.class})
public String getRevision() {
return rev;
}
@ApiModelProperty(required = true, value = "ID of the session, the answer is assigned to")
@JsonView({View.Persistence.class, View.Public.class})
public final String getSessionId() {
return sessionId;
}
@JsonView({View.Persistence.class, View.Public.class})
public final void setSessionId(final String sessionId) {
this.sessionId = sessionId;
}
@ApiModelProperty(required = true, value = "used to display question id")
@JsonView({View.Persistence.class, View.Public.class})
public final String getQuestionId() {
return questionId;
}
@JsonView({View.Persistence.class, View.Public.class})
public final void setQuestionId(final String questionId) {
this.questionId = questionId;
}
@ApiModelProperty(required = true, value = "the answer text")
@JsonView({View.Persistence.class, View.Public.class})
public final String getAnswerText() {
return answerText;
}
@JsonView({View.Persistence.class, View.Public.class})
public final void setAnswerText(final String answerText) {
this.answerText = answerText;
}
@JsonView({View.Persistence.class, View.Public.class})
public final String getAnswerTextRaw() {
return this.answerTextRaw;
}
@JsonView({View.Persistence.class, View.Public.class})
public final void setAnswerTextRaw(final String answerTextRaw) {
this.answerTextRaw = answerTextRaw;
}
@ApiModelProperty(required = true, value = "the answer subject")
@JsonView({View.Persistence.class, View.Public.class})
public final String getAnswerSubject() {
return answerSubject;
}
@JsonView({View.Persistence.class, View.Public.class})
public final void setAnswerSubject(final String answerSubject) {
this.answerSubject = answerSubject;
}
@JsonView({View.Persistence.class, View.Public.class})
public final boolean isSuccessfulFreeTextAnswer() {
return this.successfulFreeTextAnswer;
}
@JsonView({View.Persistence.class, View.Public.class})
public final void setSuccessfulFreeTextAnswer(final boolean successfulFreeTextAnswer) {
this.successfulFreeTextAnswer = successfulFreeTextAnswer;
}
@ApiModelProperty(required = true, value = "the peer instruction round nr.")
@JsonView({View.Persistence.class, View.Public.class})
public int getPiRound() {
return piRound;
}
@JsonView({View.Persistence.class, View.Public.class})
public void setPiRound(int piRound) {
this.piRound = piRound;
}
/* TODO: use JsonViews instead of JsonIgnore when supported by Spring (4.1)
* http://wiki.fasterxml.com/JacksonJsonViews
* https://jira.spring.io/browse/SPR-7156 */
@ApiModelProperty(required = true, value = "the user")
@JsonIgnore
@JsonView(View.Persistence.class)
public final String getUser() {
return user;
}
@JsonView({View.Persistence.class, View.Public.class})
public final void setUser(final String user) {
this.user = user;
}
@ApiModelProperty(required = true, value = "the answer image")
@JsonIgnore
@JsonView(View.Persistence.class)
public String getAnswerImage() {
return answerImage;
}
@JsonView({View.Persistence.class, View.Public.class})
public void setAnswerImage(String answerImage) {
this.answerImage = answerImage;
}
@ApiModelProperty(required = true, value = "the answer thumbnail")
@JsonView({View.Persistence.class, View.Public.class})
public String getAnswerThumbnailImage() {
return answerThumbnailImage;
}
@JsonView({View.Persistence.class, View.Public.class})
public void setAnswerThumbnailImage(String answerThumbnailImage) {
this.answerThumbnailImage = answerThumbnailImage;
}
public final void setUser(final String user) {
this.user = user;
}
@ApiModelProperty(required = true, value = "the creation date timestamp")
@JsonView({View.Persistence.class, View.Public.class})
public long getTimestamp() {
return timestamp;
}
@JsonView(View.Persistence.class)
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
@ApiModelProperty(required = true, value = "displays whether the answer is read")
@JsonView({View.Persistence.class, View.Public.class})
public boolean isRead() {
return read;
}
@JsonView({View.Persistence.class, View.Public.class})
public void setRead(boolean read) {
this.read = read;
}
@ApiModelProperty(required = true, value = "the number of answers given. used for statistics")
@JsonView(View.Public.class)
public final int getAnswerCount() {
return answerCount;
}
......@@ -204,15 +220,18 @@ public class Answer implements Serializable {
}
@ApiModelProperty(required = true, value = "the abstention")
@JsonView({View.Persistence.class, View.Public.class})
public boolean isAbstention() {
return abstention;
}
@JsonView({View.Persistence.class, View.Public.class})
public void setAbstention(boolean abstention) {
this.abstention = abstention;
}
@ApiModelProperty(required = true, value = "the number of abstentions given. used for statistics")
@JsonView(View.Public.class)
public int getAbstentionCount() {
return abstentionCount;
}
......@@ -222,19 +241,23 @@ public class Answer implements Serializable {
}
@ApiModelProperty(required = true, value = "either lecture or preparation")
@JsonView({View.Persistence.class, View.Public.class})
public String getQuestionVariant() {
return questionVariant;
}
@JsonView({View.Persistence.class, View.Public.class})
public void setQuestionVariant(String questionVariant) {
this.questionVariant = questionVariant;
}
@ApiModelProperty(required = true, value = "used to display question value")
@JsonView({View.Persistence.class, View.Public.class})
public int getQuestionValue() {
return questionValue;
}
@JsonView({View.Persistence.class, View.Public.class})
public void setQuestionValue(int questionValue) {
this.questionValue = questionValue;
}
......@@ -255,8 +278,8 @@ public class Answer implements Serializable {
// auto generated!
final int prime = 31;
int result = 1;
result = prime * result + ((_id == null) ? 0 : _id.hashCode());
result = prime * result + ((_rev == null) ? 0 : _rev.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((rev == null) ? 0 : rev.hashCode());
result = prime * result + ((answerSubject == null) ? 0 : answerSubject.hashCode());
result = prime * result + ((answerText == null) ? 0 : answerText.hashCode());
result = prime * result + piRound;
......@@ -280,18 +303,18 @@ public class Answer implements Serializable {
return false;
}
Answer other = (Answer) obj;
if (_id == null) {
if (other._id != null) {
if (id == null) {
if (other.id != null) {
return false;
}
} else if (!_id.equals(other._id)) {
} else if (!id.equals(other.id)) {
return false;
}
if (_rev == null) {
if (other._rev != null) {
if (rev == null) {
if (other.rev != null) {
return false;
}
} else if (!_rev.equals(other._rev)) {
} else if (!rev.equals(other.rev)) {
return false;
}
if (answerSubject == null) {
......
......@@ -20,6 +20,7 @@ package de.thm.arsnova.entities.serialization;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.util.Converter;
import de.thm.arsnova.entities.Answer;
import de.thm.arsnova.entities.Comment;
import de.thm.arsnova.entities.DbUser;
import de.thm.arsnova.entities.Entity;
......@@ -41,6 +42,7 @@ public class CouchDbTypeFieldConverter implements Converter<Class<? extends Enti
typeMapping.put(Session.class, "session");
typeMapping.put(Comment.class, "interposed_question");
typeMapping.put(Content.class, "skill_question");
typeMapping.put(Answer.class, "skill_question_answer");
}
@Override
......
/*
* 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.persistance;
import de.thm.arsnova.entities.Answer;
import de.thm.arsnova.entities.Content;
import de.thm.arsnova.entities.Session;
import de.thm.arsnova.entities.User;
import java.util.List;
public interface AnswerRepository {
Answer get(String id);
Answer getMyAnswer(User me, String questionId, int piRound);
List<Answer> getAnswers(Content content, int piRound);
List<Answer> getAnswers(Content content);
List<Answer> getAllAnswers(Content content);
int getAnswerCount(Content content, int piRound);
int getTotalAnswerCountByQuestion(Content content);
int getAbstentionAnswerCount(String questionId);
List<Answer> getFreetextAnswers(String questionId, final int start, final int limit);
List<Answer> getMyAnswers(User me, Session session);
int getTotalAnswerCount(String sessionKey);
int deleteAnswers(Content content);
Answer saveAnswer(Answer answer, User user, Content content, Session session);
Answer updateAnswer(Answer answer);
void deleteAnswer(String answerId);
int countLectureQuestionAnswers(Session session);
int countPreparationQuestionAnswers(Session session);
int deleteAllQuestionsAnswers(Session session);
int deleteAllPreparationAnswers(Session session);
int deleteAllLectureAnswers(Session session);
int[] deleteAllAnswersWithQuestions(List<Content> contents);
}
package de.thm.arsnova.persistance.couchdb;
import de.thm.arsnova.dao.IDatabaseDao;
import de.thm.arsnova.entities.Content;
import de.thm.arsnova.entities.Session;
import de.thm.arsnova.entities.User;
import de.thm.arsnova.persistance.AnswerRepository;
import de.thm.arsnova.persistance.ContentRepository;
import de.thm.arsnova.persistance.LogEntryRepository;
import org.ektorp.ComplexKey;
......@@ -37,7 +37,7 @@ public class CouchDbContentRepository extends CouchDbRepositorySupport<Content>
private LogEntryRepository dbLogger;
@Autowired
private IDatabaseDao databaseDao;
private AnswerRepository answerRepository;
public CouchDbContentRepository(Class<Content> type, CouchDbConnector db, boolean createIfNotExists) {
super(type, db, createIfNotExists);
......@@ -145,7 +145,7 @@ public class CouchDbContentRepository extends CouchDbRepositorySupport<Content>
@Override
public int deleteQuestionWithAnswers(final Content content) {
try {
int count = databaseDao.deleteAnswers(content);
int count = answerRepository.deleteAnswers(content);
db.delete(content);
dbLogger.log("delete", "type", "content", "answerCount", count);
......@@ -181,7 +181,7 @@ public class CouchDbContentRepository extends CouchDbRepositorySupport<Content>
contents.add(q);
}
int[] count = databaseDao.deleteAllAnswersWithQuestions(contents);
int[] count = answerRepository.deleteAllAnswersWithQuestions(contents);
dbLogger.log("delete", "type", "question", "questionCount", count[0]);
dbLogger.log("delete", "type", "answer", "answerCount", count[1]);
......@@ -191,7 +191,7 @@ public class CouchDbContentRepository extends CouchDbRepositorySupport<Content>
@Override
public List<String> getUnAnsweredQuestionIds(final Session session, final User user) {
final ViewResult result = db.queryView(createQuery("questionid_by_user_sessionid_variant")
.designDocId("_design/answer")
.designDocId("_design/Answer")
.startKey(ComplexKey.of(user.getUsername(), session.getId()))
.endKey(ComplexKey.of(user.getUsername(), session.getId(), ComplexKey.emptyObject())));
List<String> answeredIds = new ArrayList<>();
......@@ -204,7 +204,7 @@ public class CouchDbContentRepository extends CouchDbRepositorySupport<Content>
@Override
public List<String> getUnAnsweredLectureQuestionIds(final Session session, final User user) {
final ViewResult result = db.queryView(createQuery("questionid_piround_by_user_sessionid_variant")
.designDocId("_design/answer")
.designDocId("_design/Answer")
.key(ComplexKey.of(user.getUsername(), session.getId(), "lecture")));
Map<String, Integer> answeredQuestions = new HashMap<>();
for (ViewResult.Row row : result.getRows()) {
......@@ -217,7 +217,7 @@ public class CouchDbContentRepository extends CouchDbRepositorySupport<Content>
@Override
public List<String> getUnAnsweredPreparationQuestionIds(final Session session, final User user) {
final ViewResult result = db.queryView(createQuery("questionid_piround_by_user_sessionid_variant")
.designDocId("_design/answer")
.designDocId("_design/Answer")
.key(ComplexKey.of(user.getUsername(), session.getId(), "preparation")));
Map<String, Integer> answeredQuestions = new HashMap<>();
for (ViewResult.Row row : result.getRows()) {
......
......@@ -504,11 +504,10 @@ public class CouchDbSessionRepository extends CouchDbRepositorySupport<Session>
}
private List<SessionInfo> getInfosForSessions(final List<Session> sessions) {
/* TODO: migrate to new view */
List<String> sessionIds = sessions.stream().map(Session::getId).collect(Collectors.toList());
final ViewQuery questionCountView = createQuery("by_sessionid").designDocId("_design/Content")
.group(true).keys(sessionIds);
final ViewQuery answerCountView = createQuery("by_sessionid").designDocId("_design/answer")
final ViewQuery answerCountView = createQuery("by_sessionid").designDocId("_design/Answer")
.group(true).keys(sessionIds);
final ViewQuery commentCountView = createQuery("by_sessionid").designDocId("_design/Comment")
.group(true).keys(sessionIds);
......@@ -519,7 +518,7 @@ public class CouchDbSessionRepository extends CouchDbRepositorySupport<Session>
}
private List<SessionInfo> getInfosForVisitedSessions(final List<Session> sessions, final User user) {
final ViewQuery answeredQuestionsView = createQuery("by_user_sessionid").designDocId("_design/answer")
final ViewQuery answeredQuestionsView = createQuery("by_user_sessionid").designDocId("_design/Answer")
.keys(sessions.stream().map(session -> ComplexKey.of(user.getUsername(), session.getId())).collect(Collectors.toList()));
final ViewQuery questionIdsView = createQuery("by_sessionid").designDocId("_design/Content")
.keys(sessions.stream().map(Session::getId).collect(Collectors.toList()));
......
......@@ -96,118 +96,18 @@ public class StubDatabaseDao implements IDatabaseDao {
stubQuestions.put("12345678", contents);
}
@Override
public Answer getMyAnswer(User user, String questionId, int piRound) {
// TODO Auto-generated method stub
return null;
}
@Override
public int getAnswerCount(Content content, int piRound) {
// TODO Auto-generated method stub
return 0;
}
@Override
public List<Answer> getFreetextAnswers(String questionId, final int start, final int limit) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<Answer> getMyAnswers(User user, Session session) {
return new ArrayList<>();
}
@Override
public int getTotalAnswerCount(String sessionKey) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int deleteAnswers(Content content) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Answer updateAnswer(Answer answer) {
// TODO Auto-generated method stub
return null;
}
@Override
public void deleteAnswer(String answerId) {
// TODO Auto-generated method stub
}
@Override
public int deleteInactiveGuestVisitedSessionLists(long lastActivityBefore) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int countLectureQuestionAnswers(Session session) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int countPreparationQuestionAnswers(Session session) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int deleteAllQuestionsAnswers(Session session) {
// TODO Auto-generated method stub
return 0;
}
@Override
public CourseScore getLearningProgress(Session session) {
// TODO Auto-generated method stub
return null;
}
@Override
public int deleteAllPreparationAnswers(Session session) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int deleteAllLectureAnswers(Session session) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getAbstentionAnswerCount(String questionId) {
// TODO Auto-generated method stub
return 0;
}
@Override
public List<Answer> getAnswers(Content content, int piRound) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<Answer> getAnswers(Content content) {
// TODO Auto-generated method stub
return null;
}
@Override
public Answer saveAnswer(Answer answer, User user, Content content, Session session) {
// TODO Auto-generated method stub
return null;
}
@Override
public Statistics getStatistics() {
final Statistics stats = new Statistics();
......@@ -219,18 +119,6 @@ public class StubDatabaseDao implements IDatabaseDao {
return stats;
}
@Override
public List<Answer> getAllAnswers(Content content) {
// TODO Auto-generated method stub
return null;
}
@Override
public int getTotalAnswerCountByQuestion(Content content) {
// TODO Auto-generated method stub
return 0;
}
@Override
public <T> T getObjectFromId(String documentId, Class<T> klass) {
// TODO Auto-generated method stub
......@@ -248,10 +136,4 @@ public class StubDatabaseDao implements IDatabaseDao {
// TODO Auto-generated method stub
return null;
}
@Override
public int[] deleteAllAnswersWithQuestions(List<Content> contents) {
// TODO Auto-generated method stub
return new int[0];
}
}
Markdown is supported
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