com.epam.ipodromproject.controller.AdminController.java Source code

Java tutorial

Introduction

Here is the source code for com.epam.ipodromproject.controller.AdminController.java

Source

/*
 * 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.Role;
import com.epam.ipodromproject.domain.User;
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 AdminController {

    @Autowired
    UserService userService;

    @RequestMapping(value = "admin", method = RequestMethod.GET)
    public String goToAdmin(Model model) {
        model.addAttribute("roles", Role.values());
        return "adminPage";
    }

    @RequestMapping(value = "blockUser", method = RequestMethod.POST)
    @ResponseBody
    public String blockUser(@RequestParam("userID") Long userID, @RequestParam("block") Boolean block) {
        User user = userService.getUserByUsername(SecurityContextHolder.getContext().getAuthentication().getName());
        if (user.getEnabled() == false) {
            return "this_user_is_blocked";
        }
        if (!userService.blockOrUnblockUser(userID, block)) {
            return (block == true) ? "Cant_block_this_user" : "Cant_unblock_this_user";
        } else {
            return (block == true) ? "User_successfully_blocked" : "User_successfully_unblocked";
        }
    }

    @RequestMapping(value = "changeRole", method = RequestMethod.POST)
    @ResponseBody
    public String changeRoleOfUser(@RequestParam("userID") Long userID, @RequestParam("role") Role role) {
        User user = userService.getUserByUsername(SecurityContextHolder.getContext().getAuthentication().getName());
        if (user.getEnabled() == false) {
            return "this_user_is_blocked";
        }
        if (!userService.changeRole(userID, role)) {
            return "Cant_change_role_of_this_user";
        } else {
            return "User_role_changed_successfully";
        }
    }

    @RequestMapping(value = "getUsersPagesByPartOfName", method = RequestMethod.GET)
    @ResponseBody
    public long getUsersPagesByPartOfName(@RequestParam("partOfName") String partOfName,
            @RequestParam("resultsPerPage") Integer resultsPerPage,
            @RequestParam(value = "blocked", required = false, defaultValue = "false") Boolean blocked,
            @RequestParam(value = "unblocked", required = false, defaultValue = "false") Boolean unblocked) {
        return userService.getTotalUsersPages(partOfName, resultsPerPage, blocked, unblocked);
    }

    @RequestMapping(value = "getUsersPageByPartOfName", method = RequestMethod.GET)
    public String getUsersByPartOfName(@RequestParam("partOfName") String partOfName,
            @RequestParam(value = "page") Integer page,
            @RequestParam(value = "resultsPerPage") Integer resultsPerPage,
            @RequestParam(value = "blocked", required = false, defaultValue = "false") Boolean blocked,
            @RequestParam(value = "unblocked", required = false, defaultValue = "false") Boolean unblocked,
            Model model) {
        int totalPages = (int) userService.getTotalUsersPages(partOfName, resultsPerPage, blocked, unblocked);
        int pageToShow;
        if (page <= 0) {
            pageToShow = 1;
        } else if (page > totalPages) {
            pageToShow = totalPages;
        } else {
            pageToShow = page;
        }
        model.addAttribute("users",
                userService.getUsersPageByPartOfName(partOfName, pageToShow, resultsPerPage, blocked, unblocked));
        return "lists/usersDropList";
    }

}