Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package hu.bme.iit.quiz.controller; import hu.bme.iit.quiz.model.QuizPresentation; import hu.bme.iit.quiz.service.QuizService; import hu.bme.iit.quiz.service.UserService; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.annotation.Secured; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller public class QuizController { private static final Logger logger = Logger.getLogger(QuizController.class); @Autowired private QuizService quizService; @Autowired private UserService userService; /** * ************************************************* * URL: /quiz/start/{innerkey} * ************************************************** */ @Secured(value = { "isAuthenticated()" }) @RequestMapping(value = "/quiz/start/{innerkey}", method = RequestMethod.GET) public String startQuiz(@PathVariable String innerkey) { try { User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); int quizPresentationID = quizService.startQuizByInnerKeyForUser(innerkey, user.getUsername()); return "redirect:/quiz/room/" + quizPresentationID; } catch (Exception e) { logger.error(e); e.printStackTrace(); return "redirect:/my-quizzes?start-error"; } } /** * ************************************************* * URL: /quiz/start/{innerkey} * ************************************************** */ @Secured(value = { "isAuthenticated()" }) @RequestMapping(value = "/quiz/stop/{innerkey}", method = RequestMethod.GET) public String stopQuiz(@PathVariable String innerkey) { try { User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); quizService.stopQuizByInnerKeyForUser(innerkey, user.getUsername()); return "redirect:/my-quizzes"; } catch (Exception e) { logger.error(e); e.printStackTrace(); return "redirect:/my-quizzes?start-error"; } } /** * ************************************************* * URL: /quiz/room/{quizPresentationID} * ************************************************** */ @Secured(value = { "isAuthenticated()" }) @RequestMapping(value = "/quiz/room/{quizPresentationID}", method = RequestMethod.GET) public ModelAndView defaultQuiz(@PathVariable int quizPresentationID) { ModelAndView model = new ModelAndView(); logger.warn("defaultQuiz contoller handler"); QuizPresentation quizPresentation = quizService.getQuizPresentation(quizPresentationID); model.addObject("title", quizPresentation.getQuiz().getName()); model.addObject("message", "Watch & Learn"); model.addObject("user", userService.getCurrentUser()); model.addObject("quizData", quizService.getFullQuizData(quizPresentationID)); model.addObject("quizPresentation", quizPresentation); model.addObject("hasRightToSeeFullQuizPresentationData", quizService.hasRightToSeeFullQuizPresentationData( quizPresentationID, userService.getCurrentUser().getLogin())); model.addObject("webSocketEndpointName", "pollendpoint/" + quizPresentation.getId()); model.addObject("webSocketStarter", "JOIN"); model.setViewName("quiz"); return model; } }