Skip to content
Snippets Groups Projects
RoomController.java 13 KiB
Newer Older
 * This file is part of ARSnova Backend.
 * Copyright (C) 2012-2017 The ARSnova Team
 * ARSnova Backend is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ARSnova Backend is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.thm.arsnova.controller;
import de.thm.arsnova.entities.Room;
import de.thm.arsnova.entities.transport.ImportExportSession;
import de.thm.arsnova.entities.transport.ScoreStatistics;
import de.thm.arsnova.exceptions.UnauthorizedException;
import de.thm.arsnova.services.RoomService;
import de.thm.arsnova.services.RoomServiceImpl.SessionNameComparator;
import de.thm.arsnova.services.RoomServiceImpl.SessionShortNameComparator;
import de.thm.arsnova.services.UserService;
import de.thm.arsnova.web.DeprecatedApi;
import de.thm.arsnova.web.Pagination;
Daniel Gerhardt's avatar
Daniel Gerhardt committed
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.PathVariable;
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 javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 * Handles requests related to ARSnova rooms.
Daniel Gerhardt's avatar
Daniel Gerhardt committed
@RequestMapping("/room")
@Api(value = "/room", description = "the Room Controller API")
public class RoomController extends PaginationController {
	private RoomService roomService;
	private UserService userService;
Daniel Gerhardt's avatar
Daniel Gerhardt committed
			nickname = "joinRoom")
Daniel Gerhardt's avatar
Daniel Gerhardt committed
	@RequestMapping(value = "/{shortId}", method = RequestMethod.GET)
	public Room get(
			@ApiParam(value = "Room-Key from current session", required = true) @PathVariable final String shortId,
			@ApiParam(value = "Adminflag", required = false) @RequestParam(value = "admin", defaultValue = "false")	final boolean admin
			) {
		if (admin) {
Daniel Gerhardt's avatar
Daniel Gerhardt committed
			return roomService.getForAdmin(shortId);
Daniel Gerhardt's avatar
Daniel Gerhardt committed
			return roomService.getByKey(shortId);
	@ApiOperation(value = "deletes a session",
Daniel Gerhardt's avatar
Daniel Gerhardt committed
			nickname = "delete")
	@RequestMapping(value = "/{shortId}", method = RequestMethod.DELETE)
	public void delete(@ApiParam(value = "Room-Key from current session", required = true) @PathVariable final String shortId) {
		Room room = roomService.getByKey(shortId);
		roomService.deleteCascading(room);
	@ApiOperation(value = "count active users",
			nickname = "countActiveUsers")
Daniel Gerhardt's avatar
Daniel Gerhardt committed
	@RequestMapping(value = "/{shortId}/activeusercount", method = RequestMethod.GET)
	public int countActiveUsers(@ApiParam(value = "Room-Key from current session", required = true) @PathVariable final String shortId) {
		return roomService.activeUsers(shortId);
	@ApiOperation(value = "Creates a new Room and returns the Room's data",
Daniel Gerhardt's avatar
Daniel Gerhardt committed
			nickname = "create")
	@ApiResponses(value = {
		@ApiResponse(code = 201, message = HTML_STATUS_201),
		@ApiResponse(code = 503, message = HTML_STATUS_503)
	@RequestMapping(value = "/", method = RequestMethod.POST)
Daniel Gerhardt's avatar
Daniel Gerhardt committed
	public Room create(@ApiParam(value = "current session", required = true) @RequestBody final Room room, final HttpServletResponse response) {
		/* FIXME: migrate LMS course support
		if (room != null && room.isCourseSession()) {
Daniel Gerhardt's avatar
Daniel Gerhardt committed
			final List<Course> courses = new ArrayList<>();
			final Course course = new Course();
			course.setId(room.getCourseId());
			final int sessionCount = roomService.countSessionsByCourses(courses);
			if (sessionCount > 0) {
				final String appendix = " (" + (sessionCount + 1) + ")";
				room.setName(room.getName() + appendix);
				room.setAbbreviation(room.getAbbreviation() + appendix);
	@ApiOperation(value = "updates a session",
Daniel Gerhardt's avatar
Daniel Gerhardt committed
			nickname = "create")
	@RequestMapping(value = "/{shortId}", method = RequestMethod.PUT)
	public Room update(
			@ApiParam(value = "session-key from current session", required = true) @PathVariable final String shortId,
			@ApiParam(value = "current session", required = true) @RequestBody final Room room
Daniel Gerhardt's avatar
Daniel Gerhardt committed
		return roomService.update(shortId, room);
	@ApiOperation(value = "Retrieves a list of Sessions",
Daniel Gerhardt's avatar
Daniel Gerhardt committed
			nickname = "getAll")
	@ApiResponses(value = {
		@ApiResponse(code = 204, message = HTML_STATUS_204),
		@ApiResponse(code = 501, message = HTML_STATUS_501)
	@RequestMapping(value = "/", method = RequestMethod.GET)
Daniel Gerhardt's avatar
Daniel Gerhardt committed
	public List<Room> getAll(
			@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,
Tom Käsler's avatar
Tom Käsler committed
			try {
				if (ownedOnly && !visitedOnly) {
					rooms = roomService.getUserSessions(username);
Tom Käsler's avatar
Tom Käsler committed
				} else if (visitedOnly && !ownedOnly) {
					rooms = roomService.getUserVisitedSessions(username);
Tom Käsler's avatar
Tom Käsler committed
				} else {
					response.setStatus(HttpStatus.NOT_IMPLEMENTED.value());
					return null;
				}
			} catch (final AccessDeniedException e) {
				throw new UnauthorizedException();
Tom Käsler's avatar
Tom Käsler committed
		} else {
			/* TODO implement all parameter combinations, implement use of user parameter */
			try {
				if (ownedOnly && !visitedOnly) {
					rooms = roomService.getMySessions(offset, limit);
Tom Käsler's avatar
Tom Käsler committed
				} else if (visitedOnly && !ownedOnly) {
					rooms = roomService.getMyVisitedSessions(offset, limit);
Tom Käsler's avatar
Tom Käsler committed
				} 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 SessionShortNameComparator());
			Collections.sort(rooms, new SessionNameComparator());
	/**
	 * Returns a list of my own sessions with only the necessary information like name, keyword, or counters.
	 */
	@ApiOperation(value = "Retrieves a Room",
			nickname = "getMySessions")
	@ApiResponses(value = {
		@ApiResponse(code = 204, message = HTML_STATUS_204)
	@RequestMapping(value = "/", method = RequestMethod.GET, params = "statusonly=true")
	public List<Room> getMySessions(
			@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
			rooms = roomService.getMySessionsInfo(offset, limit);
			rooms = roomService.getMyVisitedSessionsInfo(offset, limit);
		if (rooms == null || rooms.isEmpty()) {
			response.setStatus(HttpServletResponse.SC_NO_CONTENT);
			return null;
		}

		if ("shortname".equals(sortby)) {
			Collections.sort(rooms, new SessionShortNameComparator());
			Collections.sort(rooms, new SessionNameComparator());
	@ApiOperation(value = "Retrieves all public pool sessions for the current user",
			nickname = "getMyPublicPoolSessions")
	@ApiResponses(value = {
		@ApiResponse(code = 204, message = HTML_STATUS_204)
	@RequestMapping(value = "/publicpool", method = RequestMethod.GET, params = "statusonly=true")
	public List<Room> getMyPublicPoolSessions(
			final HttpServletResponse response
		List<Room> sessions = roomService.getMyPublicPoolSessionsInfo();
		if (sessions == null || sessions.isEmpty()) {
			response.setStatus(HttpServletResponse.SC_NO_CONTENT);
			return null;
		}

		return sessions;
	}
	@ApiOperation(value = "Retrieves all public pool sessions",
			nickname = "getMyPublicPoolSessions")
	@ApiResponses(value = {
		@ApiResponse(code = 204, message = HTML_STATUS_204)
Daniel Vogel's avatar
Daniel Vogel committed
	@RequestMapping(value = "/publicpool", method = RequestMethod.GET)
	public List<Room> getPublicPoolSessions(
			final HttpServletResponse response
		List<Room> rooms = roomService.getPublicPoolSessionsInfo();
		if (rooms == null || rooms.isEmpty()) {
			response.setStatus(HttpServletResponse.SC_NO_CONTENT);
			return null;
		}

	@ApiOperation(value = "imports a session",
			nickname = "importSession")
	@RequestMapping(value = "/import", method = RequestMethod.POST)
			@ApiParam(value = "current session", required = true) @RequestBody final ImportExportSession session,
			final HttpServletResponse response
		return roomService.importSession(session);
	@ApiOperation(value = "export sessions", nickname = "exportSession")
	@RequestMapping(value = "/export", method = RequestMethod.GET)
	public List<ImportExportSession> getExport(
Daniel Gerhardt's avatar
Daniel Gerhardt committed
			@ApiParam(value = "shortId", required = true) @RequestParam(value = "shortId", defaultValue = "") final List<String> shortId,
			@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
Daniel Gerhardt's avatar
Daniel Gerhardt committed
		List<ImportExportSession> sessions = new ArrayList<>();
Daniel Gerhardt's avatar
Daniel Gerhardt committed
		for (String key : shortId) {
			roomService.setActive(key, false);
			temp = roomService.exportSession(key, withAnswerStatistics, withFeedbackQuestions);
			if (temp != null) {
				sessions.add(temp);
			}
			roomService.setActive(key, true);
	@ApiOperation(value = "copy a session to the public pool if enabled")
Daniel Gerhardt's avatar
Daniel Gerhardt committed
	@RequestMapping(value = "/{shortId}/copytopublicpool", method = RequestMethod.POST)
	public Room copyToPublicPool(
Daniel Gerhardt's avatar
Daniel Gerhardt committed
			@ApiParam(value = "session-key from current session", required = true) @PathVariable final String shortId,
Tom Käsler's avatar
Tom Käsler committed
			@ApiParam(value = "public pool attributes for session", required = true) @RequestBody final de.thm.arsnova.entities.transport.ImportExportSession.PublicPool publicPool
Daniel Gerhardt's avatar
Daniel Gerhardt committed
		roomService.setActive(shortId, false);
		Room roomInfo = roomService.copySessionToPublicPool(shortId, publicPool);
		roomService.setActive(shortId, true);
	@ApiOperation(value = "retrieves a value for the score",
			nickname = "getLearningProgress")
Daniel Gerhardt's avatar
Daniel Gerhardt committed
	@RequestMapping(value = "/{shortId}/learningprogress", method = RequestMethod.GET)
	public ScoreStatistics getLearningProgress(
Daniel Gerhardt's avatar
Daniel Gerhardt committed
			@ApiParam(value = "session-key from current session", 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
Daniel Gerhardt's avatar
Daniel Gerhardt committed
		return roomService.getLearningProgress(shortId, type, questionVariant);
	@ApiOperation(value = "retrieves a value for the learning progress for the current user",
			nickname = "getMyLearningProgress")
Daniel Gerhardt's avatar
Daniel Gerhardt committed
	@RequestMapping(value = "/{shortId}/mylearningprogress", method = RequestMethod.GET)
	public ScoreStatistics getMyLearningProgress(
Daniel Gerhardt's avatar
Daniel Gerhardt committed
			@ApiParam(value = "session-key from current session", 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
Daniel Gerhardt's avatar
Daniel Gerhardt committed
		return roomService.getMyLearningProgress(shortId, type, questionVariant);
Daniel Gerhardt's avatar
Daniel Gerhardt committed
	}