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 com.epam.ipodromproject.controller; import com.epam.ipodromproject.domain.User; import com.epam.ipodromproject.service.HorseService; import com.epam.ipodromproject.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; 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.ResponseBody; /** * * @author Oleh */ @Controller public class HorseController { @Autowired UserService userService; @Autowired HorseService horseService; @RequestMapping(value = "addHorse", method = RequestMethod.GET) public String goToAddHorse() { return "addHorse"; } @RequestMapping(value = "registerHorse", method = RequestMethod.POST) @ResponseBody public String registerHorse(@RequestParam("horseName") String horseName, Model model) { User user = userService.getUserByUsername(SecurityContextHolder.getContext().getAuthentication().getName()); if (user.getEnabled() == false) { return "this_user_is_blocked"; } horseName = horseName.trim(); if (horseName.isEmpty()) { return "name_of_horse_cannot_be_empty"; } if (horseService.registerHorse(horseName)) { return "horse_successfully_added"; } else { return "horse_with_this_name_already_exists"; } } @RequestMapping(value = "blockHorse", method = RequestMethod.POST) @ResponseBody public String blockUser(@RequestParam("horseID") Long horseID, @RequestParam("block") Boolean block, Model model) { User user = userService.getUserByUsername(SecurityContextHolder.getContext().getAuthentication().getName()); if (user.getEnabled() == false) { return "this_user_is_blocked"; } StringBuilder message = new StringBuilder(); if (!horseService.blockOrUnblockHorse(horseID, block)) { message.append("cant_").append((block == true) ? "block_" : "unblock_").append("this_horse"); } else { message.append("horse_successfully_").append((block == true) ? "blocked" : "unblocked"); } return message.toString(); } @RequestMapping(value = "getHorsesPageByName", method = RequestMethod.GET) public String getHorsesByName(@RequestParam(value = "page") Integer page, @RequestParam(value = "partOfName", required = false) String partOfName, @RequestParam(value = "resultsPerPage") Integer resultsPerPage, Model model) { int totalPages = horseService.getPagesOfHorses(partOfName, resultsPerPage); if (page < 1) { page = 1; } else if (page > totalPages) { page = totalPages; } model.addAttribute("horses", horseService.getHorsesByPageAndPartOfName(partOfName, page, resultsPerPage)); return "lists/horsesDropList"; } @RequestMapping(value = "getHorsePagesByPartOfName", method = RequestMethod.GET) @ResponseBody public int getNumberOfPagesOhHorsesByName(@RequestParam(value = "partOfName") String partOfName, @RequestParam(value = "resultsPerPage") Integer resultsPerPage) { return horseService.getPagesOfHorses(partOfName, resultsPerPage); } }