Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • arsnova/arsnova-backend
  • pcvl72/arsnova-backend
  • tksl38/arsnova-backend
3 results
Show changes
Showing
with 1424 additions and 2735 deletions
/*
* This file is part of ARSnova Backend.
* Copyright (C) 2012-2015 The ARSnova Team
* Copyright (C) 2012-2019 The ARSnova Team and Contributors
*
* 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
......@@ -15,87 +15,100 @@
* 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.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
package de.thm.arsnova.controller.v2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import de.thm.arsnova.entities.Feedback;
import de.thm.arsnova.entities.User;
import de.thm.arsnova.exceptions.NotFoundException;
import de.thm.arsnova.services.IFeedbackService;
import de.thm.arsnova.services.IUserService;
import de.thm.arsnova.controller.AbstractController;
import de.thm.arsnova.model.Feedback;
import de.thm.arsnova.security.User;
import de.thm.arsnova.service.FeedbackService;
import de.thm.arsnova.service.RoomService;
import de.thm.arsnova.service.UserService;
import de.thm.arsnova.web.DeprecatedApi;
import de.thm.arsnova.web.exceptions.NotFoundException;
import de.thm.arsnova.websocket.ArsnovaSocketioServerImpl;
@RestController
/**
* Handles requests concerning the user's feedback, i.e., "too fast" or "faster, please". This HTTP API is
* deprecated in favor of the socket implementation.
*
* @see ArsnovaSocketioServerImpl
*/
@RestController("v2FeedbackController")
@RequestMapping("/v2/session/{shortId}")
public class FeedbackController extends AbstractController {
public static final Logger LOGGER = LoggerFactory.getLogger(FeedbackController.class);
@Autowired
private FeedbackService feedbackService;
@Autowired
private IFeedbackService feedbackService;
private RoomService roomService;
@Autowired
private IUserService userService;
private UserService userService;
@DeprecatedApi
@RequestMapping(value = "/session/{sessionkey}/feedback", method = RequestMethod.GET)
public final Feedback getFeedback(@PathVariable final String sessionkey) {
return feedbackService.getFeedback(sessionkey);
@Deprecated
@GetMapping("/feedback")
public Feedback getFeedback(@PathVariable final String shortId) {
return feedbackService.getByRoomId(roomService.getIdByShortId(shortId));
}
@DeprecatedApi
@RequestMapping(value = "/session/{sessionkey}/myfeedback", method = RequestMethod.GET)
public final Integer getMyFeedback(@PathVariable final String sessionkey) {
Integer value = feedbackService.getMyFeedback(sessionkey, userService.getCurrentUser());
@Deprecated
@GetMapping(value = "/myfeedback", produces = MediaType.TEXT_PLAIN_VALUE)
public String getMyFeedback(@PathVariable final String shortId) {
final String roomId = roomService.getIdByShortId(shortId);
final Integer value = feedbackService.getByRoomIdAndUserId(roomId, userService.getCurrentUser().getId());
if (value != null && value >= Feedback.MIN_FEEDBACK_TYPE && value <= Feedback.MAX_FEEDBACK_TYPE) {
return value;
return value.toString();
}
throw new NotFoundException();
}
@DeprecatedApi
@RequestMapping(value = "/session/{sessionkey}/feedbackcount", method = RequestMethod.GET)
public final int getFeedbackCount(@PathVariable final String sessionkey) {
return feedbackService.getFeedbackCount(sessionkey);
@Deprecated
@GetMapping(value = "/feedbackcount", produces = MediaType.TEXT_PLAIN_VALUE)
public String getFeedbackCount(@PathVariable final String shortId) {
return String.valueOf(feedbackService.countFeedbackByRoomId(roomService.getIdByShortId(shortId)));
}
@DeprecatedApi
@RequestMapping(value = "/session/{sessionkey}/roundedaveragefeedback", method = RequestMethod.GET)
public final long getAverageFeedbackRounded(@PathVariable final String sessionkey) {
return feedbackService.getAverageFeedbackRounded(sessionkey);
@Deprecated
@GetMapping(value = "/roundedaveragefeedback", produces = MediaType.TEXT_PLAIN_VALUE)
public String getAverageFeedbackRounded(@PathVariable final String shortId) {
return String.valueOf(feedbackService.calculateRoundedAverageFeedback(roomService.getIdByShortId(shortId)));
}
@DeprecatedApi
@RequestMapping(value = "/session/{sessionkey}/averagefeedback", method = RequestMethod.GET)
public final double getAverageFeedback(@PathVariable final String sessionkey) {
return feedbackService.getAverageFeedback(sessionkey);
@Deprecated
@GetMapping(value = "/averagefeedback", produces = MediaType.TEXT_PLAIN_VALUE)
public String getAverageFeedback(@PathVariable final String shortId) {
return String.valueOf(feedbackService.calculateAverageFeedback(roomService.getIdByShortId(shortId)));
}
@DeprecatedApi
@RequestMapping(value = "/session/{sessionkey}/feedback", method = RequestMethod.POST)
@Deprecated
@PostMapping("/feedback")
@ResponseStatus(HttpStatus.CREATED)
public final Feedback postFeedback(
@PathVariable final String sessionkey,
@RequestBody final int value
) {
User user = userService.getCurrentUser();
if (feedbackService.saveFeedback(sessionkey, value, user)) {
Feedback feedback = feedbackService.getFeedback(sessionkey);
if (feedback != null) {
return feedback;
}
throw new RuntimeException();
}
public Feedback postFeedback(
@PathVariable final String shortId,
@RequestBody final int value) {
final String roomId = roomService.getIdByShortId(shortId);
final User user = userService.getCurrentUser();
feedbackService.save(roomId, value, user.getId());
final Feedback feedback = feedbackService.getByRoomId(roomId);
throw new NotFoundException();
return feedback;
}
}
/*
* This file is part of ARSnova Backend.
* Copyright (C) 2012-2019 The ARSnova Team and Contributors
*
* 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.controller.v2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import de.thm.arsnova.controller.AbstractController;
import de.thm.arsnova.service.CommentService;
import de.thm.arsnova.service.ContentService;
import de.thm.arsnova.web.DeprecatedApi;
/**
* This controller forwards requests from deprecated URLs to their new controller, where the requests are handled.
*/
@Controller("v2LegacyController")
@RequestMapping("/v2")
public class LegacyController extends AbstractController {
@Autowired
private ContentService contentService;
@Autowired
private CommentService commentService;
/* specific routes */
@DeprecatedApi
@GetMapping("/session/mysessions")
public String redirectSessionMy() {
return "forward:/v2/session/?ownedonly=true";
}
@DeprecatedApi
@GetMapping("/session/visitedsessions")
public String redirectSessionVisited() {
return "forward:/v2/session/?visitedonly=true";
}
@DeprecatedApi
@RequestMapping(value = "/session/{shortId}/question")
public String redirectQuestionByLecturer(@PathVariable final String shortId) {
return String.format("forward:/v2/lecturerquestion/?sessionkey=%s", shortId);
}
@DeprecatedApi
@GetMapping("/session/{shortId}/skillquestions")
public String redirectQuestionByLecturerList(@PathVariable final String shortId) {
return String.format("forward:/v2/lecturerquestion/?sessionkey=%s", shortId);
}
@DeprecatedApi
@GetMapping("/session/{shortId}/skillquestioncount")
public String redirectQuestionByLecturerCount(@PathVariable final String shortId) {
return String.format("forward:/v2/lecturerquestion/count?sessionkey=%s", shortId);
}
@DeprecatedApi
@GetMapping("/session/{shortId}/answercount")
public String redirectQuestionByLecturerAnswerCount(@PathVariable final String shortId) {
return String.format("forward:/v2/lecturerquestion/answercount?sessionkey=%s", shortId);
}
@DeprecatedApi
@GetMapping("/session/{shortId}/unanswered")
public String redirectQuestionByLecturerUnnsweredCount(@PathVariable final String shortId) {
return String.format("forward:/v2/lecturerquestion/answercount?sessionkey=%s", shortId);
}
@DeprecatedApi
@GetMapping("/session/{shortId}/myanswers")
public String redirectQuestionByLecturerMyAnswers(@PathVariable final String shortId) {
return String.format("forward:/v2/lecturerquestion/myanswers?sessionkey=%s", shortId);
}
@DeprecatedApi
@RequestMapping(value = "/session/{shortId}/interposed")
public String redirectQuestionByAudience(@PathVariable final String shortId) {
return String.format("forward:/v2/audiencequestion/?sessionkey=%s", shortId);
}
@DeprecatedApi
@DeleteMapping("/session/{shortId}/interposed")
@ResponseBody
public void deleteAllInterposedQuestions(@PathVariable final String shortId) {
commentService.deleteByRoomId(shortId);
}
@DeprecatedApi
@GetMapping("/session/{shortId}/interposedcount")
public String redirectQuestionByAudienceCount(@PathVariable final String shortId) {
return String.format("forward:/v2/audiencequestion/count?sessionkey=%s", shortId);
}
@DeprecatedApi
@GetMapping("/session/{shortId}/interposedreadingcount")
public String redirectQuestionByAudienceReadCount(@PathVariable final String shortId) {
return String.format("forward:/v2/audiencequestion/readcount?sessionkey=%s", shortId);
}
@DeprecatedApi
@GetMapping(value = { "/whoami", "/whoami.json" })
public String redirectWhoami() {
return "forward:/v2/auth/whoami";
}
@DeprecatedApi
@PostMapping(value = "/doLogin")
public String redirectLogin() {
return "forward:/v2/auth/login";
}
/* generalized routes */
@DeprecatedApi
@RequestMapping(value = { "/session/{shortId}/question/{arg1}", "/session/{shortId}/questions/{arg1}" })
public String redirectQuestionByLecturerWithOneArgument(
@PathVariable final String shortId,
@PathVariable final String arg1) {
return String.format("forward:/v2/lecturerquestion/%s/?sessionkey=%s", arg1, shortId);
}
@DeprecatedApi
@RequestMapping(
value = { "/session/{shortId}/question/{arg1}/{arg2}", "/session/{shortId}/questions/{arg1}/{arg2}" }
)
public String redirectQuestionByLecturerWithTwoArguments(
@PathVariable final String shortId,
@PathVariable final String arg1,
@PathVariable final String arg2) {
return String.format("forward:/v2/lecturerquestion/%s/%s/?sessionkey=%s", arg1, arg2, shortId);
}
@DeprecatedApi
@RequestMapping(value = "/session/{shortId}/interposed/{arg1}")
public String redirectQuestionByAudienceWithOneArgument(
@PathVariable final String shortId,
@PathVariable final String arg1) {
return String.format("forward:/v2/audiencequestion/%s/?sessionkey=%s", arg1, shortId);
}
@DeprecatedApi
@RequestMapping(value = "/session/{shortId}/interposed/{arg1}/{arg2}")
public String redirectQuestionByAudienceWithTwoArguments(
@PathVariable final String shortId,
@PathVariable final String arg1,
@PathVariable final String arg2) {
return String.format("forward:/v2/audiencequestion/%s/%s/?sessionkey=%s", arg1, arg2, shortId);
}
}
/*
* This file is part of ARSnova Backend.
* Copyright (C) 2012-2019 The ARSnova Team and Contributors
*
* 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.controller.v2;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import de.thm.arsnova.controller.AbstractController;
import de.thm.arsnova.model.UserProfile;
import de.thm.arsnova.model.migration.FromV2Migrator;
import de.thm.arsnova.model.migration.ToV2Migrator;
import de.thm.arsnova.model.migration.v2.Motd;
import de.thm.arsnova.model.migration.v2.MotdList;
import de.thm.arsnova.security.User;
import de.thm.arsnova.service.MotdService;
import de.thm.arsnova.service.RoomService;
import de.thm.arsnova.service.UserService;
import de.thm.arsnova.web.exceptions.ForbiddenException;
@RestController("v2MotdController")
@RequestMapping("/v2/motd")
@Api(value = "/motd", description = "Message of the Day API")
public class MotdController extends AbstractController {
@Autowired
private MotdService motdService;
@Autowired
private RoomService roomService;
@Autowired
private UserService userService;
@Autowired
private ToV2Migrator toV2Migrator;
@Autowired
private FromV2Migrator fromV2Migrator;
@ApiOperation(value = "get messages. if adminview=false,"
+ " only messages with startdate<clientdate<enddate are returned")
@GetMapping("/")
@ApiResponses(value = {
@ApiResponse(code = 204, message = HTML_STATUS_204),
@ApiResponse(code = 501, message = HTML_STATUS_501)
})
public List<Motd> getMotd(
@ApiParam(value = "clientdate", required = false)
@RequestParam(value = "clientdate", defaultValue = "")
final String clientdate,
@ApiParam(value = "adminview", required = false)
@RequestParam(value = "adminview", defaultValue = "false")
final Boolean adminview,
@ApiParam(value = "audience", required = false)
@RequestParam(value = "audience", defaultValue = "all")
final String audience,
@ApiParam(value = "sessionkey", required = false)
@RequestParam(value = "sessionkey", required = false)
final String roomShortId) {
final List<de.thm.arsnova.model.Motd> motds;
final Date date = new Date(System.currentTimeMillis());
if (!clientdate.isEmpty()) {
date.setTime(Long.parseLong(clientdate));
}
String roomId = "";
if (roomShortId != null) {
roomId = roomService.getIdByShortId(roomShortId);
}
if (adminview) {
motds = roomShortId != null
? motdService.getAllRoomMotds(roomId)
: motdService.getAdminMotds();
} else {
motds = roomShortId != null
? motdService.getCurrentRoomMotds(date, roomId)
: motdService.getCurrentMotds(date, audience);
}
return motds.stream().map(toV2Migrator::migrate).collect(Collectors.toList());
}
@ApiOperation(value = "create a new message of the day", nickname = "createMotd")
@ApiResponses(value = {
@ApiResponse(code = 201, message = HTML_STATUS_201),
@ApiResponse(code = 503, message = HTML_STATUS_503)
})
@PostMapping("/")
@ResponseStatus(HttpStatus.CREATED)
public Motd postNewMotd(
@ApiParam(value = "current motd", required = true) @RequestBody final Motd motd,
final HttpServletResponse response) {
final de.thm.arsnova.model.Motd motdV3 = fromV2Migrator.migrate(motd);
final String roomId = roomService.getIdByShortId(motd.getSessionkey());
if (de.thm.arsnova.model.Motd.Audience.ROOM == motdV3.getAudience() && roomId != null) {
motdService.save(roomId, motdV3);
} else {
motdService.save(motdV3);
}
return toV2Migrator.migrate(motdV3);
}
@ApiOperation(value = "update a message of the day", nickname = "updateMotd")
@PutMapping("/{motdId}")
public Motd updateMotd(
@ApiParam(value = "motdkey from current motd", required = true) @PathVariable final String motdId,
@ApiParam(value = "current motd", required = true) @RequestBody final Motd motd) {
final de.thm.arsnova.model.Motd motdV3 = fromV2Migrator.migrate(motd);
final String roomId = roomService.getIdByShortId(motd.getSessionkey());
if (motdV3.getAudience() == de.thm.arsnova.model.Motd.Audience.ROOM && roomId != null) {
motdService.update(roomId, motdV3);
} else {
motdService.update(motdV3);
}
return toV2Migrator.migrate(motdV3);
}
@ApiOperation(value = "deletes a message of the day", nickname = "deleteMotd")
@DeleteMapping("/{motdId}")
public void deleteMotd(
@ApiParam(value = "Motd-key from the message that shall be deleted", required = true)
@PathVariable
final String motdId) {
final de.thm.arsnova.model.Motd motd = motdService.get(motdId);
motdService.delete(motd);
}
@GetMapping("/userlist")
public MotdList getAcknowledgedIds(@AuthenticationPrincipal final User user, @RequestParam final String username) {
if (user == null || !user.getUsername().equals(username)) {
throw new ForbiddenException();
}
final UserProfile profile = userService.get(user.getId());
return toV2Migrator.migrateMotdList(profile);
}
@PutMapping("/userlist")
public void putAcknowledgedIds(@AuthenticationPrincipal final User user, @RequestBody final MotdList motdList) {
if (user == null || !user.getUsername().equals(motdList.getUsername())) {
throw new ForbiddenException();
}
final UserProfile profile = userService.get(user.getId());
profile.setAcknowledgedMotds(fromV2Migrator.migrate(motdList));
userService.update(profile);
}
}
/*
* This file is part of ARSnova Backend.
* Copyright (C) 2012-2019 The ARSnova Team and Contributors
*
* 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.controller.v2;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import de.thm.arsnova.controller.PaginationController;
import de.thm.arsnova.model.migration.FromV2Migrator;
import de.thm.arsnova.model.migration.ToV2Migrator;
import de.thm.arsnova.model.migration.v2.Room;
import de.thm.arsnova.model.migration.v2.RoomFeature;
import de.thm.arsnova.model.migration.v2.RoomInfo;
import de.thm.arsnova.model.transport.ImportExportContainer;
import de.thm.arsnova.model.transport.ScoreStatistics;
import de.thm.arsnova.service.RoomService;
import de.thm.arsnova.service.RoomServiceImpl;
import de.thm.arsnova.service.RoomServiceImpl.RoomNameComparator;
import de.thm.arsnova.service.RoomServiceImpl.RoomShortNameComparator;
import de.thm.arsnova.service.UserService;
import de.thm.arsnova.web.DeprecatedApi;
import de.thm.arsnova.web.Pagination;
import de.thm.arsnova.web.exceptions.NotImplementedException;
import de.thm.arsnova.web.exceptions.UnauthorizedException;
/**
* Handles requests related to ARSnova Rooms.
*/
@RestController("v2RoomController")
@RequestMapping("/v2/session")
@Api(value = "/session", description = "Room (Session) API")
public class RoomController extends PaginationController {
@Autowired
private RoomService roomService;
@Autowired
private UserService userService;
@Autowired
private ToV2Migrator toV2Migrator;
@Autowired
private FromV2Migrator fromV2Migrator;
@ApiOperation(value = "join a Room",
nickname = "joinRoom")
@DeprecatedApi
@Deprecated
@GetMapping("/{shortId}")
public Room joinRoom(
@ApiParam(value = "Room-Key from current Room", required = true)
@PathVariable final String shortId,
@ApiParam(value = "Adminflag", required = false)
@RequestParam(value = "admin", defaultValue = "false")
final boolean admin) {
if (admin) {
return toV2Migrator.migrate(roomService.getForAdmin(shortId));
} else {
return toV2Migrator.migrate(roomService.getByShortId(shortId));
}
}
@ApiOperation(value = "deletes a Room",
nickname = "deleteRoom")
@DeleteMapping("/{shortId}")
public void deleteRoom(
@ApiParam(value = "Room-Key from current Room", required = true)
@PathVariable
final String shortId) {
final de.thm.arsnova.model.Room room = roomService.getByShortId(shortId);
roomService.delete(room);
}
@ApiOperation(value = "count active users",
nickname = "countActiveUsers")
@DeprecatedApi
@Deprecated
@GetMapping(value = "/{shortId}/activeusercount", produces = MediaType.TEXT_PLAIN_VALUE)
public String countActiveUsers(
@ApiParam(value = "Room-Key from current Room", required = true) @PathVariable final String shortId) {
return String.valueOf(roomService.activeUsers(roomService.getIdByShortId(shortId)));
}
@ApiOperation(value = "Creates a new Room and returns the Room's data",
nickname = "postNewRoom")
@ApiResponses(value = {
@ApiResponse(code = 201, message = HTML_STATUS_201),
@ApiResponse(code = 503, message = HTML_STATUS_503)
})
@PostMapping("/")
@ResponseStatus(HttpStatus.CREATED)
public Room postNewRoom(
@ApiParam(value = "current Room", required = true)
@RequestBody
final Room room,
final HttpServletResponse response) {
/* FIXME: migrate LMS course support
if (room != null && room.isCourseSession()) {
final List<Course> courses = new ArrayList<>();
final Course course = new Course();
course.setId(room.getCourseId());
courses.add(course);
final int sessionCount = roomService.countSessionsByCourses(courses);
if (sessionCount > 0) {
final String appendix = " (" + (sessionCount + 1) + ")";
room.setName(room.getName() + appendix);
room.setAbbreviation(room.getAbbreviation() + appendix);
}
}
*/
return toV2Migrator.migrate(roomService.create(fromV2Migrator.migrate(room)));
}
@ApiOperation(value = "updates a Room",
nickname = "postNewRoom")
@PutMapping("/{shortId}")
public Room updateRoom(
@ApiParam(value = "Room-Key from current Room", required = true) @PathVariable final String shortId,
@ApiParam(value = "current room", required = true) @RequestBody final Room room) {
return toV2Migrator.migrate(roomService.update(fromV2Migrator.migrate(room)));
}
@ApiOperation(value = "change the Room creator (owner)", nickname = "changeRoomCreator")
@RequestMapping(value = "/{shortId}/changecreator", method = RequestMethod.PUT)
public Room changeRoomCreator(
@ApiParam(value = "Room-key from current Room", required = true) @PathVariable final String shortId,
@ApiParam(value = "new Room creator", required = true) @RequestBody final String newCreator) {
return toV2Migrator.migrate(roomService.updateCreator(roomService.getIdByShortId(shortId), newCreator));
}
@ApiOperation(value = "Retrieves a list of Rooms",
nickname = "getRooms")
@ApiResponses(value = {
@ApiResponse(code = 204, message = HTML_STATUS_204),
@ApiResponse(code = 501, message = HTML_STATUS_501)
})
@GetMapping("/")
@Pagination
public List<Room> getRooms(
@ApiParam(value = "ownedOnly", required = true)
@RequestParam(value = "ownedonly", defaultValue = "false")
final boolean ownedOnly,
@ApiParam(value = "visitedOnly", required = true)
@RequestParam(value = "visitedonly", defaultValue = "false")
final boolean visitedOnly,
@ApiParam(value = "sortby", required = true)
@RequestParam(value = "sortby", defaultValue = "name")
final String sortby,
@ApiParam(value = "for a given username. admin rights needed", required = false)
@RequestParam(value = "username", defaultValue = "")
final String username,
final HttpServletResponse response) {
final List<de.thm.arsnova.model.Room> rooms;
if (!"".equals(username)) {
final String userId = userService.getByUsername(username).getId();
try {
if (ownedOnly && !visitedOnly) {
rooms = roomService.getUserRooms(userId);
} else if (visitedOnly && !ownedOnly) {
rooms = roomService.getUserRoomHistory(username);
} else {
response.setStatus(HttpStatus.NOT_IMPLEMENTED.value());
return null;
}
} catch (final AccessDeniedException e) {
throw new UnauthorizedException();
}
} else {
/* TODO implement all parameter combinations, implement use of user parameter */
try {
if (ownedOnly && !visitedOnly) {
rooms = roomService.getMyRooms(offset, limit);
} else if (visitedOnly && !ownedOnly) {
rooms = roomService.getMyRoomHistory(offset, limit);
} else {
response.setStatus(HttpStatus.NOT_IMPLEMENTED.value());
return null;
}
} catch (final AccessDeniedException e) {
throw new UnauthorizedException();
}
}
if (rooms == null || rooms.isEmpty()) {
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
return null;
}
if ("shortname".equals(sortby)) {
Collections.sort(rooms, new RoomShortNameComparator());
} else {
Collections.sort(rooms, new RoomServiceImpl.RoomNameComparator());
}
return rooms.stream().map(toV2Migrator::migrate).collect(Collectors.toList());
}
/**
* Returns a list of my own Rooms with only the necessary information like name, keyword, or counters.
*/
@ApiOperation(value = "Retrieves a Room",
nickname = "getMyRooms")
@ApiResponses(value = {
@ApiResponse(code = 204, message = HTML_STATUS_204)
})
@GetMapping(value = "/", params = "statusonly=true")
@Pagination
public List<RoomInfo> getMyRooms(
@ApiParam(value = "visitedOnly", required = true)
@RequestParam(value = "visitedonly", defaultValue = "false")
final boolean visitedOnly,
@ApiParam(value = "sort by", required = false)
@RequestParam(value = "sortby", defaultValue = "name")
final String sortby,
final HttpServletResponse response) {
final List<de.thm.arsnova.model.Room> rooms;
if (!visitedOnly) {
rooms = roomService.getMyRoomsInfo(offset, limit);
} else {
rooms = roomService.getMyRoomHistoryInfo(offset, limit);
}
if (rooms == null || rooms.isEmpty()) {
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
return null;
}
if ("shortname".equals(sortby)) {
Collections.sort(rooms, new RoomShortNameComparator());
} else {
Collections.sort(rooms, new RoomNameComparator());
}
return rooms.stream().map(toV2Migrator::migrateStats).collect(Collectors.toList());
}
@ApiOperation(value = "Retrieves all public pool Rooms for the current user",
nickname = "getMyPublicPoolRooms")
@ApiResponses(value = {
@ApiResponse(code = 204, message = HTML_STATUS_204)
})
@GetMapping(value = "/publicpool", params = "statusonly=true")
public List<RoomInfo> getMyPublicPoolRooms(
final HttpServletResponse response) {
final List<de.thm.arsnova.model.Room> rooms = roomService.getMyPublicPoolRoomsInfo();
if (rooms == null || rooms.isEmpty()) {
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
return null;
}
return rooms.stream().map(toV2Migrator::migrateStats).collect(Collectors.toList());
}
@ApiOperation(value = "Retrieves all public pool Rooms",
nickname = "getMyPublicPoolRooms")
@ApiResponses(value = {
@ApiResponse(code = 204, message = HTML_STATUS_204)
})
@GetMapping("/publicpool")
public List<Room> getPublicPoolRooms(
final HttpServletResponse response) {
final List<de.thm.arsnova.model.Room> rooms = roomService.getPublicPoolRoomsInfo();
if (rooms == null || rooms.isEmpty()) {
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
return null;
}
return rooms.stream().map(toV2Migrator::migrate).collect(Collectors.toList());
}
@ApiOperation(value = "imports a Room",
nickname = "importRoom")
@PostMapping("/import")
public Room importRoom(
@ApiParam(value = "current Room", required = true) @RequestBody final ImportExportContainer room,
final HttpServletResponse response) {
return toV2Migrator.migrate(roomService.importRooms(room));
}
@ApiOperation(value = "export Rooms", nickname = "exportRoom")
@GetMapping("/export")
public List<ImportExportContainer> getExport(
@ApiParam(value = "Room-Key", required = true)
@RequestParam(value = "sessionkey", defaultValue = "")
final List<String> shortIds,
@ApiParam(value = "wether statistics shall be exported", required = true)
@RequestParam(value = "withAnswerStatistics", defaultValue = "false")
final Boolean withAnswerStatistics,
@ApiParam(value = "wether comments shall be exported", required = true)
@RequestParam(value = "withFeedbackQuestions", defaultValue = "false")
final Boolean withFeedbackQuestions,
final HttpServletResponse response) throws IOException {
final List<ImportExportContainer> rooms = new ArrayList<>();
ImportExportContainer temp;
for (final String shortId : shortIds) {
final String id = roomService.getIdByShortId(shortId);
roomService.setActive(id, false);
temp = roomService.exportRoom(id, withAnswerStatistics, withFeedbackQuestions);
if (temp != null) {
rooms.add(temp);
}
roomService.setActive(id, true);
}
return rooms;
}
@ApiOperation(value = "copy a Rooms to the public pool if enabled")
@PostMapping("/{shortId}/copytopublicpool")
public Room copyToPublicPool(
@ApiParam(value = "Room-Key from current Room", required = true)
@PathVariable
final String shortId,
@ApiParam(value = "public pool attributes for Room", required = true)
@RequestBody
final ImportExportContainer.PublicPool publicPool)
throws IOException {
final String id = roomService.getIdByShortId(shortId);
roomService.setActive(id, false);
final de.thm.arsnova.model.Room roomInfo = roomService.copyRoomToPublicPool(shortId, publicPool);
roomService.setActive(id, true);
return toV2Migrator.migrate(roomInfo);
}
@ApiOperation(value = "copy a Room from the public pool if enabled")
@PostMapping("/{shortId}/copyfrompublicpool")
public Room copyFromPublicPool(
@ApiParam(value = "Short ID of the Room", required = true) @PathVariable final String shortId,
@ApiParam(value = "custom attributes for Room", required = true) @RequestBody final Room sessionAttributes) {
throw new NotImplementedException();
}
@ApiOperation(value = "Locks or unlocks a Room",
nickname = "lockRoom")
@ApiResponses(value = {
@ApiResponse(code = 404, message = HTML_STATUS_404)
})
@PostMapping("/{shortId}/lock")
public Room lockRoom(
@ApiParam(value = "Room-Key from current Room", required = true) @PathVariable final String shortId,
@ApiParam(value = "lock", required = true) @RequestParam(required = false) final Boolean lock,
final HttpServletResponse response) throws IOException {
if (lock != null) {
return toV2Migrator.migrate(roomService.setActive(roomService.getIdByShortId(shortId), lock));
}
response.setStatus(HttpStatus.NOT_FOUND.value());
return null;
}
@ApiOperation(value = "retrieves a value for the score",
nickname = "getLearningProgress")
@GetMapping("/{shortId}/learningprogress")
public ScoreStatistics getLearningProgress(
@ApiParam(value = "Room-Key from current Room", required = true)
@PathVariable
final String shortId,
@ApiParam(value = "type", required = false)
@RequestParam(value = "type", defaultValue = "questions")
final String type,
@ApiParam(value = "question variant", required = false)
@RequestParam(value = "questionVariant", required = false)
final String questionVariant,
final HttpServletResponse response) {
return roomService.getLearningProgress(roomService.getIdByShortId(shortId), type, questionVariant);
}
@ApiOperation(value = "retrieves a value for the learning progress for the current user",
nickname = "getMyLearningProgress")
@GetMapping("/{shortId}/mylearningprogress")
public ScoreStatistics getMyLearningProgress(
@ApiParam(value = "Room-Key from current Room", required = true) @PathVariable final String shortId,
@RequestParam(value = "type", defaultValue = "questions") final String type,
@RequestParam(value = "questionVariant", required = false) final String questionVariant,
final HttpServletResponse response) {
return roomService.getMyLearningProgress(roomService.getIdByShortId(shortId), type, questionVariant);
}
@ApiOperation(value = "retrieves all Room features",
nickname = "getRoomFeatures")
@GetMapping("/{shortId}/features")
public RoomFeature getRoomFeatures(
@ApiParam(value = "Room-Key from current Room", required = true) @PathVariable final String shortId,
final HttpServletResponse response) {
final de.thm.arsnova.model.Room room = roomService.getByShortId(shortId);
return toV2Migrator.migrate(room.getSettings());
}
@PutMapping("/{shortId}/features")
@ApiOperation(value = "change all Room features",
nickname = "changeRoomFeatures")
public RoomFeature changeRoomFeatures(
@ApiParam(value = "Room-Key from current Room", required = true) @PathVariable final String shortId,
@ApiParam(value = "Room feature", required = true) @RequestBody final RoomFeature features,
final HttpServletResponse response) {
final de.thm.arsnova.model.Room room = roomService.getByShortId(shortId);
room.setSettings(fromV2Migrator.migrate(features));
roomService.update(room);
return toV2Migrator.migrate(room.getSettings());
}
@PostMapping(value = "/{shortId}/lockfeedbackinput", produces = MediaType.TEXT_PLAIN_VALUE)
@ApiOperation(value = "locks input of user live feedback",
nickname = "lockFeedbackInput")
public String lockFeedbackInput(
@ApiParam(value = "Room-Key from current Room", required = true) @PathVariable final String shortId,
@ApiParam(value = "lock", required = true) @RequestParam(required = true) final Boolean lock,
final HttpServletResponse response) throws IOException {
return String.valueOf(roomService.lockFeedbackInput(roomService.getIdByShortId(shortId), lock));
}
@PostMapping(value = "/{shortId}/flipflashcards", produces = MediaType.TEXT_PLAIN_VALUE)
@ApiOperation(value = "flip all flashcards in Room",
nickname = "lockFeedbackInput")
public String flipFlashcards(
@ApiParam(value = "Room-Key from current Room", required = true) @PathVariable final String shortId,
@ApiParam(value = "flip", required = true) @RequestParam(required = true) final Boolean flip,
final HttpServletResponse response) {
return String.valueOf(roomService.flipFlashcards(roomService.getIdByShortId(shortId), flip));
}
/* internal redirections */
@RequestMapping(value = "/{shortId}/lecturerquestion")
public String redirectLecturerQuestion(
@PathVariable final String shortId,
final HttpServletResponse response) {
response.addHeader(X_FORWARDED, "1");
return String.format("forward:/lecturerquestion/?sessionkey=%s", shortId);
}
@RequestMapping(value = "/{shortId}/lecturerquestion/{arg1}")
public String redirectLecturerQuestionWithOneArgument(
@PathVariable final String shortId,
@PathVariable final String arg1,
final HttpServletResponse response) {
response.addHeader(X_FORWARDED, "1");
return String.format("forward:/lecturerquestion/%s/?sessionkey=%s", arg1, shortId);
}
@RequestMapping(value = "/{shortId}/lecturerquestion/{arg1}/{arg2}")
public String redirectLecturerQuestionWithTwoArguments(
@PathVariable final String shortId,
@PathVariable final String arg1,
@PathVariable final String arg2,
final HttpServletResponse response) {
response.addHeader(X_FORWARDED, "1");
return String.format("forward:/lecturerquestion/%s/%s/?sessionkey=%s", arg1, arg2, shortId);
}
@RequestMapping(value = "/{shortId}/lecturerquestion/{arg1}/{arg2}/{arg3}")
public String redirectLecturerQuestionWithThreeArguments(
@PathVariable final String shortId,
@PathVariable final String arg1,
@PathVariable final String arg2,
@PathVariable final String arg3,
final HttpServletResponse response) {
response.addHeader(X_FORWARDED, "1");
return String.format("forward:/lecturerquestion/%s/%s/%s/?sessionkey=%s", arg1, arg2, arg3, shortId);
}
@RequestMapping(value = "/{shortId}/audiencequestion")
public String redirectAudienceQuestion(
@PathVariable final String shortId,
final HttpServletResponse response) {
response.addHeader(X_FORWARDED, "1");
return String.format("forward:/audiencequestion/?sessionkey=%s", shortId);
}
@RequestMapping(value = "/{shortId}/audiencequestion/{arg1}")
public String redirectAudienceQuestionWithOneArgument(
@PathVariable final String shortId,
@PathVariable final String arg1,
final HttpServletResponse response) {
response.addHeader(X_FORWARDED, "1");
return String.format("forward:/audiencequestion/%s/?sessionkey=%s", arg1, shortId);
}
}
/*
* This file is part of ARSnova Backend.
* Copyright (C) 2012-2015 The ARSnova Team
* Copyright (C) 2012-2019 The ARSnova Team and Contributors
*
* 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
......@@ -15,70 +15,82 @@
* 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.controller;
package de.thm.arsnova.controller.v2;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import de.thm.arsnova.entities.User;
import de.thm.arsnova.services.IUserService;
import de.thm.arsnova.services.UserSessionService;
import de.thm.arsnova.socket.ARSnovaSocketIOServer;
import de.thm.arsnova.controller.AbstractController;
import de.thm.arsnova.security.User;
import de.thm.arsnova.service.UserService;
import de.thm.arsnova.websocket.ArsnovaSocketioServer;
@RestController
@RequestMapping("/socket")
/**
* Initiates the socket communication.
*/
@RestController("v2SocketController")
@RequestMapping("/v2/socket")
@Api(value = "/socket", description = "WebSocket Initialization API")
public class SocketController extends AbstractController {
@Autowired
private IUserService userService;
private UserService userService;
@Autowired
private UserSessionService userSessionService;
private ArsnovaSocketioServer server;
@Autowired
private ARSnovaSocketIOServer server;
private static final Logger LOGGER = LoggerFactory.getLogger(SocketController.class);
private static final Logger logger = LoggerFactory.getLogger(SocketController.class);
@RequestMapping(method = RequestMethod.POST, value = "/assign")
public final void authorize(@RequestBody final Map<String, String> sessionMap, final HttpServletResponse response) {
String socketid = sessionMap.get("session");
@ApiOperation(value = "requested to assign Websocket session",
nickname = "authorize")
@ApiResponses(value = {
@ApiResponse(code = 204, message = HTML_STATUS_204),
@ApiResponse(code = 400, message = HTML_STATUS_400),
@ApiResponse(code = 403, message = HTML_STATUS_403)
})
@PostMapping("/assign")
public void authorize(
@ApiParam(value = "sessionMap", required = true) @RequestBody final Map<String, String> sessionMap,
@ApiParam(value = "response", required = true) final HttpServletResponse response) {
final String socketid = sessionMap.get("session");
if (null == socketid) {
LOGGER.debug("Expected property 'session' missing", socketid);
logger.debug("Expected property 'session' missing.");
response.setStatus(HttpStatus.BAD_REQUEST.value());
return;
}
User u = userService.getCurrentUser();
if (null == u) {
LOGGER.debug("Client {} requested to assign Websocket session but has not authenticated", socketid);
final User user = userService.getCurrentUser();
if (null == user) {
logger.debug("Client {} requested to assign Websocket session but has not authenticated.", socketid);
response.setStatus(HttpStatus.FORBIDDEN.value());
return;
}
userService.putUser2SocketId(UUID.fromString(socketid), u);
userSessionService.setSocketId(UUID.fromString(socketid));
userService.putUserIdToSocketId(UUID.fromString(socketid), user.getId());
response.setStatus(HttpStatus.NO_CONTENT.value());
}
@RequestMapping(value = "/url", method = RequestMethod.GET)
public final String getSocketUrl(final HttpServletRequest request) {
StringBuilder url = new StringBuilder();
url.append(server.isUseSSL() ? "https://" : "http://");
url.append(request.getServerName() + ":" + server.getPortNumber());
return url.toString();
@ApiOperation(value = "retrieves a socket url",
nickname = "getSocketUrl")
@GetMapping(value = "/url", produces = MediaType.TEXT_PLAIN_VALUE)
public String getSocketUrl(final HttpServletRequest request) {
return (server.isUseSsl() ? "https://" : "http://") + request.getServerName() + ":" + server.getPortNumber();
}
}
/*
* This file is part of ARSnova Backend.
* Copyright (C) 2012-2015 The ARSnova Team
* Copyright (C) 2012-2019 The ARSnova Team and Contributors
*
* 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
......@@ -15,46 +15,67 @@
* 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.controller;
package de.thm.arsnova.controller.v2;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import de.thm.arsnova.entities.Statistics;
import de.thm.arsnova.services.IStatisticsService;
import de.thm.arsnova.controller.AbstractController;
import de.thm.arsnova.model.Statistics;
import de.thm.arsnova.service.StatisticsService;
import de.thm.arsnova.web.CacheControl;
import de.thm.arsnova.web.DeprecatedApi;
@RestController
/**
* Allows retrieval of several statistics such as the number of active users.
*/
@RestController("v2StatisticsController")
@Api(value = "/statistics", description = "Statistics API")
@RequestMapping("/v2/statistics")
public class StatisticsController extends AbstractController {
@Autowired
private IStatisticsService statisticsService;
private StatisticsService statisticsService;
@RequestMapping(method = RequestMethod.GET, value = "/statistics")
@ApiOperation(value = "Retrieves global statistics",
nickname = "getStatistics")
@GetMapping("/")
@CacheControl(maxAge = 60, policy = CacheControl.Policy.PUBLIC)
public final Statistics getStatistics() {
public Statistics getStatistics() {
return statisticsService.getStatistics();
}
@ApiOperation(value = "Retrieves the amount of all active users",
nickname = "countActiveUsers")
@DeprecatedApi
@RequestMapping(method = RequestMethod.GET, value = "/statistics/activeusercount", produces = "text/plain")
public final String countActiveUsers() {
return Integer.toString(statisticsService.getStatistics().getActiveUsers());
@Deprecated
@GetMapping(value = "/activeusercount", produces = MediaType.TEXT_PLAIN_VALUE)
public String countActiveUsers() {
return String.valueOf(statisticsService.getStatistics().getActiveUsers());
}
@ApiOperation(value = "Retrieves the amount of all currently logged in users",
nickname = "countLoggedInUsers")
@DeprecatedApi
@RequestMapping(method = RequestMethod.GET, value = "/statistics/loggedinusercount", produces = "text/plain")
public final String countLoggedInUsers() {
return Integer.toString(statisticsService.getStatistics().getLoggedinUsers());
@Deprecated
@GetMapping(value = "/loggedinusercount", produces = MediaType.TEXT_PLAIN_VALUE)
public String countLoggedInUsers() {
return String.valueOf(statisticsService.getStatistics().getLoggedinUsers());
}
@ApiOperation(value = "Retrieves the total amount of all sessions",
nickname = "countSessions")
@DeprecatedApi
@RequestMapping(method = RequestMethod.GET, value = "/statistics/sessioncount", produces = "text/plain")
public final String countSessions() {
return Integer.toString(statisticsService.getStatistics().getOpenSessions()
@Deprecated
@GetMapping(value = "/sessioncount", produces = MediaType.TEXT_PLAIN_VALUE)
public String countSessions() {
return String.valueOf(statisticsService.getStatistics().getOpenSessions()
+ statisticsService.getStatistics().getClosedSessions());
}
}
/*
* This file is part of ARSnova Backend.
* Copyright (C) 2012-2019 The ARSnova Team and Contributors
*
* 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.controller.v2;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import de.thm.arsnova.controller.AbstractController;
import de.thm.arsnova.model.UserProfile;
import de.thm.arsnova.service.UserService;
/**
* Handles requests related to ARSnova's own user registration and login process.
*/
@Controller("v2UserController")
@RequestMapping("/v2/user")
public class UserController extends AbstractController {
@Autowired
private DaoAuthenticationProvider daoProvider;
@Autowired
private UserService userService;
@PostMapping(value = "/register")
public void register(@RequestParam final String username,
@RequestParam final String password,
final HttpServletRequest request, final HttpServletResponse response) {
if (null != userService.create(username, password)) {
return;
}
/* TODO: Improve error handling: send reason to client */
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}
@PostMapping(value = "/{username}/activate")
public void activate(
@PathVariable final String username,
@RequestParam final String key,
final HttpServletRequest request,
final HttpServletResponse response) {
final UserProfile userProfile = userService.getByUsername(username);
if (userProfile == null || !userService.activateAccount(userProfile.getId(), key, request.getRemoteAddr())) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}
}
@DeleteMapping(value = "/{username}/")
public void activate(
@PathVariable final String username,
final HttpServletRequest request,
final HttpServletResponse response) {
if (null == userService.deleteByUsername(username)) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
}
@PostMapping(value = "/{username}/resetpassword")
public void resetPassword(
@PathVariable final String username,
@RequestParam(required = false) final String key,
@RequestParam(required = false) final String password,
final HttpServletRequest request,
final HttpServletResponse response) {
final UserProfile userProfile = userService.getByUsername(username);
if (null == userProfile) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
if (null != key) {
if (!userService.resetPassword(userProfile, key, password)) {
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
}
} else {
userService.initiatePasswordReset(username);
}
}
}
package de.thm.arsnova.controller.v2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller("v2WelcomeController")
@RequestMapping("/v2")
public class WelcomeController {
@GetMapping(value = "/")
public String forwardHome() {
return "forward:/";
}
}
/*
* This file is part of ARSnova Backend.
* Copyright (C) 2012-2015 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.dao;
import java.io.IOException;
import java.util.AbstractMap;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.sf.ezmorph.Morpher;
import net.sf.ezmorph.MorpherRegistry;
import net.sf.ezmorph.bean.BeanMorpher;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import com.fourspaces.couchdb.Database;
import com.fourspaces.couchdb.Document;
import com.fourspaces.couchdb.View;
import com.fourspaces.couchdb.ViewResults;
import de.thm.arsnova.connector.model.Course;
import de.thm.arsnova.entities.Answer;
import de.thm.arsnova.entities.DbUser;
import de.thm.arsnova.entities.InterposedQuestion;
import de.thm.arsnova.entities.InterposedReadingCount;
import de.thm.arsnova.entities.LoggedIn;
import de.thm.arsnova.entities.PossibleAnswer;
import de.thm.arsnova.entities.Question;
import de.thm.arsnova.entities.Session;
import de.thm.arsnova.entities.SessionInfo;
import de.thm.arsnova.entities.User;
import de.thm.arsnova.entities.VisitedSession;
import de.thm.arsnova.exceptions.NotFoundException;
import de.thm.arsnova.services.ISessionService;
@Component("databaseDao")
public class CouchDBDao implements IDatabaseDao {
@Autowired
private ISessionService sessionService;
private String databaseHost;
private int databasePort;
private String databaseName;
private Database database;
public static final Logger LOGGER = LoggerFactory.getLogger(CouchDBDao.class);
@Value("${couchdb.host}")
public final void setDatabaseHost(final String newDatabaseHost) {
databaseHost = newDatabaseHost;
}
@Value("${couchdb.port}")
public final void setDatabasePort(final String newDatabasePort) {
databasePort = Integer.parseInt(newDatabasePort);
}
@Value("${couchdb.name}")
public final void setDatabaseName(final String newDatabaseName) {
databaseName = newDatabaseName;
}
public final void setSessionService(final ISessionService service) {
sessionService = service;
}
@Override
public final Session getSession(final String keyword) {
final Session result = getSessionFromKeyword(keyword);
if (result != null) {
return result;
}
throw new NotFoundException();
}
@Override
public final List<Session> getMySessions(final User user) {
final NovaView view = new NovaView("session/by_creator");
view.setStartKeyArray(user.getUsername());
view.setEndKeyArray(user.getUsername(), "{}");
final ViewResults sessions = getDatabase().view(view);
final List<Session> result = new ArrayList<Session>();
for (final Document d : sessions.getResults()) {
final Session session = (Session) JSONObject.toBean(
d.getJSONObject().getJSONObject("value"),
Session.class
);
session.setCreator(d.getJSONObject().getJSONArray("key").getString(0));
session.setName(d.getJSONObject().getJSONArray("key").getString(1));
session.set_id(d.getId());
result.add(session);
}
return result;
}
@Override
public final List<Session> getPublicPoolSessions() {
final NovaView view = new NovaView("session/public_pool_by_subject");
final ViewResults sessions = getDatabase().view(view);
final List<Session> result = new ArrayList<Session>();
for (final Document d : sessions.getResults()) {
final Session session = (Session) JSONObject.toBean(
d.getJSONObject().getJSONObject("value"),
Session.class
);
//session.set_id(d.getId());
result.add(session);
}
return result;
}
@Override
public final List<Session> getMyPublicPoolSessions(final User user) {
final NovaView view = new NovaView("session/public_pool_by_creator");
view.setStartKeyArray(user.getUsername());
view.setEndKeyArray(user.getUsername(), "{}");
final ViewResults sessions = getDatabase().view(view);
final List<Session> result = new ArrayList<Session>();
for (final Document d : sessions.getResults()) {
final Session session = (Session) JSONObject.toBean(
d.getJSONObject().getJSONObject("value"),
Session.class
);
session.setCreator(d.getJSONObject().getJSONArray("key").getString(0));
session.setName(d.getJSONObject().getJSONArray("key").getString(1));
session.set_id(d.getId());
result.add(session);
}
return result;
}
@Override
public final List<SessionInfo> getMyPublicPoolSessionsInfo(final User user) {
final List<Session> sessions = this.getMyPublicPoolSessions(user);
return getInfosForSessions(sessions);
}
@Override
public final List<SessionInfo> getMySessionsInfo(final User user) {
final List<Session> sessions = this.getMySessions(user);
return getInfosForSessions(sessions);
}
private List<SessionInfo> getInfosForSessions(final List<Session> sessions) {
final ExtendedView questionCountView = new ExtendedView("skill_question/count_by_session");
final ExtendedView answerCountView = new ExtendedView("skill_question/count_answers_by_session");
final ExtendedView interposedCountView = new ExtendedView("interposed_question/count_by_session_reading");
questionCountView.setSessionIdKeys(sessions);
questionCountView.setGroup(true);
answerCountView.setSessionIdKeys(sessions);
answerCountView.setGroup(true);
List<String> interposedQueryKeys = new ArrayList<String>();
for (Session s : sessions) {
interposedQueryKeys.add("[\"" + s.get_id() + "\",\"unread\"]");
}
interposedCountView.setKeys(interposedQueryKeys);
interposedCountView.setGroup(true);
return getSessionInfoData(sessions, questionCountView, answerCountView, interposedCountView);
}
private List<SessionInfo> getInfosForVisitedSessions(final List<Session> sessions, final User user) {
final ExtendedView answeredQuestionsView = new ExtendedView("answer/by_user");
final ExtendedView questionIdsView = new ExtendedView("skill_question/by_session_only_id_for_all");
questionIdsView.setSessionIdKeys(sessions);
List<String> answeredQuestionQueryKeys = new ArrayList<String>();
for (Session s : sessions) {
answeredQuestionQueryKeys.add("[\"" + user.getUsername() + "\",\"" + s.get_id() + "\"]");
}
answeredQuestionsView.setKeys(answeredQuestionQueryKeys);
return getVisitedSessionInfoData(sessions, answeredQuestionsView, questionIdsView);
}
private List<SessionInfo> getVisitedSessionInfoData(List<Session> sessions,
ExtendedView answeredQuestionsView, ExtendedView questionIdsView) {
final Map<String, Set<String>> answeredQuestionsMap = new HashMap<String, Set<String>>();
final Map<String, Set<String>> questionIdMap = new HashMap<String, Set<String>>();
final ViewResults answeredQuestionsViewResults = getDatabase().view(answeredQuestionsView);
final ViewResults questionIdsViewResults = getDatabase().view(questionIdsView);
// Maps a session ID to a set of question IDs of answered questions of that session
for (final Document d : answeredQuestionsViewResults.getResults()) {
final String sessionId = d.getJSONArray("key").getString(1);
final String questionId = d.getString("value");
Set<String> questionIdsInSession = answeredQuestionsMap.get(sessionId);
if (questionIdsInSession == null) {
questionIdsInSession = new HashSet<String>();
}
questionIdsInSession.add(questionId);
answeredQuestionsMap.put(sessionId, questionIdsInSession);
}
// Maps a session ID to a set of question IDs of that session
for (final Document d : questionIdsViewResults.getResults()) {
final String sessionId = d.getString("key");
final String questionId = d.getId();
Set<String> questionIdsInSession = questionIdMap.get(sessionId);
if (questionIdsInSession == null) {
questionIdsInSession = new HashSet<String>();
}
questionIdsInSession.add(questionId);
questionIdMap.put(sessionId, questionIdsInSession);
}
// For each session, count the question IDs that are not yet answered
Map<String, Integer> unansweredQuestionsCountMap = new HashMap<String, Integer>();
for (final Session s : sessions) {
if (!questionIdMap.containsKey(s.get_id())) {
continue;
}
// Note: create a copy of the first set so that we don't modify the contents in the original set
Set<String> questionIdsInSession = new HashSet<String>(questionIdMap.get(s.get_id()));
Set<String> answeredQuestionIdsInSession = answeredQuestionsMap.get(s.get_id());
if (answeredQuestionIdsInSession == null) {
answeredQuestionIdsInSession = new HashSet<String>();
}
questionIdsInSession.removeAll(answeredQuestionIdsInSession);
unansweredQuestionsCountMap.put(s.get_id(), questionIdsInSession.size());
}
List<SessionInfo> sessionInfos = new ArrayList<SessionInfo>();
for (Session session : sessions) {
int numUnanswered = 0;
if (unansweredQuestionsCountMap.containsKey(session.get_id())) {
numUnanswered = unansweredQuestionsCountMap.get(session.get_id());
}
SessionInfo info = new SessionInfo(session);
info.setNumUnanswered(numUnanswered);
sessionInfos.add(info);
}
return sessionInfos;
}
private List<SessionInfo> getSessionInfoData(final List<Session> sessions,
final ExtendedView questionCountView,
final ExtendedView answerCountView,
final ExtendedView interposedCountView) {
final ViewResults questionCountViewResults = getDatabase().view(questionCountView);
final ViewResults answerCountViewResults = getDatabase().view(answerCountView);
final ViewResults interposedCountViewResults = getDatabase().view(interposedCountView);
Map<String, Integer> questionCountMap = new HashMap<String, Integer>();
for (final Document d : questionCountViewResults.getResults()) {
questionCountMap.put(d.getString("key"), d.getInt("value"));
}
Map<String, Integer> answerCountMap = new HashMap<String, Integer>();
for (final Document d : answerCountViewResults.getResults()) {
answerCountMap.put(d.getString("key"), d.getInt("value"));
}
Map<String, Integer> interposedCountMap = new HashMap<String, Integer>();
for (final Document d : interposedCountViewResults.getResults()) {
interposedCountMap.put(d.getJSONArray("key").getString(0), d.getInt("value"));
}
List<SessionInfo> sessionInfos = new ArrayList<SessionInfo>();
for (Session session : sessions) {
int numQuestions = 0;
int numAnswers = 0;
int numInterposed = 0;
if (questionCountMap.containsKey(session.get_id())) {
numQuestions = questionCountMap.get(session.get_id());
}
if (answerCountMap.containsKey(session.get_id())) {
numAnswers = answerCountMap.get(session.get_id());
}
if (interposedCountMap.containsKey(session.get_id())) {
numInterposed = interposedCountMap.get(session.get_id());
}
SessionInfo info = new SessionInfo(session);
info.setNumQuestions(numQuestions);
info.setNumAnswers(numAnswers);
info.setNumInterposed(numInterposed);
sessionInfos.add(info);
}
return sessionInfos;
}
@Override
public final List<Question> getSkillQuestions(final User user, final Session session) {
String viewName;
if (session.getCreator().equals(user.getUsername())) {
viewName = "skill_question/by_session_sorted_by_subject_and_text";
} else {
viewName = "skill_question/by_session_for_all_full";
}
return getQuestions(new NovaView(viewName), session);
}
@Override
public final int getSkillQuestionCount(final Session session) {
return getQuestionCount(new NovaView("skill_question/count_by_session"), session);
}
@Override
public final Session getSessionFromKeyword(final String keyword) {
final NovaView view = new NovaView("session/by_keyword");
view.setKey(keyword);
final ViewResults results = getDatabase().view(view);
if (results.getJSONArray("rows").optJSONObject(0) == null) {
throw new NotFoundException();
}
return (Session) JSONObject.toBean(
results.getJSONArray("rows").optJSONObject(0).optJSONObject("value"),
Session.class
);
}
@Override
public final Session getSessionFromId(final String sessionId) {
final NovaView view = new NovaView("session/by_id");
view.setKey(sessionId);
final ViewResults sessions = getDatabase().view(view);
if (sessions.getJSONArray("rows").optJSONObject(0) == null) {
return null;
}
return (Session) JSONObject.toBean(
sessions.getJSONArray("rows").optJSONObject(0).optJSONObject("value"),
Session.class
);
}
@Override
public final Session saveSession(final User user, final Session session) {
final Document sessionDocument = new Document();
sessionDocument.put("type", "session");
sessionDocument.put("name", session.getName());
sessionDocument.put("shortName", session.getShortName());
sessionDocument.put("keyword", sessionService.generateKeyword());
sessionDocument.put("creator", user.getUsername());
sessionDocument.put("active", true);
sessionDocument.put("courseType", session.getCourseType());
sessionDocument.put("courseId", session.getCourseId());
sessionDocument.put("creationTime", session.getCreationTime());
sessionDocument.put("ppAuthorName", session.getPpAuthorName());
sessionDocument.put("ppAuthorMail", session.getPpAuthorMail());
sessionDocument.put("ppUniversity", session.getPpUniversity());
sessionDocument.put("ppLogo", session.getPpLogo());
sessionDocument.put("ppSubject", session.getPpSubject());
sessionDocument.put("ppLicense", session.getPpLicense());
sessionDocument.put("ppDescription", session.getPpDescription());
sessionDocument.put("ppFaculty", session.getPpFaculty());
sessionDocument.put("ppLevel", session.getPpLevel());
sessionDocument.put("sessionType", session.getSessionType());
try {
database.saveDocument(sessionDocument);
} catch (final IOException e) {
return null;
}
return getSession(sessionDocument.getString("keyword"));
}
@Override
@Transactional(isolation = Isolation.READ_COMMITTED)
public final boolean sessionKeyAvailable(final String keyword) {
final View view = new View("session/by_keyword");
final ViewResults results = getDatabase().view(view);
return !results.containsKey(keyword);
}
private String getSessionKeyword(final String internalSessionId) throws IOException {
final Document document = getDatabase().getDocument(internalSessionId);
if (document.has("keyword")) {
return (String) document.get("keyword");
}
LOGGER.error("No session found for internal id: {}", internalSessionId);
return null;
}
private Database getDatabase() {
if (database == null) {
try {
final com.fourspaces.couchdb.Session session = new com.fourspaces.couchdb.Session(
databaseHost,
databasePort
);
database = session.getDatabase(databaseName);
} catch (final Exception e) {
LOGGER.error(
"Cannot connect to CouchDB database '" + databaseName
+ "' on host '" + databaseHost
+ "' using port " + databasePort
);
}
}
return database;
}
@Override
public final Question saveQuestion(final Session session, final Question question) {
final Document q = toQuestionDocument(session, question);
try {
database.saveDocument(q);
question.set_id(q.getId());
question.set_rev(q.getRev());
return question;
} catch (final IOException e) {
LOGGER.error("Could not save question {}", question);
}
return null;
}
private Document toQuestionDocument(final Session session, final Question question) {
final Document q = new Document();
q.put("type", "skill_question");
q.put("questionType", question.getQuestionType());
q.put("questionVariant", question.getQuestionVariant());
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 is now unused. A clean up is necessary.
q.put("releasedFor", question.getReleasedFor());
q.put("possibleAnswers", question.getPossibleAnswers());
q.put("noCorrect", question.isNoCorrect());
q.put("piRound", question.getPiRound());
q.put("showStatistic", question.isShowStatistic());
q.put("showAnswer", question.isShowAnswer());
q.put("abstention", question.isAbstention());
q.put("image", question.getImage());
q.put("fcImage", question.getFcImage());
q.put("gridSize", question.getGridSize());
q.put("offsetX", question.getOffsetX());
q.put("offsetY", question.getOffsetY());
q.put("zoomLvl", question.getZoomLvl());
q.put("gridOffsetX", question.getGridOffsetX());
q.put("gridOffsetY", question.getGridOffsetY());
q.put("gridZoomLvl", question.getGridZoomLvl());
q.put("gridSizeX", question.getGridSizeX());
q.put("gridSizeY", question.getGridSizeY());
q.put("gridIsHidden", question.getGridIsHidden());
q.put("imgRotation", question.getImgRotation());
q.put("toggleFieldsLeft", question.getToggleFieldsLeft());
q.put("numClickableFields", question.getNumClickableFields());
q.put("thresholdCorrectAnswers", question.getThresholdCorrectAnswers());
q.put("cvIsColored", question.getCvIsColored());
q.put("gridLineColor", question.getGridLineColor());
q.put("numberOfDots", question.getNumberOfDots());
q.put("gridType", question.getGridType());
q.put("scaleFactor", question.getScaleFactor());
q.put("gridScaleFactor", question.getGridScaleFactor());
return q;
}
@Override
public final Question updateQuestion(final Question question) {
try {
final Document q = database.getDocument(question.get_id());
q.put("subject", question.getSubject());
q.put("text", question.getText());
q.put("active", question.isActive());
q.put("releasedFor", question.getReleasedFor());
q.put("possibleAnswers", question.getPossibleAnswers());
q.put("noCorrect", question.isNoCorrect());
q.put("piRound", question.getPiRound());
q.put("showStatistic", question.isShowStatistic());
q.put("showAnswer", question.isShowAnswer());
q.put("abstention", question.isAbstention());
q.put("image", question.getImage());
q.put("fcImage", question.getFcImage());
q.put("gridSize", question.getGridSize());
q.put("offsetX", question.getOffsetX());
q.put("offsetY", question.getOffsetY());
q.put("zoomLvl", question.getZoomLvl());
q.put("gridOffsetX", question.getGridOffsetX());
q.put("gridOffsetY", question.getGridOffsetY());
q.put("gridZoomLvl", question.getGridZoomLvl());
q.put("gridSizeX", question.getGridSizeX());
q.put("gridSizeY", question.getGridSizeY());
q.put("gridIsHidden", question.getGridIsHidden());
q.put("imgRotation", question.getImgRotation());
q.put("toggleFieldsLeft", question.getToggleFieldsLeft());
q.put("numClickableFields", question.getNumClickableFields());
q.put("thresholdCorrectAnswers", question.getThresholdCorrectAnswers());
q.put("cvIsColored", question.getCvIsColored());
q.put("gridLineColor", question.getGridLineColor());
q.put("numberOfDots", question.getNumberOfDots());
q.put("gridType", question.getGridType());
q.put("scaleFactor", question.getScaleFactor());
q.put("gridScaleFactor", question.getGridScaleFactor());
database.saveDocument(q);
question.set_rev(q.getRev());
return question;
} catch (final IOException e) {
LOGGER.error("Could not update question {}", question);
}
return null;
}
@Override
public final InterposedQuestion saveQuestion(final Session session, final InterposedQuestion question, User user) {
final Document q = new Document();
q.put("type", "interposed_question");
q.put("sessionId", session.get_id());
q.put("subject", question.getSubject());
q.put("text", question.getText());
if (question.getTimestamp() != 0) {
q.put("timestamp", question.getTimestamp());
} else {
q.put("timestamp", System.currentTimeMillis());
}
q.put("read", false);
q.put("creator", user.getUsername());
try {
database.saveDocument(q);
question.set_id(q.getId());
question.set_rev(q.getRev());
return question;
} catch (final IOException e) {
LOGGER.error("Could not save interposed question {}", question);
}
return null;
}
@Override
public final Question getQuestion(final String id) {
try {
final NovaView view = new NovaView("skill_question/by_id");
view.setKey(id);
final ViewResults results = getDatabase().view(view);
if (results.getJSONArray("rows").optJSONObject(0) == null) {
return null;
}
final Question q = (Question) JSONObject.toBean(
results.getJSONArray("rows").optJSONObject(0).optJSONObject("value"),
Question.class
);
final JSONArray possibleAnswers = results.getJSONArray("rows").optJSONObject(0).optJSONObject("value")
.getJSONArray("possibleAnswers");
@SuppressWarnings("unchecked")
final Collection<PossibleAnswer> answers = JSONArray.toCollection(possibleAnswers, PossibleAnswer.class);
q.setPossibleAnswers(new ArrayList<PossibleAnswer>(answers));
q.setSessionKeyword(getSessionKeyword(q.getSessionId()));
return q;
} catch (final IOException e) {
LOGGER.error("Could not get question with id {}", id);
}
return null;
}
@Override
public final LoggedIn registerAsOnlineUser(final User user, final Session session) {
try {
final NovaView view = new NovaView("logged_in/all");
view.setKey(user.getUsername());
final ViewResults results = getDatabase().view(view);
LoggedIn loggedIn = new LoggedIn();
if (results.getJSONArray("rows").optJSONObject(0) != null) {
final JSONObject json = results.getJSONArray("rows").optJSONObject(0).optJSONObject("value");
loggedIn = (LoggedIn) JSONObject.toBean(json, LoggedIn.class);
final JSONArray vs = json.optJSONArray("visitedSessions");
if (vs != null) {
@SuppressWarnings("unchecked")
final Collection<VisitedSession> visitedSessions = JSONArray.toCollection(vs, VisitedSession.class);
loggedIn.setVisitedSessions(new ArrayList<VisitedSession>(visitedSessions));
}
/* Do not clutter CouchDB. Only update once every 3 hours per session. */
if (loggedIn.getSessionId().equals(session.get_id()) && loggedIn.getTimestamp() > System.currentTimeMillis() - 3 * 3600000) {
return loggedIn;
}
}
loggedIn.setUser(user.getUsername());
loggedIn.setSessionId(session.get_id());
loggedIn.addVisitedSession(session);
loggedIn.updateTimestamp();
final JSONObject json = JSONObject.fromObject(loggedIn);
final Document doc = new Document(json);
if (doc.getId().isEmpty()) {
// If this is a new user without a logged_in document, we have
// to remove the following
// pre-filled fields. Otherwise, CouchDB will take these empty
// fields as genuine
// identifiers, and will throw errors afterwards.
doc.remove("_id");
doc.remove("_rev");
}
getDatabase().saveDocument(doc);
final LoggedIn l = (LoggedIn) JSONObject.toBean(doc.getJSONObject(), LoggedIn.class);
final JSONArray vs = doc.getJSONObject().optJSONArray("visitedSessions");
if (vs != null) {
@SuppressWarnings("unchecked")
final Collection<VisitedSession> visitedSessions = JSONArray.toCollection(vs, VisitedSession.class);
l.setVisitedSessions(new ArrayList<VisitedSession>(visitedSessions));
}
return l;
} catch (final IOException e) {
return null;
}
}
@Override
public final void updateSessionOwnerActivity(final Session session) {
try {
/* Do not clutter CouchDB. Only update once every 3 hours. */
if (session.getLastOwnerActivity() > System.currentTimeMillis() - 3 * 3600000) {
return;
}
session.setLastOwnerActivity(System.currentTimeMillis());
final JSONObject json = JSONObject.fromObject(session);
getDatabase().saveDocument(new Document(json));
} catch (final IOException e) {
LOGGER.error("Failed to update lastOwnerActivity for Session {}", session);
return;
}
}
@Override
public final List<String> getQuestionIds(final Session session, final User user) {
NovaView view = new NovaView("skill_question/by_session_only_id_for_all");
view.setKey(session.get_id());
return collectQuestionIds(view);
}
@Override
public final void deleteQuestionWithAnswers(final Question question) {
try {
deleteAnswers(question);
deleteDocument(question.get_id());
} catch (final IOException e) {
LOGGER.error("IOException: Could not delete question {}", question.get_id());
}
}
@Override
public final void deleteAllQuestionsWithAnswers(final Session session) {
final NovaView view = new NovaView("skill_question/by_session");
deleteAllQuestionDocumentsWithAnswers(session, view);
}
private void deleteAllQuestionDocumentsWithAnswers(final Session session, final NovaView view) {
view.setStartKeyArray(session.get_id());
view.setEndKey(session.get_id(), "{}");
final ViewResults results = getDatabase().view(view);
for (final Document d : results.getResults()) {
final Question q = new Question();
q.set_id(d.getId());
deleteQuestionWithAnswers(q);
}
}
private void deleteDocument(final String documentId) throws IOException {
final Document d = getDatabase().getDocument(documentId);
getDatabase().deleteDocument(d);
}
@Override
public final void deleteAnswers(final Question question) {
try {
final NovaView view = new NovaView("answer/cleanup");
view.setKey(question.get_id());
final ViewResults results = getDatabase().view(view);
for (final Document d : results.getResults()) {
deleteDocument(d.getId());
}
} catch (final IOException e) {
LOGGER.error("IOException: Could not delete answers for question {}", question.get_id());
}
}
@Override
public final List<String> getUnAnsweredQuestionIds(final Session session, final User user) {
final NovaView view = new NovaView("answer/by_user");
view.setKey(user.getUsername(), session.get_id());
return collectUnansweredQuestionIds(getQuestionIds(session, user), view);
}
@Override
public final Answer getMyAnswer(final User me, final String questionId, final int piRound) {
final NovaView view = new NovaView("answer/by_question_and_user_and_piround");
if (2 == piRound) {
view.setKey(questionId, me.getUsername(), "2");
} else {
/* needed for legacy questions whose piRound property has not been set */
view.setStartKey(questionId, me.getUsername());
view.setEndKey(questionId, me.getUsername(), "1");
}
final ViewResults results = getDatabase().view(view);
if (results.getResults().isEmpty()) {
return null;
}
return (Answer) JSONObject.toBean(
results.getJSONArray("rows").optJSONObject(0).optJSONObject("value"),
Answer.class
);
}
@Override
public final List<Answer> getAnswers(final String questionId, final int piRound) {
final NovaView view = new NovaView("skill_question/count_answers_by_question_and_piround");
if (2 == piRound) {
view.setStartKey(questionId, "2");
view.setEndKey(questionId, "2", "{}");
} else {
/* needed for legacy questions whose piRound property has not been set */
view.setStartKeyArray(questionId);
view.setEndKeyArray(questionId, "1", "{}");
}
view.setGroup(true);
final ViewResults results = getDatabase().view(view);
final int abstentionCount = getAbstentionAnswerCount(questionId);
final List<Answer> answers = new ArrayList<Answer>();
for (final Document d : results.getResults()) {
final Answer a = new Answer();
a.setAnswerCount(d.getInt("value"));
a.setAbstentionCount(abstentionCount);
a.setQuestionId(d.getJSONObject().getJSONArray("key").getString(0));
a.setPiRound(piRound);
final String answerText = d.getJSONObject().getJSONArray("key").getString(2);
a.setAnswerText("null".equals(answerText) ? null : answerText);
answers.add(a);
}
return answers;
}
private int getAbstentionAnswerCount(final String questionId) {
final NovaView view = new NovaView("skill_question/count_abstention_answers_by_question");
view.setKey(questionId);
view.setGroup(true);
final ViewResults results = getDatabase().view(view);
if (results.getResults().size() == 0) {
return 0;
}
return results.getJSONArray("rows").optJSONObject(0).optInt("value");
}
@Override
public final int getAnswerCount(final Question question, final int piRound) {
final NovaView view = new NovaView("skill_question/count_total_answers_by_question_and_piround");
view.setGroup(true);
view.setStartKey(question.get_id(), String.valueOf(piRound));
view.setEndKey(question.get_id(), String.valueOf(piRound), "{}");
final ViewResults results = getDatabase().view(view);
if (results.getResults().size() == 0) {
return 0;
}
return results.getJSONArray("rows").optJSONObject(0).optInt("value");
}
private boolean isEmptyResults(final ViewResults results) {
return results == null || results.getResults().isEmpty() || results.getJSONArray("rows").size() == 0;
}
@Override
public List<Answer> getFreetextAnswers(final String questionId) {
final List<Answer> answers = new ArrayList<Answer>();
final NovaView view = new NovaView("skill_question/freetext_answers_full");
view.setKey(questionId);
final ViewResults results = getDatabase().view(view);
if (results.getResults().isEmpty()) {
return answers;
}
for (final Document d : results.getResults()) {
final Answer a = (Answer) JSONObject.toBean(d.getJSONObject().getJSONObject("value"), Answer.class);
a.setQuestionId(questionId);
answers.add(a);
}
return answers;
}
@Override
public List<Answer> getMyAnswers(final User me, final String sessionKey) {
final Session s = getSessionFromKeyword(sessionKey);
if (s == null) {
throw new NotFoundException();
}
final NovaView view = new NovaView("answer/by_user_and_session_full");
view.setKey(me.getUsername(), s.get_id());
final ViewResults results = getDatabase().view(view);
final List<Answer> answers = new ArrayList<Answer>();
if (results == null || results.getResults() == null || results.getResults().isEmpty()) {
return answers;
}
for (final Document d : results.getResults()) {
final Answer a = (Answer) JSONObject.toBean(d.getJSONObject().getJSONObject("value"), Answer.class);
a.set_id(d.getId());
a.set_rev(d.getRev());
a.setUser(me.getUsername());
a.setSessionId(s.get_id());
answers.add(a);
}
return answers;
}
@Override
public int getTotalAnswerCount(final String sessionKey) {
final Session s = getSessionFromKeyword(sessionKey);
if (s == null) {
throw new NotFoundException();
}
final NovaView view = new NovaView("skill_question/count_answers_by_session");
view.setKey(s.get_id());
final ViewResults results = getDatabase().view(view);
if (results.getResults().size() == 0) {
return 0;
}
return results.getJSONArray("rows").optJSONObject(0).optInt("value");
}
@Override
public int getInterposedCount(final String sessionKey) {
final Session s = getSessionFromKeyword(sessionKey);
if (s == null) {
throw new NotFoundException();
}
final NovaView view = new NovaView("interposed_question/count_by_session");
view.setKey(s.get_id());
view.setGroup(true);
final ViewResults results = getDatabase().view(view);
if (results.size() == 0 || results.getResults().size() == 0) {
return 0;
}
return results.getJSONArray("rows").optJSONObject(0).optInt("value");
}
@Override
public InterposedReadingCount getInterposedReadingCount(final Session session) {
final NovaView view = new NovaView("interposed_question/count_by_session_reading");
view.setStartKeyArray(session.get_id());
view.setEndKeyArray(session.get_id(), "{}");
view.setGroup(true);
return getInterposedReadingCount(view);
}
@Override
public InterposedReadingCount getInterposedReadingCount(final Session session, final User user) {
final NovaView view = new NovaView("interposed_question/count_by_session_reading_for_creator");
view.setStartKeyArray(session.get_id(), user.getUsername());
view.setEndKeyArray(session.get_id(), user.getUsername(), "{}");
view.setGroup(true);
return getInterposedReadingCount(view);
}
private InterposedReadingCount getInterposedReadingCount(final NovaView view) {
final ViewResults results = getDatabase().view(view);
if (results.size() == 0 || results.getResults().size() == 0) {
return new InterposedReadingCount();
}
// A complete result looks like this. Note that the second row is optional, and that the first one may be
// 'unread' or 'read', i.e., results may be switched around or only one result may be present.
// count = {"rows":[
// {"key":["cecebabb21b096e592d81f9c1322b877","Guestc9350cf4a3","read"],"value":1},
// {"key":["cecebabb21b096e592d81f9c1322b877","Guestc9350cf4a3","unread"],"value":1}
// ]}
int read = 0, unread = 0;
String type = "";
final JSONObject fst = results.getJSONArray("rows").getJSONObject(0);
final JSONObject snd = results.getJSONArray("rows").optJSONObject(1);
final JSONArray fstkey = fst.getJSONArray("key");
if (fstkey.size() == 2) {
type = fstkey.getString(1);
} else if (fstkey.size() == 3) {
type = fstkey.getString(2);
}
if (type.equals("read")) {
read = fst.optInt("value");
} else if (type.equals("unread")) {
unread = fst.optInt("value");
}
if (snd != null) {
final JSONArray sndkey = snd.getJSONArray("key");
if (sndkey.size() == 2) {
type = sndkey.getString(1);
} else {
type = sndkey.getString(2);
}
if (type.equals("read")) {
read = snd.optInt("value");
} else if (type.equals("unread")) {
unread = snd.optInt("value");
}
}
return new InterposedReadingCount(read, unread);
}
@Override
public List<InterposedQuestion> getInterposedQuestions(final Session session) {
final NovaView view = new NovaView("interposed_question/by_session_full");
view.setKey(session.get_id());
final ViewResults questions = getDatabase().view(view);
if (questions == null || questions.isEmpty()) {
return null;
}
return createInterposedList(session, questions);
}
@Override
public List<InterposedQuestion> getInterposedQuestions(final Session session, final User user) {
final NovaView view = new NovaView("interposed_question/by_session_and_creator");
view.setKey(session.get_id(), user.getUsername());
final ViewResults questions = getDatabase().view(view);
if (questions == null || questions.isEmpty()) {
return null;
}
return createInterposedList(session, questions);
}
private List<InterposedQuestion> createInterposedList(
final Session session, final ViewResults questions) {
final List<InterposedQuestion> result = new ArrayList<InterposedQuestion>();
for (final Document document : questions.getResults()) {
final InterposedQuestion question = (InterposedQuestion) JSONObject.toBean(
document.getJSONObject().getJSONObject("value"),
InterposedQuestion.class
);
question.setSessionId(session.getKeyword());
question.set_id(document.getId());
result.add(question);
}
return result;
}
public InterposedQuestion getInterposedQuestion(final String sessionKey, final String documentId) {
try {
final Document document = getDatabase().getDocument(documentId);
if (document == null) {
LOGGER.error("Document is NULL");
return null;
}
return (InterposedQuestion) JSONObject.toBean(document.getJSONObject(), InterposedQuestion.class);
} catch (final IOException e) {
LOGGER.error("Error while retrieving interposed question", e);
}
return null;
}
@Override
public int countSessions() {
return sessionsCountValue("openSessions")
+ sessionsCountValue("closedSessions");
}
@Override
public int countClosedSessions() {
return sessionsCountValue("closedSessions");
}
@Override
public int countOpenSessions() {
return sessionsCountValue("openSessions");
}
@Override
public int countAnswers() {
return sessionsCountValue("answers");
}
@Override
public int countQuestions() {
return sessionsCountValue("questions");
}
private int sessionsCountValue(final String key) {
try {
final View view = new View("statistic/count_sessions");
view.setGroup(true);
final ViewResults results = getDatabase().view(view);
if (isEmptyResults(results)) {
return 0;
}
int result = 0;
final JSONArray rows = results.getJSONArray("rows");
for (int i = 0; i < rows.size(); i++) {
final JSONObject row = rows.getJSONObject(i);
if (
row.getString("key").equals(key)
) {
result += row.getInt("value");
}
}
return result;
} catch (final Exception e) {
LOGGER.error("Error while retrieving session count", e);
}
return 0;
}
@Override
public InterposedQuestion getInterposedQuestion(final String questionId) {
try {
final Document document = getDatabase().getDocument(questionId);
final InterposedQuestion question = (InterposedQuestion) JSONObject.toBean(document.getJSONObject(),
InterposedQuestion.class);
question.setSessionId(getSessionKeyword(question.getSessionId()));
return question;
} catch (final IOException e) {
LOGGER.error("Could not load interposed question {}", questionId);
}
return null;
}
@Override
public void markInterposedQuestionAsRead(final InterposedQuestion question) {
try {
question.setRead(true);
final Document document = getDatabase().getDocument(question.get_id());
document.put("read", question.isRead());
getDatabase().saveDocument(document);
} catch (final IOException e) {
LOGGER.error("Coulg not mark interposed question as read {}", question.get_id());
}
}
@Override
public List<Session> getMyVisitedSessions(final User user) {
final NovaView view = new NovaView("logged_in/visited_sessions_by_user");
view.setKey(user.getUsername());
final ViewResults sessions = getDatabase().view(view);
final List<Session> allSessions = new ArrayList<Session>();
for (final Document d : sessions.getResults()) {
// Not all users have visited sessions
if (d.getJSONObject().optJSONArray("value") != null) {
@SuppressWarnings("unchecked")
final Collection<Session> visitedSessions = JSONArray.toCollection(
d.getJSONObject().getJSONArray("value"),
Session.class
);
allSessions.addAll(visitedSessions);
}
}
// Filter sessions that don't exist anymore, also filter my own sessions
final List<Session> result = new ArrayList<Session>();
final List<Session> filteredSessions = new ArrayList<Session>();
for (final Session s : allSessions) {
try {
final Session session = getSessionFromKeyword(s.getKeyword());
if (session != null && !session.isCreator(user)) {
result.add(session);
} else {
filteredSessions.add(s);
}
} catch (final NotFoundException e) {
filteredSessions.add(s);
}
}
if (filteredSessions.isEmpty()) {
return result;
}
// Update document to remove sessions that don't exist anymore
try {
List<VisitedSession> visitedSessions = new ArrayList<VisitedSession>();
for (final Session s : result) {
visitedSessions.add(new VisitedSession(s));
}
final LoggedIn loggedIn = new LoggedIn();
final Document loggedInDocument = getDatabase().getDocument(sessions.getResults().get(0).getString("id"));
loggedIn.setSessionId(loggedInDocument.getString("sessionId"));
loggedIn.setUser(user.getUsername());
loggedIn.setTimestamp(loggedInDocument.getLong("timestamp"));
loggedIn.setType(loggedInDocument.getString("type"));
loggedIn.setVisitedSessions(visitedSessions);
loggedIn.set_id(loggedInDocument.getId());
loggedIn.set_rev(loggedInDocument.getRev());
final JSONObject json = JSONObject.fromObject(loggedIn);
final Document doc = new Document(json);
getDatabase().saveDocument(doc);
} catch (IOException e) {
LOGGER.error("Could not clean up logged_in document of {}", user.getUsername());
}
return result;
}
@Override
public List<SessionInfo> getMyVisitedSessionsInfo(final User user) {
List<Session> sessions = this.getMyVisitedSessions(user);
return this.getInfosForVisitedSessions(sessions, user);
}
@Override
public Answer saveAnswer(final Answer answer, final User user) {
try {
final Document a = new Document();
a.put("type", "skill_question_answer");
a.put("sessionId", answer.getSessionId());
a.put("questionId", answer.getQuestionId());
a.put("answerSubject", answer.getAnswerSubject());
a.put("questionVariant", answer.getQuestionVariant());
a.put("questionValue", answer.getQuestionValue());
a.put("answerText", answer.getAnswerText());
a.put("timestamp", answer.getTimestamp());
a.put("user", user.getUsername());
a.put("piRound", answer.getPiRound());
a.put("abstention", answer.isAbstention());
database.saveDocument(a);
answer.set_id(a.getId());
answer.set_rev(a.getRev());
return answer;
} catch (final IOException e) {
LOGGER.error("Could not save answer {}", answer);
}
return null;
}
@Override
public Answer updateAnswer(final Answer answer) {
try {
final Document a = database.getDocument(answer.get_id());
a.put("answerSubject", answer.getAnswerSubject());
a.put("answerText", answer.getAnswerText());
a.put("timestamp", answer.getTimestamp());
a.put("abstention", answer.isAbstention());
a.put("questionValue", answer.getQuestionValue());
database.saveDocument(a);
answer.set_rev(a.getRev());
return answer;
} catch (final IOException e) {
LOGGER.error("Could not save answer {}", answer);
}
return null;
}
@Override
public void deleteAnswer(final String answerId) {
try {
database.deleteDocument(database.getDocument(answerId));
} catch (final IOException e) {
LOGGER.error("Could not delete answer {} because of {}", answerId, e.getMessage());
}
}
@Override
public void deleteInterposedQuestion(final InterposedQuestion question) {
try {
deleteDocument(question.get_id());
} catch (final IOException e) {
LOGGER.error("Could not delete interposed question {} because of {}", question.get_id(), e.getMessage());
}
}
@Override
public List<Session> getCourseSessions(final List<Course> courses) {
final ExtendedView view = new ExtendedView("session/by_courseid");
view.setCourseIdKeys(courses);
final ViewResults sessions = getDatabase().view(view);
final List<Session> result = new ArrayList<Session>();
for (final Document d : sessions.getResults()) {
final Session session = (Session) JSONObject.toBean(
d.getJSONObject().getJSONObject("value"),
Session.class
);
result.add(session);
}
return result;
}
private static class ExtendedView extends NovaView {
public ExtendedView(final String fullname) {
super(fullname);
}
public void setCourseIdKeys(final List<Course> courses) {
List<String> courseIds = new ArrayList<String>();
for (Course c : courses) {
courseIds.add(c.getId());
}
setKeys(courseIds);
}
public void setSessionIdKeys(final List<Session> sessions) {
List<String> sessionIds = new ArrayList<String>();
for (Session s : sessions) {
sessionIds.add(s.get_id());
}
setKeys(sessionIds);
}
}
@Override
public Session lockSession(final Session session, final Boolean lock) {
try {
final Document s = database.getDocument(session.get_id());
s.put("active", lock);
database.saveDocument(s);
session.set_rev(s.getRev());
return session;
} catch (final IOException e) {
LOGGER.error("Could not lock session {}", session);
}
return null;
}
@Override
public Session updateSession(final Session session) {
try {
final Document s = database.getDocument(session.get_id());
s.put("name", session.getName());
s.put("shortName", session.getShortName());
s.put("active", session.isActive());
database.saveDocument(s);
session.set_rev(s.getRev());
return session;
} catch (final IOException e) {
LOGGER.error("Could not lock session {}", session);
}
return null;
}
@Override
public void deleteSession(final Session session) {
try {
deleteDocument(session.get_id());
} catch (final IOException e) {
LOGGER.error("Could not delete session {}", session);
}
}
@Override
public List<Question> getLectureQuestions(final User user, final Session session) {
String viewName;
if (session.isCreator(user)) {
viewName = "skill_question/lecture_question_by_session";
} else {
viewName = "skill_question/lecture_question_by_session_for_all";
}
return getQuestions(new NovaView(viewName), session);
}
@Override
public List<Question> getFlashcards(final User user, final Session session) {
String viewName;
if (session.isCreator(user)) {
viewName = "skill_question/flashcard_by_session";
} else {
viewName = "skill_question/flashcard_by_session_for_all";
}
return getQuestions(new NovaView(viewName), session);
}
@Override
public List<Question> getPreparationQuestions(final User user, final Session session) {
String viewName;
if (session.isCreator(user)) {
viewName = "skill_question/preparation_question_by_session";
} else {
viewName = "skill_question/preparation_question_by_session_for_all";
}
return getQuestions(new NovaView(viewName), session);
}
private List<Question> getQuestions(final NovaView view, final Session session) {
view.setStartKeyArray(session.get_id());
view.setEndKeyArray(session.get_id(), "{}");
final ViewResults questions = getDatabase().view(view);
if (questions == null || questions.isEmpty()) {
return null;
}
final List<Question> result = new ArrayList<Question>();
final MorpherRegistry morpherRegistry = JSONUtils.getMorpherRegistry();
final Morpher dynaMorpher = new BeanMorpher(PossibleAnswer.class, morpherRegistry);
morpherRegistry.registerMorpher(dynaMorpher);
for (final Document document : questions.getResults()) {
final Question question = (Question) JSONObject.toBean(
document.getJSONObject().getJSONObject("value"),
Question.class
);
@SuppressWarnings("unchecked")
final
Collection<PossibleAnswer> answers = JSONArray.toCollection(
document.getJSONObject().getJSONObject("value").getJSONArray("possibleAnswers"),
PossibleAnswer.class
);
question.setPossibleAnswers(new ArrayList<PossibleAnswer>(answers));
question.setSessionKeyword(session.getKeyword());
if (!"freetext".equals(question.getQuestionType()) && 0 == question.getPiRound()) {
/* needed for legacy questions whose piRound property has not been set */
question.setPiRound(1);
}
result.add(question);
}
return result;
}
@Override
public int getLectureQuestionCount(final Session session) {
return getQuestionCount(new NovaView("skill_question/lecture_question_count_by_session"), session);
}
@Override
public int getFlashcardCount(final Session session) {
return getQuestionCount(new NovaView("skill_question/flashcard_count_by_session"), session);
}
@Override
public int getPreparationQuestionCount(final Session session) {
return getQuestionCount(new NovaView("skill_question/preparation_question_count_by_session"), session);
}
private int getQuestionCount(final NovaView view, final Session session) {
view.setKey(session.get_id());
final ViewResults results = getDatabase().view(view);
if (results.getJSONArray("rows").optJSONObject(0) == null) {
return 0;
}
return results.getJSONArray("rows").optJSONObject(0).optInt("value");
}
@Override
public int countLectureQuestionAnswers(final Session session) {
return countQuestionVariantAnswers(session, "lecture");
}
@Override
public int countPreparationQuestionAnswers(final Session session) {
return countQuestionVariantAnswers(session, "preparation");
}
private int countQuestionVariantAnswers(final Session session, final String variant) {
final NovaView view = new NovaView("skill_question/count_answers_by_session_and_question_variant");
view.setKey(session.get_id(), variant);
final ViewResults results = getDatabase().view(view);
if (results.getResults().size() == 0) {
return 0;
}
return results.getJSONArray("rows").optJSONObject(0).optInt("value");
}
@Override
public void deleteAllLectureQuestionsWithAnswers(final Session session) {
final NovaView view = new NovaView("skill_question/lecture_question_by_session");
deleteAllQuestionDocumentsWithAnswers(session, view);
}
@Override
public void deleteAllFlashcardsWithAnswers(final Session session) {
final NovaView view = new NovaView("skill_question/flashcard_by_session");
deleteAllQuestionDocumentsWithAnswers(session, view);
}
@Override
public void deleteAllPreparationQuestionsWithAnswers(final Session session) {
final NovaView view = new NovaView("skill_question/preparation_question_by_session");
deleteAllQuestionDocumentsWithAnswers(session, view);
}
@Override
public List<String> getUnAnsweredLectureQuestionIds(final Session session, final User user) {
final NovaView view = new NovaView("answer/variant_by_user");
view.setKey(user.getUsername(), session.get_id(), "lecture");
return collectUnansweredQuestionIds(getLectureQuestionIds(session, user), view);
}
private List<String> getLectureQuestionIds(final Session session, final User user) {
NovaView view = new NovaView("skill_question/lecture_question_ids_by_session_for_all");
view.setStartKeyArray(session.get_id());
view.setEndKeyArray(session.get_id(), "{}");
return collectQuestionIds(view);
}
@Override
public List<String> getUnAnsweredPreparationQuestionIds(final Session session, final User user) {
final NovaView view = new NovaView("answer/variant_by_user");
view.setKey(user.getUsername(), session.get_id(), "preparation");
return collectUnansweredQuestionIds(getPreparationQuestionIds(session, user), view);
}
private List<String> getPreparationQuestionIds(final Session session, final User user) {
NovaView view = new NovaView("skill_question/preparation_question_ids_by_session_for_all");
view.setStartKeyArray(session.get_id());
view.setEndKeyArray(session.get_id(), "{}");
return collectQuestionIds(view);
}
private List<String> collectUnansweredQuestionIds(
final List<String> questions,
final NovaView view
) {
final ViewResults answeredQuestions = getDatabase().view(view);
final List<String> answered = new ArrayList<String>();
for (final Document d : answeredQuestions.getResults()) {
answered.add(d.getString("value"));
}
final List<String> unanswered = new ArrayList<String>();
for (final String questionId : questions) {
if (!answered.contains(questionId)) {
unanswered.add(questionId);
}
}
return unanswered;
}
private List<String> collectQuestionIds(final NovaView view) {
final ViewResults results = getDatabase().view(view);
if (results.getResults().size() == 0) {
return new ArrayList<String>();
}
final List<String> ids = new ArrayList<String>();
for (final Document d : results.getResults()) {
ids.add(d.getId());
}
return ids;
}
@Override
public void deleteAllInterposedQuestions(final Session session) {
final NovaView view = new NovaView("interposed_question/by_session");
view.setKey(session.get_id());
final ViewResults questions = getDatabase().view(view);
deleteAllInterposedQuestions(session, questions);
}
@Override
public void deleteAllInterposedQuestions(final Session session, final User user) {
final NovaView view = new NovaView("interposed_question/by_session_and_creator");
view.setKey(session.get_id(), user.getUsername());
final ViewResults questions = getDatabase().view(view);
deleteAllInterposedQuestions(session, questions);
}
private void deleteAllInterposedQuestions(final Session session, final ViewResults questions) {
if (questions == null || questions.isEmpty()) {
return;
}
for (final Document document : questions.getResults()) {
try {
deleteDocument(document.getId());
} catch (final IOException e) {
LOGGER.error("Could not delete all interposed questions {}", session);
}
}
}
@Override
public void publishAllQuestions(final Session session, final boolean publish) {
final List<Question> questions = getQuestions(new NovaView("skill_question/by_session"), session);
publishQuestions(session, publish, questions);
}
@Override
public void publishQuestions(final Session session, final boolean publish, List<Question> questions) {
for (final Question q : questions) {
q.setActive(publish);
}
final List<Document> documents = new ArrayList<Document>();
for (final Question q : questions) {
final Document d = toQuestionDocument(session, q);
d.setId(q.get_id());
d.setRev(q.get_rev());
documents.add(d);
}
try {
database.bulkSaveDocuments(documents.toArray(new Document[documents.size()]));
} catch (final IOException e) {
LOGGER.error("Could not bulk publish all questions: {}", e.getMessage());
}
}
@Override
public void deleteAllQuestionsAnswers(final Session session) {
final List<Question> questions = getQuestions(new NovaView("skill_question/by_session"), session);
for (final Question q : questions) {
deleteAnswers(q);
}
}
@Override
public void deleteAllPreparationAnswers(final Session session) {
final List<Question> questions = getQuestions(new NovaView("skill_question/preparation_question_by_session"), session);
for (final Question q : questions) {
deleteAnswers(q);
}
}
@Override
public void deleteAllLectureAnswers(final Session session) {
final List<Question> questions = getQuestions(new NovaView("skill_question/lecture_question_by_session"), session);
for (final Question q : questions) {
deleteAnswers(q);
}
}
@Override
public int getLearningProgress(final Session session) {
// Note: we have to use this many views because our CouchDB version does not support
// advanced features like summing over lists. Thus, we have to do it all by ourselves...
final NovaView maximumValueView = new NovaView("learning_progress_maximum_value/max");
final NovaView answerSumView = new NovaView("learning_progress_user_values/sum");
final NovaView answerDocumentCountView = new NovaView("learning_progress_course_answers/count");
maximumValueView.setKey(session.get_id());
answerSumView.setStartKeyArray(session.get_id());
answerSumView.setEndKeyArray(session.get_id(), "{}");
answerDocumentCountView.setStartKeyArray(session.get_id());
answerDocumentCountView.setEndKeyArray(session.get_id(), "{}");
answerDocumentCountView.setGroup(true);
final List<Document> maximumValueResult = getDatabase().view(maximumValueView).getResults();
final List<Document> answerSumResult = getDatabase().view(answerSumView).getResults();
final List<Document> answerDocumentCountResult = getDatabase().view(answerDocumentCountView).getResults();
if (maximumValueResult.isEmpty() || answerSumResult.isEmpty() || answerDocumentCountResult.isEmpty()) {
return 0;
}
final double courseMaximumValue = maximumValueResult.get(0).getInt("value");
final double userTotalValue = answerSumResult.get(0).getInt("value");
final double numUsers = answerDocumentCountResult.size();
if (courseMaximumValue == 0 || numUsers == 0) {
return 0;
}
final double courseAverageValue = userTotalValue / numUsers;
final double courseProgress = courseAverageValue / courseMaximumValue;
return (int)Math.min(100, Math.round(courseProgress * 100));
}
@Override
public SimpleEntry<Integer,Integer> getMyLearningProgress(final Session session, final User user) {
final int courseProgress = getLearningProgress(session);
final NovaView maximumValueView = new NovaView("learning_progress_maximum_value/max");
final NovaView answerSumView = new NovaView("learning_progress_user_values/sum");
maximumValueView.setKey(session.get_id());
answerSumView.setKey(session.get_id(), user.getUsername());
final List<Document> maximumValueResult = getDatabase().view(maximumValueView).getResults();
final List<Document> answerSumResult = getDatabase().view(answerSumView).getResults();
if (maximumValueResult.isEmpty() || answerSumResult.isEmpty()) {
return new AbstractMap.SimpleEntry<Integer, Integer>(0, courseProgress);
}
final double courseMaximumValue = maximumValueResult.get(0).getInt("value");
final double userTotalValue = answerSumResult.get(0).getInt("value");
if (courseMaximumValue == 0) {
return new AbstractMap.SimpleEntry<Integer, Integer>(0, courseProgress);
}
final double myProgress = userTotalValue / courseMaximumValue;
final int myLearningProgress = (int)Math.min(100, Math.round(myProgress*100));
return new AbstractMap.SimpleEntry<Integer, Integer>(myLearningProgress, courseProgress);
}
@Override
public DbUser createOrUpdateUser(DbUser user) {
try {
String id = user.getId();
String rev = user.getRev();
Document d = new Document();
if (null != id) {
d = database.getDocument(id, rev);
}
d.put("type", "userdetails");
d.put("username", user.getUsername());
d.put("password", user.getPassword());
d.put("activationKey", user.getActivationKey());
d.put("passwordResetKey", user.getPasswordResetKey());
d.put("passwordResetTime", user.getPasswordResetTime());
d.put("creation", user.getCreation());
d.put("lastLogin", user.getLastLogin());
database.saveDocument(d, id);
user.setId(d.getId());
user.setRev(d.getRev());
return user;
} catch (IOException e) {
LOGGER.error("Could not save user {}", user);
}
return null;
}
@Override
public DbUser getUser(String username) {
NovaView view = new NovaView("user/all");
view.setKey(username);
ViewResults results = this.getDatabase().view(view);
if (results.getJSONArray("rows").optJSONObject(0) == null) {
return null;
}
return (DbUser) JSONObject.toBean(
results.getJSONArray("rows").optJSONObject(0).optJSONObject("value"),
DbUser.class
);
}
@Override
public boolean deleteUser(DbUser dbUser) {
try {
this.deleteDocument(dbUser.getId());
return true;
} catch (IOException e) {
LOGGER.error("Could not delete user {}", dbUser.getId());
}
return false;
}
}
/*
* This file is part of ARSnova Backend.
* Copyright (C) 2012-2015 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.dao;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import de.thm.arsnova.connector.model.Course;
import de.thm.arsnova.entities.Answer;
import de.thm.arsnova.entities.DbUser;
import de.thm.arsnova.entities.InterposedQuestion;
import de.thm.arsnova.entities.InterposedReadingCount;
import de.thm.arsnova.entities.LoggedIn;
import de.thm.arsnova.entities.Question;
import de.thm.arsnova.entities.Session;
import de.thm.arsnova.entities.SessionInfo;
import de.thm.arsnova.entities.User;
public interface IDatabaseDao {
Session getSessionFromKeyword(String keyword);
Session getSession(String keyword);
List<Session> getMySessions(User user);
List<Session> getPublicPoolSessions();
List<Session> getMyPublicPoolSessions(User user);
Session saveSession(User user, Session session);
boolean sessionKeyAvailable(String keyword);
Question saveQuestion(Session session, Question question);
InterposedQuestion saveQuestion(Session session, InterposedQuestion question, User user);
Question getQuestion(String id);
List<Question> getSkillQuestions(User user, Session session);
int getSkillQuestionCount(Session session);
LoggedIn registerAsOnlineUser(User u, Session s);
void updateSessionOwnerActivity(Session session);
List<String> getQuestionIds(Session session, User user);
void deleteQuestionWithAnswers(Question question);
void deleteAllQuestionsWithAnswers(Session session);
List<String> getUnAnsweredQuestionIds(Session session, User user);
Answer getMyAnswer(User me, String questionId, int piRound);
List<Answer> getAnswers(String questionId, int piRound);
int getAnswerCount(Question question, int piRound);
List<Answer> getFreetextAnswers(String questionId);
List<Answer> getMyAnswers(User me, String sessionKey);
int getTotalAnswerCount(String sessionKey);
int getInterposedCount(String sessionKey);
InterposedReadingCount getInterposedReadingCount(Session session);
InterposedReadingCount getInterposedReadingCount(Session session, User user);
List<InterposedQuestion> getInterposedQuestions(Session session);
List<InterposedQuestion> getInterposedQuestions(Session session, User user);
int countSessions();
int countOpenSessions();
int countClosedSessions();
int countAnswers();
int countQuestions();
InterposedQuestion getInterposedQuestion(String questionId);
void markInterposedQuestionAsRead(InterposedQuestion question);
List<Session> getMyVisitedSessions(User user);
Question updateQuestion(Question question);
void deleteAnswers(Question question);
Answer saveAnswer(Answer answer, User user);
Answer updateAnswer(Answer answer);
Session getSessionFromId(String sessionId);
void deleteAnswer(String answerId);
void deleteInterposedQuestion(InterposedQuestion question);
List<Session> getCourseSessions(List<Course> courses);
Session lockSession(Session session, Boolean lock);
Session updateSession(Session session);
void deleteSession(Session session);
List<Question> getLectureQuestions(User user, Session session);
List<Question> getFlashcards(User user, Session session);
List<Question> getPreparationQuestions(User user, Session session);
int getLectureQuestionCount(Session session);
int getFlashcardCount(Session session);
int getPreparationQuestionCount(Session session);
int countLectureQuestionAnswers(Session session);
int countPreparationQuestionAnswers(Session session);
void deleteAllLectureQuestionsWithAnswers(Session session);
void deleteAllFlashcardsWithAnswers(Session session);
void deleteAllPreparationQuestionsWithAnswers(Session session);
List<String> getUnAnsweredLectureQuestionIds(Session session, User user);
List<String> getUnAnsweredPreparationQuestionIds(Session session, User user);
void deleteAllInterposedQuestions(Session session);
void deleteAllInterposedQuestions(Session session, User user);
void publishQuestions(Session session, boolean publish, List<Question> questions);
void publishAllQuestions(Session session, boolean publish);
void deleteAllQuestionsAnswers(Session session);
DbUser createOrUpdateUser(DbUser user);
DbUser getUser(String username);
boolean deleteUser(DbUser dbUser);
int getLearningProgress(Session session);
SimpleEntry<Integer, Integer> getMyLearningProgress(Session session, User user);
List<SessionInfo> getMySessionsInfo(User user);
List<SessionInfo> getMyPublicPoolSessionsInfo(final User user);
List<SessionInfo> getMyVisitedSessionsInfo(User currentUser);
void deleteAllPreparationAnswers(Session session);
void deleteAllLectureAnswers(Session session);
}
/*
* This file is part of ARSnova Backend.
* Copyright (C) 2012-2015 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.dao;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import com.fourspaces.couchdb.View;
public class NovaView extends View {
protected String keys;
public NovaView(final String fullname) {
super(fullname);
}
@Override
public void setStartKey(final String key) {
startKey = quote(key);
}
public void setStartKeyArray(final String key) {
if (isNumber(key)) {
startKey = encode("[" + key + "]");
} else {
startKey = encode("[\"" + key + "\"]");
}
}
public void setStartKeyArray(final String... keys) {
this.setStartKey(keys);
}
@Override
public void setEndKey(final String key) {
endKey = quote(key);
}
public void setEndKeyArray(final String key) {
if (isNumber(key)) {
endKey = encode("[" + key + "]");
} else {
endKey = encode("[\"" + key + "\"]");
}
}
public void setEndKeyArray(final String... keys) {
this.setEndKey(keys);
}
public void setStartKey(final String... keys) {
startKey = toJsonArray(keys);
}
public void setEndKey(final String... keys) {
endKey = toJsonArray(keys);
}
@Override
public void setKey(final String key) {
this.key = quote(key);
}
public void setKey(final String... keys) {
key = toJsonArray(keys);
}
public void setKeys(List<String> keys) {
this.keys = toJsonArray(keys.toArray(new String[keys.size()]));
}
@Override
public String getQueryString() {
final String tempQuery = super.getQueryString();
final StringBuilder query = new StringBuilder();
if (tempQuery != null) {
query.append(tempQuery);
}
if (keys != null) {
if (query.length() > 0) {
query.append("&");
}
query.append("keys=" + keys);
}
if (query.length() == 0) {
return null;
}
return query.toString();
}
private String toJsonArray(final String[] strs) {
final List<String> strings = new ArrayList<String>();
for (final String string : strs) {
if (isNumber(string) || isPlaceholder(string) || isArray(string)) {
strings.add(string);
} else {
strings.add("\"" + string + "\"");
}
}
return encode("[" + StringUtils.join(strings, ",") + "]");
}
private String quote(final String string) {
return encode("\"" + string + "\"");
}
private boolean isNumber(final String string) {
return string.matches("^[0-9]+$");
}
private boolean isPlaceholder(final String string) {
return string.equals("{}");
}
private boolean isArray(final String string) {
return string.startsWith("[") && string.endsWith("]");
}
private String encode(final String string) {
try {
return URLEncoder.encode(string, "UTF-8");
} catch (final UnsupportedEncodingException e) {
// Since we're using 'UTF-8', this should Exception should never occur.
return "";
}
}
}
package de.thm.arsnova.dao;
/*
* This file is part of ARSnova Backend.
* Copyright (C) 2012-2015 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.entities;
import com.fasterxml.jackson.annotation.JsonIgnore;
public class Answer {
private String _id;
private String _rev;
private String type;
private String sessionId;
private String questionId;
private String answerText;
private String answerSubject;
private String questionVariant;
private int questionValue;
private int piRound;
private String user;
private long timestamp;
private int answerCount = 1;
private boolean abstention;
private int abstentionCount;
public Answer() {
this.type = "skill_question_answer";
}
public final String get_id() {
return _id;
}
public final void set_id(String _id) {
this._id = _id;
}
public final String get_rev() {
return _rev;
}
public final void set_rev(final String _rev) {
this._rev = _rev;
}
public final String getType() {
return type;
}
public final void setType(final String type) {
this.type = type;
}
public final String getSessionId() {
return sessionId;
}
public final void setSessionId(final String sessionId) {
this.sessionId = sessionId;
}
public final String getQuestionId() {
return questionId;
}
public final void setQuestionId(final String questionId) {
this.questionId = questionId;
}
public final String getAnswerText() {
return answerText;
}
public final void setAnswerText(final String answerText) {
this.answerText = answerText;
}
public final String getAnswerSubject() {
return answerSubject;
}
public final void setAnswerSubject(final String answerSubject) {
this.answerSubject = answerSubject;
}
public int getPiRound() {
return piRound;
}
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 */
@JsonIgnore
public final String getUser() {
return user;
}
public final void setUser(final String user) {
this.user = user;
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
public final int getAnswerCount() {
return answerCount;
}
public final void setAnswerCount(final int answerCount) {
this.answerCount = answerCount;
}
public boolean isAbstention() {
return abstention;
}
public void setAbstention(boolean abstention) {
this.abstention = abstention;
}
public int getAbstentionCount() {
return abstentionCount;
}
public void setAbstentionCount(int abstentionCount) {
this.abstentionCount = abstentionCount;
}
public String getQuestionVariant() {
return questionVariant;
}
public void setQuestionVariant(String questionVariant) {
this.questionVariant = questionVariant;
}
public int getQuestionValue() {
return questionValue;
}
public void setQuestionValue(int questionValue) {
this.questionValue = questionValue;
}
@Override
public final String toString() {
return "Answer type:'" + type + "'"
+ ", session: " + sessionId
+ ", question: " + questionId
+ ", subject: " + answerSubject
+ ", answerCount: " + answerCount
+ ", answer: " + answerText
+ ", user: " + user;
}
}
/*
* This file is part of ARSnova Backend.
* Copyright (C) 2012-2015 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.entities;
import java.util.List;
public class Question {
private String type;
private String questionType;
private String questionVariant;
private String subject;
private String text;
private boolean active;
private String releasedFor;
private List<PossibleAnswer> possibleAnswers;
private boolean noCorrect;
// TODO: We currently need both sessionId and sessionKeyword, but sessionKeyword will not be persisted.
private String sessionId;
private String sessionKeyword;
private long timestamp;
private int number;
private int duration;
private int piRound;
private boolean showStatistic; // sic
private boolean showAnswer;
private boolean abstention;
private String _id;
private String _rev;
private String image;
private String fcImage;
private int gridSize;
private int offsetX;
private int offsetY;
private int zoomLvl;
private int gridOffsetX;
private int gridOffsetY;
private int gridZoomLvl;
private int gridSizeX;
private int gridSizeY;
private boolean gridIsHidden;
private int imgRotation;
private boolean toggleFieldsLeft;
private int numClickableFields;
private int thresholdCorrectAnswers;
private boolean cvIsColored;
private String gridLineColor;
private int numberOfDots;
private String gridType;
private String scaleFactor;
private String gridScaleFactor;
public final String getType() {
return type;
}
public final void setType(final String type) {
this.type = type;
}
public final String getQuestionType() {
return questionType;
}
public final void setQuestionType(final String questionType) {
this.questionType = questionType;
}
public final String getQuestionVariant() {
return questionVariant;
}
public final void setQuestionVariant(final String questionVariant) {
this.questionVariant = questionVariant;
}
public final String getSubject() {
return subject;
}
public final void setSubject(final String subject) {
this.subject = subject;
}
public final String getText() {
return text;
}
public final void setText(final String text) {
this.text = text;
}
public final boolean isActive() {
return active;
}
public final void setActive(final boolean active) {
this.active = active;
}
public final String getReleasedFor() {
return releasedFor;
}
public final void setReleasedFor(final String releasedFor) {
this.releasedFor = releasedFor;
}
public final List<PossibleAnswer> getPossibleAnswers() {
return possibleAnswers;
}
public final void setPossibleAnswers(final List<PossibleAnswer> possibleAnswers) {
this.possibleAnswers = possibleAnswers;
}
public final boolean isNoCorrect() {
return noCorrect;
}
public final void setNoCorrect(final boolean noCorrect) {
this.noCorrect = noCorrect;
}
public final String getSessionId() {
return sessionId;
}
public final void setSessionId(final String sessionId) {
this.sessionId = sessionId;
}
public final String getSession() {
return sessionId;
}
public final void setSession(final String session) {
sessionId = session;
}
public final String getSessionKeyword() {
return sessionKeyword;
}
public final void setSessionKeyword(final String keyword) {
sessionKeyword = keyword;
}
public final long getTimestamp() {
return timestamp;
}
public final void setTimestamp(final long timestamp) {
this.timestamp = timestamp;
}
public final int getNumber() {
return number;
}
public final void setNumber(final int number) {
this.number = number;
}
public final int getDuration() {
return duration;
}
public final void setDuration(final int duration) {
this.duration = duration;
}
public int getPiRound() {
return piRound;
}
public void setPiRound(final int piRound) {
this.piRound = piRound;
}
public boolean isShowStatistic() {
return showStatistic;
}
public void setShowStatistic(final boolean showStatistic) {
this.showStatistic = showStatistic;
}
public boolean getCvIsColored() {
return cvIsColored;
}
public void setCvIsColored(boolean cvIsColored) {
this.cvIsColored = cvIsColored;
}
public boolean isShowAnswer() {
return showAnswer;
}
public void setShowAnswer(final boolean showAnswer) {
this.showAnswer = showAnswer;
}
public boolean isAbstention() {
return abstention;
}
public void setAbstention(final boolean abstention) {
this.abstention = abstention;
}
public final String get_id() {
return _id;
}
public final void set_id(final String _id) {
this._id = _id;
}
public final String get_rev() {
return _rev;
}
public final void set_rev(final String _rev) {
this._rev = _rev;
}
public String getImage() {
return image;
}
public void setImage(final String image) {
this.image = image;
}
public String getFcImage() {
return fcImage;
}
public void setFcImage(final String fcImage) {
this.fcImage = fcImage;
}
public int getGridSize() {
return gridSize;
}
public void setGridSize(final int gridSize) {
this.gridSize = gridSize;
}
public int getOffsetX() {
return offsetX;
}
public void setOffsetX(final int offsetX) {
this.offsetX = offsetX;
}
public int getOffsetY() {
return offsetY;
}
public void setOffsetY(final int offsetY) {
this.offsetY = offsetY;
}
public int getZoomLvl() {
return zoomLvl;
}
public void setZoomLvl(final int zoomLvl) {
this.zoomLvl = zoomLvl;
}
public int getGridOffsetX() {
return gridOffsetX;
}
public void setGridOffsetX(int gridOffsetX) {
this.gridOffsetX = gridOffsetX;
}
public int getGridOffsetY() {
return gridOffsetY;
}
public void setGridOffsetY(int gridOffsetY) {
this.gridOffsetY = gridOffsetY;
}
public int getGridZoomLvl() {
return gridZoomLvl;
}
public void setGridZoomLvl(int gridZoomLvl) {
this.gridZoomLvl = gridZoomLvl;
}
public int getGridSizeX() {
return gridSizeX;
}
public void setGridSizeX(int gridSizeX) {
this.gridSizeX = gridSizeX;
}
public int getGridSizeY() {
return gridSizeY;
}
public void setGridSizeY(int gridSizeY) {
this.gridSizeY = gridSizeY;
}
public boolean getGridIsHidden() {
return gridIsHidden;
}
public void setGridIsHidden(boolean gridIsHidden) {
this.gridIsHidden = gridIsHidden;
}
public int getImgRotation() {
return imgRotation;
}
public void setImgRotation(int imgRotation) {
this.imgRotation = imgRotation;
}
public boolean getToggleFieldsLeft() {
return toggleFieldsLeft;
}
public void setToggleFieldsLeft(boolean toggleFieldsLeft) {
this.toggleFieldsLeft = toggleFieldsLeft;
}
public int getNumClickableFields() {
return numClickableFields;
}
public void setNumClickableFields(int numClickableFields) {
this.numClickableFields = numClickableFields;
}
public int getThresholdCorrectAnswers() {
return thresholdCorrectAnswers;
}
public void setThresholdCorrectAnswers(int thresholdCorrectAnswers) {
this.thresholdCorrectAnswers = thresholdCorrectAnswers;
}
public String getGridLineColor() {
return gridLineColor;
}
public void setGridLineColor(String gridLineColor) {
this.gridLineColor = gridLineColor;
}
public int getNumberOfDots() {
return numberOfDots;
}
public void setNumberOfDots(int numberOfDots) {
this.numberOfDots = numberOfDots;
}
public String getGridType() {
return gridType;
}
public void setGridType(String gridType) {
this.gridType = gridType;
}
public void setScaleFactor(String scaleFactor) {
this.scaleFactor = scaleFactor;
}
public String getScaleFactor() {
return this.scaleFactor;
}
public void setGridScaleFactor(String scaleFactor) {
this.gridScaleFactor = scaleFactor;
}
public String getGridScaleFactor() {
return this.gridScaleFactor;
}
@Override
public final String toString() {
return "Question type '" + type + "': " + subject + ";\n" + text + possibleAnswers;
}
}
/*
* This file is part of ARSnova Backend.
* Copyright (C) 2012-2015 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.entities;
import java.util.ArrayList;
import java.util.List;
public class SessionInfo {
private String name;
private String shortName;
private String keyword;
private boolean active;
private String courseType;
private long creationTime;
private int numQuestions;
private int numAnswers;
private int numInterposed;
private int numUnanswered;
public SessionInfo(Session session) {
this.name = session.getName();
this.shortName = session.getShortName();
this.keyword = session.getKeyword();
this.active = session.isActive();
this.courseType = session.getCourseType();
this.creationTime = session.getCreationTime();
}
public static List<SessionInfo> fromSessionList(List<Session> sessions) {
List<SessionInfo> infos = new ArrayList<SessionInfo>();
for (Session s : sessions) {
infos.add(new SessionInfo(s));
}
return infos;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getShortName() {
return shortName;
}
public void setShortName(String shortName) {
this.shortName = shortName;
}
public String getKeyword() {
return keyword;
}
public void setKeyword(String keyword) {
this.keyword = keyword;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
public String getCourseType() {
return courseType;
}
public void setCourseType(String courseType) {
this.courseType = courseType;
}
public int getNumQuestions() {
return numQuestions;
}
public void setNumQuestions(int numQuestions) {
this.numQuestions = numQuestions;
}
public int getNumAnswers() {
return numAnswers;
}
public void setNumAnswers(int numAnswers) {
this.numAnswers = numAnswers;
}
public int getNumInterposed() {
return numInterposed;
}
public void setNumInterposed(int numInterposed) {
this.numInterposed = numInterposed;
}
public int getNumUnanswered() {
return numUnanswered;
}
public void setNumUnanswered(int numUnanswered) {
this.numUnanswered = numUnanswered;
}
public long getCreationTime() {
return creationTime;
}
public void setCreationTime(long creationTime) {
this.creationTime = creationTime;
}
}
package de.thm.arsnova.entities;
/*
* This file is part of ARSnova Backend.
* Copyright (C) 2012-2019 The ARSnova Team and Contributors
*
* 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.event;
import de.thm.arsnova.model.Entity;
public class AfterCreationEvent<E extends Entity> extends CrudEvent<E> {
public AfterCreationEvent(final Object source, final E entity) {
super(source, entity);
}
}
/*
* This file is part of ARSnova Backend.
* Copyright (C) 2012-2019 The ARSnova Team and Contributors
*
* 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.event;
import de.thm.arsnova.model.Entity;
public class AfterDeletionEvent<E extends Entity> extends CrudEvent<E> {
public AfterDeletionEvent(final Object source, final E entity) {
super(source, entity);
}
}
/*
* This file is part of ARSnova Backend.
* Copyright (C) 2012-2019 The ARSnova Team and Contributors
*
* 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.event;
import de.thm.arsnova.model.Entity;
public class AfterFullUpdateEvent<E extends Entity> extends AfterUpdateEvent<E> {
private final E oldEntity;
public AfterFullUpdateEvent(final Object source, final E entity, final E oldEntity) {
super(source, entity);
this.oldEntity = oldEntity;
}
public E getOldEntity() {
return oldEntity;
}
}
/*
* This file is part of ARSnova Backend.
* Copyright (C) 2012-2019 The ARSnova Team and Contributors
*
* 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.event;
import java.util.Map;
import java.util.function.Function;
import de.thm.arsnova.model.Entity;
public class AfterPatchEvent<E extends Entity> extends AfterUpdateEvent<E> {
private final Function<E, ? extends Object> propertyGetter;
private final Map<String, Object> changes;
public AfterPatchEvent(final Object source, final E entity, final Function<E, ? extends Object> propertyGetter,
final Map<String, Object> changes) {
super(source, entity);
this.propertyGetter = propertyGetter;
this.changes = changes;
}
public Function<E, ? extends Object> getPropertyGetter() {
return propertyGetter;
}
public Map<String, Object> getChanges() {
return changes;
}
}