cz.PA165.vozovyPark.controller.UserController.java Source code

Java tutorial

Introduction

Here is the source code for cz.PA165.vozovyPark.controller.UserController.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 cz.PA165.vozovyPark.controller;

import cz.PA165.vozovyPark.dto.UserDTO;
import cz.PA165.vozovyPark.enums.UserClassEnum;
import cz.PA165.vozovyPark.service.DriveService;
import cz.PA165.vozovyPark.service.UserService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.validation.Valid;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.ui.Model;

/**
 *
 * @author jzelezny
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private DriveService driveService;

    @RequestMapping(value = "/allUsers")
    public String viewAllUsers(ModelMap model) {
        model.put("allUsers", userService.findAll());
        return "/user/allUsers";
    }

    @RequestMapping(value = "/{userId}")
    public String viewUser(ModelMap model, @PathVariable Long userId) {
        model.put("userById", userService.getById(userId));
        return "/user/userView";
    }

    @RequestMapping(value = "/userProfile")
    public String viewUserProfile(ModelMap model) {
        List<UserDTO> userLoggedInList = userService.getByUsername(getLoggedUsername());
        UserDTO user = userLoggedInList.get(0);
        model.put("userById", user);
        return "/user/userView";
    }

    @RequestMapping(value = "/new", method = RequestMethod.GET)
    public String showAddUserForm(Model model) {
        model.addAttribute("user", new UserDTO());
        model.addAttribute("userClass", UserClassEnum.values());

        return "/user/userForm";
    }

    @RequestMapping(value = "/new", method = RequestMethod.POST)
    public String addUser(@Valid @ModelAttribute("user") UserDTO user, BindingResult result) {

        //        model.addAttribute("user", user);  
        //        user.setEnabled(Boolean.TRUE); //- testing purpose
        //        user.setIsAdmin(Boolean.TRUE);
        //        user.setPassword("hesloslohe");
        if (result.hasErrors()) {
            return "/user/userForm";
        }
        userService.createUser(user);
        return "redirect:/user/allUsers";
    }

    @RequestMapping(value = "/update/{userId}", method = RequestMethod.GET)
    public String showEditForm(ModelMap model, @PathVariable("userId") Long id) {
        UserDTO user = userService.getById(id);
        model.put("user", user);
        model.put("userClass", UserClassEnum.values());
        return "/user/userUpdateForm";
    }

    @RequestMapping(value = "/update/{userId}", method = RequestMethod.POST)
    public String updateUser(@ModelAttribute("user") UserDTO user, @PathVariable("userId") Long id,
            BindingResult result) {

        if (result.hasErrors()) {
            return "/user/userUpdateForm";
        }
        userService.updateUser(user);
        return "redirect:/user/allUsers";
    }

    @RequestMapping(value = "/delete/{userId}", method = RequestMethod.GET)
    public String deleteUser(@PathVariable("userId") Long id, ModelMap model, HttpServletRequest req) {
        UserDTO user = userService.getById(id);
        if (driveService.findByUser(user).isEmpty()) {
            userService.removeUser(user);
        } else {
            req.getSession().setAttribute("message", "User has active drives and cannot be removed.");
            return "redirect:/user/" + user.getId();
        }
        return "redirect:/user/allUsers";
    }

    public String getLoggedUsername() {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        return auth.getName();
    }
}